Start work on functions
This commit is contained in:
@@ -46,6 +46,17 @@ void Interpreter::initInterpreter(vector<string> args) {
|
|||||||
variables["args"] = buf;
|
variables["args"] = buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
valtype Interpreter::keywordToValtype(keywords in) {
|
||||||
|
switch (in) {
|
||||||
|
case keywords::INT: return valtype::INT;
|
||||||
|
case keywords::DEC: return valtype::DEC;
|
||||||
|
case keywords::BOOL: return valtype::BOOL;
|
||||||
|
case keywords::STR: return valtype::STR;
|
||||||
|
default: return valtype::KEYWORD;
|
||||||
|
}
|
||||||
|
return valtype::KEYWORD;
|
||||||
|
}
|
||||||
|
|
||||||
void Interpreter::interpret(vector<Token> tokenList) {
|
void Interpreter::interpret(vector<Token> tokenList) {
|
||||||
if (debugMode) log.toggleDebugPrint();
|
if (debugMode) log.toggleDebugPrint();
|
||||||
tokens = tokenList;
|
tokens = tokenList;
|
||||||
@@ -532,6 +543,17 @@ void Interpreter::executeCode(vector<Token> tokens) {
|
|||||||
} else {
|
} else {
|
||||||
syntaxError.generalError("Achievement get: How did we get here?");
|
syntaxError.generalError("Achievement get: How did we get here?");
|
||||||
}
|
}
|
||||||
|
} else if (tokens[i].keyword == keywords::FUNCTION) {
|
||||||
|
i++;
|
||||||
|
Function buf;
|
||||||
|
if (tokens[i].type != valtype::KEYWORD) syntaxError.generalError("fun needs a keyword, not a value");
|
||||||
|
buf.type = keywordToValtype(tokens[i].keyword);
|
||||||
|
i++;
|
||||||
|
string valName;
|
||||||
|
if (tokens[i].value.type == valtype::STR) valName = get<string>(tokens[i].value.value);
|
||||||
|
buf.startToken = tokenIndex;
|
||||||
|
i++;
|
||||||
|
if (tokens[i].keyword != )
|
||||||
} else {
|
} else {
|
||||||
if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get<string>(tokens[i].value.value)) != variables.end()) {
|
if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get<string>(tokens[i].value.value)) != variables.end()) {
|
||||||
log.debug("Manipulating variable...");
|
log.debug("Manipulating variable...");
|
||||||
|
@@ -30,6 +30,7 @@ private:
|
|||||||
SyntaxError syntaxError;
|
SyntaxError syntaxError;
|
||||||
vector<Token> tokens;
|
vector<Token> tokens;
|
||||||
map<string, Value> variables;
|
map<string, Value> variables;
|
||||||
|
map<string, Function> functions;
|
||||||
Logger log;
|
Logger log;
|
||||||
int loop;
|
int loop;
|
||||||
int lengthOfLine;
|
int lengthOfLine;
|
||||||
@@ -37,6 +38,7 @@ private:
|
|||||||
optional<Token> consume();
|
optional<Token> consume();
|
||||||
optional<Token> peek(int offset = 1);
|
optional<Token> peek(int offset = 1);
|
||||||
void executeCode(vector<Token> tokens);
|
void executeCode(vector<Token> tokens);
|
||||||
|
valtype keywordToValtype(keywords in);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void initInterpreter(vector<string> args);
|
void initInterpreter(vector<string> args);
|
||||||
|
@@ -72,7 +72,7 @@ void Parser::parseLines(string in, Logger log) {
|
|||||||
}
|
}
|
||||||
terms.push_back(termBuffer);
|
terms.push_back(termBuffer);
|
||||||
termBuffer.clear();
|
termBuffer.clear();
|
||||||
} else if ((c == ',' || c == '{' || c == '}' || c == '(' || c == ')' || c == '[' || c == ']') && !isString) {
|
} else if ((c == ',' || c == '{' || c == '}' || c == '(' || c == ')' || c == '[' || c == ']' || c == ',') && !isString) {
|
||||||
if (!buffer.empty()) {
|
if (!buffer.empty()) {
|
||||||
termBuffer.push_back(buffer);
|
termBuffer.push_back(buffer);
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
@@ -130,6 +130,7 @@ void Parser::processLines() {
|
|||||||
else if (ct == "bool") token.keyword = keywords::BOOL;
|
else if (ct == "bool") token.keyword = keywords::BOOL;
|
||||||
else if (ct == "if") token.keyword = keywords::IF;
|
else if (ct == "if") token.keyword = keywords::IF;
|
||||||
else if (ct == "while") token.keyword = keywords::WHILE;
|
else if (ct == "while") token.keyword = keywords::WHILE;
|
||||||
|
else if (ct == "fun") token.keyword = keywords::FUNCTION;
|
||||||
else if (ct == "{") token.keyword = keywords::OBRAC;
|
else if (ct == "{") token.keyword = keywords::OBRAC;
|
||||||
else if (ct == "}") token.keyword = keywords::CBRAC;
|
else if (ct == "}") token.keyword = keywords::CBRAC;
|
||||||
else if (ct == "(") token.keyword = keywords::OPARE;
|
else if (ct == "(") token.keyword = keywords::OPARE;
|
||||||
@@ -152,6 +153,7 @@ void Parser::processLines() {
|
|||||||
else if (ct == "//") token.keyword = keywords::COMMENT;
|
else if (ct == "//") token.keyword = keywords::COMMENT;
|
||||||
else if (ct == "[") token.keyword = keywords::OSQUA;
|
else if (ct == "[") token.keyword = keywords::OSQUA;
|
||||||
else if (ct == "]") token.keyword = keywords::CSQUA;
|
else if (ct == "]") token.keyword = keywords::CSQUA;
|
||||||
|
else if (ct == ",") token.keyword = keywords::COMMA;
|
||||||
else {
|
else {
|
||||||
token.keyword = keywords::VALUE;
|
token.keyword = keywords::VALUE;
|
||||||
// Convert the value based on its type
|
// Convert the value based on its type
|
||||||
|
@@ -38,7 +38,7 @@ enum class keywords {
|
|||||||
ADD, SUBTRACT, MULTIPLY, DIVIDE,
|
ADD, SUBTRACT, MULTIPLY, DIVIDE,
|
||||||
EQUAL, INEQUAL, LESS, GREATER, EQLESS, EQGREATER,
|
EQUAL, INEQUAL, LESS, GREATER, EQLESS, EQGREATER,
|
||||||
INCREMENT, DECREMENT,
|
INCREMENT, DECREMENT,
|
||||||
PRINT, PRINTLN, PRINTLIST, LET, INPUT, EXIT,
|
PRINT, PRINTLN, PRINTLIST, LET, FUNCTION, INPUT, EXIT,
|
||||||
VALUE, LISTOBJ, SEMICOLON, VARIABLE,
|
VALUE, LISTOBJ, SEMICOLON, VARIABLE,
|
||||||
COMMENT
|
COMMENT
|
||||||
};
|
};
|
||||||
@@ -55,6 +55,12 @@ struct Value {
|
|||||||
variant<int, double, string, bool, List> value;
|
variant<int, double, string, bool, List> value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Function {
|
||||||
|
valtype type;
|
||||||
|
map<string, valtype> arguments;
|
||||||
|
int startToken;
|
||||||
|
};
|
||||||
|
|
||||||
struct Token {
|
struct Token {
|
||||||
keywords keyword;
|
keywords keyword;
|
||||||
Value value;
|
Value value;
|
||||||
|
Reference in New Issue
Block a user