Bug fixes

This commit is contained in:
Bobby Lucero 2023-05-28 14:55:07 -04:00
parent b38f6bff25
commit 31b334ec65
6 changed files with 32 additions and 4 deletions

View File

@ -4,7 +4,7 @@
CC = g++ CC = g++
# Compiler flags # 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 # Source directory
SRC_DIR = ./source SRC_DIR = ./source

View File

@ -23,6 +23,23 @@ enum TokenType{
END_OF_FILE 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<std::string, TokenType> KEYWORDS { const std::map<std::string, TokenType> KEYWORDS {
{"and", AND}, {"and", AND},
{"or", OR}, {"or", OR},

View File

@ -24,6 +24,8 @@ sptr(Object) Interpreter::visitLiteralExpr(sptr(LiteralExpr) expr) {
} }
return msptr(Number)(num); 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); return msptr(String)(expr->value);
} }
@ -55,6 +57,9 @@ sptr(Object) Interpreter::visitUnaryExpr(sptr(UnaryExpr) expression)
return msptr(Boolean)(!isTruthy(right)); return msptr(Boolean)(!isTruthy(right));
} }
//unreachable
throw std::runtime_error("Invalid unary expression");
} }
sptr(Object) Interpreter::visitBinaryExpr(sptr(BinaryExpr) 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<Boolean>(object)) if(auto boolean = std::dynamic_pointer_cast<Boolean>(object))
{ {
boolean->value; return boolean->value;
} }
if(auto obj = std::dynamic_pointer_cast<None>(object)) if(auto obj = std::dynamic_pointer_cast<None>(object))
@ -193,6 +198,8 @@ bool Interpreter::isEqual(sptr(Object) a, sptr(Object) b) {
return false; return false;
} }
throw std::runtime_error("Invalid isEqual compariosn");
} }
void Interpreter::interpret(std::shared_ptr<Expr> expr) { void Interpreter::interpret(std::shared_ptr<Expr> expr) {
@ -239,6 +246,8 @@ std::string Interpreter::stringify(std::shared_ptr<Object> object) {
{ {
return Bool->value == 1 ? "true" : "false"; return Bool->value == 1 ? "true" : "false";
} }
throw std::runtime_error("Could not convert object to string");
} }
bool Interpreter::isWholeNumer(double num) { bool Interpreter::isWholeNumer(double num) {

View File

@ -287,6 +287,8 @@ char Lexer::peekNext()
{ {
return src[1]; return src[1];
} }
return '\0';
} }

View File

@ -83,7 +83,7 @@ sptr(Expr) Parser::unary()
sptr(Expr) Parser::primary() 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({TRUE})) return msptr(LiteralExpr)("true", false, false);
if(match({NONE})) return msptr(LiteralExpr)("none", false, false); if(match({NONE})) return msptr(LiteralExpr)("none", false, false);

View File

@ -58,7 +58,7 @@ void Bob::run(string source)
//cout << dynamic_pointer_cast<String>(printer.print(expr))->value << endl; //cout << dynamic_pointer_cast<String>(printer.print(expr))->value << endl;
for(Token t : tokens){ for(Token t : tokens){
//cout << "{type: " << t.type << ", value: " << t.lexeme << "}" << endl; //cout << "{type: " << enum_mapping[t.type] << ", value: " << t.lexeme << "}" << endl;
} }