commit 0bcc434a07e05ccb176a83d857d71de1fe648a30
parent 6fe12160abed3e3ebc51e913f2650ee3f57c610d
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sun, 19 Mar 2023 16:40:08 -0400
halcc: assignops()
Diffstat:
3 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/fs/comp/c/egen.fs b/fs/comp/c/egen.fs
@@ -81,7 +81,19 @@ swappable _&, and, swappable _^, xor, swappable _|, or,
fixed _-, -, fixed _/, /, fixed _%, %,
fixed _<<, <<, fixed _>>, >>,
-: _=, ( left right ) Result :>W Result :hal# !, Result :W ;
+: assign doer ' , does> @ ( left right w ) >r
+ Result :>W W>A, dup Result :hal# lea, W<>A, \ A='left W=right
+ Result :hal# r> execute A) !, Result :W ;
+assign _=, drop assign _+=, +,
+assign _*=, *, assign _/=, /, assign _%=, %,
+assign _&=, and, assign _^=, xor, assign _|=, or,
+
+\ TODO: deduplicate
+: rassign doer ' , does> @ ( left right w ) >r
+ Result :>W W>A, dup Result :hal# lea, W<>A, \ A='left W=right
+ Result :hal# dup @!, r> execute A) !, Result :W ;
+rassign _-=, -, rassign _/=, /, rassign _%=, %,
+rassign _<<=, <<, rassign _>>=, >>,
\ Our implementation of "x ? y : z" suffers a significant limitation because
\ we're single pass: by the time _? is called, it's possible that code
@@ -98,8 +110,8 @@ fixed _<<, <<, fixed _>>, >>,
BOPSCNT wordtbl _tbl ( left right -- res )
'w _+, 'w _-, 'w _*, 'w _/, 'w _%, 'w _<<, 'w _>>, 'w _err
'w _err 'w _err 'w _err 'w _err 'w _err 'w _&, 'w _^, 'w _|,
-'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 _?, 'w _:,
+'w _err 'w _err 'w _=, 'w _+=, 'w _-=, 'w _*=, 'w _/=, 'w _%=,
+'w _<<=, 'w _>>=, 'w _&=, 'w _^=, 'w _|=, 'w _?, 'w _:,
: bothconst? ( left right -- f ) Result :isconst? swap Result :isconst? and ;
diff --git a/fs/tests/comp/c/cc.fs b/fs/tests/comp/c/cc.fs
@@ -18,8 +18,8 @@ binopdiv 14 #eq
binopmod 1 #eq
1 binopcondeval 42 #eq
0 binopcondeval 12 #eq
-testend \s
assignops 83 #eq
+testend \s
boolops 0 #eq
funcall 42 #eq
42 pushpop 42 #eq
diff --git a/fs/tests/comp/c/test2.c b/fs/tests/comp/c/test2.c
@@ -55,3 +55,11 @@ int binopmod() {
int binopcondeval(int x) {
return x ? 42 : 12 ;
}
+int assignops() {
+ int a=42, b=2, c=3;
+ a += b; // 44
+ a -= c; // 41
+ a *= b; // 82
+ a |= c; // 83
+ return a;
+}