mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Short circuits are now functioning correctly, resolved #73
This commit is contained in:
@@ -339,6 +339,28 @@ static Toy_Opcode grouping(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
}
|
||||
}
|
||||
|
||||
static Toy_Opcode circuit(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
advance(parser);
|
||||
|
||||
//handle short-circuitable operators - && ||
|
||||
switch (parser->previous.type) {
|
||||
case TOY_TOKEN_AND_AND: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_AND + 1);
|
||||
return TOY_OP_AND;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_OR_OR: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_OR + 1);
|
||||
return TOY_OP_OR;
|
||||
}
|
||||
|
||||
default: {
|
||||
error(parser, parser->previous, "Unexpected token passed to grouping precedence rule");
|
||||
return TOY_OP_EOF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
advance(parser);
|
||||
|
||||
@@ -432,16 +454,6 @@ static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
return TOY_OP_COMPARE_GREATER_EQUAL;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_AND: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_AND + 1);
|
||||
return TOY_OP_AND;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_OR: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_OR + 1);
|
||||
return TOY_OP_OR;
|
||||
}
|
||||
|
||||
default:
|
||||
error(parser, parser->previous, "Unexpected token passed to binary precedence rule");
|
||||
return TOY_OP_EOF;
|
||||
@@ -1002,8 +1014,8 @@ ParseRule parseRules[] = { //must match the token types
|
||||
{NULL, binary, PREC_COMPARISON},// TOKEN_GREATER,
|
||||
{NULL, binary, PREC_COMPARISON},// TOKEN_LESS_EQUAL,
|
||||
{NULL, binary, PREC_COMPARISON},// TOKEN_GREATER_EQUAL,
|
||||
{NULL, binary, PREC_AND},// TOKEN_AND,
|
||||
{NULL, binary, PREC_OR},// TOKEN_OR,
|
||||
{NULL, circuit, PREC_AND},// TOKEN_AND,
|
||||
{NULL, circuit, PREC_OR},// TOKEN_OR,
|
||||
|
||||
//other operators
|
||||
{NULL, question, PREC_TERNARY}, //TOKEN_QUESTION,
|
||||
@@ -1285,6 +1297,16 @@ static void parsePrecedence(Toy_Parser* parser, Toy_ASTNode** nodeHandle, Preced
|
||||
continue;
|
||||
}
|
||||
|
||||
if (opcode == TOY_OP_AND) {
|
||||
Toy_emitASTNodeAnd(nodeHandle, rhsNode);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (opcode == TOY_OP_OR) {
|
||||
Toy_emitASTNodeOr(nodeHandle, rhsNode);
|
||||
continue;
|
||||
}
|
||||
|
||||
Toy_emitASTNodeBinary(nodeHandle, rhsNode, opcode);
|
||||
|
||||
//optimise away the constants
|
||||
|
||||
Reference in New Issue
Block a user