If statements
This commit is contained in:
56
src/main.cpp
56
src/main.cpp
@@ -14,6 +14,7 @@
|
|||||||
namespace HighGround {
|
namespace HighGround {
|
||||||
|
|
||||||
int tmpIdIterator = 0;
|
int tmpIdIterator = 0;
|
||||||
|
int labelIterator = 0;
|
||||||
|
|
||||||
namespace Parser {
|
namespace Parser {
|
||||||
|
|
||||||
@@ -98,7 +99,7 @@ namespace HighGround {
|
|||||||
}
|
}
|
||||||
const std::vector<HGGroundCodeBlock> generateCode() {
|
const std::vector<HGGroundCodeBlock> generateCode() {
|
||||||
std::vector<HGGroundCodeBlock> code;
|
std::vector<HGGroundCodeBlock> code;
|
||||||
for (auto& child : children) {
|
if (nodeType != HGNodeType::If) for (auto& child : children) {
|
||||||
auto childCode = child.generateCode();
|
auto childCode = child.generateCode();
|
||||||
code.insert(code.end(), childCode.begin(), childCode.end());
|
code.insert(code.end(), childCode.begin(), childCode.end());
|
||||||
}
|
}
|
||||||
@@ -174,6 +175,32 @@ namespace HighGround {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case HGNodeType::If: {
|
||||||
|
auto conditionCode = children[0].generateCode();
|
||||||
|
code.insert(code.end(), conditionCode.begin(), conditionCode.end());
|
||||||
|
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
||||||
|
HGGroundCodeBlock codeBlock;
|
||||||
|
GroundInstruction gi = groundCreateInstruction(NOT);
|
||||||
|
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
|
||||||
|
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
|
||||||
|
codeBlock.code.push_back(gi);
|
||||||
|
std::string labelId = "if_" + std::to_string(labelIterator++);
|
||||||
|
GroundInstruction gi2 = groundCreateInstruction(IF);
|
||||||
|
groundAddReferenceToInstruction(&gi2, groundCreateReference(VALREF, outputId.data()));
|
||||||
|
groundAddReferenceToInstruction(&gi2, groundCreateReference(LINEREF, labelId.data()));
|
||||||
|
codeBlock.code.push_back(gi2);
|
||||||
|
code.push_back(codeBlock);
|
||||||
|
for (size_t i = 1; i < children.size(); i++) {
|
||||||
|
auto childCode = children[i].generateCode();
|
||||||
|
code.insert(code.end(), childCode.begin(), childCode.end());
|
||||||
|
}
|
||||||
|
codeBlock.code.clear();
|
||||||
|
GroundInstruction gi3 = groundCreateInstruction(CREATELABEL);
|
||||||
|
groundAddReferenceToInstruction(&gi3, groundCreateReference(LABEL, labelId.data()));
|
||||||
|
codeBlock.code.push_back(gi3);
|
||||||
|
code.push_back(codeBlock);
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: {}
|
default: {}
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
@@ -368,10 +395,6 @@ namespace HighGround {
|
|||||||
tokens.push_back(tokenopt.value());
|
tokens.push_back(tokenopt.value());
|
||||||
}
|
}
|
||||||
auto children = Parser(tokens).parse();
|
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]);
|
ifNode.addNode(children.children[0]);
|
||||||
tokens.clear();
|
tokens.clear();
|
||||||
size_t brackets = 1;
|
size_t brackets = 1;
|
||||||
@@ -392,17 +415,36 @@ namespace HighGround {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::cout << "I want a code block\n";
|
std::cout << "I want a code block instead of a " + tokenopt.value() + "\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::cout << "FEED ME MORE TOKENSSSSS\n";
|
std::cout << "FEED ME MORE TOKENSSSSS\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
auto childCodeBlock = Parser(tokens).parse();
|
||||||
|
ifNode.addNode(childCodeBlock.children[0]);
|
||||||
|
rootNode.addNode(ifNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::CodeBlockStart: {
|
case HGNodeType::CodeBlockStart: {
|
||||||
HGNode codeBlockNode(HGNodeType::CodeBlock);
|
HGNode codeBlockNode(HGNodeType::CodeBlock);
|
||||||
|
size_t brackets = 1;
|
||||||
|
std::vector<std::string> tokens;
|
||||||
|
while (auto tokenopt = consume()) {
|
||||||
|
if (tokenopt.value() == "{") {
|
||||||
|
brackets++;
|
||||||
|
}
|
||||||
|
if (tokenopt.value() == "}") {
|
||||||
|
brackets--;
|
||||||
|
}
|
||||||
|
if (brackets == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokens.push_back(tokenopt.value());
|
||||||
|
}
|
||||||
|
codeBlockNode.children = Parser(tokens).parse().children;
|
||||||
|
rootNode.addNode(codeBlockNode);
|
||||||
// WIP
|
// WIP
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -517,8 +559,8 @@ namespace HighGround {
|
|||||||
tokens.push_back(buf);
|
tokens.push_back(buf);
|
||||||
buf.clear();
|
buf.clear();
|
||||||
}
|
}
|
||||||
tokens.push_back(std::string(1, c));
|
|
||||||
tokens.push_back("\n");
|
tokens.push_back("\n");
|
||||||
|
tokens.push_back(std::string(1, c));
|
||||||
}
|
}
|
||||||
// tokens which do not need to be included
|
// tokens which do not need to be included
|
||||||
case ' ':
|
case ' ':
|
||||||
|
|||||||
7
tests/if.hg
Normal file
7
tests/if.hg
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
if true {
|
||||||
|
puts "huzzah"
|
||||||
|
}
|
||||||
|
|
||||||
|
if false {
|
||||||
|
puts "aww"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user