diff --git a/source/parser.c b/source/parser.c index 8badb03..217ca71 100644 --- a/source/parser.c +++ b/source/parser.c @@ -419,7 +419,7 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) { parsePrecedence(parser, &tmpNode, PREC_TERNARY); //can be a literal //optimisation: check for negative literals - if (tmpNode->type == AST_NODE_LITERAL && (IS_INTEGER(tmpNode->atomic.literal) || IS_FLOAT(tmpNode->atomic.literal))) { + if (tmpNode != NULL && tmpNode->type == AST_NODE_LITERAL && (IS_INTEGER(tmpNode->atomic.literal) || IS_FLOAT(tmpNode->atomic.literal))) { //negate directly, if int or float Literal lit = tmpNode->atomic.literal; @@ -438,7 +438,7 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) { } //check for negated boolean errors - if (tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) { + if (tmpNode != NULL && tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) { error(parser, parser->previous, "Negative booleans are not allowed"); return OP_EOF; } @@ -451,8 +451,8 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) { //temp handle to potentially negate values parsePrecedence(parser, &tmpNode, PREC_CALL); //can be a literal, grouping, fn call, etc. - //check for inverted booleans - if (tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) { + //optimisation: check for inverted booleans + if (tmpNode != NULL && tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) { //negate directly, if boolean Literal lit = tmpNode->atomic.literal; @@ -694,6 +694,13 @@ static Opcode fnCall(Parser* parser, ASTNode** nodeHandle) { ASTNode* tmpNode = NULL; parsePrecedence(parser, &tmpNode, PREC_TERNARY); + + //BUGFIX + if (!tmpNode) { + error(parser, parser->previous, "[internal] No token found in fnCall"); + return OP_EOF; + } + arguments->fnCollection.nodes[arguments->fnCollection.count++] = *tmpNode; FREE(ASTNode, tmpNode); //simply free the tmpNode, so you don't free the children } while(match(parser, TOKEN_COMMA)); @@ -1303,6 +1310,12 @@ static void returnStmt(Parser* parser, ASTNode** nodeHandle) { ASTNode* node = NULL; parsePrecedence(parser, &node, PREC_TERNARY); + //BUGFIX + if (!node) { + error(parser, parser->previous, "[internal] No token found in return"); + return; + } + returnValues->fnCollection.nodes[returnValues->fnCollection.count++] = *node; FREE(ASTNode, node); //free manually } while(match(parser, TOKEN_COMMA)); diff --git a/test/scripts/mustfail/unary-inverted-nothing.toy b/test/scripts/mustfail/unary-inverted-nothing.toy new file mode 100644 index 0000000..7fa2514 --- /dev/null +++ b/test/scripts/mustfail/unary-inverted-nothing.toy @@ -0,0 +1 @@ +print ! \ No newline at end of file diff --git a/test/scripts/mustfail/unary-negative-nothing.toy b/test/scripts/mustfail/unary-negative-nothing.toy new file mode 100644 index 0000000..31a8c6c --- /dev/null +++ b/test/scripts/mustfail/unary-negative-nothing.toy @@ -0,0 +1 @@ +print - \ No newline at end of file diff --git a/test/test_mustfail.c b/test/test_mustfail.c index 9261ae3..e53edce 100644 --- a/test/test_mustfail.c +++ b/test/test_mustfail.c @@ -69,7 +69,7 @@ unsigned char* compileString(char* source, size_t* size) { while(node != NULL) { //pack up and leave if (node->type == AST_NODE_ERROR) { - printf(ERROR "error node detected\n" RESET); + errorsTriggered++; freeASTNode(node); freeCompiler(&compiler); freeParser(&parser); @@ -131,6 +131,8 @@ int main() { "declare-types-dictionary-key.toy", "declare-types-dictionary-value.toy", "index-arrays-non-integer.toy", + "unary-inverted-nothing.toy", + "unary-negative-nothing.toy", NULL };