From 23c78a3c353aaf623b1df3e5b1a288a17b4418c5 Mon Sep 17 00:00:00 2001 From: Maxwell Morgan Date: Sat, 5 Sep 2026 19:36:07 -0400 Subject: [PATCH] feat(ast): add Stmt and Expr structs --- compiler/CMakeLists.txt | 2 + compiler/include/phonk/ast/expression.hpp | 63 +++++++++++++++++++++++ compiler/include/phonk/ast/statement.hpp | 45 ++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 compiler/include/phonk/ast/expression.hpp create mode 100644 compiler/include/phonk/ast/statement.hpp diff --git a/compiler/CMakeLists.txt b/compiler/CMakeLists.txt index a547e69..1628e8c 100644 --- a/compiler/CMakeLists.txt +++ b/compiler/CMakeLists.txt @@ -4,6 +4,8 @@ add_library(phonkc STATIC src/diagnostics/diagnostic_engine.cpp src/lexer/lexer.cpp src/compiler.cpp + include/phonk/ast/expression.hpp + include/phonk/ast/statement.hpp ) target_include_directories(phonkc PUBLIC include) diff --git a/compiler/include/phonk/ast/expression.hpp b/compiler/include/phonk/ast/expression.hpp new file mode 100644 index 0000000..d307ccd --- /dev/null +++ b/compiler/include/phonk/ast/expression.hpp @@ -0,0 +1,63 @@ +// +// Created by Maxwell Morgan on 9/5/26. +// + +#ifndef PHONK_EXPRESSION_HPP +#define PHONK_EXPRESSION_HPP + +#include +#include +#include +#include + +#include +#include + +namespace phonk::ast { + +struct Expr; + +// Expressions can contain other expressions +using ExprPtr = std::unique_ptr; + +using LiteralValue = std::variant< + std::int64_t, + double, + std::string, + bool>; + +struct LiteralExpr { + LiteralValue value; +}; + +struct IdentifierExpr { + lexer::Token name; +}; + +struct UnaryExpr { + lexer::Token op; + ExprPtr operand; +}; + +struct BinaryExpr { + ExprPtr left; + lexer::Token op; + ExprPtr right; +}; + + +// Eliminates need for many classes and inheritance +using ExprNode = std::variant< + LiteralExpr, + IdentifierExpr, + UnaryExpr, + BinaryExpr>; + +struct Expr { + source::SourceRange range; + ExprNode node; +}; + +} + +#endif // PHONK_EXPRESSION_HPP \ No newline at end of file diff --git a/compiler/include/phonk/ast/statement.hpp b/compiler/include/phonk/ast/statement.hpp new file mode 100644 index 0000000..4110d7a --- /dev/null +++ b/compiler/include/phonk/ast/statement.hpp @@ -0,0 +1,45 @@ +// +// Created by Maxwell Morgan on 9/5/26. +// + +#ifndef PHONK_STATEMENT_HPP +#define PHONK_STATEMENT_HPP + +#include +#include +#include + +#include +#include + +namespace phonk::ast { + +struct Stmt; + +using StmtPtr = std::unique_ptr; + +struct ExprStmt { + ExprPtr expression; +}; + +struct BlockStmt { + std::vector statements; +}; + +struct ReturnStmt { + ExprPtr value; +}; + +using StmtNode = std::variant< + ExprStmt, + BlockStmt, + ReturnStmt>; + +struct Stmt { + source::SourceRange range; + StmtNode node; +}; + +} + +#endif // PHONK_STATEMENT_HPP \ No newline at end of file