diff --git a/Makefile b/Makefile index 0f0b78e..a305d21 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CC = g++ # Compiler flags -CFLAGS = -Wall -Wextra -std=c++17 -Wno-unused-variable -Wno-unused-parameter +CFLAGS = -Wall -Wextra -std=c++17 -Wno-unused-variable -Wno-unused-parameter -Wno-switch # Source directory SRC_DIR = ./source diff --git a/headers/Lexer.h b/headers/Lexer.h index a582c7e..7c6be66 100644 --- a/headers/Lexer.h +++ b/headers/Lexer.h @@ -23,6 +23,23 @@ enum TokenType{ END_OF_FILE }; +inline std::string enum_mapping[] = {"OPEN_PAREN", "CLOSE_PAREN", "OPEN_BRACE", "CLOSE_BRACE", + "COMMA", "DOT", "MINUS", "PLUS", "SEMICOLON", "SLASH", "STAR", + + "BINARY_OP", + + "BANG", "BANG_EQUAL", + "EQUAL", "DOUBLE_EQUAL", + "GREATER", "GREATER_EQUAL", + "LESS", "LESS_EQUAL", + + "IDENTIFIER", "STRING", "NUMBER", + + "AND", "OR", "TRUE", "FALSE", "IF", "ELSE", "FUNCTION", "FOR", + "WHILE", "VAR", "CLASS", "SUPER", "THIS", "NONE", "RETURN", + + "END_OF_FILE"}; + const std::map KEYWORDS { {"and", AND}, {"or", OR}, diff --git a/source/Interpreter.cpp b/source/Interpreter.cpp index f77dbbb..03fe954 100644 --- a/source/Interpreter.cpp +++ b/source/Interpreter.cpp @@ -24,6 +24,8 @@ sptr(Object) Interpreter::visitLiteralExpr(sptr(LiteralExpr) expr) { } return msptr(Number)(num); } + if(expr->value == "true") return msptr(Boolean)(true); + if(expr->value == "false") return msptr(Boolean)(false); return msptr(String)(expr->value); } @@ -55,6 +57,9 @@ sptr(Object) Interpreter::visitUnaryExpr(sptr(UnaryExpr) expression) return msptr(Boolean)(!isTruthy(right)); } + //unreachable + throw std::runtime_error("Invalid unary expression"); + } sptr(Object) Interpreter::visitBinaryExpr(sptr(BinaryExpr) expression) @@ -145,7 +150,7 @@ bool Interpreter::isTruthy(sptr(Object) object) { if(auto boolean = std::dynamic_pointer_cast(object)) { - boolean->value; + return boolean->value; } if(auto obj = std::dynamic_pointer_cast(object)) @@ -193,6 +198,8 @@ bool Interpreter::isEqual(sptr(Object) a, sptr(Object) b) { return false; } + + throw std::runtime_error("Invalid isEqual compariosn"); } void Interpreter::interpret(std::shared_ptr expr) { @@ -239,6 +246,8 @@ std::string Interpreter::stringify(std::shared_ptr object) { { return Bool->value == 1 ? "true" : "false"; } + + throw std::runtime_error("Could not convert object to string"); } bool Interpreter::isWholeNumer(double num) { diff --git a/source/Lexer.cpp b/source/Lexer.cpp index 50ed1f5..507e15f 100644 --- a/source/Lexer.cpp +++ b/source/Lexer.cpp @@ -287,6 +287,8 @@ char Lexer::peekNext() { return src[1]; } + + return '\0'; } diff --git a/source/Parser.cpp b/source/Parser.cpp index 131e714..cf17b39 100644 --- a/source/Parser.cpp +++ b/source/Parser.cpp @@ -83,7 +83,7 @@ sptr(Expr) Parser::unary() sptr(Expr) Parser::primary() { - if(match({FALSE})) return msptr(LiteralExpr)("true", false, false); + if(match({FALSE})) return msptr(LiteralExpr)("false", false, false); if(match({TRUE})) return msptr(LiteralExpr)("true", false, false); if(match({NONE})) return msptr(LiteralExpr)("none", false, false); diff --git a/source/bob.cpp b/source/bob.cpp index 247b20b..2c3f588 100644 --- a/source/bob.cpp +++ b/source/bob.cpp @@ -58,7 +58,7 @@ void Bob::run(string source) //cout << dynamic_pointer_cast(printer.print(expr))->value << endl; for(Token t : tokens){ - //cout << "{type: " << t.type << ", value: " << t.lexeme << "}" << endl; + //cout << "{type: " << enum_mapping[t.type] << ", value: " << t.lexeme << "}" << endl; }