commit 8b9a6db7482dad27c29e73b13619bb001bbd674b
parent e30d4b11b6f0e92aca1120b6cdf62cc566755d97
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sun, 19 Mar 2023 08:03:34 -0400
halcc: negate()
Diffstat:
3 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/fs/comp/c/egen.fs b/fs/comp/c/egen.fs
@@ -36,11 +36,21 @@ struct[ Result
HALOP of = @, endof
_err endcase W swap to type ;
: :hal# ( self -- halop ) dup type HALOP = _assert arg ;
+ : :isconst? ( self -- f ) type CONST = ;
]struct
alias noop parseExpression ( tok -- res ) \ forward declaration
alias noop parseFactor ( tok -- res ) \ forward declaration
+: unaryop doer ' , ' , does> ( res 'op -- res )
+ over Result :isconst? if
+ CELLSZ + @ over Result arg swap execute over to Result arg
+ else @ over Result :>W execute then ( res ) ;
+unaryop _neg, neg, neg
+
+UOPSCNT wordtbl _tbl ( res -- res )
+'w _neg, 'w _err 'w _err 'w _err 'w _err 'w _err 'w _err
+
\ A factor can be:
\ 1. A constant
\ 2. A lvalue
@@ -65,7 +75,8 @@ alias noop parseFactor ( tok -- res ) \ forward declaration
S" NULL" of s= 0 Result :const endof
S" sizeof" of s=
read( nextt parseType _assert typesize Result :const read) endof
- of uopid ( opid ) abort" TODO" endof
+ of uopid ( opid )
+ nextt parseFactor ( opid res ) _tbl rot wexec endof
of isIdent? \ lvalue, FunCall or macro
r@ findIdent ?dup _assert CType :halop Result :hal ( parsePostfixOp ) endof
r@ parse if Result :const else _err then
diff --git a/fs/tests/comp/c/cc.fs b/fs/tests/comp/c/cc.fs
@@ -6,8 +6,8 @@ testbegin
cc<< tests/comp/c/test2.c
retconst 42 #eq
variables 82 #eq
-testend \s
negate -42 #eq
+testend \s
bwnot $ffffffd5 #eq
exprbinops 7 #eq
binopand 42 #eq
diff --git a/fs/tests/comp/c/test2.c b/fs/tests/comp/c/test2.c
@@ -13,3 +13,5 @@ short variables() {
_bar = foo + _bar;
return foo + _bar;
}
+// test unary op and that we don't require whitespace around symbols
+int negate() {int a=$2a; return -a;}