Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 63 additions & 0 deletions compiler/include/phonk/ast/expression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Created by Maxwell Morgan on 9/5/26.
//

#ifndef PHONK_EXPRESSION_HPP
#define PHONK_EXPRESSION_HPP

#include <cstdint>
#include <memory>
#include <string>
#include <variant>

#include <phonk/lexer/token.hpp>
#include <phonk/source/source_range.hpp>

namespace phonk::ast {

struct Expr;

// Expressions can contain other expressions
using ExprPtr = std::unique_ptr<Expr>;

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
45 changes: 45 additions & 0 deletions compiler/include/phonk/ast/statement.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Created by Maxwell Morgan on 9/5/26.
//

#ifndef PHONK_STATEMENT_HPP
#define PHONK_STATEMENT_HPP

#include <memory>
#include <variant>
#include <vector>

#include <phonk/ast/expression.hpp>
#include <phonk/source/source_range.hpp>

namespace phonk::ast {

struct Stmt;

using StmtPtr = std::unique_ptr<Stmt>;

struct ExprStmt {
ExprPtr expression;
};

struct BlockStmt {
std::vector<StmtPtr> statements;
};

struct ReturnStmt {
ExprPtr value;
};

using StmtNode = std::variant<
ExprStmt,
BlockStmt,
ReturnStmt>;

struct Stmt {
source::SourceRange range;
StmtNode node;
};

}

#endif // PHONK_STATEMENT_HPP