Function calling and input() builtin

This commit is contained in:
2025-12-20 20:25:48 +11:00
parent 3c66df5be0
commit a3a9553189
2 changed files with 83 additions and 3 deletions

View File

@@ -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<SolNode> 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<std::string> 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<std::string> 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) {

13
tests/input.sols Normal file
View File

@@ -0,0 +1,13 @@
accessNotGranted = true
while accessNotGranted {
password = input("Password: ")
if password == "dingus" {
accessNotGranted = false
}
if password != "dingus" {
puts "Incorrect password!"
}
}
puts "Welcome!"