commit a526bce947a2cbed6f857b1363dd3bec506116b3
parent 3dcaa758f8ab85eead088913619af06950665d91
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sun, 9 Oct 2022 17:23:32 -0400
cc: rename :cfunc to :c and change its semantics slightly
Diffstat:
5 files changed, 25 insertions(+), 21 deletions(-)
diff --git a/fs/cc/cc.fs b/fs/cc/cc.fs
@@ -15,10 +15,10 @@
curunit trnode _debug if curunit printast nl> then
curunit gennode ;
-: :cfunc
- cctypes$ ccast$ newparseunit nextt parseUnit
- curunit Node firstchild dup _assert ( node )
- _debug if dup printast nl> then
- dup trnode _debug if dup printast nl> then gennode ;
+: :c
+ newparseunit nextt parseUnit
+ curunit Node firstchild ?dup if
+ _debug if dup printast nl> then
+ dup trnode _debug if dup printast nl> then gennode then ;
: cc<< ( -- ) ['] cc1, word with-stdin-file ;
diff --git a/fs/cc/lib.fs b/fs/cc/lib.fs
@@ -41,7 +41,7 @@ $100 MemIO :new const _sio
: printd . ;
\ TODO: switch statements
-:cfunc void fprintf() {
+:c void fprintf() {
void *iohdl = pspop();
char *fmt = pspop();
int len = strlen(fmt++);
@@ -81,7 +81,7 @@ $100 MemIO :new const _sio
(S c -- f )
: isdigit 0-9? ;
-:cfunc void fscanf() {
+:c void fscanf() {
void *iohdl = pspop();
char *fmt = pspop();
int len = strlen(fmt++);
diff --git a/fs/doc/cc.txt b/fs/doc/cc.txt
@@ -32,15 +32,19 @@ through "cc<<", which compiles the specified file. For example, "cc<< foo.c"
Reads the file "foo.c" as a unit and compile every element in it. Functions will
be added to the system dictionary unless they have the "static" storage type.
-Another method is to compile in an "inline" manner with ":cfunc". This word
-reads a single function from the input stream and compiles it. It returns to
-normal Forth interpretation when it parses the last closing "}" character.
-Example:
+Another method is to compile in an "inline" manner with ":c". This word reads a
+single "unit element" (a function definition, a global variable or a typedef)
+from the input stream and compiles it. It returns to normal Forth interpretation
+when it parses the last token of the element. Example:
-:cfunc int foo() {
+:c int foo() {
return 42;
} foo . \ prints 42
+Unit local symbols persist across :c definitions. You can also access local
+symbols of the last unit loaded with "cc<<" through ":c" definitions. The
+emptying of local buffers occurs at the beginning of "cc<<".
+
## Differences in the core language
* no C preprocessor, the preprocessor is Forth itself, through macros.
diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs
@@ -72,6 +72,6 @@ binop3 $605 #eq
structop1 44 #eq
\ and what about inline functions?
-:cfunc int myinline() { return 42; }
+:c int myinline() { return 42; }
myinline 42 #eq
testend
diff --git a/fs/tests/cc/lib.fs b/fs/tests/cc/lib.fs
@@ -3,32 +3,32 @@
?f<< cc/lib.fs
testbegin
\ Tests for the C library
-:cfunc int foo() { return strlen("Hello World!"); }
+:c int foo() { return strlen("Hello World!"); }
foo 12 #eq
create foo 4 nc, 1 2 3 4
-:cfunc void bar(int a) { memset(a, 0, 4); }
+:c void bar(int a) { memset(a, 0, 4); }
foo bar
foo @ 0 #eq
-:cfunc void foo(char *str, int n1, int n2, int n3, int n4) {
+:c void foo(char *str, int n1, int n2, int n3, int n4) {
printf(str, n1, n2, n3, n4, "foo %d %b %w %x %s"); }
S" bar" 42 43 44 45 capture foo S" foo 45 2c 002b 0000002a bar" #s=
-:cfunc char* foo(int n) { return sprintf(n, "foo %d bar"); }
+:c char* foo(int n) { return sprintf(n, "foo %d bar"); }
54 foo S" foo 54 bar" #s=
-:cfunc void foo() { puts("Hello World!"); }
+:c void foo() { puts("Hello World!"); }
capture foo S" Hello World!" #s=
$20 MemIO :new const memio
S" What about this?" const s
-:cfunc void foo() { fputs("What about this?", memio()); }
+:c void foo() { fputs("What about this?", memio()); }
foo
memio MemIO ptr s strlen #eq
memio MemIO :buf( s c@+ []= #
-:cfunc int foo(char *s) {
+:c int foo(char *s) {
if (sscanf("foo %d bar", s)) {
return pspop();
} else {
@@ -40,7 +40,7 @@ S" foo 42 bar" foo 42 #eq
S" foo baz bar" foo -1 #eq
S" ( ----- %d )" S" ( ----- 000 )" sscanf # 0 #eq
-:cfunc int foo() {
+:c int foo() {
int hdl = fopen("/tests/reffile");
if (fgetc(hdl) != 'T') return 0;
fseek($100, hdl);