commit 24fba064cd34cd5a45fa4d49066ebf73aa5aaab3
parent 25bf0b553e1df59ad5b7b423c97668bc538afe5e
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 19 Sep 2022 13:07:12 -0400
cc: allow ident inside [] operator
Diffstat:
3 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -307,6 +307,8 @@ alias noop parseExpression ( tok -- node ) \ forward declaration
endcase
nextt again ;
+alias noop parseFactor ( tok -- node ) \ forward declaration
+
\ parses, if possible, a postfix operator. If none, this is a noop.
\ We parse postfix args as long as there are any.
: parsePostfixOp ( node -- node )
@@ -314,8 +316,7 @@ alias noop parseExpression ( tok -- node ) \ forward declaration
'[' of isChar?^ ( inode ) \ x[y] is the equivalent of *(x+y)
0 ( + ) BinaryOp :new ( inode bnode )
tuck Node :add ( bnode )
- nextt parse _assert Constant :new ( bnode cnode )
- nextt ']' expectChar
+ nextt parseFactor nextt ']' expectChar ( bnode node )
over Node :add ( bnode )
4 ( * ) UnaryOp :new ( bnode unode )
tuck Node :add ( unode ) parsePostfixOp
@@ -341,7 +342,7 @@ alias noop parseExpression ( tok -- node ) \ forward declaration
\ 5. An expression inside () parens.
\ 6. A string literal
\ 7. pspop()
-: parseFactor ( tok -- node )
+: _ ( tok -- node )
case
'(' of isChar?^ ( )
nextt parseExpression nextt ')' expectChar endof
@@ -363,6 +364,7 @@ alias noop parseExpression ( tok -- node ) \ forward declaration
( case else ) \ Constant
r@ parse if Constant :new else _err then
endcase ;
+current to parseFactor
\ An expression can be 2 things:
\ 1. a factor
diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs
@@ -45,8 +45,10 @@ capture helloworld S" Hello World!" #s=
-1 0 ltu not #
0 boolnot 1 #eq
42 boolnot 0 #eq
-S" foobar" get8b2nd 'o' #eq
-S" foobar" dup 'X' set8b2nd S" fXobar" #s=
+S" foobar" 2 get8b 'o' #eq
+S" foobar" 4 get8b 'b' #eq
+S" foobar" dup 2 'X' set8b S" fXobar" #s=
+S" foobar" dup 6 'X' set8b S" foobaX" #s=
\ and what about inline functions?
:cfunc int myinline() { return 42; }
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -168,10 +168,9 @@ extern int ltu(unsigned int a, unsigned int b) {
extern int boolnot(int x) {
return !x;
}
-// TODO: allow variable indexing
-extern char get8b2nd(char *s) {
- return s[2]; // 0th is length
+extern char get8b(char *s, int i) {
+ return s[i]; // 0th is length
}
-extern void set8b2nd(char *s, char c) {
- s[2] = c;
+extern void set8b(char *s, int i, char c) {
+ s[i] = c;
}