bl-compiler

compiler for the bl programming language.
Log | Files | Refs | README

commit 5b1a49ccc676d9a3564050e203a7a68bf0ece578
parent 8a95bb221d348966fe85a9735ae61722502e0d23
Author: Cori Barker <coribarker2@gmail.com>
Date:   Wed,  4 Mar 2026 15:26:45 +0000

removed bnf since not needed, and added some more nodes to the AST

Diffstat:
Dbnf | 116-------------------------------------------------------------------------------
Minclude/ast_node.hpp | 94++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
2 files changed, 64 insertions(+), 146 deletions(-)

diff --git a/bnf b/bnf @@ -1,115 +0,0 @@ -<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 @@ -13,62 +13,96 @@ public: int col; virtual ~ASTNode() = default; -}; +} class ProgramNode : ASTNode { public: - std::vector<ASTNode> function_declarations; + std::vector<FunctionDeclarationNode> function_declarations; - ProgramNode(std::vector<ASTNode> function_declarations, int line, int col) : function_declarations(function_declarations), line(line), col(col) { } + ProgramNode(std::vector<FunctionDeclarationNode> function_declarations, int line, int col) : function_declarations(function_declarations), line(line), col(col) { } }; -class FunctionDeclNode : ASTNode { - Type type; +class FunctionDeclarationNode : public ASTNode { +public: + std::string identifier; std::vector<ParameterNode> parameters; - std::vector<ASTNode> body; + Type return_type; + std::vector<StatementNode> body; - 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) { } + FunctionDeclarationNode(std::string identifier, std::vector<ParameterNode> parameters, Type return_type, std::vector<StatementNode> body, int line, int col) : identifier(identifier), parameters(parameters), return_type(return_type), body(body), line(line), col(col) { } }; -class ParameterNode : ASTNode { - std::string name; +class ParameterNode : public ASTNode { +public: Type type; + std::string identifier; - ParameterNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line) col(col) { } + ParameterNode(Type type, std::string identifier, int line, int column) : type(type), identifier(identifier), line(line), column(column) { } }; -class VarDeclNode : ASTNode { - std::string name; +class ArgumentNode : public ASTNode { + ASTNode* expression; + ASTNode* variable; + + ArgumentNode(ASTNode* expression, ASTNode* variable, int line int column) : expression(expression), variable(variable), line(line), column(column) { } +}; + +class VariableDeclarationNode : public ASTNode { +public: Type type; + std::string identifier; + ASTNode* value; - VarDeclNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line), col(col) { } + VariableDeclarationNode(Type type, std::string identifier, ASTNode* value, int line, int column) : type(type), identifier(identifier), value(value), line(line), column(column) { } }; -class AssignmentNode : ASTNode { - std::string var_name; - Type var_type; +class VariableAssignmentNode : public ASTNode { +public: + std::string identifier; 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) { } + + VariableAssignmentNode(std::string identifier, ASTNode* value, int line, int column) : identifier(identifier), value(value), line(line), column(column) { } }; -class IfStatementNode : ASTNode { +class ReturnNode : public ASTNode { +public: 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) { } + ReturnNode(ASTNode* expression, int line, int column) : expression(expression), line(line), column(column) { } }; -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 BinaryOperationNode : public ASTNode { + char operation; + ASTNode* left; + ASTNode* right; + + BinaryOperationNode(std::string operation, ASTNode* left, ASTNode* right, int line, int column) : operation(operation), left(left), right(right), line(line), column(column) { } }; -class ForStatementNode : ASTNode { - StatementNode* init; - ASTNode* +class UnaryOperationNode : public ASTNode { + char operation; + ASTNode* operand; + + UnaryOperationNode(char operation, ASTNode* operand, int line, int column) : operation(operation), operand(operand), line(line), column(column) { } +}; + +class StringLiteralNode : public ASTNode { + std::string value; + + StringLiteralNode(std::string value, int line, int column) : value(value), line(line), column(column) { } +}; + +class NumberLiteralNode : public ASTNode { + int value; + + NumberLiteralNode(int value, int line, int column) : value(value), line(line), column(column) { } +}; + +class ComparisonNode : public ASTNode { + std::string operation; + ASTNode* left; + ASTNode* right; + + ComparisonNode(std::string operation, ASTNode* left, ASTNode* right, int line, int col) : operation(operation), left(left), right(right), line(line), column(column) { } };