11#pragma once
2+
23#include < memory>
34#include < string>
45#include < vector>
56#include < variant>
67#include " tokens.hpp"
78
9+ namespace pseu ::ast {
810
911/* ======================
1012 * Literal Nodes
1113 * ====================== */
12- struct NumberNode ;
13- struct StringLiteralNode ;
14- struct IdentifierNode ;
15- struct BinOpNode ;
16- struct Statement ;
17- struct Condition ;
18- struct IfStatement ;
19- struct WhileStatement ;
20- struct PrintStatement ;
21- struct Assignment ;
22- struct Declaration ;
23-
24- using ASTNode = std::variant<
25- NumberNode,
26- StringLiteralNode,
27- IdentifierNode,
28- BinOpNode,
29- Statement,
30- Condition,
31- IfStatement,
32- WhileStatement,
33- PrintStatement,
34- Assignment,
35- Declaration
36- >;
37-
38- struct NumberNode final
39- {
40- Token tok;
41- explicit NumberNode (Token t) : tok(std::move(t)) {}
42- [[nodiscard]] std::string getValue () const { return tok.value ; }
43- };
44-
45- struct IdentifierNode final
46- {
47- Token tok;
48- explicit IdentifierNode (Token t) : tok(std::move(t)) {}
49- [[nodiscard]] std::string getValue () const { return tok.value ; }
50- };
51-
52-
53- struct StringLiteralNode final
54- {
55- Token tok;
56- explicit StringLiteralNode (Token t) : tok(std::move(t)) {}
57- [[nodiscard]] std::string getValue () const { return tok.value ; }
58- };
14+ struct NumberNode ;
15+ struct StringLiteralNode ;
16+ struct IdentifierNode ;
17+ struct BinOpNode ;
18+ struct Statement ;
19+ struct Condition ;
20+ struct IfStatement ;
21+ struct WhileStatement ;
22+ struct PrintStatement ;
23+ struct Assignment ;
24+ struct Declaration ;
25+
26+ using ASTNode = std::variant<
27+ NumberNode,
28+ StringLiteralNode,
29+ IdentifierNode,
30+ BinOpNode,
31+ Statement,
32+ Condition,
33+ IfStatement,
34+ WhileStatement,
35+ PrintStatement,
36+ Assignment,
37+ Declaration
38+ >;
39+
40+ struct NumberNode final {
41+ lexer::Token tok;
42+
43+ explicit NumberNode (lexer::Token t) : tok(std::move(t)) {}
44+
45+ [[nodiscard]] std::string getValue () const { return tok.value ; }
46+ };
47+
48+ struct IdentifierNode final {
49+ lexer::Token tok;
50+
51+ explicit IdentifierNode (lexer::Token t) : tok(std::move(t)) {}
52+
53+ [[nodiscard]] std::string getValue () const { return tok.value ; }
54+ };
55+
56+
57+ struct StringLiteralNode final {
58+ lexer::Token tok;
59+
60+ explicit StringLiteralNode (lexer::Token t) : tok(std::move(t)) {}
61+
62+ [[nodiscard]] std::string getValue () const { return tok.value ; }
63+ };
5964
6065
6166/* ======================
6267 * Binary Operation
6368 * ====================== */
6469
65- struct BinOpNode final
66- {
67- std::shared_ptr<ASTNode> left;
68- Token op_tok;
69- std::shared_ptr<ASTNode> right;
70- };
70+ struct BinOpNode final {
71+ std::shared_ptr<ASTNode> left;
72+ lexer::Token op_tok;
73+ std::shared_ptr<ASTNode> right;
74+ };
7175
7276
7377/* ======================
7478 * Statements List (linked tree)
7579 * ====================== */
7680
77- struct Statement final
78- {
79- std::shared_ptr<ASTNode> left;
80- std::shared_ptr<ASTNode> right; // may be null
81- };
81+ struct Statement final {
82+ std::shared_ptr<ASTNode> left;
83+ std::shared_ptr<ASTNode> right; // may be null
84+ };
8285
8386
8487/* ======================
@@ -87,19 +90,18 @@ struct Statement final
8790 * std::make_shared<Condition>(expr, token, expr)
8891 * ====================== */
8992
90- struct Condition final
91- {
92- std::shared_ptr<ASTNode> left_expression;
93- Token comparison;
94- std::shared_ptr<ASTNode> right_expression;
93+ struct Condition final {
94+ std::shared_ptr<ASTNode> left_expression;
95+ lexer::Token comparison;
96+ std::shared_ptr<ASTNode> right_expression;
9597
96- Condition (std::shared_ptr<ASTNode> left,
97- Token op,
98- std::shared_ptr<ASTNode> right)
99- : left_expression(std::move(left)),
100- comparison (std::move(op)),
101- right_expression(std::move(right)) {}
102- };
98+ Condition (std::shared_ptr<ASTNode> left,
99+ lexer:: Token op,
100+ std::shared_ptr<ASTNode> right)
101+ : left_expression(std::move(left)),
102+ comparison (std::move(op)),
103+ right_expression(std::move(right)) {}
104+ };
103105
104106
105107/* ======================
@@ -109,23 +111,21 @@ struct Condition final
109111 * else_body = Node* or null
110112 * ====================== */
111113
112- struct IfStatement final
113- {
114- std::shared_ptr<ASTNode> if_condition;
115- std::shared_ptr<ASTNode> if_body;
116- std::shared_ptr<ASTNode> else_body; // may be null
117- };
114+ struct IfStatement final {
115+ std::shared_ptr<ASTNode> if_condition;
116+ std::shared_ptr<ASTNode> if_body;
117+ std::shared_ptr<ASTNode> else_body; // may be null
118+ };
118119
119120
120121/* ======================
121122 * While
122123 * ====================== */
123124
124- struct WhileStatement final
125- {
126- std::shared_ptr<ASTNode> condition;
127- std::shared_ptr<ASTNode> body;
128- };
125+ struct WhileStatement final {
126+ std::shared_ptr<ASTNode> condition;
127+ std::shared_ptr<ASTNode> body;
128+ };
129129
130130
131131/* ======================
@@ -135,23 +135,21 @@ struct WhileStatement final
135135 * - type = "string" => strValue
136136 * ====================== */
137137
138- struct PrintStatement final
139- {
140- std::string type;
141- std::shared_ptr<ASTNode> intExpr; // if type == "int"
142- std::string strValue; // if type == "string"
143- };
138+ struct PrintStatement final {
139+ std::string type;
140+ std::shared_ptr<ASTNode> intExpr; // if type == "int"
141+ std::string strValue; // if type == "string"
142+ };
144143
145144
146145/* ======================
147146 * Assignment
148147 * ====================== */
149148
150- struct Assignment final
151- {
152- Token identifier; // e.g., T_VAR
153- std::shared_ptr<ASTNode> expression; // e.g., BinOpNode / NumberNode
154- };
149+ struct Assignment final {
150+ lexer::Token identifier; // e.g., T_VAR
151+ std::shared_ptr<ASTNode> expression; // e.g., BinOpNode / NumberNode
152+ };
155153
156154
157155/* ======================
@@ -165,10 +163,11 @@ struct Assignment final
165163 * - init_expr (optional)
166164 * ====================== */
167165
168- struct Declaration final
169- {
170- Token declaration_type; // "int" or "string"
171- std::vector<Token> identifiers; // list of var IDs
172- std::shared_ptr<ASTNode> init_expr; // only for int x = expr;
173- Declaration () : init_expr(nullptr ) {}
174- };
166+ struct Declaration final {
167+ lexer::Token declaration_type; // "int" or "string"
168+ std::vector<lexer::Token> identifiers; // list of var IDs
169+ std::shared_ptr<ASTNode> init_expr; // only for int x = expr;
170+ Declaration () : init_expr(nullptr ) {}
171+ };
172+
173+ } // namespace pseu::ast
0 commit comments