|
2 | 2 |
|
3 | 3 | namespace sleaf {
|
4 | 4 |
|
5 |
| -// BlockStmt implementation |
6 |
| -BlockStmt::BlockStmt(std::vector<std::unique_ptr<Stmt>> stmts) |
7 |
| - : statements(std::move(stmts)) {} |
8 |
| - |
9 |
| -void BlockStmt::accept(ASTVisitor& visitor) { |
10 |
| - visitor.visit(*this); |
11 |
| -} |
12 |
| - |
13 |
| -// FunctionDecl implementation |
14 |
| -FunctionDecl::FunctionDecl(std::string name, |
15 |
| - std::vector<std::pair<std::string, TokenType>> params, |
16 |
| - TokenType return_type, |
17 |
| - std::unique_ptr<BlockStmt> body) |
18 |
| - : name(std::move(name)), |
19 |
| - params(std::move(params)), |
20 |
| - return_type(return_type), |
21 |
| - body(std::move(body)) {} |
22 |
| - |
23 |
| -void FunctionDecl::accept(ASTVisitor& visitor) { |
24 |
| - visitor.visit(*this); |
25 |
| -} |
26 |
| - |
27 |
| -// VarDecl implementation |
28 |
| -VarDecl::VarDecl(TokenType type, std::string name, |
29 |
| - std::unique_ptr<Expr> initializer, bool is_const) |
30 |
| - : type(type), |
31 |
| - name(std::move(name)), |
32 |
| - initializer(std::move(initializer)), |
33 |
| - is_const(is_const) {} |
34 |
| - |
35 |
| -void VarDecl::accept(ASTVisitor& visitor) { |
36 |
| - visitor.visit(*this); |
37 |
| -} |
38 |
| - |
39 |
| -// Parameter implementation |
40 |
| -Parameter::Parameter(std::string name, TokenType type) |
41 |
| - : name(std::move(name)), type(type) {} |
42 |
| - |
43 |
| -void Parameter::accept(ASTVisitor& visitor) { |
44 |
| - visitor.visit(*this); |
45 |
| -} |
46 |
| - |
47 |
| -// IfStmt implementation |
48 |
| -IfStmt::IfStmt(std::unique_ptr<Expr> condition, |
49 |
| - std::unique_ptr<Stmt> then_branch, |
50 |
| - std::unique_ptr<Stmt> else_branch) |
51 |
| - : condition(std::move(condition)), |
52 |
| - then_branch(std::move(then_branch)), |
53 |
| - else_branch(std::move(else_branch)) {} |
54 |
| - |
55 |
| -void IfStmt::accept(ASTVisitor& visitor) { |
56 |
| - visitor.visit(*this); |
57 |
| -} |
58 |
| - |
59 |
| -// WhileStmt implementation |
60 |
| -WhileStmt::WhileStmt(std::unique_ptr<Expr> condition, |
| 5 | + // BlockStmt implementation |
| 6 | + BlockStmt::BlockStmt(std::vector<std::unique_ptr<Stmt>> stmts) |
| 7 | + : statements(std::move(stmts)) {} |
| 8 | + |
| 9 | + void BlockStmt::accept(ASTVisitor& visitor) { |
| 10 | + visitor.visit(*this); |
| 11 | + } |
| 12 | + |
| 13 | + // FunctionDecl implementation |
| 14 | + FunctionDecl::FunctionDecl(std::string name, |
| 15 | + std::vector<std::pair<std::string, TokenType>> params, |
| 16 | + TokenType return_type, |
| 17 | + std::unique_ptr<BlockStmt> body) |
| 18 | + : name(std::move(name)) |
| 19 | + , params(std::move(params)) |
| 20 | + , return_type(return_type) |
| 21 | + , body(std::move(body)) {} |
| 22 | + |
| 23 | + void FunctionDecl::accept(ASTVisitor& visitor) { |
| 24 | + visitor.visit(*this); |
| 25 | + } |
| 26 | + |
| 27 | + // VarDecl implementation |
| 28 | + VarDecl::VarDecl(TokenType type, std::string name, std::unique_ptr<Expr> initializer, bool is_const) |
| 29 | + : type(type) |
| 30 | + , name(std::move(name)) |
| 31 | + , initializer(std::move(initializer)) |
| 32 | + , is_const(is_const) {} |
| 33 | + |
| 34 | + void VarDecl::accept(ASTVisitor& visitor) { |
| 35 | + visitor.visit(*this); |
| 36 | + } |
| 37 | + |
| 38 | + // Parameter implementation |
| 39 | + Parameter::Parameter(std::string name, TokenType type) |
| 40 | + : name(std::move(name)) |
| 41 | + , type(type) {} |
| 42 | + |
| 43 | + void Parameter::accept(ASTVisitor& visitor) { |
| 44 | + visitor.visit(*this); |
| 45 | + } |
| 46 | + |
| 47 | + // IfStmt implementation |
| 48 | + IfStmt::IfStmt(std::unique_ptr<Expr> condition, |
| 49 | + std::unique_ptr<Stmt> then_branch, |
| 50 | + std::unique_ptr<Stmt> else_branch) |
| 51 | + : condition(std::move(condition)) |
| 52 | + , then_branch(std::move(then_branch)) |
| 53 | + , else_branch(std::move(else_branch)) {} |
| 54 | + |
| 55 | + void IfStmt::accept(ASTVisitor& visitor) { |
| 56 | + visitor.visit(*this); |
| 57 | + } |
| 58 | + |
| 59 | + // WhileStmt implementation |
| 60 | + WhileStmt::WhileStmt(std::unique_ptr<Expr> condition, std::unique_ptr<Stmt> body) |
| 61 | + : condition(std::move(condition)) |
| 62 | + , body(std::move(body)) {} |
| 63 | + |
| 64 | + void WhileStmt::accept(ASTVisitor& visitor) { |
| 65 | + visitor.visit(*this); |
| 66 | + } |
| 67 | + |
| 68 | + // ForStmt implementation |
| 69 | + ForStmt::ForStmt(std::unique_ptr<VarDecl> initializer, |
| 70 | + std::unique_ptr<Expr> condition, |
| 71 | + std::unique_ptr<Expr> increment, |
61 | 72 | std::unique_ptr<Stmt> body)
|
62 |
| - : condition(std::move(condition)), |
63 |
| - body(std::move(body)) {} |
64 |
| - |
65 |
| -void WhileStmt::accept(ASTVisitor& visitor) { |
66 |
| - visitor.visit(*this); |
67 |
| -} |
68 |
| - |
69 |
| -// ForStmt implementation |
70 |
| -ForStmt::ForStmt(std::unique_ptr<VarDecl> initializer, |
71 |
| - std::unique_ptr<Expr> condition, |
72 |
| - std::unique_ptr<Expr> increment, |
73 |
| - std::unique_ptr<Stmt> body) |
74 |
| - : initializer(std::move(initializer)), |
75 |
| - condition(std::move(condition)), |
76 |
| - increment(std::move(increment)), |
77 |
| - body(std::move(body)) {} |
78 |
| - |
79 |
| -void ForStmt::accept(ASTVisitor& visitor) { |
80 |
| - visitor.visit(*this); |
81 |
| -} |
82 |
| - |
83 |
| -// ReturnStmt implementation |
84 |
| -ReturnStmt::ReturnStmt(std::unique_ptr<Expr> value) |
85 |
| - : value(std::move(value)) {} |
86 |
| - |
87 |
| -void ReturnStmt::accept(ASTVisitor& visitor) { |
88 |
| - visitor.visit(*this); |
89 |
| -} |
90 |
| - |
91 |
| -// ExpressionStmt implementation |
92 |
| -ExpressionStmt::ExpressionStmt(std::unique_ptr<Expr> expr) |
93 |
| - : expr(std::move(expr)) {} |
94 |
| - |
95 |
| -void ExpressionStmt::accept(ASTVisitor& visitor) { |
96 |
| - visitor.visit(*this); |
97 |
| -} |
98 |
| - |
99 |
| -// BinaryExpr implementation |
100 |
| -BinaryExpr::BinaryExpr(TokenType op, std::unique_ptr<Expr> left, std::unique_ptr<Expr> right) |
101 |
| - : op(op), left(std::move(left)), right(std::move(right)) {} |
102 |
| - |
103 |
| -void BinaryExpr::accept(ASTVisitor& visitor) { |
104 |
| - visitor.visit(*this); |
105 |
| -} |
106 |
| - |
107 |
| -TokenType BinaryExpr::get_type() const { |
108 |
| - // Type propagation rules |
109 |
| - if (left->get_type() == TokenType::F32 || right->get_type() == TokenType::F32 || |
110 |
| - left->get_type() == TokenType::F64 || right->get_type() == TokenType::F64) { |
111 |
| - return TokenType::F64; |
112 |
| - } |
113 |
| - return TokenType::I32; // Default to largest integer type |
114 |
| -} |
115 |
| - |
116 |
| -// AssignExpr implementation |
117 |
| -AssignExpr::AssignExpr(TokenType op, std::unique_ptr<Expr> target, std::unique_ptr<Expr> value) |
118 |
| - : op(op), target(std::move(target)), value(std::move(value)) {} |
119 |
| - |
120 |
| -void AssignExpr::accept(ASTVisitor& visitor) { |
121 |
| - visitor.visit(*this); |
122 |
| -} |
123 |
| - |
124 |
| -TokenType AssignExpr::get_type() const { |
125 |
| - return target->get_type(); |
126 |
| -} |
127 |
| - |
128 |
| -// UnaryExpr implementation |
129 |
| -UnaryExpr::UnaryExpr(TokenType op, std::unique_ptr<Expr> operand) |
130 |
| - : op(op), operand(std::move(operand)) {} |
131 |
| - |
132 |
| -void UnaryExpr::accept(ASTVisitor& visitor) { |
133 |
| - visitor.visit(*this); |
134 |
| -} |
135 |
| - |
136 |
| -TokenType UnaryExpr::get_type() const { |
137 |
| - return operand->get_type(); |
138 |
| -} |
139 |
| - |
140 |
| -// CallExpr implementation |
141 |
| -CallExpr::CallExpr(std::unique_ptr<Expr> callee, |
142 |
| - std::vector<std::unique_ptr<Expr>> arguments) |
143 |
| - : callee(std::move(callee)), arguments(std::move(arguments)) {} |
144 |
| - |
145 |
| -void CallExpr::accept(ASTVisitor& visitor) { |
146 |
| - visitor.visit(*this); |
147 |
| -} |
148 |
| - |
149 |
| -TokenType CallExpr::get_type() const { |
150 |
| - // In real implementation, would look up function return type |
151 |
| - return TokenType::I32; // Default return type |
152 |
| -} |
153 |
| - |
154 |
| -// Identifier implementation |
155 |
| -Identifier::Identifier(std::string name) : name(std::move(name)) {} |
156 |
| - |
157 |
| -void Identifier::accept(ASTVisitor& visitor) { |
158 |
| - visitor.visit(*this); |
159 |
| -} |
160 |
| - |
161 |
| -TokenType Identifier::get_type() const { |
162 |
| - // In real implementation, would look up in symbol table |
163 |
| - return TokenType::I32; // Default type |
164 |
| -} |
165 |
| - |
166 |
| -// Literal implementation |
167 |
| -Literal::Literal(TokenType type, std::string value) |
168 |
| - : type(type), value(std::move(value)) {} |
169 |
| - |
170 |
| -void Literal::accept(ASTVisitor& visitor) { |
171 |
| - visitor.visit(*this); |
172 |
| -} |
173 |
| - |
174 |
| -TokenType Literal::get_type() const { |
175 |
| - return type; |
176 |
| -} |
| 73 | + : initializer(std::move(initializer)) |
| 74 | + , condition(std::move(condition)) |
| 75 | + , increment(std::move(increment)) |
| 76 | + , body(std::move(body)) {} |
| 77 | + |
| 78 | + void ForStmt::accept(ASTVisitor& visitor) { |
| 79 | + visitor.visit(*this); |
| 80 | + } |
| 81 | + |
| 82 | + // ReturnStmt implementation |
| 83 | + ReturnStmt::ReturnStmt(std::unique_ptr<Expr> value) |
| 84 | + : value(std::move(value)) {} |
| 85 | + |
| 86 | + void ReturnStmt::accept(ASTVisitor& visitor) { |
| 87 | + visitor.visit(*this); |
| 88 | + } |
177 | 89 |
|
178 |
| -// GroupingExpr implementation |
179 |
| -GroupingExpr::GroupingExpr(std::unique_ptr<Expr> expression) |
180 |
| - : expression(std::move(expression)) {} |
| 90 | + // ExpressionStmt implementation |
| 91 | + ExpressionStmt::ExpressionStmt(std::unique_ptr<Expr> expr) |
| 92 | + : expr(std::move(expr)) {} |
| 93 | + |
| 94 | + void ExpressionStmt::accept(ASTVisitor& visitor) { |
| 95 | + visitor.visit(*this); |
| 96 | + } |
| 97 | + |
| 98 | + // BinaryExpr implementation |
| 99 | + BinaryExpr::BinaryExpr(TokenType op, std::unique_ptr<Expr> left, std::unique_ptr<Expr> right) |
| 100 | + : op(op) |
| 101 | + , left(std::move(left)) |
| 102 | + , right(std::move(right)) {} |
| 103 | + |
| 104 | + void BinaryExpr::accept(ASTVisitor& visitor) { |
| 105 | + visitor.visit(*this); |
| 106 | + } |
| 107 | + |
| 108 | + TokenType BinaryExpr::get_type() const { |
| 109 | + // Type propagation rules |
| 110 | + if (left->get_type() == TokenType::F32 || right->get_type() == TokenType::F32 |
| 111 | + || left->get_type() == TokenType::F64 || right->get_type() == TokenType::F64) |
| 112 | + { |
| 113 | + return TokenType::F64; |
| 114 | + } |
| 115 | + return TokenType::I32; // Default to largest integer type |
| 116 | + } |
181 | 117 |
|
182 |
| -void GroupingExpr::accept(ASTVisitor& visitor) { |
183 |
| - visitor.visit(*this); |
184 |
| -} |
| 118 | + // AssignExpr implementation |
| 119 | + AssignExpr::AssignExpr(TokenType op, std::unique_ptr<Expr> target, std::unique_ptr<Expr> value) |
| 120 | + : op(op) |
| 121 | + , target(std::move(target)) |
| 122 | + , value(std::move(value)) {} |
185 | 123 |
|
186 |
| -TokenType GroupingExpr::get_type() const { |
187 |
| - return expression->get_type(); |
188 |
| -} |
| 124 | + void AssignExpr::accept(ASTVisitor& visitor) { |
| 125 | + visitor.visit(*this); |
| 126 | + } |
| 127 | + |
| 128 | + TokenType AssignExpr::get_type() const { |
| 129 | + return target->get_type(); |
| 130 | + } |
| 131 | + |
| 132 | + // UnaryExpr implementation |
| 133 | + UnaryExpr::UnaryExpr(TokenType op, std::unique_ptr<Expr> operand) |
| 134 | + : op(op) |
| 135 | + , operand(std::move(operand)) {} |
| 136 | + |
| 137 | + void UnaryExpr::accept(ASTVisitor& visitor) { |
| 138 | + visitor.visit(*this); |
| 139 | + } |
| 140 | + |
| 141 | + TokenType UnaryExpr::get_type() const { |
| 142 | + return operand->get_type(); |
| 143 | + } |
| 144 | + |
| 145 | + // CallExpr implementation |
| 146 | + CallExpr::CallExpr(std::unique_ptr<Expr> callee, std::vector<std::unique_ptr<Expr>> arguments) |
| 147 | + : callee(std::move(callee)) |
| 148 | + , arguments(std::move(arguments)) {} |
| 149 | + |
| 150 | + void CallExpr::accept(ASTVisitor& visitor) { |
| 151 | + visitor.visit(*this); |
| 152 | + } |
| 153 | + |
| 154 | + TokenType CallExpr::get_type() const { |
| 155 | + // In real implementation, would look up function return type |
| 156 | + return TokenType::I32; // Default return type |
| 157 | + } |
| 158 | + |
| 159 | + // Identifier implementation |
| 160 | + Identifier::Identifier(std::string name) |
| 161 | + : name(std::move(name)) {} |
| 162 | + |
| 163 | + void Identifier::accept(ASTVisitor& visitor) { |
| 164 | + visitor.visit(*this); |
| 165 | + } |
| 166 | + |
| 167 | + TokenType Identifier::get_type() const { |
| 168 | + // In real implementation, would look up in symbol table |
| 169 | + return TokenType::I32; // Default type |
| 170 | + } |
| 171 | + |
| 172 | + // Literal implementation |
| 173 | + Literal::Literal(TokenType type, std::string value) |
| 174 | + : type(type) |
| 175 | + , value(std::move(value)) {} |
| 176 | + |
| 177 | + void Literal::accept(ASTVisitor& visitor) { |
| 178 | + visitor.visit(*this); |
| 179 | + } |
| 180 | + |
| 181 | + TokenType Literal::get_type() const { |
| 182 | + return type; |
| 183 | + } |
| 184 | + |
| 185 | + // GroupingExpr implementation |
| 186 | + GroupingExpr::GroupingExpr(std::unique_ptr<Expr> expression) |
| 187 | + : expression(std::move(expression)) {} |
| 188 | + |
| 189 | + void GroupingExpr::accept(ASTVisitor& visitor) { |
| 190 | + visitor.visit(*this); |
| 191 | + } |
| 192 | + |
| 193 | + TokenType GroupingExpr::get_type() const { |
| 194 | + return expression->get_type(); |
| 195 | + } |
189 | 196 |
|
190 |
| -} // namespace sleaf |
| 197 | +} // namespace sleaf |
0 commit comments