forked from solstice/solstice
Fixes, rebrand
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1 @@
|
|||||||
hg
|
solstice
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -1,22 +1,20 @@
|
|||||||
# High Ground
|
# Solstice
|
||||||
|
|
||||||
High Ground is a programming language based on Ground.
|
Solstice is a programming language based on Ground.
|
||||||
|
|
||||||
It is the reference language designed to teach you how to build your own Ground-based language.
|
|
||||||
|
|
||||||
## Compiling
|
## Compiling
|
||||||
|
|
||||||
First, ensure CGround is installed on your system with `sudo make install`. Then, compile with
|
First, ensure CGround is installed on your system with `sudo make install`. Then, compile with
|
||||||
|
|
||||||
```
|
```
|
||||||
g++ src/main.cpp -o hg -lgroundvm
|
g++ src/main.cpp -o solstice -lgroundvm
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
High Ground files use the `.hg` extension. Run files as you would with any other interpreted language.
|
Solstice files use the `.sols` extension. Run files as you would with any other interpreted language.
|
||||||
|
|
||||||
## Using High Ground
|
## Using Solstice
|
||||||
|
|
||||||
### Types
|
### Types
|
||||||
|
|
||||||
@@ -80,7 +78,7 @@ if password == "password123" {
|
|||||||
puts "Your password is insecure."
|
puts "Your password is insecure."
|
||||||
}
|
}
|
||||||
|
|
||||||
if password = "dingus" {
|
if password == "dingus" {
|
||||||
puts "Cool password!"
|
puts "Cool password!"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
242
src/main.cpp
242
src/main.cpp
@@ -12,70 +12,70 @@
|
|||||||
|
|
||||||
#define parseOneToken(token) Parser({token.value()}).parse().children[0]
|
#define parseOneToken(token) Parser({token.value()}).parse().children[0]
|
||||||
|
|
||||||
namespace HighGround {
|
namespace Solstice {
|
||||||
|
|
||||||
int tmpIdIterator = 0;
|
int tmpIdIterator = 0;
|
||||||
int labelIterator = 0;
|
int labelIterator = 0;
|
||||||
|
|
||||||
namespace Parser {
|
namespace Parser {
|
||||||
|
|
||||||
enum class HGNodeType {
|
enum class SolNodeType {
|
||||||
Add, Subtract, Equal, Inequal, 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 {
|
enum class SolDataType {
|
||||||
Int, String, Double, Bool, Char, None
|
Int, String, Double, Bool, Char, None
|
||||||
};
|
};
|
||||||
|
|
||||||
class HGNode;
|
class SolNode;
|
||||||
|
|
||||||
class HGGroundCodeBlock {
|
class SolGroundCodeBlock {
|
||||||
public:
|
public:
|
||||||
std::vector<GroundInstruction> code;
|
std::vector<GroundInstruction> code;
|
||||||
HGGroundCodeBlock() = default;
|
SolGroundCodeBlock() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
class HGData {
|
class SolData {
|
||||||
typedef std::variant<int64_t, std::string, double, bool, char> varData;
|
typedef std::variant<int64_t, std::string, double, bool, char> varData;
|
||||||
varData data;
|
varData data;
|
||||||
public:
|
public:
|
||||||
HGDataType type = HGDataType::Int;
|
SolDataType type = SolDataType::Int;
|
||||||
HGData() = default;
|
SolData() = default;
|
||||||
HGData(int64_t in) : data(in), type(HGDataType::Int) {}
|
SolData(int64_t in) : data(in), type(SolDataType::Int) {}
|
||||||
HGData(double in) : data(in), type(HGDataType::Double) {}
|
SolData(double in) : data(in), type(SolDataType::Double) {}
|
||||||
HGData(std::string in) : data(in), type(HGDataType::String) {}
|
SolData(std::string in) : data(in), type(SolDataType::String) {}
|
||||||
HGData(char in) : data(in), type(HGDataType::Char) {}
|
SolData(char in) : data(in), type(SolDataType::Char) {}
|
||||||
HGData(bool in) : data(in), type(HGDataType::Bool) {}
|
SolData(bool in) : data(in), type(SolDataType::Bool) {}
|
||||||
std::optional<int64_t> getInt() {
|
std::optional<int64_t> getInt() {
|
||||||
if (type == HGDataType::Int) {
|
if (type == SolDataType::Int) {
|
||||||
return std::get<int64_t>(data);
|
return std::get<int64_t>(data);
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::optional<double> getDouble() {
|
std::optional<double> getDouble() {
|
||||||
if (type == HGDataType::Double) {
|
if (type == SolDataType::Double) {
|
||||||
return std::get<double>(data);
|
return std::get<double>(data);
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::optional<std::string> getString() {
|
std::optional<std::string> getString() {
|
||||||
if (type == HGDataType::String) {
|
if (type == SolDataType::String) {
|
||||||
return std::get<std::string>(data);
|
return std::get<std::string>(data);
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::optional<char> getChar() {
|
std::optional<char> getChar() {
|
||||||
if (type == HGDataType::Char) {
|
if (type == SolDataType::Char) {
|
||||||
return std::get<char>(data);
|
return std::get<char>(data);
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::optional<bool> getBool() {
|
std::optional<bool> getBool() {
|
||||||
if (type == HGDataType::Bool) {
|
if (type == SolDataType::Bool) {
|
||||||
return std::get<bool>(data);
|
return std::get<bool>(data);
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return {};
|
||||||
@@ -83,63 +83,63 @@ namespace HighGround {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class HGNode {
|
class SolNode {
|
||||||
HGNodeType nodeType = HGNodeType::None;
|
SolNodeType nodeType = SolNodeType::None;
|
||||||
HGData data;
|
SolData data;
|
||||||
public:
|
public:
|
||||||
std::vector<HGNode> children;
|
std::vector<SolNode> children;
|
||||||
std::string outputId;
|
std::string outputId;
|
||||||
HGNode(HGNodeType nodeType) : nodeType(nodeType) {}
|
SolNode(SolNodeType nodeType) : nodeType(nodeType) {}
|
||||||
HGNode(HGNodeType nodeType, HGData data) : nodeType(nodeType), data(data) {}
|
SolNode(SolNodeType nodeType, SolData data) : nodeType(nodeType), data(data) {}
|
||||||
HGNode() = default;
|
SolNode() = default;
|
||||||
void addNode(HGNode in) {
|
void addNode(SolNode in) {
|
||||||
children.push_back(in);
|
children.push_back(in);
|
||||||
}
|
}
|
||||||
void setValue(HGData in) {
|
void setValue(SolData in) {
|
||||||
data = in;
|
data = in;
|
||||||
}
|
}
|
||||||
const std::vector<HGGroundCodeBlock> generateCode() {
|
const std::vector<SolGroundCodeBlock> generateCode() {
|
||||||
std::vector<HGGroundCodeBlock> code;
|
std::vector<SolGroundCodeBlock> code;
|
||||||
if (nodeType != HGNodeType::If) for (auto& child : children) {
|
if (nodeType != SolNodeType::If && nodeType != SolNodeType::While) for (auto& child : children) {
|
||||||
auto childCode = child.generateCode();
|
auto childCode = child.generateCode();
|
||||||
code.insert(code.end(), childCode.begin(), childCode.end());
|
code.insert(code.end(), childCode.begin(), childCode.end());
|
||||||
}
|
}
|
||||||
switch (nodeType) {
|
switch (nodeType) {
|
||||||
case HGNodeType::Value: {
|
case SolNodeType::Value: {
|
||||||
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock codeBlock;
|
||||||
GroundInstruction gi = groundCreateInstruction(SET);
|
GroundInstruction gi = groundCreateInstruction(SET);
|
||||||
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
|
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case HGDataType::Int: {
|
case SolDataType::Int: {
|
||||||
auto dataopt = data.getInt();
|
auto dataopt = data.getInt();
|
||||||
if (dataopt) {
|
if (dataopt) {
|
||||||
groundAddValueToInstruction(&gi, groundCreateValue(INT, dataopt.value()));
|
groundAddValueToInstruction(&gi, groundCreateValue(INT, dataopt.value()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::Double: {
|
case SolDataType::Double: {
|
||||||
auto dataopt = data.getDouble();
|
auto dataopt = data.getDouble();
|
||||||
if (dataopt) {
|
if (dataopt) {
|
||||||
groundAddValueToInstruction(&gi, groundCreateValue(DOUBLE, dataopt.value()));
|
groundAddValueToInstruction(&gi, groundCreateValue(DOUBLE, dataopt.value()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::String: {
|
case SolDataType::String: {
|
||||||
auto dataopt = data.getString();
|
auto dataopt = data.getString();
|
||||||
if (dataopt) {
|
if (dataopt) {
|
||||||
groundAddValueToInstruction(&gi, groundCreateValue(STRING, dataopt.value().c_str()));
|
groundAddValueToInstruction(&gi, groundCreateValue(STRING, dataopt.value().c_str()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::Char: {
|
case SolDataType::Char: {
|
||||||
auto dataopt = data.getChar();
|
auto dataopt = data.getChar();
|
||||||
if (dataopt) {
|
if (dataopt) {
|
||||||
groundAddValueToInstruction(&gi, groundCreateValue(CHAR, dataopt.value()));
|
groundAddValueToInstruction(&gi, groundCreateValue(CHAR, dataopt.value()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::Bool: {
|
case SolDataType::Bool: {
|
||||||
auto dataopt = data.getBool();
|
auto dataopt = data.getBool();
|
||||||
if (dataopt) {
|
if (dataopt) {
|
||||||
groundAddValueToInstruction(&gi, groundCreateValue(BOOL, dataopt.value()));
|
groundAddValueToInstruction(&gi, groundCreateValue(BOOL, dataopt.value()));
|
||||||
@@ -151,8 +151,8 @@ namespace HighGround {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Add: {
|
case SolNodeType::Add: {
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock codeBlock;
|
||||||
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
||||||
GroundInstruction gi = groundCreateInstruction(ADD);
|
GroundInstruction gi = groundCreateInstruction(ADD);
|
||||||
if (children.size() < 2) {
|
if (children.size() < 2) {
|
||||||
@@ -165,8 +165,8 @@ namespace HighGround {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Equal: {
|
case SolNodeType::Equal: {
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock codeBlock;
|
||||||
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
||||||
GroundInstruction gi = groundCreateInstruction(EQUAL);
|
GroundInstruction gi = groundCreateInstruction(EQUAL);
|
||||||
if (children.size() < 2) {
|
if (children.size() < 2) {
|
||||||
@@ -179,8 +179,8 @@ namespace HighGround {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Inequal: {
|
case SolNodeType::Inequal: {
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock codeBlock;
|
||||||
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
||||||
GroundInstruction gi = groundCreateInstruction(INEQUAL);
|
GroundInstruction gi = groundCreateInstruction(INEQUAL);
|
||||||
if (children.size() < 2) {
|
if (children.size() < 2) {
|
||||||
@@ -193,8 +193,8 @@ namespace HighGround {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Puts: {
|
case SolNodeType::Puts: {
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock codeBlock;
|
||||||
GroundInstruction gi = groundCreateInstruction(PRINTLN);
|
GroundInstruction gi = groundCreateInstruction(PRINTLN);
|
||||||
if (children.size() < 1) {
|
if (children.size() < 1) {
|
||||||
std::cout << "Need more stuff to puts\n";
|
std::cout << "Need more stuff to puts\n";
|
||||||
@@ -204,11 +204,11 @@ namespace HighGround {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::If: {
|
case SolNodeType::If: {
|
||||||
auto conditionCode = children[0].generateCode();
|
auto conditionCode = children[0].generateCode();
|
||||||
code.insert(code.end(), conditionCode.begin(), conditionCode.end());
|
code.insert(code.end(), conditionCode.begin(), conditionCode.end());
|
||||||
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock codeBlock;
|
||||||
GroundInstruction gi = groundCreateInstruction(NOT);
|
GroundInstruction gi = groundCreateInstruction(NOT);
|
||||||
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
|
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
|
||||||
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
|
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
|
||||||
@@ -232,48 +232,55 @@ namespace HighGround {
|
|||||||
code.push_back(codeBlock);
|
code.push_back(codeBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::While: {
|
case SolNodeType::While: {
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock startLabelBlock;
|
||||||
std::string startLabelIdString = "whilestart_" + std::to_string(labelIterator++);
|
std::string startLabelIdString = "whilestart_" + std::to_string(labelIterator++);
|
||||||
std::string endLabelIdString = "whileend_" + std::to_string(labelIterator);
|
std::string endLabelIdString = "whileend_" + std::to_string(labelIterator);
|
||||||
char* startLabelId = (char*) malloc(sizeof(char) * (startLabelIdString.size() + 1));
|
char* startLabelId = (char*) malloc(sizeof(char) * (startLabelIdString.size() + 1));
|
||||||
strcpy(startLabelId, startLabelIdString.data());
|
strcpy(startLabelId, startLabelIdString.data());
|
||||||
char* endLabelId = (char*) malloc(sizeof(char) * (endLabelIdString.size() + 1));
|
char* endLabelId = (char*) malloc(sizeof(char) * (endLabelIdString.size() + 1));
|
||||||
strcpy(endLabelId, endLabelIdString.data());
|
strcpy(endLabelId, endLabelIdString.data());
|
||||||
|
|
||||||
GroundInstruction startLabel = groundCreateInstruction(CREATELABEL);
|
GroundInstruction startLabel = groundCreateInstruction(CREATELABEL);
|
||||||
groundAddReferenceToInstruction(&startLabel, groundCreateReference(LABEL, startLabelId));
|
groundAddReferenceToInstruction(&startLabel, groundCreateReference(LABEL, startLabelId));
|
||||||
codeBlock.code.push_back(startLabel);
|
startLabelBlock.code.push_back(startLabel);
|
||||||
|
code.push_back(startLabelBlock);
|
||||||
|
|
||||||
auto conditionCode = children[0].generateCode();
|
auto conditionCode = children[0].generateCode();
|
||||||
code.insert(code.end(), conditionCode.begin(), conditionCode.end());
|
code.insert(code.end(), conditionCode.begin(), conditionCode.end());
|
||||||
|
|
||||||
|
SolGroundCodeBlock checkBlock;
|
||||||
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
outputId = "tmp_" + std::to_string(tmpIdIterator++);
|
||||||
GroundInstruction gi = groundCreateInstruction(NOT);
|
GroundInstruction gi = groundCreateInstruction(NOT);
|
||||||
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
|
groundAddReferenceToInstruction(&gi, groundCreateReference(VALREF, children[0].outputId.data()));
|
||||||
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
|
groundAddReferenceToInstruction(&gi, groundCreateReference(DIRREF, outputId.data()));
|
||||||
codeBlock.code.push_back(gi);
|
checkBlock.code.push_back(gi);
|
||||||
GroundInstruction gi2 = groundCreateInstruction(IF);
|
GroundInstruction gi2 = groundCreateInstruction(IF);
|
||||||
groundAddReferenceToInstruction(&gi2, groundCreateReference(VALREF, outputId.data()));
|
groundAddReferenceToInstruction(&gi2, groundCreateReference(VALREF, outputId.data()));
|
||||||
groundAddReferenceToInstruction(&gi2, groundCreateReference(LINEREF, endLabelId));
|
groundAddReferenceToInstruction(&gi2, groundCreateReference(LINEREF, endLabelId));
|
||||||
codeBlock.code.push_back(gi2);
|
checkBlock.code.push_back(gi2);
|
||||||
code.push_back(codeBlock);
|
code.push_back(checkBlock);
|
||||||
|
|
||||||
for (size_t i = 1; i < children.size(); i++) {
|
for (size_t i = 1; i < children.size(); i++) {
|
||||||
auto childCode = children[i].generateCode();
|
auto childCode = children[i].generateCode();
|
||||||
code.insert(code.end(), childCode.begin(), childCode.end());
|
code.insert(code.end(), childCode.begin(), childCode.end());
|
||||||
}
|
}
|
||||||
codeBlock.code.clear();
|
|
||||||
|
SolGroundCodeBlock endBlock;
|
||||||
GroundInstruction gi3 = groundCreateInstruction(JUMP);
|
GroundInstruction gi3 = groundCreateInstruction(JUMP);
|
||||||
groundAddReferenceToInstruction(&gi3, groundCreateReference(LINEREF, startLabelId));
|
groundAddReferenceToInstruction(&gi3, groundCreateReference(LINEREF, startLabelId));
|
||||||
codeBlock.code.push_back(gi3);
|
endBlock.code.push_back(gi3);
|
||||||
GroundInstruction gi4 = groundCreateInstruction(CREATELABEL);
|
GroundInstruction gi4 = groundCreateInstruction(CREATELABEL);
|
||||||
groundAddReferenceToInstruction(&gi4, groundCreateReference(LABEL, endLabelId));
|
groundAddReferenceToInstruction(&gi4, groundCreateReference(LABEL, endLabelId));
|
||||||
codeBlock.code.push_back(gi4);
|
endBlock.code.push_back(gi4);
|
||||||
code.push_back(codeBlock);
|
code.push_back(endBlock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Identifier: {
|
case SolNodeType::Identifier: {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Set: {
|
case SolNodeType::Set: {
|
||||||
HGGroundCodeBlock codeBlock;
|
SolGroundCodeBlock codeBlock;
|
||||||
GroundInstruction setInstruction = groundCreateInstruction(SET);
|
GroundInstruction setInstruction = groundCreateInstruction(SET);
|
||||||
groundAddReferenceToInstruction(&setInstruction, groundCreateReference(DIRREF, children[0].outputId.data()));
|
groundAddReferenceToInstruction(&setInstruction, groundCreateReference(DIRREF, children[0].outputId.data()));
|
||||||
groundAddReferenceToInstruction(&setInstruction, groundCreateReference(VALREF, children[1].outputId.data()));
|
groundAddReferenceToInstruction(&setInstruction, groundCreateReference(VALREF, children[1].outputId.data()));
|
||||||
@@ -349,97 +356,97 @@ namespace HighGround {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
HGDataType getDataType(std::string in) {
|
SolDataType getDataType(std::string in) {
|
||||||
if (isInt(in)) {
|
if (isInt(in)) {
|
||||||
return HGDataType::Int;
|
return SolDataType::Int;
|
||||||
}
|
}
|
||||||
if (isDouble(in)) {
|
if (isDouble(in)) {
|
||||||
return HGDataType::Double;
|
return SolDataType::Double;
|
||||||
}
|
}
|
||||||
if (isString(in)) {
|
if (isString(in)) {
|
||||||
return HGDataType::String;
|
return SolDataType::String;
|
||||||
}
|
}
|
||||||
if (isChar(in)) {
|
if (isChar(in)) {
|
||||||
return HGDataType::Char;
|
return SolDataType::Char;
|
||||||
}
|
}
|
||||||
if (isBool(in)) {
|
if (isBool(in)) {
|
||||||
return HGDataType::Bool;
|
return SolDataType::Bool;
|
||||||
}
|
}
|
||||||
return HGDataType::None;
|
return SolDataType::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
HGNodeType getNodeType(std::string in) {
|
SolNodeType getNodeType(std::string in) {
|
||||||
if (getDataType(in) != HGDataType::None) {
|
if (getDataType(in) != SolDataType::None) {
|
||||||
return HGNodeType::Value;
|
return SolNodeType::Value;
|
||||||
}
|
}
|
||||||
if (in == "+") {
|
if (in == "+") {
|
||||||
return HGNodeType::Add;
|
return SolNodeType::Add;
|
||||||
}
|
}
|
||||||
if (in == "=") {
|
if (in == "=") {
|
||||||
return HGNodeType::Set;
|
return SolNodeType::Set;
|
||||||
}
|
}
|
||||||
if (in == "==") {
|
if (in == "==") {
|
||||||
return HGNodeType::Equal;
|
return SolNodeType::Equal;
|
||||||
}
|
}
|
||||||
if (in == "!=") {
|
if (in == "!=") {
|
||||||
return HGNodeType::Inequal;
|
return SolNodeType::Inequal;
|
||||||
}
|
}
|
||||||
if (in == "puts") {
|
if (in == "puts") {
|
||||||
return HGNodeType::Puts;
|
return SolNodeType::Puts;
|
||||||
}
|
}
|
||||||
if (in == "if") {
|
if (in == "if") {
|
||||||
return HGNodeType::If;
|
return SolNodeType::If;
|
||||||
}
|
}
|
||||||
if (in == "while") {
|
if (in == "while") {
|
||||||
return HGNodeType::While;
|
return SolNodeType::While;
|
||||||
}
|
}
|
||||||
if (in == "{") {
|
if (in == "{") {
|
||||||
return HGNodeType::CodeBlockStart;
|
return SolNodeType::CodeBlockStart;
|
||||||
}
|
}
|
||||||
if (in == "}") {
|
if (in == "}") {
|
||||||
return HGNodeType::CodeBlockEnd;
|
return SolNodeType::CodeBlockEnd;
|
||||||
}
|
}
|
||||||
return HGNodeType::Identifier;
|
return SolNodeType::Identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Parser(std::vector<std::string> in) : tokensToParse(in) {}
|
Parser(std::vector<std::string> in) : tokensToParse(in) {}
|
||||||
|
|
||||||
HGNode parse() {
|
SolNode parse() {
|
||||||
current = 0;
|
current = 0;
|
||||||
size = tokensToParse.size();
|
size = tokensToParse.size();
|
||||||
HGNode rootNode(HGNodeType::Root);
|
SolNode rootNode(SolNodeType::Root);
|
||||||
while (auto tokenopt = consume()) {
|
while (auto tokenopt = consume()) {
|
||||||
std::string token = tokenopt.value();
|
std::string token = tokenopt.value();
|
||||||
switch (getNodeType(token)) {
|
switch (getNodeType(token)) {
|
||||||
case HGNodeType::Value: {
|
case SolNodeType::Value: {
|
||||||
switch (getDataType(token)) {
|
switch (getDataType(token)) {
|
||||||
case HGDataType::Int: {
|
case SolDataType::Int: {
|
||||||
HGNode intNode(HGNodeType::Value);
|
SolNode intNode(SolNodeType::Value);
|
||||||
intNode.setValue((int64_t) std::stoll(token));
|
intNode.setValue((int64_t) std::stoll(token));
|
||||||
rootNode.addNode(intNode);
|
rootNode.addNode(intNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::Double: {
|
case SolDataType::Double: {
|
||||||
HGNode doubleNode(HGNodeType::Value);
|
SolNode doubleNode(SolNodeType::Value);
|
||||||
doubleNode.setValue(std::stod(token));
|
doubleNode.setValue(std::stod(token));
|
||||||
rootNode.addNode(doubleNode);
|
rootNode.addNode(doubleNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::String: {
|
case SolDataType::String: {
|
||||||
HGNode stringNode(HGNodeType::Value);
|
SolNode stringNode(SolNodeType::Value);
|
||||||
stringNode.setValue(token.substr(1, token.size() - 2));
|
stringNode.setValue(token.substr(1, token.size() - 2));
|
||||||
rootNode.addNode(stringNode);
|
rootNode.addNode(stringNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::Char: {
|
case SolDataType::Char: {
|
||||||
HGNode charNode(HGNodeType::Value);
|
SolNode charNode(SolNodeType::Value);
|
||||||
charNode.setValue(token[1]);
|
charNode.setValue(token[1]);
|
||||||
rootNode.addNode(charNode);
|
rootNode.addNode(charNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGDataType::Bool: {
|
case SolDataType::Bool: {
|
||||||
HGNode boolNode(HGNodeType::Value);
|
SolNode boolNode(SolNodeType::Value);
|
||||||
boolNode.setValue(token == "true");
|
boolNode.setValue(token == "true");
|
||||||
rootNode.addNode(boolNode);
|
rootNode.addNode(boolNode);
|
||||||
break;
|
break;
|
||||||
@@ -447,8 +454,8 @@ namespace HighGround {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Add: {
|
case SolNodeType::Add: {
|
||||||
HGNode addNode(HGNodeType::Add);
|
SolNode addNode(SolNodeType::Add);
|
||||||
addNode.addNode(rootNode.children.back());
|
addNode.addNode(rootNode.children.back());
|
||||||
rootNode.children.pop_back();
|
rootNode.children.pop_back();
|
||||||
auto tokenopt = consume();
|
auto tokenopt = consume();
|
||||||
@@ -461,8 +468,8 @@ namespace HighGround {
|
|||||||
rootNode.addNode(addNode);
|
rootNode.addNode(addNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Equal: {
|
case SolNodeType::Equal: {
|
||||||
HGNode equalNode(HGNodeType::Equal);
|
SolNode equalNode(SolNodeType::Equal);
|
||||||
equalNode.addNode(rootNode.children.back());
|
equalNode.addNode(rootNode.children.back());
|
||||||
rootNode.children.pop_back();
|
rootNode.children.pop_back();
|
||||||
auto tokenopt = consume();
|
auto tokenopt = consume();
|
||||||
@@ -475,8 +482,8 @@ namespace HighGround {
|
|||||||
rootNode.addNode(equalNode);
|
rootNode.addNode(equalNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Inequal: {
|
case SolNodeType::Inequal: {
|
||||||
HGNode inequalNode(HGNodeType::Inequal);
|
SolNode inequalNode(SolNodeType::Inequal);
|
||||||
inequalNode.addNode(rootNode.children.back());
|
inequalNode.addNode(rootNode.children.back());
|
||||||
rootNode.children.pop_back();
|
rootNode.children.pop_back();
|
||||||
auto tokenopt = consume();
|
auto tokenopt = consume();
|
||||||
@@ -489,8 +496,8 @@ namespace HighGround {
|
|||||||
rootNode.addNode(inequalNode);
|
rootNode.addNode(inequalNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Puts: {
|
case SolNodeType::Puts: {
|
||||||
HGNode putsNode(HGNodeType::Puts);
|
SolNode putsNode(SolNodeType::Puts);
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
while (auto tokenopt = consume()) {
|
while (auto tokenopt = consume()) {
|
||||||
if (tokenopt.value() == "\n") {
|
if (tokenopt.value() == "\n") {
|
||||||
@@ -505,8 +512,8 @@ namespace HighGround {
|
|||||||
rootNode.addNode(putsNode);
|
rootNode.addNode(putsNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::If: {
|
case SolNodeType::If: {
|
||||||
HGNode ifNode(HGNodeType::If);
|
SolNode ifNode(SolNodeType::If);
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
while (auto tokenopt = consume()) {
|
while (auto tokenopt = consume()) {
|
||||||
if (tokenopt.value() == "\n") {
|
if (tokenopt.value() == "\n") {
|
||||||
@@ -547,8 +554,8 @@ namespace HighGround {
|
|||||||
rootNode.addNode(ifNode);
|
rootNode.addNode(ifNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::While: {
|
case SolNodeType::While: {
|
||||||
HGNode whileNode(HGNodeType::While);
|
SolNode whileNode(SolNodeType::While);
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
while (auto tokenopt = consume()) {
|
while (auto tokenopt = consume()) {
|
||||||
if (tokenopt.value() == "\n") {
|
if (tokenopt.value() == "\n") {
|
||||||
@@ -589,8 +596,8 @@ namespace HighGround {
|
|||||||
rootNode.addNode(whileNode);
|
rootNode.addNode(whileNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::CodeBlockStart: {
|
case SolNodeType::CodeBlockStart: {
|
||||||
HGNode codeBlockNode(HGNodeType::CodeBlock);
|
SolNode codeBlockNode(SolNodeType::CodeBlock);
|
||||||
size_t brackets = 1;
|
size_t brackets = 1;
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
while (auto tokenopt = consume()) {
|
while (auto tokenopt = consume()) {
|
||||||
@@ -609,14 +616,14 @@ namespace HighGround {
|
|||||||
rootNode.addNode(codeBlockNode);
|
rootNode.addNode(codeBlockNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Identifier: {
|
case SolNodeType::Identifier: {
|
||||||
HGNode idNode(HGNodeType::Identifier);
|
SolNode idNode(SolNodeType::Identifier);
|
||||||
idNode.outputId = token;
|
idNode.outputId = token;
|
||||||
rootNode.addNode(idNode);
|
rootNode.addNode(idNode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case HGNodeType::Set: {
|
case SolNodeType::Set: {
|
||||||
HGNode setNode(HGNodeType::Set);
|
SolNode setNode(SolNodeType::Set);
|
||||||
setNode.addNode(rootNode.children.back());
|
setNode.addNode(rootNode.children.back());
|
||||||
rootNode.children.pop_back();
|
rootNode.children.pop_back();
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
@@ -637,7 +644,7 @@ namespace HighGround {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GroundProgram assembleProgram(HGNode& rootNode) {
|
GroundProgram assembleProgram(SolNode& rootNode) {
|
||||||
GroundProgram gp = groundCreateProgram();
|
GroundProgram gp = groundCreateProgram();
|
||||||
auto code = rootNode.generateCode();
|
auto code = rootNode.generateCode();
|
||||||
for (int i = 0; i < code.size(); i++) {
|
for (int i = 0; i < code.size(); i++) {
|
||||||
@@ -735,6 +742,7 @@ namespace HighGround {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// tokens which may be followed by an equals sign
|
// tokens which may be followed by an equals sign
|
||||||
|
case '!':
|
||||||
case '*':
|
case '*':
|
||||||
case '/':
|
case '/':
|
||||||
case '=':
|
case '=':
|
||||||
@@ -790,7 +798,7 @@ namespace HighGround {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} // namespace HighGround
|
} // namespace HigSolround
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
@@ -801,8 +809,8 @@ int main(int argc, char** argv) {
|
|||||||
std::ifstream file(argv[1]);
|
std::ifstream file(argv[1]);
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
ss << file.rdbuf();
|
ss << file.rdbuf();
|
||||||
auto lexed = HighGround::Lexer(ss.str()).lex();
|
auto lexed = Solstice::Lexer(ss.str()).lex();
|
||||||
auto parsed = HighGround::Parser::Parser(lexed).parse();
|
auto parsed = Solstice::Parser::Parser(lexed).parse();
|
||||||
GroundProgram program = HighGround::Parser::assembleProgram(parsed);
|
GroundProgram program = Solstice::Parser::assembleProgram(parsed);
|
||||||
groundRunProgram(&program);
|
groundRunProgram(&program);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user