From a3a9553189c6c58d40eaa09b2ef3c67cb9bf32c8 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 20 Dec 2025 20:25:48 +1100 Subject: [PATCH] Function calling and input() builtin --- src/main.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++-- tests/input.sols | 13 +++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 tests/input.sols diff --git a/src/main.cpp b/src/main.cpp index 15b0132..a3f4d87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ namespace Solstice { namespace Parser { enum class SolNodeType { - Add, Subtract, Equal, Inequal, Set, While, If, Value, Identifier, None, Root, CodeBlock, CodeBlockStart, CodeBlockEnd, Puts + Add, Subtract, Equal, Inequal, Set, While, If, Value, Identifier, None, Root, CodeBlock, CodeBlockStart, CodeBlockEnd, FunctionCall, Expression, BracketStart, BracketEnd, Puts }; enum class SolDataType { @@ -84,9 +84,9 @@ namespace Solstice { }; class SolNode { - SolNodeType nodeType = SolNodeType::None; SolData data; public: + SolNodeType nodeType = SolNodeType::None; std::vector children; std::string outputId; SolNode(SolNodeType nodeType) : nodeType(nodeType) {} @@ -288,6 +288,25 @@ namespace Solstice { code.push_back(codeBlock); break; } + case SolNodeType::FunctionCall: { + // Take care of in built functions + if (children[0].outputId == "input") { + SolGroundCodeBlock inputCodeBlock; + if (children.size() > 1) { + GroundInstruction printInstruction = groundCreateInstruction(PRINT); + groundAddReferenceToInstruction(&printInstruction, groundCreateReference(VALREF, children[1].outputId.data())); + inputCodeBlock.code.push_back(printInstruction); + } + GroundInstruction inputInstruction = groundCreateInstruction(INPUT); + outputId = "tmp_" + std::to_string(tmpIdIterator++); + groundAddReferenceToInstruction(&inputInstruction, groundCreateReference(DIRREF, outputId.data())); + inputCodeBlock.code.push_back(inputInstruction); + code.push_back(inputCodeBlock); + break; + } + + break; + } default: {} } return code; @@ -406,6 +425,12 @@ namespace Solstice { if (in == "}") { return SolNodeType::CodeBlockEnd; } + if (in == "(") { + return SolNodeType::BracketStart; + } + if (in == ")") { + return SolNodeType::BracketEnd; + } return SolNodeType::Identifier; } @@ -638,6 +663,48 @@ namespace Solstice { rootNode.addNode(setNode); break; } + case SolNodeType::BracketStart: { + if (rootNode.children.back().nodeType == SolNodeType::Identifier) { + SolNode fnCallNode(SolNodeType::FunctionCall); + fnCallNode.addNode(rootNode.children.back()); + rootNode.children.pop_back(); + std::vector tokens; + size_t brackets = 1; + while (auto tokenopt = consume()) { + if (tokenopt.value() == "(") { + brackets++; + } + if (tokenopt.value() == ")") { + brackets--; + } + if (brackets < 1) { + break; + } + tokens.push_back(tokenopt.value()); + } + auto node = Parser(tokens).parse(); + fnCallNode.children.insert(fnCallNode.children.end(), node.children.begin(), node.children.end()); + rootNode.addNode(fnCallNode); + } else { + std::vector tokens; + size_t brackets = 1; + while (auto tokenopt = consume()) { + if (tokenopt.value() == "(") { + brackets++; + } + if (tokenopt.value() == ")") { + brackets--; + } + if (brackets < 1) { + break; + } + tokens.push_back(tokenopt.value()); + } + auto node = Parser(tokens).parse(); + node.nodeType = SolNodeType::Expression; + rootNode.addNode(node); + } + } } } return rootNode; @@ -798,7 +865,7 @@ namespace Solstice { }; -} // namespace HigSolround +} // namespace Solstice int main(int argc, char** argv) { diff --git a/tests/input.sols b/tests/input.sols new file mode 100644 index 0000000..3feacfa --- /dev/null +++ b/tests/input.sols @@ -0,0 +1,13 @@ +accessNotGranted = true + +while accessNotGranted { + password = input("Password: ") + if password == "dingus" { + accessNotGranted = false + } + if password != "dingus" { + puts "Incorrect password!" + } +} + +puts "Welcome!"