From ce8f1619a4bba001c57de06f8aef21a4941b219c Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Mon, 28 Apr 2025 21:16:54 +0100 Subject: [PATCH] Variables can now be read from --- Parser/src/AST-LLVM.cpp | 5 ++++- Parser/src/Parser.cpp | 4 ++++ README.md | 2 +- example/main.lx | 12 ++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Parser/src/AST-LLVM.cpp b/Parser/src/AST-LLVM.cpp index e5d1525..2adb8ae 100644 --- a/Parser/src/AST-LLVM.cpp +++ b/Parser/src/AST-LLVM.cpp @@ -92,6 +92,9 @@ namespace LX::AST llvm::Value* VariableAccess::GenIR(InfoLLVM& LLVM) { - return nullptr; + llvm::AllocaInst* var = LLVM.scope->GetVar(m_Name); + ThrowIf(var == nullptr); + + return LLVM.builder.CreateLoad(llvm::Type::getInt32Ty(LLVM.context), var, m_Name + "_val"); } } diff --git a/Parser/src/Parser.cpp b/Parser/src/Parser.cpp index 9934497..ddfb8af 100644 --- a/Parser/src/Parser.cpp +++ b/Parser/src/Parser.cpp @@ -42,6 +42,10 @@ namespace LX case Token::NUMBER_LITERAL: return std::make_unique(p.tokens[p.index++].GetContents()); + // If an Identifier has got here it means a variable is being accessed // + case Token::IDENTIFIER: + return std::make_unique(p.tokens[p.index++].GetContents()); + // TODO: Fix this // case Token::OPEN_BRACKET: p.scopeDepth++; diff --git a/README.md b/README.md index ad59f17..1fa9995 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,10 @@ This is my custom compiled language written in C++ based off of the LLVM toolchain. Do not use the language in it's current state unless you are insane. ### Planned features -- Variables - Operations (Maths + BinOp) - Functions - More than just int as a type +- References / Pointers - Structs / Classes (Polymorphism + vtables) ### Stuff I want to do later diff --git a/example/main.lx b/example/main.lx index bf8aa63..6aa1f43 100644 --- a/example/main.lx +++ b/example/main.lx @@ -1,5 +1,13 @@ func main() { - int result - result = 4 + int a + a = 4 + + int b + b = 7 + + int c + c = a + b + + return c }