Skip to content

Commit

Permalink
More hints
Browse files Browse the repository at this point in the history
  • Loading branch information
SingularityT3 committed Mar 30, 2024
1 parent 146e4a5 commit ae0d74c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/lexer/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ const std::vector<Token*> Lexer::makeTokens() {
}
continue;
} else if (currentChar == '(') {
if (idx > 0 && tokens.size() > 0 && (*expr)[idx-1] != ' ') {
TokenType t = tokens[tokens.size()-1]->type;
if (t == TokenType::INPUT) {
throw PSC::LexerError(line, column, "Unexpected '('\n'INTPUT' is statement(not a function) used as 'INPUT <var>' where <var> is a variable to store the input");
} else if (t == TokenType::OUTPUT) {
throw PSC::LexerError(line, column, "Unexpected '('\n'OUTPUT' is statement(not a function) used as 'OUTPUT <value>' or 'OUTPUT <value1>, <value2>, ...'");
} else if (t == TokenType::OPENFILE) {
throw PSC::LexerError(line, column, "Unexpected '('\n'OPENFILE' is statement(not a function) used as 'OPENFILE <filename> FOR <mode>'");
} else if (t == TokenType::READFILE) {
throw PSC::LexerError(line, column, "Unexpected '('\n'READFILE' is statement(not a function) used as 'READFILE <filename>, <var>'");
} else if (t == TokenType::WRITEFILE) {
throw PSC::LexerError(line, column, "Unexpected '('\n'WRITEFILE' is statement(not a function) used as 'WRITEFILE <filename>, <line>'");
} else if (t == TokenType::CLOSEFILE) {
throw PSC::LexerError(line, column, "Unexpected '('\n'CLOSEFILE' is statement(not a function) used as 'CLOSEFILE <filename>'");
}
}
tokens.emplace_back(new Token(TokenType::LPAREN, line, column));
} else if (currentChar == ')') {
tokens.emplace_back(new Token(TokenType::RPAREN, line, column));
Expand All @@ -70,7 +86,7 @@ const std::vector<Token*> Lexer::makeTokens() {
} else if (currentChar == '=') {
advance();
if (idx >= expr->size() || currentChar != '=') {
tokens.emplace_back(new Token(TokenType::EQUALS, line, column));
tokens.emplace_back(new Token(TokenType::EQUALS, line, column - 1));
continue;
} else {
throw PSC::LexerError(line, column - 1, "Invalid token '=='\nIf you want to check for equality, use single '=' instead");
Expand Down
11 changes: 4 additions & 7 deletions src/nodes/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#include "nodes/nodeResult.h"

class Node {
protected:
public:
const Token &token;

public:
Node(const Token &token);

virtual ~Node() = default;
Expand All @@ -22,21 +21,19 @@ class Node {
};

class UnaryNode : public Node {
protected:
Node &node;

public:
Node &node;

UnaryNode(const Token &token, Node &node);

std::string toStr() const override;
};

class BinaryNode : public Node {
protected:
public:
Node &left;
Node &right;

public:
BinaryNode(const Token &token, Node &left, Node &right);

std::string toStr() const override;
Expand Down
7 changes: 7 additions & 0 deletions src/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ PSC::Block *Parser::parseBlock(BlockType blockType) {
node = parseFunction();
} else {
node = parseExpression();
ComparisonNode *cn = dynamic_cast<ComparisonNode*>(node);
if (cn != nullptr) {
AccessNode *an = dynamic_cast<AccessNode*>(&(cn->left));
if (an != nullptr) {
std::cout << "Warning on line " << cn->token.line << " column " << cn->token.column << ": Comparison result is ignored. Use '<-' instead of '=' if you wanted to assign." << std::endl;
}
}
}

block->addNode(node);
Expand Down

0 comments on commit ae0d74c

Please sign in to comment.