mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Null pointer exceptions in parser unary function
This commit is contained in:
@@ -419,7 +419,7 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) {
|
|||||||
parsePrecedence(parser, &tmpNode, PREC_TERNARY); //can be a literal
|
parsePrecedence(parser, &tmpNode, PREC_TERNARY); //can be a literal
|
||||||
|
|
||||||
//optimisation: check for negative literals
|
//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
|
//negate directly, if int or float
|
||||||
Literal lit = tmpNode->atomic.literal;
|
Literal lit = tmpNode->atomic.literal;
|
||||||
|
|
||||||
@@ -438,7 +438,7 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//check for negated boolean errors
|
//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");
|
error(parser, parser->previous, "Negative booleans are not allowed");
|
||||||
return OP_EOF;
|
return OP_EOF;
|
||||||
}
|
}
|
||||||
@@ -451,8 +451,8 @@ static Opcode unary(Parser* parser, ASTNode** nodeHandle) {
|
|||||||
//temp handle to potentially negate values
|
//temp handle to potentially negate values
|
||||||
parsePrecedence(parser, &tmpNode, PREC_CALL); //can be a literal, grouping, fn call, etc.
|
parsePrecedence(parser, &tmpNode, PREC_CALL); //can be a literal, grouping, fn call, etc.
|
||||||
|
|
||||||
//check for inverted booleans
|
//optimisation: check for inverted booleans
|
||||||
if (tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) {
|
if (tmpNode != NULL && tmpNode->type == AST_NODE_LITERAL && IS_BOOLEAN(tmpNode->atomic.literal)) {
|
||||||
//negate directly, if boolean
|
//negate directly, if boolean
|
||||||
Literal lit = tmpNode->atomic.literal;
|
Literal lit = tmpNode->atomic.literal;
|
||||||
|
|
||||||
@@ -694,6 +694,13 @@ static Opcode fnCall(Parser* parser, ASTNode** nodeHandle) {
|
|||||||
|
|
||||||
ASTNode* tmpNode = NULL;
|
ASTNode* tmpNode = NULL;
|
||||||
parsePrecedence(parser, &tmpNode, PREC_TERNARY);
|
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;
|
arguments->fnCollection.nodes[arguments->fnCollection.count++] = *tmpNode;
|
||||||
FREE(ASTNode, tmpNode); //simply free the tmpNode, so you don't free the children
|
FREE(ASTNode, tmpNode); //simply free the tmpNode, so you don't free the children
|
||||||
} while(match(parser, TOKEN_COMMA));
|
} while(match(parser, TOKEN_COMMA));
|
||||||
@@ -1303,6 +1310,12 @@ static void returnStmt(Parser* parser, ASTNode** nodeHandle) {
|
|||||||
ASTNode* node = NULL;
|
ASTNode* node = NULL;
|
||||||
parsePrecedence(parser, &node, PREC_TERNARY);
|
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;
|
returnValues->fnCollection.nodes[returnValues->fnCollection.count++] = *node;
|
||||||
FREE(ASTNode, node); //free manually
|
FREE(ASTNode, node); //free manually
|
||||||
} while(match(parser, TOKEN_COMMA));
|
} while(match(parser, TOKEN_COMMA));
|
||||||
|
|||||||
1
test/scripts/mustfail/unary-inverted-nothing.toy
Normal file
1
test/scripts/mustfail/unary-inverted-nothing.toy
Normal file
@@ -0,0 +1 @@
|
|||||||
|
print !
|
||||||
1
test/scripts/mustfail/unary-negative-nothing.toy
Normal file
1
test/scripts/mustfail/unary-negative-nothing.toy
Normal file
@@ -0,0 +1 @@
|
|||||||
|
print -
|
||||||
@@ -69,7 +69,7 @@ unsigned char* compileString(char* source, size_t* size) {
|
|||||||
while(node != NULL) {
|
while(node != NULL) {
|
||||||
//pack up and leave
|
//pack up and leave
|
||||||
if (node->type == AST_NODE_ERROR) {
|
if (node->type == AST_NODE_ERROR) {
|
||||||
printf(ERROR "error node detected\n" RESET);
|
errorsTriggered++;
|
||||||
freeASTNode(node);
|
freeASTNode(node);
|
||||||
freeCompiler(&compiler);
|
freeCompiler(&compiler);
|
||||||
freeParser(&parser);
|
freeParser(&parser);
|
||||||
@@ -131,6 +131,8 @@ int main() {
|
|||||||
"declare-types-dictionary-key.toy",
|
"declare-types-dictionary-key.toy",
|
||||||
"declare-types-dictionary-value.toy",
|
"declare-types-dictionary-value.toy",
|
||||||
"index-arrays-non-integer.toy",
|
"index-arrays-non-integer.toy",
|
||||||
|
"unary-inverted-nothing.toy",
|
||||||
|
"unary-negative-nothing.toy",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user