-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.hpp
151 lines (126 loc) · 3.43 KB
/
ast.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once
#include <iostream>
#include <vector>
namespace ast {
enum class NodeType {
// STATEMENTS
Program = 0,
VarDeclaration,
// EXPRESSIONS
AssignmentExpr,
MemberExpr,
CallExpr,
// Literals
Property,
ObjectLiteral,
NumericLiteral,
NullLiteral,
Identifier,
BinaryExpr
};
class Stmt {
public:
std::string value;
NodeType kind;
Stmt(NodeType type) : kind(type) {}
Stmt() {}
virtual ~Stmt() {}
};
class Expr : public Stmt {
public:
Expr(NodeType nodetype) : Stmt(nodetype){}
Expr(){}
virtual ~Expr() {}
};
// Defines a block that contains many statements.
// Only one program will be contained in a file.
class Program : public Stmt {
public:
std::vector< std::shared_ptr<Stmt> > body;
Program():Stmt(NodeType::Program) {}
virtual ~Program(){}
};
class VarDeclaration : public Stmt {
public:
NodeType kind;
std::string identifier;
bool constant;
std::shared_ptr<Expr> val;
VarDeclaration(std::string identifier, bool constant) :
Stmt(NodeType::VarDeclaration),
kind(NodeType::VarDeclaration),
identifier(identifier),
constant(constant){ }
VarDeclaration(std::string identifier, bool constant, std::shared_ptr<Expr> value) :
Stmt(NodeType::VarDeclaration),
kind(NodeType::VarDeclaration),
identifier(identifier),
constant(constant), val(value) { }
virtual ~VarDeclaration() { }
};
// A operation with two sides separated by an operator.
// Both sides can be ANY Complex Expression.
// Supported Operators -> + | - | / | * | %
class BinaryExpr : public Expr {
public:
std::shared_ptr<ast::Expr> left;
std::shared_ptr<ast::Expr> right;
std::string op;
std::string val;
BinaryExpr() : Expr(NodeType::BinaryExpr), left(nullptr), right(nullptr) {}
virtual ~BinaryExpr() {}
/*
BinaryExpr(BinaryExpr&& other) noexcept
: Expr(std::move(other)), left(std::move(other.left)),
right(std::move(other.right)),
op(std::move(other.op)) {}
BinaryExpr& operator=(BinaryExpr&& other) noexcept {
if (this != &other) {
Expr::operator=(std::move(other));
left = std::move(other.left);
right = std::move(other.right);
op = std::move(other.op);
}
return *this;
}*/
};
class AssignmentExpr : public Expr {
public:
NodeType kind;
std::shared_ptr<ast::Expr> assigne;
std::shared_ptr<ast::Expr> value;
AssignmentExpr(std::shared_ptr<ast::Expr> value,
std::shared_ptr<ast::Expr> assigne,
NodeType kind): kind(kind), value(value), assigne(assigne),
Expr(NodeType::AssignmentExpr) {}
virtual ~AssignmentExpr() {}
};
class CallExpr : Expr {
NodeType kind;
std::vector<Expr*> args;
Expr* caller;
};
class MemberExpr : Expr {
NodeType kind;
Expr* object;
Expr* property;
bool computed;
};
class NumericLiteral : public Expr {
public:
NumericLiteral() : Expr(NodeType::NumericLiteral) { }
virtual ~NumericLiteral() {}
};
class Identifier : public Expr {
public:
std::string symbol;
Identifier() : Expr(NodeType::Identifier) {}
};
class NullLiteral : public Expr {
public:
NodeType kind;
std::string value;
NullLiteral() : kind(NodeType::NullLiteral), value("null") {}
virtual ~NullLiteral() { }
};
} // namespace ast