Start adding AST and Result

This commit is contained in:
Sofia 2026-04-02 00:31:19 +03:00
parent 856709e46a
commit 04a6ec1ded
7 changed files with 124 additions and 4 deletions

View File

@ -6,10 +6,17 @@
"${workspaceFolder}/**" "${workspaceFolder}/**"
], ],
"defines": [], "defines": [],
"compilerPath": "/usr/bin/clang", "compilerPath": "/usr/bin/gcc",
"cStandard": "c17", "cStandard": "c17",
"cppStandard": "c++20", "cppStandard": "c++20",
"intelliSenseMode": "linux-clang-x86" "intelliSenseMode": "linux-gcc-x86",
"compilerArgs": [
"-Wall",
"-Weffc++",
"-Wextra",
"-Wconversion",
"-Wsign-conversion"
]
} }
], ],
"version": 4 "version": 4

View File

@ -15,7 +15,7 @@ separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST}) add_definitions(${LLVM_DEFINITIONS_LIST})
# Executable # Executable
add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp) add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp src/parsing.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
# Find the libraries that correspond to the LLVM components # Find the libraries that correspond to the LLVM components

46
src/ast.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef AST_H
#define AST_H
#include <string>
#include <vector>
#include <memory>
namespace AST {
class Node {};
class Expression : Node {};
class Type {};
class Statement : Node {};
class IntLiteralExpression : Expression {
private:
int m_value;
};
class ReturnStatement : Statement {
private:
std::unique_ptr<Expression> m_expr;
};
class TopLevelStatement : Node {};
class Function : TopLevelStatement {
private:
std::unique_ptr<Type> m_return_ty;
std::vector<std::pair<std::string, std::unique_ptr<Type>>> m_params;
std::string m_name;
std::vector<std::unique_ptr<Statement>> m_statements;
};
enum class FundamentalTypeKind {
Int,
};
class FundamentalType : Type {
private:
FundamentalTypeKind m_ty;
};
}
#endif

View File

@ -17,6 +17,7 @@
#include <cctype> #include <cctype>
#include "tokens.h" #include "tokens.h"
#include "parsing.h"
void llvm_hello_world(); void llvm_hello_world();
@ -51,7 +52,13 @@ int main() {
} }
auto stream = token::TokenStream{ tokens }; auto stream = token::TokenStream{ tokens };
stream.expect(token::Type::Eof);
std::vector<AST::TopLevelStatement> statements;
auto statement = parsing::parse_top_level_statement(stream);
while (statement.ok()) {
statements.push_back(statement.unwrap());
statement = parsing::parse_top_level_statement(stream);
}
// LLVM Hello World // LLVM Hello World
// llvm_hello_world(); // llvm_hello_world();

8
src/parsing.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "parsing.h"
namespace parsing {
Result<AST::TopLevelStatement, std::string> parse_top_level_statement(token::TokenStream& stream) {
return Result<AST::TopLevelStatement, std::string>{ "test" };
}
}

12
src/parsing.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef PARSING_H
#define PARSING_H
#include "ast.h"
#include "result.h"
#include "tokens.h"
namespace parsing {
Result<AST::TopLevelStatement, std::string> parse_top_level_statement(token::TokenStream& stream);
}
#endif

40
src/result.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef RESULT_H
#define RESULT_H
#include <memory>
template<typename T, typename TErr>
class Result {
private:
std::unique_ptr<T> m_value;
std::unique_ptr<TErr> m_error;
public:
Result(T value) : m_value{ std::make_unique(value) }, m_error{ nullptr } {}
Result(TErr error) : m_value{ nullptr }, m_error{ std::make_unique<TErr>(error) } {}
bool ok() {
return m_error == nullptr;
}
T unwrap() {
if (m_value) {
return *m_value;
}
else {
throw std::runtime_error("Tried to unwrap " + *m_error + "!");
}
}
TErr unwrap_err() {
if (m_error) {
return *m_error;
}
else {
throw std::runtime_error("Tried to unwrap_err a value!");
}
}
};
#endif