Skip to content

Commit

Permalink
feat: add more asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
xXenvy committed Jul 28, 2024
1 parent 55ecf87 commit 38ced28
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/execution/evaluator.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "../common/include/errors.hpp"
#include "include/evaluator.hpp"
#include <memory>
#include <cassert>


void Evaluator::evaluate(ASTBase* ast) {
assert(ast != nullptr && "Ast should not be nullptr");

switch (ast->type) {
case ASTType::Program:
evaluateProgram(static_cast<ASTProgram*>(ast));
Expand Down
2 changes: 2 additions & 0 deletions src/gc/gc.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "include/gc.hpp"
#include "include/block.hpp"
#include <iostream>
#include <cassert>

void GarbageCollector::mark(GCObject* obj) {
if (!obj) return;
Expand All @@ -10,6 +11,7 @@ void GarbageCollector::mark(GCObject* obj) {

void GarbageCollector::sweep() {
for (auto it = trackingObjects.begin(); it != trackingObjects.end();) {
assert(*it != nullptr && "Object should not be nullptr");
if (!(*it)->isMarked()) {
it = trackingObjects.erase(it);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/parsing/include/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum class TokenType {
INT, FLOAT, STRING, BOOL, // Types.

ADD, SUBTRACT, MULTIPLY, // Operators.
DIVIDE, EQUAL, NOT_EQUAL, EQUALITY, // Equal is =, Equality is ==
DIVIDE, ASSIGN, NOT_EQUAL, EQUAL, // Assign is =, Equal is ==

IDENTIFIER, COLON, LBRACE, // Syntax.
RBRACE, SEMICOLON, LBRACKET,
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ Token Lexer::next() {
case '(': return Token(TokenType::LBRACKET, "(", currentLine);
case ')': return Token(TokenType::RBRACKET, ")", currentLine);
case '=': {
if (peek() != '=') return Token(TokenType::EQUAL, "=", currentLine);
if (peek() != '=') return Token(TokenType::ASSIGN, "=", currentLine);
moveForward();
return Token(TokenType::EQUALITY, "==", currentLine);
return Token(TokenType::EQUAL, "==", currentLine);
}
case '!': {
if (peek() != '=') return Token(TokenType::UNKNOWN, std::string(1, current), currentLine);
Expand Down
6 changes: 3 additions & 3 deletions src/parsing/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ std::unique_ptr<ASTBase> Parser::parseCurrent() {
case TokenType::IDENTIFIER:
moveForward();
if (currentToken().type == TokenType::LBRACKET) return parseFunctionCall();
if (currentToken().type == TokenType::EQUAL) return parseVariableModify();
if (currentToken().type == TokenType::ASSIGN) return parseVariableModify();
[[fallthrough]];
default:
throw ZynkError{ ZynkErrorType::UnknownError, "Notimplemented: " + current.value, &current.line };
Expand Down Expand Up @@ -91,7 +91,7 @@ std::unique_ptr<ASTBase> Parser::parseVariableDeclaration() {
&currentLine,
};
}
consume({ TokenType::EQUAL, "=", currentLine });
consume({ TokenType::ASSIGN, "=", currentLine });
auto varDeclaration = std::make_unique<ASTVariableDeclaration>(
varName, varType, parseExpression(0)
);
Expand All @@ -104,7 +104,7 @@ std::unique_ptr<ASTBase> Parser::parseVariableModify() {
const Token current = currentToken();

consume({ TokenType::IDENTIFIER, current.value, current.line});
consume({ TokenType::EQUAL, "=", current.line });
consume({ TokenType::ASSIGN, "=", current.line });
std::unique_ptr<ASTBase> newValue = parseExpression(0);
consume({ TokenType::SEMICOLON, ";", current.line });

Expand Down
2 changes: 1 addition & 1 deletion tests/test_lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ TEST(LexerTokenizeTest, EqualityOperator) {
size_t operators = 0;

for (const Token& token : tokens) {
if (token.type == TokenType::EQUALITY) operators++;
if (token.type == TokenType::EQUAL) operators++;
}
EXPECT_TRUE(operators == 2);
EXPECT_TRUE(tokens.size() == 9);
Expand Down

0 comments on commit 38ced28

Please sign in to comment.