diff options
| author | iamcheeseman <[email protected]> | 2026-04-17 20:05:38 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-04-17 20:05:38 -0400 |
| commit | bdc4806f5ddc90aea8840225fd8993e305b7ad10 (patch) | |
| tree | c4546f4821378c5bfda9582c9cdf586a6e9ae645 /uscript/parser.c | |
| parent | 35adf15fead519d9cede35008ce78aca93853220 (diff) | |
microscript: add and and or
Diffstat (limited to 'uscript/parser.c')
| -rw-r--r-- | uscript/parser.c | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/uscript/parser.c b/uscript/parser.c index 9fd4e27..7909f98 100644 --- a/uscript/parser.c +++ b/uscript/parser.c @@ -15,6 +15,8 @@ enum precedence { PREC_NONE, PREC_ASSIGN, // = + PREC_OR, // or + PREC_AND, // and PREC_EQL, // == != PREC_COMP, // < <= > >= PREC_CONCAT, @@ -463,6 +465,26 @@ void parse_ident(struct parser *p) } static +void parse_and(struct parser *p) +{ + int jmp = begin_jump(p, BC_FALSEY_JMP); + parser_add_byte(p, BC_POP); + parse_expr(p, PREC_AND + 1); + end_jump(p, jmp); +} + +static +void parse_or(struct parser *p) +{ + int jmp = begin_jump(p, BC_FALSEY_JMP); + int else_jmp = begin_jump(p, BC_JMP); + end_jump(p, jmp); + parser_add_byte(p, BC_POP); + parse_expr(p, PREC_OR + 1); + end_jump(p, else_jmp); +} + +static void parse_binary(struct parser *p) { struct token op = p->prev; @@ -580,13 +602,14 @@ struct expr expressions[] = { ['>'] = {NULL, parse_binary, PREC_COMP}, ['='] = {NULL, NULL, PREC_NONE}, ['!'] = {parse_unary, NULL, PREC_NONE}, - [TOKEN_EOF] = {NULL, NULL, PREC_NONE}, + [TOKEN_AND] = {NULL, parse_and, PREC_AND}, [TOKEN_BREAK] = {NULL, NULL, PREC_NONE}, [TOKEN_DIV_EQL] = {NULL, NULL, PREC_NONE}, [TOKEN_DOT_DOT] = {NULL, parse_binary, PREC_CONCAT}, [TOKEN_DO] = {NULL, NULL, PREC_NONE}, [TOKEN_ELSE] = {NULL, NULL, PREC_NONE}, [TOKEN_END] = {NULL, NULL, PREC_NONE}, + [TOKEN_EOF] = {NULL, NULL, PREC_NONE}, [TOKEN_EQL] = {NULL, parse_binary, PREC_EQL}, [TOKEN_ERR] = {NULL, NULL, PREC_NONE}, [TOKEN_FALSE] = {parse_literal, NULL, PREC_NONE}, @@ -605,6 +628,7 @@ struct expr expressions[] = { [TOKEN_NEQL] = {NULL, parse_binary, PREC_EQL}, [TOKEN_NEXT] = {NULL, NULL, PREC_NONE}, [TOKEN_NUM] = {parse_number, NULL, PREC_NONE}, + [TOKEN_OR] = {NULL, parse_or, PREC_OR}, [TOKEN_PLUS_EQL] = {NULL, NULL, PREC_NONE}, [TOKEN_RET] = {NULL, NULL, PREC_NONE}, [TOKEN_STR] = {parse_string, NULL, PREC_NONE}, |
