|
| 1 | +#include "ast/ast.hpp" |
| 2 | + |
| 3 | +namespace sleaf { |
| 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, |
| 61 | + 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 | +} |
| 177 | + |
| 178 | +// GroupingExpr implementation |
| 179 | +GroupingExpr::GroupingExpr(std::unique_ptr<Expr> expression) |
| 180 | + : expression(std::move(expression)) {} |
| 181 | + |
| 182 | +void GroupingExpr::accept(ASTVisitor& visitor) { |
| 183 | + visitor.visit(*this); |
| 184 | +} |
| 185 | + |
| 186 | +TokenType GroupingExpr::get_type() const { |
| 187 | + return expression->get_type(); |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace sleaf |
0 commit comments