commit c4531c702391d3adc889de08153a8e5d136b3be5
parent f1cf5427a0a1961f44a08b4aa88cb58da517b96d
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 5 Oct 2022 11:25:47 -0400
cc: allow macros in array "nbelem" definitions
Diffstat:
7 files changed, 36 insertions(+), 23 deletions(-)
diff --git a/fs/app/cos/cvm.c b/fs/app/cos/cvm.c
@@ -10,7 +10,7 @@
\ 3 - blkid LSB
\ 4 - dest addr MSB
\ 5 - dest addr LSB
- $03 BLK_PORT
+ $03 const BLK_PORT
4 const BLKOP_CMD_SZ
]#
@@ -20,13 +20,14 @@ typedef byte (*IORD) ();
typedef void (*IOWR) (byte);
struct COSVM {
- byte mem[ #[ MEMSIZE c]# ];
+ byte mem[ #[ MEMSIZE ]# ];
word SP; /* parameter Stack Pointer */
word RS; /* Return Stack pointer */
word IP; /* Interpreter Pointer */
word PC; /* Program Counter for bytecode interpreter */
/* Array of 0x100 function pointers to IO read and write routines. Leave to
* NULL when IO port is unhandled. */
+// Compiles up to this point!
IORD iord[0x100];
IOWR iowr[0x100];
/* Used for keeping track of max RS and min SP during the lifetime of the
diff --git a/fs/app/cos/cvm.fs b/fs/app/cos/cvm.fs
@@ -0,0 +1,2 @@
+?f<< /cc/lib.fs
+cc<< /app/cos/cvm.c
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -5,6 +5,7 @@
?f<< lib/wordtbl.fs
?f<< cc/tok.fs
?f<< cc/tree.fs
+?f<< cc/macro.fs
?f<< cc/type.fs
: _err ( -- ) abort" ast error" ;
@@ -220,6 +221,10 @@ extends ASTNode struct[ Arrow
: :new AST_ARROW ASTNode :new swap s, ;
]struct
+\ Macro shortcuts
+: c]# Constant :new ]# ;
+: i]# Ident :new ]# ;
+
ASTIDCNT stringlist astidnames
"declare" "unit" "function" "return" "constant" "stmts" "funcsig" "ident"
"unaryop" "postop" "binop" "list" "if" "str" "call" "for" "push" "pop" "break"
@@ -269,23 +274,6 @@ ASTIDCNT wordtbl astdatatbl ( node -- node )
: newnode ( parent nodeid -- newnode )
ASTNode :new ( parent node ) dup rot Node :add ( node ) ;
-\ Macros. See doc/cc
--1 value _pslvl \ PS level at last #[
-
-: _ begin word runword _pslvl 0< until ;
-: runmacro ['] _ with-stdin< ;
-
-: #[0 scnt to _pslvl runmacro ;
-: #[1 scnt 1+ to _pslvl runmacro ;
-
-: ]#
- scnt _pslvl - ?dup if abort" PS imbalance during macros" then
- -1 to _pslvl ;
-
-: c]# Constant :new ]# ;
-: i]# Ident :new ]# ;
-: +]# over Node :add ]# ;
-
\ Parse words
alias noop parseExpression ( tok -- node ) \ forward declaration
diff --git a/fs/cc/macro.fs b/fs/cc/macro.fs
@@ -0,0 +1,17 @@
+\ CC macros
+?f<< /cc/tree.fs
+
+\ Macros. See doc/cc
+-1 value _pslvl \ PS level at last #[
+
+: _ begin word runword _pslvl 0< until ;
+: runmacro ['] _ with-stdin< ;
+
+: #[0 scnt to _pslvl runmacro ;
+: #[1 scnt 1+ to _pslvl runmacro ;
+
+: ]#
+ scnt _pslvl - ?dup if abort" PS imbalance during macros" then
+ -1 to _pslvl ;
+
+: +]# over Node :add ]# ;
diff --git a/fs/cc/type.fs b/fs/cc/type.fs
@@ -1,6 +1,7 @@
\ C compiler types
-?f<< lib/str.fs
-?f<< cc/tok.fs
+?f<< /lib/str.fs
+?f<< /cc/tok.fs
+?f<< /cc/macro.fs
: _err ( -- ) abort" type error" ;
: _assert ( f -- ) not if _err then ;
@@ -160,7 +161,8 @@ current to _typesize
nextt dup '(' isChar? if drop parseFuncSig exit else to nexttputback then
parseType* expectIdent swap CType :new ( ctype )
nextt dup S" [" s= if ( ctype tok )
- drop nextt parse _assert nextt ']' expectChar ( ctype nbelem )
+ drop nextt dup S" #[" s= if drop #[1 else parse _assert then
+ nextt ']' expectChar ( ctype nbelem )
over to CType nbelem
else to nexttputback then ( ctype ) ;
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -127,7 +127,8 @@ int array() {
}
static int global1 = 1234;
-static int global2[3] = {4, 5, 6};
+#[ 3 const GLOB2SZ ]#
+static int global2[ #[ GLOB2SZ ]# ] = {4, 5, 6};
int global() {
return global1;
diff --git a/testcvm.fs b/testcvm.fs
@@ -0,0 +1,2 @@
+f<< /app/cos/cvm.fs
+bye