From 57f5466506b707bfb673c2b2fbb9626d7efc37af Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sun, 14 Dec 2025 17:17:19 +1100 Subject: [PATCH] Start work on if --- src/main.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-- tests/data.hg | 10 ++++---- tests/math.hg | 3 +-- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ba5a6e6..ea5514e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ namespace HighGround { namespace Parser { enum class HGNodeType { - Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock, Puts + Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock, CodeBlockStart, CodeBlockEnd, Puts }; enum class HGDataType { @@ -271,6 +271,15 @@ namespace HighGround { if (in == "puts") { return HGNodeType::Puts; } + if (in == "if") { + return HGNodeType::If; + } + if (in == "{") { + return HGNodeType::CodeBlockStart; + } + if (in == "}") { + return HGNodeType::CodeBlockEnd; + } return HGNodeType::None; } @@ -328,6 +337,7 @@ namespace HighGround { addNode.addNode(parseOneToken(tokenopt)); } else { std::cout << "FEED ME MORE TOKENS\n"; + exit(1); } rootNode.addNode(addNode); break; @@ -348,6 +358,54 @@ namespace HighGround { rootNode.addNode(putsNode); break; } + case HGNodeType::If: { + HGNode ifNode(HGNodeType::If); + std::vector tokens; + while (auto tokenopt = consume()) { + if (tokenopt.value() == "\n") { + break; + } + tokens.push_back(tokenopt.value()); + } + auto children = Parser(tokens).parse(); + if (children.children.size() != 1) { + std::cout << "Too many or too little conditions for if\n"; + exit(1); + } + ifNode.addNode(children.children[0]); + tokens.clear(); + size_t brackets = 1; + auto tokenopt = consume(); + if (tokenopt) { + if (tokenopt.value() == "{") { + tokens.push_back(tokenopt.value()); + while (auto tokenopt = consume()) { + tokens.push_back(tokenopt.value()); + if (tokenopt.value() == "{") { + brackets++; + } + if (tokenopt.value() == "}") { + brackets--; + } + if (brackets == 0) { + break; + } + } + } else { + std::cout << "I want a code block\n"; + exit(1); + } + } else { + std::cout << "FEED ME MORE TOKENSSSSS\n"; + exit(1); + } + break; + } + case HGNodeType::CodeBlockStart: { + HGNode codeBlockNode(HGNodeType::CodeBlock); + // WIP + break; + } } } return rootNode; @@ -401,7 +459,6 @@ namespace HighGround { case '\n': case '(': case ')': - case '{': case '}': { if (!buf.empty()) { @@ -453,6 +510,16 @@ namespace HighGround { tokens.push_back(newToken); break; } + // tokens which need a newline inserted for them + case '{': + { + if (!buf.empty()) { + tokens.push_back(buf); + buf.clear(); + } + tokens.push_back(std::string(1, c)); + tokens.push_back("\n"); + } // tokens which do not need to be included case ' ': { diff --git a/tests/data.hg b/tests/data.hg index 5509d65..79332ec 100644 --- a/tests/data.hg +++ b/tests/data.hg @@ -1,5 +1,5 @@ -"dingus" -432 -3.141 -'c' -true +puts "dingus" +puts 432 +puts 3.141 +puts 'c' +puts true diff --git a/tests/math.hg b/tests/math.hg index 5792ae6..f6c721b 100644 --- a/tests/math.hg +++ b/tests/math.hg @@ -1,2 +1 @@ -puts 1 + 1 - +puts 1 + 1 + 463278