Variables can now have an initializer

This commit is contained in:
Pasha Bibko
2025-05-08 15:04:01 +01:00
parent 141bb1530b
commit 794173e14f
2 changed files with 12 additions and 6 deletions

View File

@@ -129,10 +129,15 @@ namespace LX
p.index++; // Skips over Token::ASSIGN p.index++; // Skips over Token::ASSIGN
// Gets the value to be assigned to the variable // // Gets the value to be assigned to the variable //
std::unique_ptr<AST::Node> defaultVal = ParsePrimary(p); std::unique_ptr<AST::Node> defaultVal = ParseOperation(p);
ThrowIf<UnexpectedToken>(defaultVal.get() == nullptr, Token::UNDEFINED, p.tokens[p.index - 1], "value", p); ThrowIf<UnexpectedToken>(defaultVal.get() == nullptr, Token::UNDEFINED, p.tokens[p.index - 1], "value", p);
return std::make_unique<AST::VariableDeclaration>(name); // Creates a multi-node of the variable creation and assignment //
std::unique_ptr<AST::MultiNode> node = std::make_unique<AST::MultiNode>();
node->nodes.push_back(std::make_unique<AST::VariableDeclaration>(name));
node->nodes.push_back(std::make_unique<AST::VariableAssignment>(name, std::move(defaultVal)));
return node;
} }
// Else goes down the call stack // // Else goes down the call stack //
@@ -181,7 +186,7 @@ namespace LX
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, const std::filesystem::path& path) FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, const std::filesystem::path& path)
{ {
// Logs the start of the parsing // Logs the start of the parsing
Log::LogNewSection("Started parsing tokens"); Log::LogNewSection("Started parsing tokens");
// Creates the output storer and the parser // // Creates the output storer and the parser //
FileAST output; FileAST output;
@@ -236,7 +241,7 @@ namespace LX
for (std::unique_ptr<AST::Node>& containedNode : ((AST::MultiNode*)node.get())->nodes) for (std::unique_ptr<AST::Node>& containedNode : ((AST::MultiNode*)node.get())->nodes)
{ {
// Logs the node to the log // // Logs the node to the log //
node->Log(0); containedNode->Log(0);
// Adds it to the vector // // Adds it to the vector //
func.body.push_back(std::move(containedNode)); func.body.push_back(std::move(containedNode));

View File

@@ -1,8 +1,9 @@
func main() func main()
{ {
int a = 65465 int a
a = 5 + 2
int b = 6 int b = 6 + 1
return a + b return a + b
} }