commit d841f36f933a42f1cc5a36b55c039a19639a5c80
parent 9f994e1e77ffde6a31afb46ffda1bc7efce8636f
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Thu, 16 Jun 2022 14:31:07 -0400
lib/str: add "stringlist" structure
Diffstat:
4 files changed, 22 insertions(+), 28 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -14,11 +14,9 @@ create uopssyms ," -~!&*?"
\ Binary operators
13 const BOPSCNT
-create BOPTlist 1 c, ," +" 1 c, ," -" 1 c, ," *" 1 c, ," /"
- 1 c, ," <" 1 c, ," >" 2 c, ," <=" 2 c, ," >="
- 2 c, ," ==" 2 c, ," !=" 2 c, ," &&" 2 c, ," ||"
- 1 c, ," ="
- 0 c,
+BOPSCNT stringlist BOPTlist
+"+" "-" "*" "/" "<" ">" "<=" ">=" "==" "!=" "&&" "||" "="
+
\ binary ops precedence. lower means more precedence
create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c,
3 c, 3 c, 4 c, 4 c, 5 c,
@@ -46,12 +44,9 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c,
\ 13 = unused
14 const AST_FUNCALL \ data1=name data2=MAP_FUNCTION
-create astidnames 7 c, ," declare" 4 c, ," unit" 8 c, ," function"
- 6 c, ," return" 8 c, ," constant" 5 c, ," stmts"
- 4 c, ," args" 6 c, ," lvalue" 7 c, ," unaryop"
- 1 c, ," _" 5 c, ," binop" 1 c, ," _"
- 2 c, ," if" 1 c, ," _" 4 c, ," call"
- 0 c,
+ASTIDCNT stringlist astidnames
+"declare" "unit" "function" "return" "constant" "stmts" "args" "lvalue"
+"unaryop" "_" "binop" "_" "if" "_" "call"
0 value curunit \ points to current Unit, the beginning of the AST
@@ -194,7 +189,7 @@ ASTIDCNT wordtbl astdatatbl ( node -- node )
alias noop parseStatements ( funcnode -- ) \ forward declaration
-create statementnames 6 c, ," return" 2 c, ," if" 0 c,
+2 stringlist statementnames "return" "if"
2 wordtbl statementhandler ( snode -- snode )
:w ( return )
dup AST_RETURN newnode ( snode rnode )
diff --git a/fs/cc/tok.fs b/fs/cc/tok.fs
@@ -11,17 +11,11 @@
3 const TOK_STRLIT
4 const TOK_SYMBOL
-create keywords 5 c, ," break" 4 c, ," case" 4 c, ," char"
- 5 c, ," const" 8 c, ," continue" 7 c, ," default"
- 2 c, ," do" 6 c, ," double" 4 c, ," else"
- 4 c, ," enum" 6 c, ," extern" 5 c, ," float"
- 3 c, ," for" 4 c, ," goto" 2 c, ," if"
- 6 c, ," inline" 3 c, ," int" 8 c, ," register"
- 6 c, ," return" 5 c, ," short" 6 c, ," signed"
- 6 c, ," sizeof" 6 c, ," static" 6 c, ," struct"
- 6 c, ," switch" 7 c, ," typedef" 5 c, ," union"
- 8 c, ," unsigned" 4 c, ," void" 8 c, ," volatile"
- 5 c, ," while" 0 c,
+31 stringlist keywords "break" "case" "char" "const" "continue" "default" "do"
+ "double" "else" "enum" "extern" "float" "for" "goto" "if"
+ "inline" "int" "register" "return" "short" "signed"
+ "sizeof" "static" "struct" "switch" "typedef" "union"
+ "unsigned" "void" "volatile" "while"
: isKeyword? ( s -- f ) keywords sfind 0>= ;
diff --git a/fs/lib/str.fs b/fs/lib/str.fs
@@ -35,3 +35,12 @@ create _ 4 c, ," AZaz"
: A-Za-z? ( c -- f ) _ rmatch ;
create _ 6 c, ," AZaz09"
: alnum? ( c -- f ) _ rmatch ;
+
+\ Create a list of strings (same format as sfind above) with the specified
+\ number of elements. Each element must be "quoted" with no space (unless you
+\ want them in the string) in the quotes.
+\ Example: 3 stringlist mylist "foo" "bar" "hello world!"
+: stringlist create >r begin
+ 0 begin drop in< dup ws? not until ( c )
+ '"' = not if '"' emit abort" expected" then
+ [compile] S" drop next 0 c, ;
diff --git a/fs/tests/str.fs b/fs/tests/str.fs
@@ -3,11 +3,7 @@ f<< lib/str.fs
testbegin
\ Tests for str.fs
-create list
- 5 c, ," hello"
- 3 c, ," foo"
- 3 c, ," bar"
- 0 c,
+3 stringlist list "hello" "foo" "bar"
S" foo" list sfind 1 #eq
S" hello" list sfind 0 #eq