From 04a6ec1dedd6b72a983ae514745851f5d3b55623 Mon Sep 17 00:00:00 2001 From: Sofia Date: Thu, 2 Apr 2026 00:31:19 +0300 Subject: [PATCH] Start adding AST and Result --- .vscode/c_cpp_properties.json | 11 +++++++-- CMakeLists.txt | 2 +- src/ast.h | 46 +++++++++++++++++++++++++++++++++++ src/main.cpp | 9 ++++++- src/parsing.cpp | 8 ++++++ src/parsing.h | 12 +++++++++ src/result.h | 40 ++++++++++++++++++++++++++++++ 7 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 src/ast.h create mode 100644 src/parsing.cpp create mode 100644 src/parsing.h create mode 100644 src/result.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 5444ab6..32efc61 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,10 +6,17 @@ "${workspaceFolder}/**" ], "defines": [], - "compilerPath": "/usr/bin/clang", + "compilerPath": "/usr/bin/gcc", "cStandard": "c17", "cppStandard": "c++20", - "intelliSenseMode": "linux-clang-x86" + "intelliSenseMode": "linux-gcc-x86", + "compilerArgs": [ + "-Wall", + "-Weffc++", + "-Wextra", + "-Wconversion", + "-Wsign-conversion" + ] } ], "version": 4 diff --git a/CMakeLists.txt b/CMakeLists.txt index 667c981..92a2e8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS_LIST}) # 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) # Find the libraries that correspond to the LLVM components diff --git a/src/ast.h b/src/ast.h new file mode 100644 index 0000000..b3f5976 --- /dev/null +++ b/src/ast.h @@ -0,0 +1,46 @@ +#ifndef AST_H +#define AST_H + +#include +#include +#include + +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 m_expr; + }; + + class TopLevelStatement : Node {}; + + class Function : TopLevelStatement { + private: + std::unique_ptr m_return_ty; + std::vector>> m_params; + std::string m_name; + std::vector> m_statements; + }; + + enum class FundamentalTypeKind { + Int, + }; + + class FundamentalType : Type { + private: + FundamentalTypeKind m_ty; + }; +} + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 79b283e..04f8979 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include #include "tokens.h" +#include "parsing.h" void llvm_hello_world(); @@ -51,7 +52,13 @@ int main() { } auto stream = token::TokenStream{ tokens }; - stream.expect(token::Type::Eof); + + std::vector 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(); diff --git a/src/parsing.cpp b/src/parsing.cpp new file mode 100644 index 0000000..ff1e14f --- /dev/null +++ b/src/parsing.cpp @@ -0,0 +1,8 @@ +#include "parsing.h" + + +namespace parsing { + Result parse_top_level_statement(token::TokenStream& stream) { + return Result{ "test" }; + } +} \ No newline at end of file diff --git a/src/parsing.h b/src/parsing.h new file mode 100644 index 0000000..79391fb --- /dev/null +++ b/src/parsing.h @@ -0,0 +1,12 @@ +#ifndef PARSING_H +#define PARSING_H + +#include "ast.h" +#include "result.h" +#include "tokens.h" + +namespace parsing { + Result parse_top_level_statement(token::TokenStream& stream); +} + +#endif \ No newline at end of file diff --git a/src/result.h b/src/result.h new file mode 100644 index 0000000..acae79d --- /dev/null +++ b/src/result.h @@ -0,0 +1,40 @@ + +#ifndef RESULT_H +#define RESULT_H + +#include + +template +class Result { +private: + std::unique_ptr m_value; + std::unique_ptr 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(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 \ No newline at end of file