commit 4b33b1f31862cd505fd9c90cf8d2e696c6f6ec97
parent 5c1f937a7eb7a9cc9cd86cb69b3263f41f24408f
Author: Cori Barker <coribarker2@gmail.com>
Date: Mon, 23 Feb 2026 15:45:03 +0000
partial refactor of ast_node.hpp
Diffstat:
| A | #test.bl# | | | 3 | +++ |
| A | bnf | | | 116 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | include/ast_node.hpp | | | 46 | ++++++++++++++++++++++++++++++++++++++++++---- |
3 files changed, 161 insertions(+), 4 deletions(-)
diff --git a/#test.bl# b/#test.bl#
@@ -0,0 +1,3 @@
+int a = 6;
+int b = 7;
+int c = a * b;
diff --git a/bnf b/bnf
@@ -0,0 +1,115 @@
+<program> ::= { <function_declaration> }
+
+<function_declaration> ::=
+ "function" <identifier> "(" [ <parameter_list> ] ")"
+ "->" <type>
+ <block>
+
+<parameter_list> ::= <parameter> { "," <parameter> }
+
+<parameter> ::= <type> <identifier>
+
+<type> ::= "int"
+ | "bool"
+
+
+<block> ::= "{"
+ <statement_list>
+ "}"
+
+<statement_list> ::= { <statement> }
+
+<statement> ::= <variable_declaration>
+ | <assignment_statement>
+ | <if_statement>
+ | <while_statement>
+ | <for_statement>
+ | <return_statement>
+ | <expression_statement>
+
+
+<variable_declaration> ::=
+ <type> <identifier> [ "=" <expression> ] ";"
+
+<assignment_statement> ::=
+ <identifier> "=" <expression> ";"
+
+<return_statement> ::=
+ "return" [ <expression> ] ";"
+
+<expression_statement> ::=
+ <expression> ";"
+
+
+<if_statement> ::=
+ "if" "(" <expression> ")"
+ <block>
+ [ "else" <block> ]
+
+<while_statement> ::=
+ "while" "(" <expression> ")"
+ <block>
+
+<for_statement> ::=
+ "for" "("
+ [ <for_init> ]
+ ";"
+ [ <expression> ]
+ ";"
+ [ <expression> ]
+ ")"
+ <block>
+
+<for_init> ::= <type> <identifier> [ "=" <expression> ]
+ | <expression>
+
+
+<expression> ::= <logical_or>
+
+<logical_or> ::= <logical_and>
+ { "||" <logical_and> }
+
+<logical_and> ::= <equality>
+ { "&&" <equality> }
+
+<equality> ::= <comparison>
+ { ( "==" | "!=" ) <comparison> }
+
+<comparison> ::= <term>
+ { ( "<" | ">" | "<=" | ">=" ) <term> }
+
+<term> ::= <factor>
+ { ( "+" | "-" ) <factor> }
+
+<factor> ::= <unary>
+ { ( "*" | "/" | "%" ) <unary> }
+
+<unary> ::= ( "!" | "-" ) <unary>
+ | <primary>
+
+<primary> ::= <number>
+ | <boolean>
+ | <identifier>
+ | <function_call>
+ | "(" <expression> ")"
+
+
+<function_call> ::=
+ <identifier> "(" [ <argument_list> ] ")"
+
+<argument_list> ::= <expression>
+ { "," <expression> }
+
+
+<identifier> ::= <letter> { <letter> | <digit> }
+
+<number> ::= <digit> { <digit> }
+
+<boolean> ::= "true"
+ | "false"
+
+<letter> ::= "a" | ... | "z"
+ | "A" | ... | "Z"
+ | "_"
+
+<digit> ::= "0" | ... | "9"
+\ No newline at end of file
diff --git a/include/ast_node.hpp b/include/ast_node.hpp
@@ -10,7 +10,7 @@
class ASTNode {
public:
int line;
- int column;
+ int col;
virtual ~ASTNode() = default;
};
@@ -19,7 +19,7 @@ class ProgramNode : ASTNode {
public:
std::vector<ASTNode> function_declarations;
- explicit ProgramNode(std::vector<ASTNode> f) : function_declarations(f) { }
+ ProgramNode(std::vector<ASTNode> function_declarations, int line, int col) : function_declarations(function_declarations), line(line), col(col) { }
};
class FunctionDeclNode : ASTNode {
@@ -27,10 +27,48 @@ class FunctionDeclNode : ASTNode {
std::vector<ParameterNode> parameters;
std::vector<ASTNode> body;
- FunctionDeclNode(std::string t,std::vector<ASTNode> p, std::vector<ASTNode> b) : type(t), parameters(p), body(b) { }
+ FunctionDeclNode(Type type, std::vector<ASTNode> parameters, std::vector<ASTNode> body, int line, int col) : type(type), parameters(parameters), body(body), line(line), col(col) { }
};
class ParameterNode : ASTNode {
std::string name;
- std::string type;
+ Type type;
+
+ ParameterNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line) col(col) { }
+};
+
+class VarDeclNode : ASTNode {
+ std::string name;
+ Type type;
+
+ VarDeclNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line), col(col) { }
+};
+
+class AssignmentNode : ASTNode {
+ std::string var_name;
+ Type var_type;
+ ASTNode* value;
+
+ AssignmentNode(std::string var_name, Type var_type, ASTNode* value) : var_name(var_name), var_type(var_type), value(value), line(line), col(col) { }
+};
+
+class IfStatementNode : ASTNode {
+ ASTNode* expression;
+ ASTNode* body;
+ ASTNode* else_statement = nullptr;
+ ASTNode* elif_statement = nullptr;
+
+ IfStatementNode(ASTNode* expression, ASTNode* body, ASTNode* else_statement, ASTNode* elif_statement, int line, int col) : expression(expression), body(body), else_statement(else_statement), elif_statement(elif_statement), line(line), col(col) { }
+};
+
+class WhileStatementNode : ASTNode {
+ ASTNode* expression;
+ ASTNode* body;
+
+ WhileStatementNode(ASTNode* expression, ASTNode* body, int line, int col) : expression(expression), body(body), line(line), col(col) { }
+};
+
+class ForStatementNode : ASTNode {
+ StatementNode* init;
+ ASTNode*
};