From 1a208ef30adee36a716a68e251962bf38ea0fea7 Mon Sep 17 00:00:00 2001 From: David Schmenk Date: Mon, 20 Jan 2020 11:39:58 -0800 Subject: [PATCH] Make sure ternary op has lower precedence than AND and OR --- src/toolsrc/parse.c | 23 +++++++++++++++++------ src/toolsrc/parse.pla | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/toolsrc/parse.c b/src/toolsrc/parse.c index 619fc00..44d3cf0 100755 --- a/src/toolsrc/parse.c +++ b/src/toolsrc/parse.c @@ -685,7 +685,7 @@ t_opseq *parse_value(t_opseq *codeseq, int rvalue, int *stackdepth) } return (cat_seq(codeseq, valseq)); } -t_opseq *parse_expr(t_opseq *codeseq, int *stackdepth) +t_opseq *parse_subexpr(t_opseq *codeseq, int *stackdepth) { int prevmatch; int matchop = 0; @@ -744,7 +744,7 @@ t_opseq *parse_expr(t_opseq *codeseq, int *stackdepth) parse_error("AND must evaluate to single value"); tag_and = tag_new(BRANCH_TYPE); codeseq = gen_brand(codeseq, tag_and); - codeseq = parse_expr(codeseq, &stackdepth1); + codeseq = parse_subexpr(codeseq, &stackdepth1); if (stackdepth1 != *stackdepth) parse_error("Inconsistent AND value counts"); codeseq = gen_codetag(codeseq, tag_and); @@ -761,12 +761,24 @@ t_opseq *parse_expr(t_opseq *codeseq, int *stackdepth) parse_error("OR must evaluate to single value"); tag_or = tag_new(BRANCH_TYPE); codeseq = gen_bror(codeseq, tag_or); - codeseq = parse_expr(codeseq, &stackdepth1); + codeseq = parse_subexpr(codeseq, &stackdepth1); if (stackdepth1 != *stackdepth) parse_error("Inconsistent AND value counts"); codeseq = gen_codetag(codeseq, tag_or); } - else if (scantoken == TERNARY_TOKEN) + return (codeseq); +} +t_opseq *parse_expr(t_opseq *codeseq, int *stackdepth) +{ + int prevmatch; + int matchop = 0; + int optos = opsptr; + int i, valdepth; + int prevtype, type = 0; + t_opseq *valseq; + + codeseq = parse_subexpr(codeseq, stackdepth); + if (scantoken == TERNARY_TOKEN) { int tag_else, tag_endtri; int stackdepth1; @@ -790,8 +802,7 @@ t_opseq *parse_expr(t_opseq *codeseq, int *stackdepth) codeseq = gen_codetag(codeseq, tag_endtri); } return (codeseq); -} -t_opseq *parse_set(t_opseq *codeseq) +}t_opseq *parse_set(t_opseq *codeseq) { char *setptr = tokenstr; int lparms = 0, rparms = 0; diff --git a/src/toolsrc/parse.pla b/src/toolsrc/parse.pla index 1e63a36..aaa495d 100644 --- a/src/toolsrc/parse.pla +++ b/src/toolsrc/parse.pla @@ -493,7 +493,7 @@ def parse_value(codeseq, r_val)#2 fin return cat_seq(codeseq, valseq), stackdepth end -def parse_expr(codeseq)#2 +def parse_subexpr(codeseq)#2 byte stackdepth, matchdepth, stkdepth1, prevmatch, matchop, i word optos word tag_else, tag_endop @@ -530,17 +530,25 @@ def parse_expr(codeseq)#2 if stackdepth <> 1; exit_err(ERR_OVER|ERR_SYNTAX); fin tag_endop = new_tag(RELATIVE_FIXUP) codeseq = gen_oprel(codeseq, BRAND_CODE, tag_endop) - codeseq, stkdepth1 = parse_expr(codeseq) + codeseq, stkdepth1 = parse_subexpr(codeseq) if stkdepth1 <> stackdepth; exit_err(ERR_INVAL|ERR_CODE); fin codeseq = gen_ctag(codeseq, tag_endop) elsif token == LOGIC_OR_TKN if stackdepth <> 1; exit_err(ERR_OVER|ERR_SYNTAX); fin tag_endop = new_tag(RELATIVE_FIXUP) codeseq = gen_oprel(codeseq, BROR_CODE, tag_endop) - codeseq, stkdepth1 = parse_expr(codeseq) + codeseq, stkdepth1 = parse_subexpr(codeseq) if stkdepth1 <> stackdepth; exit_err(ERR_INVAL|ERR_CODE); fin codeseq = gen_ctag(codeseq, tag_endop) - elsif token == TERNARY_TKN + fin + return codeseq, stackdepth +end +def parse_expr(codeseq)#2 + byte stackdepth, stkdepth1 + word tag_else, tag_endop + + codeseq, stackdepth = parse_subexpr(codeseq) + if token == TERNARY_TKN if stackdepth <> 1; exit_err(ERR_OVER|ERR_SYNTAX); fin tag_else = new_tag(RELATIVE_FIXUP) tag_endop = new_tag(RELATIVE_FIXUP)