This commit is contained in:
Maxwell 2025-04-23 15:27:25 +10:00
parent 97bc3f447a
commit da3d9a536b
4 changed files with 28 additions and 3 deletions

BIN
main

Binary file not shown.

BIN
mx

Binary file not shown.

View File

@ -131,6 +131,26 @@ class Logger {
} }
}; };
class SyntaxError {
private:
public:
void fnTypeMismatch(string function, vector<string> validTypes, valtype typeGiven) {
cout << "TypeError: function type mismatch" << endl;
cout << "Function '" << function << "' expected one of the following types: ";
for (int i = 0; i < validTypes.size(); i++) {
if (i != 0) cout << ", ";
cout << validTypes[i];
}
cout << endl << "Got type '";
if (typeGiven == valtype::INT) cout << "int";
else if (typeGiven == valtype::DEC) cout << "dec";
else if (typeGiven == valtype::STR) cout << "str";
else if (typeGiven == valtype::BOOL) cout << "bool";
else cout << "unknown";
cout << "' instead" << endl;
}
};
class Parser { class Parser {
private: private:
string buffer; string buffer;
@ -333,6 +353,7 @@ class Interpreter {
} }
} }
void executeCode(vector<Token> tokens) { void executeCode(vector<Token> tokens) {
SyntaxError syntaxError;
for (int i = 0; i < tokens.size(); i++) { for (int i = 0; i < tokens.size(); i++) {
if (tokens[i].keyword == keywords::PRINT) { if (tokens[i].keyword == keywords::PRINT) {
i++; i++;
@ -368,8 +389,12 @@ class Interpreter {
case valtype::INT: case valtype::INT:
exit(get<int>(nextToken.value.value)); exit(get<int>(nextToken.value.value));
break; break;
case valtype::DEC:
exit(get<double>(nextToken.value.value));
break;
default: default:
log.error("Unsupported value type"); vector<string> validTypes = {"int", "dec"};
syntaxError.fnTypeMismatch("exit", validTypes, nextToken.type);
} }
} }
} }
@ -404,4 +429,4 @@ int main(int argc, char* argv[]) {
// Convert to tokens and run the code // Convert to tokens and run the code
interpreter.convertToTokens(parser.getTokens()); interpreter.convertToTokens(parser.getTokens());
return 0; return 0;
} }

View File

@ -1,2 +1,2 @@
print "Hello world!"; print "Hello world!";
exit 1; exit fjdskfdkls;