Skip to content

Commit

Permalink
Merge pull request #404 from s-hadinger/parser_ternary_fix
Browse files Browse the repository at this point in the history
Fix ternary parser bug
  • Loading branch information
skiars authored Mar 24, 2024
2 parents b39f463 + fa2541b commit 7d3fe77
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/be_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static int codeABx(bfuncinfo *finfo, bopcode op, int a, int bx)
/* returns false if the move operation happened, or true if there was a register optimization and `b` should be replaced by `a` */
static bbool code_move(bfuncinfo *finfo, int a, int b)
{
if (finfo->pc) { /* If not the first instruction of the function */
if (finfo->pc > finfo->binfo->lastjmp) { /* If not the first instruction of the function */
binstruction *i = be_vector_end(&finfo->code); /* get the last instruction */
bopcode op = IGET_OP(*i);
if (op <= OP_LDNIL) { /* binop or unop */
Expand Down Expand Up @@ -171,6 +171,13 @@ static int get_jump(bfuncinfo *finfo, int pc)

static void patchlistaux(bfuncinfo *finfo, int list, int vtarget, int dtarget)
{
/* mark the last destination point of a jump to avoid false register optimization */
if (vtarget > finfo->binfo->lastjmp) {
finfo->binfo->lastjmp = vtarget;
}
if (dtarget > finfo->binfo->lastjmp) {
finfo->binfo->lastjmp = dtarget;
}
while (list != NO_JUMP) {
int next = get_jump(finfo, list);
if (isjumpbool(finfo, list)) {
Expand Down
1 change: 1 addition & 0 deletions src/be_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ static void begin_block(bfuncinfo *finfo, bblockinfo *binfo, int type)
binfo->type = (bbyte)type;
binfo->hasupval = 0;
binfo->sideeffect = 0;
binfo->lastjmp = 0;
binfo->beginpc = finfo->pc; /* set starting pc for this block */
binfo->nactlocals = (bbyte)be_list_count(finfo->local); /* count number of local variables in previous block */
if (type & BLOCK_LOOP) {
Expand Down
5 changes: 3 additions & 2 deletions src/be_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ typedef struct {
int t; /* patch list of 'exit when true' */
int f; /* patch list of 'exit when false' */
bbyte not; /* not mark */
bbyte type;
exptype_t type;
} bexpdesc;

typedef struct bblockinfo {
struct bblockinfo *prev;
bbyte nactlocals; /* number of active local variables */
bbyte type; /* block type mask */
bbyte hasupval; /* has upvalue mark */
bbyte sideeffect; /* did the last expr/statement had a side effect */
bbyte sideeffect; /* did the last expr/statement had a side effect */
int lastjmp; /* pc for the last jump, prevents false register optimizations */
int breaklist; /* break list */
int beginpc; /* begin pc */
int continuelist; /* continue list */
Expand Down
11 changes: 11 additions & 0 deletions tests/parser.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Test some sparser specific bugs

# https://github.com/berry-lang/berry/issues/396
def f()
if true
var a = 1
a = true ? a+1 : a+2
return a
end
end
assert(f() == 2)

0 comments on commit 7d3fe77

Please sign in to comment.