Skip to content

Commit

Permalink
if stmt done
Browse files Browse the repository at this point in the history
  • Loading branch information
amanuel2 committed Jun 27, 2024
1 parent 18e96d5 commit a277325
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/lib/riftlang",
"args": ["-p"],
"args": ["--i"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
Expand All @@ -28,7 +28,7 @@
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/tests/riftlangtest",
"args": ["-p"],
"args": ["--i"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
Expand Down
3 changes: 2 additions & 1 deletion include/ast/grmr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ namespace rift
__DEFAULT_FORWARD_NONE_VA(
Stmt,
StmtPrint,
StmtExpr
StmtExpr,
StmtIf
)

__DEFAULT_FORWARD_NONE_VA(
Expand Down
9 changes: 0 additions & 9 deletions include/reader/reader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ namespace rift
return false;
}

inline bool match_kw(T expected/*, std::string kw*/) {
if (peek(expected)) {
// for (size_t i=0; i<=kw.size(); i++) advance();
advance();
return true;
}
return false;
}

/// @brief peeks a word (useful for keywords/identifiers/statements)
inline bool peek_word(std::vector<T> expected, int n) {
for (int i=0; i<n; i++) {
Expand Down
17 changes: 14 additions & 3 deletions include/utils/macros.hh
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,25 @@
else \
rift::error::report(op_tok.line, "Arithmetic Error", "Invalid operands for arithmetic operation", Token(), std::exception());

#define _STRING_ARITHMETIC() \
if (expr.op.type == TokenType::GREATER) \
return (strcmp(castString(left).c_str(),castString(right).c_str())>0) ? Token(TokenType::TRUE, "true", "true", expr.op.line) : Token(TokenType::FALSE, "false", "false", expr.op.line); \
else if (expr.op.type == TokenType::LESS) \
return (strcmp(castString(left).c_str(),castString(right).c_str())<0) ? Token(TokenType::TRUE, "true", "true", expr.op.line) : Token(TokenType::FALSE, "false", "false", expr.op.line); \
else if (expr.op.type == TokenType::GREATER_EQUAL) \
return (strcmp(castString(left).c_str(),castString(right).c_str())>=0) ? Token(TokenType::TRUE, "true", "true", expr.op.line) : Token(TokenType::FALSE, "false", "false", expr.op.line); \
else if (expr.op.type == TokenType::LESS_EQUAL) \
return (strcmp(castString(left).c_str(),castString(right).c_str())<=0) ? Token(TokenType::TRUE, "true", "true", expr.op.line) : Token(TokenType::FALSE, "false", "false", expr.op.line); \
else if (expr.op.type == TokenType::EQUAL_EQUAL) \
return (strcmp(castString(left).c_str(),castString(right).c_str())==0) ? Token(TokenType::TRUE, "true", "true", expr.op.line) : Token(TokenType::FALSE, "false", "false", expr.op.line); \
else if (expr.op.type == TokenType::BANG_EQUAL) \
return (strcmp(castString(left).c_str(),castString(right).c_str())!=0) ? Token(TokenType::TRUE, "true", "true", expr.op.line) : Token(TokenType::FALSE, "false", "false", expr.op.line); \

#pragma mark - Specific Codebase

#define _BOOL_LOGIC(op) if (isNumber(left) && isNumber(right)) {\
resBool = std::any_cast<bool>(any_arithmetic(left, right, op));\
return Token(resBool?TokenType::TRUE:TokenType::FALSE, resBool?"true":"false", resBool, op.line);\
} else if (isString(left) && isString(right)) {\
resBool = castString(left) > castString(right);\
return Token(resBool?TokenType::TRUE:TokenType::FALSE, resBool?"true":"false", resBool, op.line);\
_STRING_ARITHMETIC()\
}\

1 change: 0 additions & 1 deletion lib/ast/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace rift
{
Environment *curr = this;
while (curr != nullptr) {
std::cout << "IN ";
for (const auto& [key, value] : curr->values) {
std::cout << key << " => " << value.to_string() << std::endl;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/ast/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ namespace rift

/* comparison ops */
case TokenType::GREATER:
_BOOL_LOGIC(expr.op);
if(strcmp(castString(left).c_str(),castString(right).c_str())>0)
return Token(TokenType::TRUE, "true", "true", expr.op.line);
return Token(TokenType::FALSE, "false", "false", expr.op.line);
rift::error::runTimeError("Expected a number or string for '>' operator");
case TokenType::GREATER_EQUAL:
_BOOL_LOGIC(expr.op);
Expand Down
9 changes: 5 additions & 4 deletions lib/ast/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ namespace rift
std::unique_ptr<Stmt> Parser::ret_stmt()
{
std::unique_ptr<Stmt> stmt;
if (match_kw (Token(TokenType::PRINT, "", "", line))) {
if (consume (Token(TokenType::PRINT, "", "", line))) {
stmt = statement_print();
} else if (consume(Token(TokenType::IF, "if", "if", line))) {
stmt = statement_if();
} else {
stmt = statement_expression();
}
Expand All @@ -197,7 +199,6 @@ namespace rift
std::unique_ptr<StmtExpr> Parser::statement_expression()
{
auto expr = expression();
consume(Token(TokenType::SEMICOLON, ";", "", line), std::unique_ptr<ParserException>(new ParserException("Expected ';' after expression")));
return std::unique_ptr<StmtExpr>(new StmtExpr(std::move(expr)));
}

Expand Down Expand Up @@ -307,7 +308,7 @@ namespace rift
if (match({Token(TokenType::LEFT_BRACE, "{", "", line)})) {
auto inner_decls = std::move(block()->decls);
decls->insert(decls->end(), std::make_move_iterator(inner_decls->begin()), std::make_move_iterator(inner_decls->end()));
} else if (match_kw (Token(TokenType::VAR, "", "", line))) {
} else if (consume (Token(TokenType::VAR, "", "", line))) {
auto decl = declaration_variable();
decls->push_back(std::move(decl));
} else {
Expand All @@ -327,7 +328,7 @@ namespace rift
vec_prog decls = std::make_unique<std::vector<std::unique_ptr<Decl>>>();

while (!atEnd()) {
if (match_kw (Token(TokenType::VAR, "", "", line))) {
if (consume (Token(TokenType::VAR, "", "", line))) {
decls->push_back(declaration_variable());
} else if(match({Token(TokenType::LEFT_BRACE, "{", "", line)})) {
decls->push_back(block());
Expand Down
8 changes: 4 additions & 4 deletions lib/scanner/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ namespace rift
case '+': addToken(Type::PLUS, "+");break;
case ';': addToken(Type::SEMICOLON, ";");break;
case '*': addToken(Type::STAR, "*");break;
case '!': addToken(match_one('=') ? Type::BANG_EQUAL : Type::BANG, "!");break;
case '=': addToken(match_one('=') ? Type::EQUAL_EQUAL : Type::EQUAL, "=");break;
case '<': addToken(match_one('=') ? Type::LESS_EQUAL : Type::LESS, "<");break;
case '>': addToken(match_one('=') ? Type::GREATER_EQUAL : Type::GREATER, ">");break;
case '!': addToken(match_one('=') ? Type::BANG_EQUAL : Type::BANG);break;
case '=': addToken(match_one('=') ? Type::EQUAL_EQUAL : Type::EQUAL);break;
case '<': addToken(match_one('=') ? Type::LESS_EQUAL : Type::LESS);break;
case '>': addToken(match_one('=') ? Type::GREATER_EQUAL : Type::GREATER);break;
case '/': match_one('/') ? scanComment() : addToken(Type::SLASH, "/");break;
case '"': string(); break;
case ' ': break;
Expand Down

0 comments on commit a277325

Please sign in to comment.