forked from solstice/solstice
Function calling and input() builtin
This commit is contained in:
73
src/main.cpp
73
src/main.cpp
@@ -20,7 +20,7 @@ namespace Solstice {
|
|||||||
namespace Parser {
|
namespace Parser {
|
||||||
|
|
||||||
enum class SolNodeType {
|
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 {
|
enum class SolDataType {
|
||||||
@@ -84,9 +84,9 @@ namespace Solstice {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class SolNode {
|
class SolNode {
|
||||||
SolNodeType nodeType = SolNodeType::None;
|
|
||||||
SolData data;
|
SolData data;
|
||||||
public:
|
public:
|
||||||
|
SolNodeType nodeType = SolNodeType::None;
|
||||||
std::vector<SolNode> children;
|
std::vector<SolNode> children;
|
||||||
std::string outputId;
|
std::string outputId;
|
||||||
SolNode(SolNodeType nodeType) : nodeType(nodeType) {}
|
SolNode(SolNodeType nodeType) : nodeType(nodeType) {}
|
||||||
@@ -288,6 +288,25 @@ namespace Solstice {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
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: {}
|
default: {}
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
@@ -406,6 +425,12 @@ namespace Solstice {
|
|||||||
if (in == "}") {
|
if (in == "}") {
|
||||||
return SolNodeType::CodeBlockEnd;
|
return SolNodeType::CodeBlockEnd;
|
||||||
}
|
}
|
||||||
|
if (in == "(") {
|
||||||
|
return SolNodeType::BracketStart;
|
||||||
|
}
|
||||||
|
if (in == ")") {
|
||||||
|
return SolNodeType::BracketEnd;
|
||||||
|
}
|
||||||
return SolNodeType::Identifier;
|
return SolNodeType::Identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -638,6 +663,48 @@ namespace Solstice {
|
|||||||
rootNode.addNode(setNode);
|
rootNode.addNode(setNode);
|
||||||
break;
|
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;
|
return rootNode;
|
||||||
@@ -798,7 +865,7 @@ namespace Solstice {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} // namespace HigSolround
|
} // namespace Solstice
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|||||||
13
tests/input.sols
Normal file
13
tests/input.sols
Normal 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!"
|
||||||
Reference in New Issue
Block a user