ready to build AST

This commit is contained in:
2024-08-08 13:05:56 +00:00
parent f1eb6abcc1
commit 9900d5aaf3
23 changed files with 683 additions and 110 deletions

8
include/ast/ast.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef AST_H
#define AST_H
#include "astnode.h"
#include "astnode_visitor.h"
#include "expr_astnode.h"
#include "statement_astnode.h"
#include "structural_astnode.h"
#endif // AST_H

31
include/ast/astnode.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef ASTNODE_H
#define ASTNODE_H
#include <memory>
#include <variant>
#include <vector>
using IdentifierType = std::string;
struct ArrayType {
bool has_base_type;
IdentifierType basetype;
size_t level;
};
using ExprTypeInfo = std::variant<IdentifierType, ArrayType>;
class ASTNodeVisitorBase {
public:
virtual ~ASTNodeVisitorBase() = default;
virtual void visit(class ASTNodeBase *context) = 0;
};
enum class ASTNodeType {
};
class ASTNodeBase {
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;
};
#endif

View File

@ -0,0 +1,6 @@
#ifndef ASTNODE_VISITOR_H
#define ASTNODE_VISITOR_H
#include "astnode.h"
#include "expr_astnode.h"
class ASTNodeVisitor : public ASTNodeVisitorBase {};
#endif

126
include/ast/expr_astnode.h Normal file
View File

@ -0,0 +1,126 @@
#ifndef EXPR_ASTNODE_H
#define EXPR_ASTNODE_H
#include <cstdint>
#include <string>
#include <variant>
#include "astnode.h"
class Expr_ASTNode : public ASTNodeBase {
ExprTypeInfo expr_type_info;
public:
virtual ~Expr_ASTNode() = default;
};
class BasicExpr_ASTNode : public Expr_ASTNode {}; // This is a virtual class
class NewArrayExpr_ASTNode : public Expr_ASTNode {
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 AccessExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> base;
IdentifierType member;
};
class MemberVariableAccessExpr_ASTNode : public AccessExpr_ASTNode {};
class MemberFunctionAccessExpr_ASTNode : public AccessExpr_ASTNode {
std::vector<std::shared_ptr<Expr_ASTNode>> arguments;
};
class IndexExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> base;
std::vector<std::shared_ptr<Expr_ASTNode>> indices;
};
class SuffixExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> base;
};
class PrefixExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> base;
};
class OppositeExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> base;
};
class LNotExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BNotExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class MDMExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class PMExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class RLExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class GGLLExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class NEExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BAndExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BXorExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class BOrExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class LAndExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class LOrExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> left;
std::shared_ptr<Expr_ASTNode> right;
};
class TernaryExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> condition;
std::shared_ptr<Expr_ASTNode> src1;
std::shared_ptr<Expr_ASTNode> src2;
};
class AssignExpr_ASTNode : public Expr_ASTNode {
std::shared_ptr<Expr_ASTNode> dest;
std::shared_ptr<Expr_ASTNode> src;
};
class ThisExpr_ASTNode : public BasicExpr_ASTNode {};
class ParenExpr_ASTNode : public BasicExpr_ASTNode {
std::shared_ptr<Expr_ASTNode> expr;
};
class IDExpr_ASTNode : public BasicExpr_ASTNode {
IdentifierType id;
};
class FunctionCallExpr_ASTNode : public BasicExpr_ASTNode {
IdentifierType func_name;
std::vector<std::shared_ptr<Expr_ASTNode>> arguments;
};
class FormattedStringExpr_ASTNode : public BasicExpr_ASTNode {
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 {
std::vector<std::variant<std::shared_ptr<ArrayConstantType>, NullType, AtomicConstantType>> elements;
size_t level;
};
class ConstantExpr_ASTNode : public BasicExpr_ASTNode {
std::variant<AtomicConstantType, ArrayConstantType> value;
};
#endif // EXPR_ASTNODE_H

View File

@ -0,0 +1,42 @@
#ifndef STATEMENT_ASTNODE_H
#define STATEMENT_ASTNODE_H
#include <vector>
#include "astnode.h"
#include "expr_astnode.h"
class Statement_ASTNode : public ASTNodeBase {
public:
virtual ~Statement_ASTNode() = default;
};
class EmptyStatement_ASTNode : public Statement_ASTNode {};
class DefinitionStatement_ASTNode : public Statement_ASTNode {
ExprTypeInfo type;
std::vector<std::pair<IdentifierType, std::shared_ptr<Expr_ASTNode>>> vars;
};
class ExprStatement_ASTNode : public Statement_ASTNode {
std::shared_ptr<Expr_ASTNode> expr;
};
class IfStatement_ASTNode : public Statement_ASTNode {
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 {
std::shared_ptr<Expr_ASTNode> condition;
std::shared_ptr<Statement_ASTNode> loop_body;
};
class ForStatement_ASTNode : public Statement_ASTNode {
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 {
std::shared_ptr<Expr_ASTNode> return_value;
};
class SuiteStatement_ASTNode : public Statement_ASTNode {
std::vector<std::shared_ptr<Statement_ASTNode>> statements;
};
#endif // STATEMENT_ASTNODE_H

View File

@ -0,0 +1,27 @@
#ifndef STRUCTURAL_ASTNODE_H
#define STRUCTURAL_ASTNODE_H
#include <memory>
#include <variant>
#include <vector>
#include "astnode.h"
#include "expr_astnode.h"
#include "statement_astnode.h"
class FuncDef_ASTNode : public ASTNodeBase {
IdentifierType func_name;
std::vector<std::pair<IdentifierType, ExprTypeInfo>> params;
std::shared_ptr<SuiteStatement_ASTNode> func_body;
};
class Constructor_ASTNode : public FuncDef_ASTNode {};
class ClassVariable_ASTNode : public DefinitionStatement_ASTNode {};
class ClassDef_ASTNode : public ASTNodeBase {
private:
using ClassElement = std::variant<std::shared_ptr<Constructor_ASTNode>, std::shared_ptr<ClassVariable_ASTNode>,
std::shared_ptr<FuncDef_ASTNode>>;
std::vector<ClassElement> elements;
};
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;
};
#endif