diff --git a/e b/e deleted file mode 100644 index e69de29..0000000 diff --git a/mx b/mx index 04f2300..e640673 100755 Binary files a/mx and b/mx differ diff --git a/names.mx b/names.mx new file mode 100644 index 0000000..81b22cc --- /dev/null +++ b/names.mx @@ -0,0 +1,4 @@ +println "Hello there!"; +print "What is your name? "; +let str yourName input; +println "Hello, " + yourName + "!"; \ No newline at end of file diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index 0870949..38b11ad 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -29,11 +29,13 @@ void Interpreter::convertToTokens(vector tokenList) { if (nextToken->keyword == keywords::SEMICOLON || nextToken->keyword == keywords::OBRAC || nextToken->keyword == keywords::CBRAC) { consume(); // consume the semicolon break; + } else if (nextToken->keyword == keywords::COMMENT) { + break; } consume(); // consume the peeked token currentInstruction.push_back(nextToken.value()); } - // Apply variables to tokens + // Apply variables to tokens, as well as allow users to input strings // We start at 1 so we can reassign variables in the execution of code for (int i = 1; i < currentInstruction.size(); i++) { if (currentInstruction[i].type == valtype::STR) { @@ -48,6 +50,15 @@ void Interpreter::convertToTokens(vector tokenList) { newToken.value = varIt->second; currentInstruction[i] = newToken; } + } else if (currentInstruction[i].keyword == keywords::INPUT) { + Token newToken; + string buffer; + getline(cin, buffer); + newToken.value.value = buffer; + newToken.keyword = keywords::VALUE; + newToken.type = valtype::STR; + newToken.value.type = valtype::STR; + currentInstruction[i] = newToken; } } // Do math @@ -148,6 +159,42 @@ void Interpreter::convertToTokens(vector tokenList) { get(currentInstruction[i + 1].value.value)); } + currentInstruction[i - 1] = newToken; + currentInstruction.erase(currentInstruction.begin() + i); + currentInstruction.erase(currentInstruction.begin() + i); + i -= 1; + } + if (currentInstruction[i].keyword == keywords::INEQUAL) { + Token newToken; + newToken.keyword = keywords::VALUE; + newToken.type = valtype::BOOL; + newToken.value.type = valtype::BOOL; + if (currentInstruction.size() < i + 1) syntaxError.mathTooFewArgs(); + if (currentInstruction[i - 1].type != currentInstruction[i + 1].type) syntaxError.mathTypeMismatch(); + log.debug("Comparing values of type: " + to_string(static_cast(currentInstruction[i - 1].type))); + + // Make sure both operands are values + if (currentInstruction[i - 1].keyword != keywords::VALUE || + currentInstruction[i + 1].keyword != keywords::VALUE) { + syntaxError.generalError("Can only compare values of"); + return; + } + + // Ensure value types are set correctly + if (currentInstruction[i - 1].value.type == valtype::INT) { + newToken.value.value = (get(currentInstruction[i - 1].value.value) != + get(currentInstruction[i + 1].value.value)); + } else if (currentInstruction[i - 1].value.type == valtype::DEC) { + newToken.value.value = (get(currentInstruction[i - 1].value.value) != + get(currentInstruction[i + 1].value.value)); + } else if (currentInstruction[i - 1].value.type == valtype::STR) { + newToken.value.value = (get(currentInstruction[i - 1].value.value) != + get(currentInstruction[i + 1].value.value)); + } else if (currentInstruction[i - 1].value.type == valtype::BOOL) { + newToken.value.value = (get(currentInstruction[i - 1].value.value) != + get(currentInstruction[i + 1].value.value)); + } + currentInstruction[i - 1] = newToken; currentInstruction.erase(currentInstruction.begin() + i); currentInstruction.erase(currentInstruction.begin() + i); @@ -170,7 +217,7 @@ void Interpreter::executeCode(vector tokens) { skipScope --; return; } - if (tokens[i].keyword == keywords::PRINT) { + if (tokens[i].keyword == keywords::PRINTLN) { i++; if (tokens.size() <= i) break; Token nextToken = tokens[i]; @@ -189,6 +236,32 @@ void Interpreter::executeCode(vector tokens) { case valtype::BOOL: cout << (get(nextToken.value.value) ? "true" : "false") << endl; break; + default: + vector validTypes = {"int", "dec", "str", "bool"}; + syntaxError.fnTypeMismatch("println", validTypes, nextToken.type); + } + } else { + syntaxError.fnNotSufficientArgs("println", 1, 1, 0); + } + } else if (tokens[i].keyword == keywords::PRINT) { + i++; + if (tokens.size() <= i) break; + Token nextToken = tokens[i]; + // Handle different value types + if (nextToken.keyword == keywords::VALUE) { + switch (nextToken.type) { + case valtype::INT: + cout << get(nextToken.value.value); + break; + case valtype::DEC: + cout << get(nextToken.value.value); + break; + case valtype::STR: + cout << get(nextToken.value.value); + break; + case valtype::BOOL: + cout << (get(nextToken.value.value) ? "true" : "false"); + break; default: vector validTypes = {"int", "dec", "str", "bool"}; syntaxError.fnTypeMismatch("print", validTypes, nextToken.type); diff --git a/src/Parser.cpp b/src/Parser.cpp index 234daae..0826da3 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -101,6 +101,8 @@ void Parser::processLines() { else if (ct == "fun") token.keyword = keywords::FUN; else if (ct == "let") token.keyword = keywords::LET; else if (ct == "print") token.keyword = keywords::PRINT; + else if (ct == "println") token.keyword = keywords::PRINTLN; + else if (ct == "input") token.keyword = keywords::INPUT; else if (ct == "return") token.keyword = keywords::RETURN; else if (ct == "exit") token.keyword = keywords::EXIT; else if (ct == "int") token.keyword = keywords::INT; @@ -125,6 +127,10 @@ void Parser::processLines() { else if (ct == "*=") token.keyword = keywords::MULTIPLYTO; else if (ct == "/=") token.keyword = keywords::DIVIDEFROM; else if (ct == "==") token.keyword = keywords::EQUAL; + else if (ct == "!=") token.keyword = keywords::INEQUAL; + else if (ct == "<") token.keyword = keywords::LESS; + else if (ct == ">") token.keyword = keywords::GREATER; + else if (ct == "//") token.keyword = keywords::COMMENT; else { token.keyword = keywords::VALUE; // Convert the value based on its type diff --git a/src/common.h b/src/common.h index 9da9965..091d468 100644 --- a/src/common.h +++ b/src/common.h @@ -20,8 +20,9 @@ enum class keywords { ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, LESS, GREATER, EQLESS, EQGREATER, INCREMENT, DECREMENT, - PRINT, LET, INPUT, EXIT, - VALUE, SEMICOLON, VARIABLE + PRINT, PRINTLN, LET, INPUT, EXIT, + VALUE, SEMICOLON, VARIABLE, + COMMENT }; struct Value { diff --git a/test.fish b/test.fish index 19c2e8a..9e82140 100644 --- a/test.fish +++ b/test.fish @@ -2,4 +2,4 @@ bob -./mx --debug test.mx \ No newline at end of file +./mx test.mx \ No newline at end of file diff --git a/test.mx b/test.mx index 9427bc6..180f2ac 100644 --- a/test.mx +++ b/test.mx @@ -1,15 +1,12 @@ -print "dingusify"; -let str bingus "heheheha its bingusing time"; -print bingus; -print 1 + 1; -let int myInt 3; -print myInt; -myInt = 5; -print myInt; -let str myString "dingus"; -print myString; -myString = "dongus"; -print myString; -print 5 == 5; -print 843 == 122; -print "dingus" == "dongus"; \ No newline at end of file +let str thing2 "dingus"; +let str thing2 "dingus"; + +if thing1 == thing2 { + println "thing 1 and thing 2 are the same"; +} + +print "this is printing something without a new line"; +println " this is coming from a seperate print statement lols"; + +print "Say some wise words... "; +println "Ah, '" + input + "', some very wise words indeed!"; \ No newline at end of file