commit e416cfb597c148dbb48a36190fa7cf5b391acb3d
parent f151665fa2cff7b1fa575be9f622fb2e9bec8e30
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sun, 19 Mar 2023 11:34:23 -0400
halcc: exprbinops()
Diffstat:
3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/fs/comp/c/egen.fs b/fs/comp/c/egen.fs
@@ -35,8 +35,16 @@ struct[ Result
W of = drop endof
HALOP of = @, endof
_err endcase W swap to type ;
- : :hal# ( self -- halop ) dup type HALOP = _assert arg ;
+ : :hal# ( self -- halop ) dup type case ( self )
+ HALOP of = arg endof
+ CONST of = arg i) endof
+ _err endcase ;
: :isconst? ( self -- f ) type CONST = ;
+ \ Sort two result in "correct" order. If one of the ops is a W, it goes left.
+ \ If any of the results is a const, it goes right. After a :?swap, you can
+ \ check if both args are const by only checking the left one.
+ : :?swap ( left right -- left right )
+ dup type W = if swap else over :isconst? if swap then then ;
]struct
alias noop parseExpression ( tok -- res ) \ forward declaration
@@ -85,14 +93,14 @@ UOPSCNT wordtbl _tbl ( res -- res )
endcase ;
current ' parseFactor realias
-: binop doer ' , does> @ ( left right w )
- rot Result :>W swap Result :hal# swap execute Result :W ;
-binop _+, +,
+: binop doer ' , does> @ ( left right w ) >r
+ Result :?swap over Result :>W Result :hal# r> execute ;
+binop _+, +, binop _*, *,
: _=, ( left right ) Result :>W Result :hal# !, Result :W ;
BOPSCNT wordtbl _tbl ( -- )
-'w _+, 'w _err 'w _err 'w _err 'w _err 'w _err 'w _err 'w _err
+'w _+, 'w _err 'w _*, 'w _err 'w _err 'w _err 'w _err 'w _err
'w _err 'w _err 'w _err 'w _err 'w _err 'w _err 'w _err 'w _err
'w _err 'w _err 'w _=, 'w _err 'w _err 'w _err 'w _err 'w _err
'w _err 'w _err 'w _err 'w _err 'w _err 'w _err 'w _err
diff --git a/fs/tests/comp/c/cc.fs b/fs/tests/comp/c/cc.fs
@@ -8,8 +8,8 @@ retconst 42 #eq
variables 82 #eq
negate -42 #eq
bwnot $ffffffd5 #eq
-testend \s
exprbinops 7 #eq
+testend \s
binopand 42 #eq
binopor 42 #eq
binopxor 42 #eq
diff --git a/fs/tests/comp/c/test2.c b/fs/tests/comp/c/test2.c
@@ -19,3 +19,8 @@ int bwnot() {
int a='*';
return ~a;
}
+// test binop precedence
+int exprbinops() {
+ unsigned int a=1, b=2;
+ return a + b * 3 + NULL;
+}