diff options
Diffstat (limited to 'compiler.c')
-rw-r--r-- | compiler.c | 153 |
1 files changed, 116 insertions, 37 deletions
@@ -1092,10 +1092,7 @@ uc_compiler_emit_variable_rw(uc_compiler_t *compiler, uc_value_t *varname, uc_to case TK_ASBOR: sub_insn = I_BOR; break; case TK_ASLEFT: sub_insn = I_LSHIFT; break; case TK_ASRIGHT: sub_insn = I_RSHIFT; break; - case TK_ASAND: sub_insn = I_LTRUE; break; - case TK_ASOR: sub_insn = I_LFALSE; break; case TK_ASEXP: sub_insn = I_EXP; break; - case TK_ASNULLISH: sub_insn = I_LNULL; break; default: sub_insn = 0; break; } @@ -1153,6 +1150,100 @@ uc_compiler_emit_variable_rw(uc_compiler_t *compiler, uc_value_t *varname, uc_to } static void +uc_compiler_emit_variable_copy(uc_compiler_t *compiler, uc_value_t *var) +{ + if (!var) { + uc_compiler_emit_insn(compiler, 0, I_COPY); + uc_compiler_emit_u8(compiler, 0, 1); + uc_compiler_emit_insn(compiler, 0, I_COPY); + uc_compiler_emit_u8(compiler, 0, 1); + } + + uc_compiler_emit_variable_rw(compiler, var, 0); +} + +static void +uc_compiler_compile_and_common(uc_compiler_t *compiler, bool assignment, uc_value_t *var) +{ + uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); + size_t jmpz_off; + + if (assignment) + uc_compiler_emit_variable_copy(compiler, var); + + uc_compiler_emit_insn(compiler, 0, I_COPY); + uc_compiler_emit_u8(compiler, 0, 0); + jmpz_off = uc_compiler_emit_jmpz(compiler, 0); + uc_compiler_emit_insn(compiler, 0, I_POP); + + if (assignment) { + uc_compiler_parse_precedence(compiler, P_ASSIGN); + uc_compiler_emit_variable_rw(compiler, var, TK_ASSIGN); + } + else { + uc_compiler_parse_precedence(compiler, P_AND); + } + + uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); +} + +static void +uc_compiler_compile_or_common(uc_compiler_t *compiler, bool assignment, uc_value_t *var) +{ + uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); + size_t jmpz_off, jmp_off; + + if (assignment) + uc_compiler_emit_variable_copy(compiler, var); + + uc_compiler_emit_insn(compiler, 0, I_COPY); + uc_compiler_emit_u8(compiler, 0, 0); + jmpz_off = uc_compiler_emit_jmpz(compiler, 0); + jmp_off = uc_compiler_emit_jmp(compiler, 0); + uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); + uc_compiler_emit_insn(compiler, 0, I_POP); + + if (assignment) { + uc_compiler_parse_precedence(compiler, P_ASSIGN); + uc_compiler_emit_variable_rw(compiler, var, TK_ASSIGN); + } + else { + uc_compiler_parse_precedence(compiler, P_OR); + } + + uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count); +} + +static void +uc_compiler_compile_nullish_common(uc_compiler_t *compiler, bool assignment, uc_value_t *var) +{ + uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); + size_t jmpz_off, jmp_off; + + if (assignment) + uc_compiler_emit_variable_copy(compiler, var); + + uc_compiler_emit_insn(compiler, 0, I_COPY); + uc_compiler_emit_u8(compiler, 0, 0); + uc_compiler_emit_insn(compiler, 0, I_LNULL); + uc_compiler_emit_insn(compiler, 0, I_NES); + jmpz_off = uc_compiler_emit_jmpz(compiler, 0); + jmp_off = uc_compiler_emit_jmp(compiler, 0); + uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); + uc_compiler_emit_insn(compiler, 0, I_POP); + + if (assignment) { + uc_compiler_parse_precedence(compiler, P_ASSIGN); + uc_compiler_emit_variable_rw(compiler, var, TK_ASSIGN); + } + else { + uc_compiler_parse_precedence(compiler, P_OR); + } + + uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count); +} + +static void uc_compiler_compile_expression(uc_compiler_t *compiler) { uc_compiler_parse_precedence(compiler, P_COMMA); @@ -1163,7 +1254,25 @@ uc_compiler_compile_assignment(uc_compiler_t *compiler, uc_value_t *var) { uc_tokentype_t type = compiler->parser->curr.type; - if (uc_compiler_parse_at_assignment_op(compiler)) { + if (type == TK_ASNULLISH) { + uc_compiler_parse_advance(compiler); + uc_compiler_compile_nullish_common(compiler, true, var); + + return true; + } + else if (type == TK_ASOR) { + uc_compiler_parse_advance(compiler); + uc_compiler_compile_or_common(compiler, true, var); + + return true; + } + else if (type == TK_ASAND) { + uc_compiler_parse_advance(compiler); + uc_compiler_compile_and_common(compiler, true, var); + + return true; + } + else if (uc_compiler_parse_at_assignment_op(compiler)) { uc_compiler_parse_advance(compiler); uc_compiler_parse_precedence(compiler, P_ASSIGN); uc_compiler_emit_variable_rw(compiler, var, type); @@ -1750,49 +1859,19 @@ uc_compiler_compile_funcdecl(uc_compiler_t *compiler) static void uc_compiler_compile_and(uc_compiler_t *compiler) { - uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); - size_t jmpz_off; - - uc_compiler_emit_insn(compiler, 0, I_COPY); - uc_compiler_emit_u8(compiler, 0, 0); - jmpz_off = uc_compiler_emit_jmpz(compiler, 0); - uc_compiler_emit_insn(compiler, 0, I_POP); - uc_compiler_parse_precedence(compiler, P_AND); - uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); + return uc_compiler_compile_and_common(compiler, false, NULL); } static void uc_compiler_compile_or(uc_compiler_t *compiler) { - uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); - size_t jmpz_off, jmp_off; - - uc_compiler_emit_insn(compiler, 0, I_COPY); - uc_compiler_emit_u8(compiler, 0, 0); - jmpz_off = uc_compiler_emit_jmpz(compiler, 0); - jmp_off = uc_compiler_emit_jmp(compiler, 0); - uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); - uc_compiler_emit_insn(compiler, 0, I_POP); - uc_compiler_parse_precedence(compiler, P_OR); - uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count); + return uc_compiler_compile_or_common(compiler, false, NULL); } static void uc_compiler_compile_nullish(uc_compiler_t *compiler) { - uc_chunk_t *chunk = uc_compiler_current_chunk(compiler); - size_t jmpz_off, jmp_off; - - uc_compiler_emit_insn(compiler, 0, I_COPY); - uc_compiler_emit_u8(compiler, 0, 0); - uc_compiler_emit_insn(compiler, 0, I_LNULL); - uc_compiler_emit_insn(compiler, 0, I_NES); - jmpz_off = uc_compiler_emit_jmpz(compiler, 0); - jmp_off = uc_compiler_emit_jmp(compiler, 0); - uc_compiler_set_jmpaddr(compiler, jmpz_off, chunk->count); - uc_compiler_emit_insn(compiler, 0, I_POP); - uc_compiler_parse_precedence(compiler, P_OR); - uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count); + return uc_compiler_compile_nullish_common(compiler, false, NULL); } static void |