From c04e631180c1300436dadd8f722bff43222c25a1 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 20 Dec 2025 14:47:24 +1100 Subject: [PATCH] Half working stuff --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 45 ++++++++++++++++++++++++++++++++++++++------- tests/count.hg | 6 ++++++ tests/set.hg | 3 ++- 4 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 tests/count.hg diff --git a/README.md b/README.md index 26c1d74..80b0394 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,51 @@ puts 1 + 1 puts 3.14 + 2.7 puts 532 + 314 + 89432 ``` + +### Comparisons + +Compare 2 values with `==` and `!=` (more operations coming soon) + +``` +puts 5 == 5 +puts 5 != 5 +puts "dingus" == "dongus" +puts "dingus" != "dongus" +``` + +### Variables + +Set a variable with `=`, similar to Python. + +``` +x = 5 +y = 10 +puts x + y +``` + +### Control Flow + +Use `if` to execute code if a condition is true. + +``` +password = "dingus" + +if password == "password123" { + puts "Your password is insecure." +} + +if password = "dingus" { + puts "Cool password!" +} +``` + +Use `while` to loop over a statement. + +``` +number = 0 + +while number != 10 { + number = number + 1 + puts number +} +``` diff --git a/src/main.cpp b/src/main.cpp index c1e033b..96955da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ namespace HighGround { namespace Parser { enum class HGNodeType { - Add, Subtract, Equal, 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, Puts }; enum class HGDataType { @@ -179,6 +179,20 @@ namespace HighGround { code.push_back(codeBlock); break; } + case HGNodeType::Inequal: { + HGGroundCodeBlock codeBlock; + outputId = "tmp_" + std::to_string(tmpIdIterator++); + GroundInstruction gi = groundCreateInstruction(INEQUAL); + if (children.size() < 2) { + std::cout << "Need more stuff to inequal\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); @@ -222,9 +236,9 @@ namespace HighGround { HGGroundCodeBlock codeBlock; std::string startLabelIdString = "whilestart_" + std::to_string(labelIterator++); std::string endLabelIdString = "whileend_" + std::to_string(labelIterator); - char* startLabelId = (char*) malloc(sizeof(char) * startLabelIdString.size()); + char* startLabelId = (char*) malloc(sizeof(char) * (startLabelIdString.size() + 1)); strcpy(startLabelId, startLabelIdString.data()); - char* endLabelId = (char*) malloc(sizeof(char) * endLabelIdString.size()); + char* endLabelId = (char*) malloc(sizeof(char) * (endLabelIdString.size() + 1)); strcpy(endLabelId, endLabelIdString.data()); GroundInstruction startLabel = groundCreateInstruction(CREATELABEL); groundAddReferenceToInstruction(&startLabel, groundCreateReference(LABEL, startLabelId)); @@ -367,6 +381,9 @@ namespace HighGround { if (in == "==") { return HGNodeType::Equal; } + if (in == "!=") { + return HGNodeType::Inequal; + } if (in == "puts") { return HGNodeType::Puts; } @@ -445,17 +462,31 @@ namespace HighGround { break; } case HGNodeType::Equal: { - HGNode addNode(HGNodeType::Equal); - addNode.addNode(rootNode.children.back()); + HGNode equalNode(HGNodeType::Equal); + equalNode.addNode(rootNode.children.back()); rootNode.children.pop_back(); auto tokenopt = consume(); if (tokenopt) { - addNode.addNode(parseOneToken(tokenopt)); + equalNode.addNode(parseOneToken(tokenopt)); } else { std::cout << "FEED ME MORE TOKENS\n"; exit(1); } - rootNode.addNode(addNode); + rootNode.addNode(equalNode); + break; + } + case HGNodeType::Inequal: { + HGNode inequalNode(HGNodeType::Inequal); + inequalNode.addNode(rootNode.children.back()); + rootNode.children.pop_back(); + auto tokenopt = consume(); + if (tokenopt) { + inequalNode.addNode(parseOneToken(tokenopt)); + } else { + std::cout << "FEED ME MORE TOKENS\n"; + exit(1); + } + rootNode.addNode(inequalNode); break; } case HGNodeType::Puts: { diff --git a/tests/count.hg b/tests/count.hg new file mode 100644 index 0000000..df2c932 --- /dev/null +++ b/tests/count.hg @@ -0,0 +1,6 @@ +number = 0 + +while number != 10 { + number = number + 1 + puts number +} diff --git a/tests/set.hg b/tests/set.hg index 9327f0d..8fe639e 100644 --- a/tests/set.hg +++ b/tests/set.hg @@ -1,2 +1,3 @@ x = 5 -puts x +y = 10 +puts x + y