ready to merge docs

This commit is contained in:
2024-08-11 08:37:17 +00:00
parent 9900d5aaf3
commit 22d18436fa
10 changed files with 346 additions and 38 deletions

View File

@ -3,6 +3,8 @@
#include <memory>
#include <variant>
#include <vector>
#include "../semantic/visitor.h"
#include "tools.h"
using IdentifierType = std::string;
struct ArrayType {
bool has_base_type;
@ -15,17 +17,15 @@ class ASTNodeVisitorBase {
virtual ~ASTNodeVisitorBase() = default;
virtual void visit(class ASTNodeBase *context) = 0;
};
enum class ASTNodeType {
};
class ASTNodeBase {
friend Visitor;
ASTNodeType type;
// std::vector<std::shared_ptr<ASTNodeBase>> children;
size_t start_line, start_char_pos, end_line, end_char_pos;
public:
virtual ~ASTNodeBase() = default;
virtual void accept(class ASTNodeVisitorBase *visitor) = 0;
// virtual void accept(class ASTNodeVisitorBase *visitor) = 0;
};
#endif

View File

@ -14,112 +14,147 @@ class Expr_ASTNode : public ASTNodeBase {
class BasicExpr_ASTNode : public Expr_ASTNode {}; // This is a virtual class
class NewArrayExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
bool has_initial_value;
std::shared_ptr<class ConstantExpr_ASTNode> initial_value;
};
class NewConstructExpr_ASTNode : public Expr_ASTNode {};
class NewExpr_ASTNode : public Expr_ASTNode {};
class NewConstructExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
};
class NewExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
};
class AccessExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> base;
IdentifierType member;
};
class MemberVariableAccessExpr_ASTNode : public AccessExpr_ASTNode {};
class MemberVariableAccessExpr_ASTNode : public AccessExpr_ASTNode {
friend Visitor;
};
class MemberFunctionAccessExpr_ASTNode : public AccessExpr_ASTNode {
friend Visitor;
std::vector<std::shared_ptr<Expr_ASTNode>> arguments;
};
class IndexExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> base;
std::vector<std::shared_ptr<Expr_ASTNode>> indices;
};
class SuffixExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> base;
};
class PrefixExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> base;
};
class OppositeExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> base;
};
class LNotExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BNotExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class MDMExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class PMExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class RLExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class GGLLExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class NEExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BAndExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BXorExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BOrExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class LAndExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class LOrExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class TernaryExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> condition;
std::shared_ptr<Expr_ASTNode> src1;
std::shared_ptr<Expr_ASTNode> src2;
};
class AssignExpr_ASTNode : public Expr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> dest;
std::shared_ptr<Expr_ASTNode> src;
};
class ThisExpr_ASTNode : public BasicExpr_ASTNode {};
class ThisExpr_ASTNode : public BasicExpr_ASTNode {
friend Visitor;
};
class ParenExpr_ASTNode : public BasicExpr_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> expr;
};
class IDExpr_ASTNode : public BasicExpr_ASTNode {
friend Visitor;
IdentifierType id;
};
class FunctionCallExpr_ASTNode : public BasicExpr_ASTNode {
friend Visitor;
IdentifierType func_name;
std::vector<std::shared_ptr<Expr_ASTNode>> arguments;
};
class FormattedStringExpr_ASTNode : public BasicExpr_ASTNode {
friend Visitor;
using SegmentType = std::variant<std::string, std::shared_ptr<Expr_ASTNode>>;
std::vector<SegmentType> segments;
};
struct NullType {};
using AtomicConstantType = std::variant<uint32_t, bool, std::string, NullType>;
struct ArrayConstantType {
friend Visitor;
std::vector<std::variant<std::shared_ptr<ArrayConstantType>, NullType, AtomicConstantType>> elements;
size_t level;
};
class ConstantExpr_ASTNode : public BasicExpr_ASTNode {
friend Visitor;
std::variant<AtomicConstantType, ArrayConstantType> value;
};

View File

@ -8,34 +8,43 @@ class Statement_ASTNode : public ASTNodeBase {
virtual ~Statement_ASTNode() = default;
};
class EmptyStatement_ASTNode : public Statement_ASTNode {};
class EmptyStatement_ASTNode : public Statement_ASTNode {
friend Visitor;
};
class DefinitionStatement_ASTNode : public Statement_ASTNode {
ExprTypeInfo type;
friend Visitor;
ExprTypeInfo var_type;
std::vector<std::pair<IdentifierType, std::shared_ptr<Expr_ASTNode>>> vars;
};
class ExprStatement_ASTNode : public Statement_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> expr;
};
class IfStatement_ASTNode : public Statement_ASTNode {
friend Visitor;
bool has_else_clause;
std::shared_ptr<Expr_ASTNode> condition;
std::shared_ptr<Statement_ASTNode> if_clause;
std::shared_ptr<Statement_ASTNode> else_clause;
};
class WhileStatement_ASTNode : public Statement_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> condition;
std::shared_ptr<Statement_ASTNode> loop_body;
};
class ForStatement_ASTNode : public Statement_ASTNode {
friend Visitor;
std::shared_ptr<Statement_ASTNode> initial;
std::shared_ptr<Expr_ASTNode> condition;
std::shared_ptr<Statement_ASTNode> update;
std::shared_ptr<Statement_ASTNode> loop_body;
};
class JmpStatement_ASTNode : public Statement_ASTNode {
friend Visitor;
std::shared_ptr<Expr_ASTNode> return_value;
};
class SuiteStatement_ASTNode : public Statement_ASTNode {
friend Visitor;
std::vector<std::shared_ptr<Statement_ASTNode>> statements;
};

View File

@ -7,21 +7,35 @@
#include "expr_astnode.h"
#include "statement_astnode.h"
class FuncDef_ASTNode : public ASTNodeBase {
friend Visitor;
bool is_constructor;
IdentifierType func_name;
ExprTypeInfo return_type;
std::vector<std::pair<IdentifierType, ExprTypeInfo>> params;
std::shared_ptr<SuiteStatement_ASTNode> func_body;
public:
FuncDef_ASTNode() = default;
};
class Constructor_ASTNode : public FuncDef_ASTNode {};
class ClassVariable_ASTNode : public DefinitionStatement_ASTNode {};
class ClassDef_ASTNode : public ASTNodeBase {
friend Visitor;
private:
using ClassElement = std::variant<std::shared_ptr<Constructor_ASTNode>, std::shared_ptr<ClassVariable_ASTNode>,
std::shared_ptr<FuncDef_ASTNode>>;
std::vector<ClassElement> elements;
std::string class_name;
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> member_variables;
std::vector<std::shared_ptr<FuncDef_ASTNode>> member_functions;
std::shared_ptr<FuncDef_ASTNode> constructor;
public:
ClassDef_ASTNode() = default;
};
class Program_ASTNode : public ASTNodeBase {
using ProgramElement = std::variant<std::shared_ptr<FuncDef_ASTNode>, std::shared_ptr<ClassDef_ASTNode>,
std::shared_ptr<DefinitionStatement_ASTNode>>;
std::vector<ProgramElement> elements;
friend Visitor;
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> global_variables;
std::vector<std::shared_ptr<ClassDef_ASTNode>> classes;
std::vector<std::shared_ptr<FuncDef_ASTNode>> functions;
public:
Program_ASTNode() = default;
};
#endif

View File

@ -4,15 +4,6 @@
#include "ast/ast.h"
#include "visitor.h"
class SemanticError : public std::exception {
std::string msg;
int error_code;
public:
SemanticError(const std::string &msg, int error_code) : msg(msg), error_code(error_code) {}
const char *what() const noexcept override { return msg.c_str(); }
int GetErrorCode() const { return error_code; }
};
std::shared_ptr<ASTNodeBase> BuildAST(Visitor *visitor, antlr4::tree::ParseTree *tree);
void SemanticCheck(std::istream &fin, std::shared_ptr<ASTNodeBase> &ast);
#endif

View File

@ -1,9 +1,13 @@
#ifndef VISITOR_H
#define VISITOR_H
#include <stack>
#include <vector>
#include "MXParserVisitor.h"
#include "tools.h"
class Visitor : public MXParserVisitor {
std::vector<ASTNodeType> nodetype_stk;
public:
std::any visitMxprog(MXParser::MxprogContext *context) override;
std::any visitFunction_def(MXParser::Function_defContext *context) override;

61
include/tools.h Normal file
View File

@ -0,0 +1,61 @@
#pragma once
#include <stdexcept>
enum class ASTNodeType {
// Expression nodes
NewArrayExpr,
NewConstructExpr,
NewExpr,
MemberVariableAccessExpr,
MemberFunctionAccessExpr,
IndexExpr,
SuffixExpr,
PrefixExpr,
OppositeExpr,
LNotExpr,
BNotExpr,
MDMExpr,
PMExpr,
RLExpr,
GGLLExpr,
NEExpr,
BAndExpr,
BXorExpr,
BOrExpr,
LAndExpr,
LOrExpr,
TernaryExpr,
AssignExpr,
ThisExpr,
ParenExpr,
IDExpr,
FunctionCallExpr,
FormattedStringExpr,
ConstantExpr,
// Statement nodes
EmptyStatement,
DefinitionStatement,
ExprStatement,
IfStatement,
WhileStatement,
ForStatement,
JmpStatement,
SuiteStatement,
// Structural nodes
FuncDef,
Constructor,
ClassVariable,
ClassDef,
Program
};
class SemanticError : public std::exception {
std::string msg;
int error_code;
public:
SemanticError(const std::string &msg, int error_code) : msg(msg), error_code(error_code) {}
const char *what() const noexcept override { return msg.c_str(); }
int GetErrorCode() const { return error_code; }
};