puts command

This commit is contained in:
2025-12-14 16:07:58 +11:00
parent adaf712441
commit e7aa2a1ca4
2 changed files with 37 additions and 7 deletions

View File

@@ -9,6 +9,8 @@
#include <optional>
#include <variant>
#define parseOneToken(token) Parser({token.value()}).parse().children[0]
namespace HighGround {
int tmpIdIterator = 0;
@@ -16,7 +18,7 @@ namespace HighGround {
namespace Parser {
enum class HGNodeType {
Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock
Add, Subtract, Equal, Set, While, If, Value, Identifier, None, Root, CodeBlock, Puts
};
enum class HGDataType {
@@ -161,9 +163,18 @@ namespace HighGround {
code.push_back(codeBlock);
break;
}
default: {
std::cout << "Not implemented yet\n";
case HGNodeType::Puts: {
HGGroundCodeBlock codeBlock;
GroundInstruction gi = groundCreateInstruction(PRINTLN);
if (children.size() < 1) {
std::cout << "Need more stuff to puts\n";
}
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
codeBlock.code.push_back(gi);
code.push_back(codeBlock);
break;
}
default: {}
}
return code;
}
@@ -257,6 +268,9 @@ namespace HighGround {
if (in == "+") {
return HGNodeType::Add;
}
if (in == "puts") {
return HGNodeType::Puts;
}
return HGNodeType::None;
}
@@ -311,13 +325,29 @@ namespace HighGround {
rootNode.children.pop_back();
auto tokenopt = consume();
if (tokenopt) {
addNode.addNode(Parser({tokenopt.value()}).parse().children[0]);
addNode.addNode(parseOneToken(tokenopt));
} else {
std::cout << "FEED ME MORE TOKENS\n";
}
rootNode.addNode(addNode);
break;
}
case HGNodeType::Puts: {
HGNode putsNode(HGNodeType::Puts);
std::vector<std::string> tokens;
while (auto tokenopt = consume()) {
if (tokenopt.value() == "\n") {
break;
}
tokens.push_back(tokenopt.value());
}
auto children = Parser(tokens).parse();
for (auto& child : children.children) {
putsNode.addNode(child);
}
rootNode.addNode(putsNode);
break;
}
}
}
return rootNode;
@@ -332,7 +362,6 @@ namespace HighGround {
groundAddInstructionToProgram(&gp, inst);
}
}
groundAddInstructionToProgram(&gp, groundCreateInstruction(PAUSE));
return gp;
}
@@ -369,6 +398,7 @@ namespace HighGround {
char c = copt.value();
switch (c) {
// tokens which are not followed by anything
case '\n':
case '(':
case ')':
case '{':
@@ -425,7 +455,6 @@ namespace HighGround {
}
// tokens which do not need to be included
case ' ':
case '\n':
{
if (!buf.empty()) {
tokens.push_back(buf);

View File

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