Start work on if

This commit is contained in:
2025-12-14 17:17:19 +11:00
parent e7aa2a1ca4
commit 57f5466506
3 changed files with 75 additions and 9 deletions

View File

@@ -18,7 +18,7 @@ namespace HighGround {
namespace Parser { namespace Parser {
enum class HGNodeType { enum class HGNodeType {
Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock, Puts Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock, CodeBlockStart, CodeBlockEnd, Puts
}; };
enum class HGDataType { enum class HGDataType {
@@ -271,6 +271,15 @@ namespace HighGround {
if (in == "puts") { if (in == "puts") {
return HGNodeType::Puts; return HGNodeType::Puts;
} }
if (in == "if") {
return HGNodeType::If;
}
if (in == "{") {
return HGNodeType::CodeBlockStart;
}
if (in == "}") {
return HGNodeType::CodeBlockEnd;
}
return HGNodeType::None; return HGNodeType::None;
} }
@@ -328,6 +337,7 @@ namespace HighGround {
addNode.addNode(parseOneToken(tokenopt)); addNode.addNode(parseOneToken(tokenopt));
} else { } else {
std::cout << "FEED ME MORE TOKENS\n"; std::cout << "FEED ME MORE TOKENS\n";
exit(1);
} }
rootNode.addNode(addNode); rootNode.addNode(addNode);
break; break;
@@ -348,6 +358,54 @@ namespace HighGround {
rootNode.addNode(putsNode); rootNode.addNode(putsNode);
break; break;
} }
case HGNodeType::If: {
HGNode ifNode(HGNodeType::If);
std::vector<std::string> tokens;
while (auto tokenopt = consume()) {
if (tokenopt.value() == "\n") {
break;
}
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;
auto tokenopt = consume();
if (tokenopt) {
if (tokenopt.value() == "{") {
tokens.push_back(tokenopt.value());
while (auto tokenopt = consume()) {
tokens.push_back(tokenopt.value());
if (tokenopt.value() == "{") {
brackets++;
}
if (tokenopt.value() == "}") {
brackets--;
}
if (brackets == 0) {
break;
}
}
} else {
std::cout << "I want a code block\n";
exit(1);
}
} else {
std::cout << "FEED ME MORE TOKENSSSSS\n";
exit(1);
}
break;
}
case HGNodeType::CodeBlockStart: {
HGNode codeBlockNode(HGNodeType::CodeBlock);
// WIP
break;
}
} }
} }
return rootNode; return rootNode;
@@ -401,7 +459,6 @@ namespace HighGround {
case '\n': case '\n':
case '(': case '(':
case ')': case ')':
case '{':
case '}': case '}':
{ {
if (!buf.empty()) { if (!buf.empty()) {
@@ -453,6 +510,16 @@ namespace HighGround {
tokens.push_back(newToken); tokens.push_back(newToken);
break; break;
} }
// tokens which need a newline inserted for them
case '{':
{
if (!buf.empty()) {
tokens.push_back(buf);
buf.clear();
}
tokens.push_back(std::string(1, c));
tokens.push_back("\n");
}
// tokens which do not need to be included // tokens which do not need to be included
case ' ': case ' ':
{ {

View File

@@ -1,5 +1,5 @@
"dingus" puts "dingus"
432 puts 432
3.141 puts 3.141
'c' puts 'c'
true puts true

View File

@@ -1,2 +1 @@
puts 1 + 1 puts 1 + 1 + 463278