forked from solstice/solstice
Half working stuff
This commit is contained in:
48
README.md
48
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
|
||||
}
|
||||
```
|
||||
|
||||
45
src/main.cpp
45
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: {
|
||||
|
||||
6
tests/count.hg
Normal file
6
tests/count.hg
Normal file
@@ -0,0 +1,6 @@
|
||||
number = 0
|
||||
|
||||
while number != 10 {
|
||||
number = number + 1
|
||||
puts number
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
x = 5
|
||||
puts x
|
||||
y = 10
|
||||
puts x + y
|
||||
|
||||
Reference in New Issue
Block a user