Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
amanuel2 committed Jun 25, 2024
1 parent 2dfbd6f commit f74d1ea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
6 changes: 4 additions & 2 deletions include/utils/literals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace rift
/// @brief casts token to a number
extern any castNumber(const Token& tok);
/// @brief casts token to a number then to a string for printing
extern std::string castNumberString(const any& val);
extern std::string castNumberString(const any& val, bool err = true);
/// @brief casts token to a string
extern std::string castString(const Token& tok);
extern std::string castString(const Token& tok, bool err = true);
/// @brief casts token to a string for printing
extern std::string castAnyString(const Token& token);
}
2 changes: 1 addition & 1 deletion lib/ast/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ namespace rift
Token Visitor::visit_print_stmt(const StmtPrint& stmt) const
{
Token val = stmt.expr->accept(*this);
std::string res = castString(val);
std::string res = castAnyString(val);
if (res.at(0) == '"' && res.at(res.size()-1) == '"')
res = res.substr(1, res.size()-2);
std::cout << res << std::endl;
Expand Down
28 changes: 15 additions & 13 deletions lib/ast/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,22 @@ namespace rift

std::unique_ptr<Expr> Parser::assignment()
{
if (match({Token(TokenType::IDENTIFIER, "", "", line)})) {
if (peek(Token(TokenType::SEMICOLON, ";", "", line))) {
prevance();
return equality();
if(peekNext() == Token(TokenType::EQUAL, "=", "", line)) {
if (match({Token(TokenType::IDENTIFIER, "", "", line)})) {
if (peek(Token(TokenType::SEMICOLON, ";", "", line))) {
prevance();
return equality();
}
auto idt = peekPrev();
consume(Token(TokenType::EQUAL, "=", "", line), std::unique_ptr<ParserException>(new ParserException("Expected '=' after variable name")));
auto expr = assignment();
if (expr == nullptr) rift::error::report(line, "assignment", "Expected expression after variable name", peekPrev(), ParserException("Expected expression after variable name"));

// assignemnt operator expects lhs to be already declared
if (rift::ast::Environment::getInstance().getEnv(castString(idt)) == Token())
rift::error::report(line, "assignment", "🛑 Undefined variable '" + castString(idt) + "' at line: " + castNumberString(idt.line), idt, ParserException("Undefined variable '" + castString(idt) + "'"));
return std::unique_ptr<Assign>(new Assign(idt, std::move(expr)));
}
auto idt = peekPrev();
consume(Token(TokenType::EQUAL, "=", "", line), std::unique_ptr<ParserException>(new ParserException("Expected '=' after variable name")));
auto expr = assignment();
if (expr == nullptr) rift::error::report(line, "assignment", "Expected expression after variable name", peekPrev(), ParserException("Expected expression after variable name"));

// assignemnt operator expects lhs to be already declared
if (rift::ast::Environment::getInstance().getEnv(castString(idt)) == Token())
rift::error::report(line, "assignment", "🛑 Undefined variable '" + castString(idt) + "' at line: " + castNumberString(idt.line), idt, ParserException("Undefined variable '" + castString(idt) + "'"));
return std::unique_ptr<Assign>(new Assign(idt, std::move(expr)));
}

return equality();
Expand Down
23 changes: 18 additions & 5 deletions lib/utils/literals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace rift
return "";
}

std::string castNumberString(const any& val) {
std::string castNumberString(const any& val, bool err) {
if (val.type() == typeid(double)) {
return std::to_string(std::any_cast<double>(val));
} else if (val.type() == typeid(float)) {
Expand All @@ -105,12 +105,12 @@ namespace rift
} else if (val.type() == typeid(Token)) {
return castNumberString(std::any_cast<Token>(val).getLiteral());
}

rift::error::runTimeError("Expected a number or token");
if (err)
rift::error::runTimeError("Expected a number or token");
return "";
}

std::string castString(const Token& tok) {
std::string castString(const Token& tok, bool err) {
any val = tok.getLiteral();
if (val.type() == typeid(std::string)) {
return std::any_cast<std::string>(val);
Expand All @@ -126,8 +126,21 @@ namespace rift
return std::string(1, std::any_cast<signed char>(val));
}

rift::error::runTimeError("Expected a string");
if (err)
rift::error::runTimeError("Expected a string");
return "";
}

std::string castAnyString(const Token& tok)
{
std::string str = castNumberString(tok.getLiteral(), false);
if (str.empty()) {
str = castString(tok, false);
if (str.empty()) {
rift::error::runTimeError("Expected a string or number");
}
}
return str;
}
}

0 comments on commit f74d1ea

Please sign in to comment.