From e7aa2a1ca4c54aad8ee7ed75aff7edf158fa039a Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sun, 14 Dec 2025 16:07:58 +1100 Subject: [PATCH] puts command --- src/main.cpp | 41 +++++++++++++++++++++++++++++++++++------ tests/math.hg | 3 ++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 82fefff..ba5a6e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,8 @@ #include #include +#define parseOneToken(token) Parser({token.value()}).parse().children[0] + namespace HighGround { int tmpIdIterator = 0; @@ -16,7 +18,7 @@ namespace HighGround { namespace Parser { enum class HGNodeType { - Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock + Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock, Puts }; enum class HGDataType { @@ -161,9 +163,18 @@ namespace HighGround { code.push_back(codeBlock); break; } - default: { - std::cout << "Not implemented yet\n"; + case HGNodeType::Puts: { + HGGroundCodeBlock codeBlock; + GroundInstruction gi = groundCreateInstruction(PRINTLN); + if (children.size() < 1) { + std::cout << "Need more stuff to puts\n"; + } + groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data())); + codeBlock.code.push_back(gi); + code.push_back(codeBlock); + break; } + default: {} } return code; } @@ -257,6 +268,9 @@ namespace HighGround { if (in == "+") { return HGNodeType::Add; } + if (in == "puts") { + return HGNodeType::Puts; + } return HGNodeType::None; } @@ -311,13 +325,29 @@ namespace HighGround { rootNode.children.pop_back(); auto tokenopt = consume(); if (tokenopt) { - addNode.addNode(Parser({tokenopt.value()}).parse().children[0]); + addNode.addNode(parseOneToken(tokenopt)); } else { std::cout << "FEED ME MORE TOKENS\n"; } rootNode.addNode(addNode); break; } + case HGNodeType::Puts: { + HGNode putsNode(HGNodeType::Puts); + std::vector tokens; + while (auto tokenopt = consume()) { + if (tokenopt.value() == "\n") { + break; + } + tokens.push_back(tokenopt.value()); + } + auto children = Parser(tokens).parse(); + for (auto& child : children.children) { + putsNode.addNode(child); + } + rootNode.addNode(putsNode); + break; + } } } return rootNode; @@ -332,7 +362,6 @@ namespace HighGround { groundAddInstructionToProgram(&gp, inst); } } - groundAddInstructionToProgram(&gp, groundCreateInstruction(PAUSE)); return gp; } @@ -369,6 +398,7 @@ namespace HighGround { char c = copt.value(); switch (c) { // tokens which are not followed by anything + case '\n': case '(': case ')': case '{': @@ -425,7 +455,6 @@ namespace HighGround { } // tokens which do not need to be included case ' ': - case '\n': { if (!buf.empty()) { tokens.push_back(buf); diff --git a/tests/math.hg b/tests/math.hg index 8d2f097..5792ae6 100644 --- a/tests/math.hg +++ b/tests/math.hg @@ -1 +1,2 @@ -1 + 1 +puts 1 + 1 +