Compare commits

3 Commits

Author SHA1 Message Date
e8bf7b70f7 Fix lexer 2025-12-19 17:01:57 +11:00
b5c8b1b7ec WIP conditionals work 2025-12-19 16:17:57 +11:00
99bc0dbdc2 If statements 2025-12-15 11:37:27 +11:00
2 changed files with 90 additions and 10 deletions

View File

@@ -14,6 +14,7 @@
namespace HighGround {
int tmpIdIterator = 0;
int labelIterator = 0;
namespace Parser {
@@ -98,7 +99,7 @@ namespace HighGround {
}
const std::vector<HGGroundCodeBlock> generateCode() {
std::vector<HGGroundCodeBlock> code;
for (auto& child : children) {
if (nodeType != HGNodeType::If) for (auto& child : children) {
auto childCode = child.generateCode();
code.insert(code.end(), childCode.begin(), childCode.end());
}
@@ -163,6 +164,20 @@ namespace HighGround {
code.push_back(codeBlock);
break;
}
case HGNodeType::Equal: {
HGGroundCodeBlock codeBlock;
outputId = "tmp_" + std::to_string(tmpIdIterator++);
GroundInstruction gi = groundCreateInstruction(EQUAL);
if (children.size() < 2) {
std::cout << "Need more stuff to equal\n";
}
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[1].outputId.data()));
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
codeBlock.code.push_back(gi);
code.push_back(codeBlock);
break;
}
case HGNodeType::Puts: {
HGGroundCodeBlock codeBlock;
GroundInstruction gi = groundCreateInstruction(PRINTLN);
@@ -174,6 +189,33 @@ namespace HighGround {
code.push_back(codeBlock);
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);
codeBlock.code.push_back(groundCreateInstruction(PAUSE));
code.push_back(codeBlock);
break;
}
default: {}
}
return code;
@@ -268,6 +310,9 @@ namespace HighGround {
if (in == "+") {
return HGNodeType::Add;
}
if (in == "==") {
return HGNodeType::Equal;
}
if (in == "puts") {
return HGNodeType::Puts;
}
@@ -342,6 +387,20 @@ namespace HighGround {
rootNode.addNode(addNode);
break;
}
case HGNodeType::Equal: {
HGNode addNode(HGNodeType::Equal);
addNode.addNode(rootNode.children.back());
rootNode.children.pop_back();
auto tokenopt = consume();
if (tokenopt) {
addNode.addNode(parseOneToken(tokenopt));
} else {
std::cout << "FEED ME MORE TOKENS\n";
exit(1);
}
rootNode.addNode(addNode);
break;
}
case HGNodeType::Puts: {
HGNode putsNode(HGNodeType::Puts);
std::vector<std::string> tokens;
@@ -368,10 +427,6 @@ namespace HighGround {
tokens.push_back(tokenopt.value());
}
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]);
tokens.clear();
size_t brackets = 1;
@@ -392,18 +447,36 @@ namespace HighGround {
}
}
} else {
std::cout << "I want a code block\n";
std::cout << "I want a code block instead of a " + tokenopt.value() + "\n";
exit(1);
}
} else {
std::cout << "FEED ME MORE TOKENSSSSS\n";
exit(1);
}
auto childCodeBlock = Parser(tokens).parse();
ifNode.addNode(childCodeBlock.children[0]);
rootNode.addNode(ifNode);
break;
}
case HGNodeType::CodeBlockStart: {
HGNode codeBlockNode(HGNodeType::CodeBlock);
// WIP
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);
break;
}
}
@@ -474,7 +547,7 @@ namespace HighGround {
case '-':
{
std::string newToken(1, c);
auto tokenopt = peek();
auto tokenopt = peek(0);
if (tokenopt) {
char token = tokenopt.value();
if (token == c || token == '=') {
@@ -495,7 +568,7 @@ namespace HighGround {
case '=':
{
std::string newToken(1, c);
auto tokenopt = peek();
auto tokenopt = peek(0);
if (tokenopt) {
char token = tokenopt.value();
if (token == '=') {
@@ -517,8 +590,8 @@ namespace HighGround {
tokens.push_back(buf);
buf.clear();
}
tokens.push_back(std::string(1, c));
tokens.push_back("\n");
tokens.push_back(std::string(1, c));
}
// tokens which do not need to be included
case ' ':

7
tests/if.hg Normal file
View File

@@ -0,0 +1,7 @@
if true {
puts "huzzah"
}
if 5 == 10 {
puts "aww"
}