Compare commits

..

2 Commits

Author SHA1 Message Date
Richard Thier
9ec2dffd1b fixes for examples - many todo for now and need refactors for separating parse / interpret... 2025-01-08 02:03:42 +01:00
Richard Thier
004f916aa3 add examples - initial examples 2025-01-08 01:59:07 +01:00
11 changed files with 113 additions and 14 deletions

View File

@ -142,7 +142,7 @@ static word_body word_processed_body(word *w) {
enum SLC_SYM_OP { SLC_SYM_SET = 0, SLC_SYM_GET = 1, SLC_SYM_ERASE = 2 };
typedef enum SLC_SYM_OP SLC_SYM_OP;
enum SLC_STACK_OP { SLC_STACK_PUSH, SLC_STACK_POP, SLC_STACK_AT, SLC_STACK_COUNT, SLC_STACK_ERASE };
enum SLC_STACK_OP { SLC_STACK_PUSH, SLC_STACK_POP, SLC_STACK_AT, SLC_STACK_POPAT, SLC_STACK_COUNT, SLC_STACK_ERASE };
typedef enum SLC_STACK_OP SLC_STACK_OP;
enum SLC_SESSION_OP {
SLC_SESSION_ALLOC,
@ -152,7 +152,8 @@ enum SLC_SESSION_OP {
SLC_SESSION_SET,
SLC_SESSION_GET32,
SLC_SESSION_SET32,
SLC_SESSION_PROCESS
SLC_SESSION_PROCESS,
SLC_SESSION_MARK
};
typedef enum SLC_SESSION_OP SLC_SESSION_OP;
enum SLC_IO_OP {
@ -197,6 +198,7 @@ typedef void* (*sym)(SLC_SYM_OP op, do_not_save_charptr key, void *ptr);
* SLC_STACK_PUSH pushes the "elem" to the stack. Returns 1 if succeeded, otherwise 0.
* SLC_STACK_POP pops the stack - does not return meaningful value, beware of underflowing!
* SLC_STACK_AT returns the "param"th element down from the top of the stack
* SLC_STACK_POPAT pops at the paramth element down from the top of the stack
* SLC_STACK_COUNT returns the number of elements in the stack
* SLC_STACK_ERASE Makes the stack empty. Basically as if you would POP the COUNT times.
*
@ -211,13 +213,14 @@ typedef uint32_t (*stack)(SLC_STACK_OP op, uint32_t param);
*
* Operations:
* SLC_SESSION_ALLOC allocates parameter amount of memory and returns an accessor index.
* SLC_SESSION_ERASE erase the session storage (all of it) - all parameters are unused
* SLC_SESSION_ERASE erase the session storage (until last marker!) - all parameters are unused
* SLC_SESSION_PUSH adds the given byte (value in i) to the end of the session storage (by growing it) - j unused
* SLC_SESSION_GET gets byte at the ith accessor index - j unused
* SLC_SESSION_SET gets byte at the ith accessor index to be of (byte)j
* SLC_SESSION_GET32 gets uint32_t at the ith accessor index - j unused. XXX: Beware, architectures unaligned access crash!
* SLC_SESSION_SET32 gets uint32_t at the ith accessor index to be of j. XXX: Beware, architectures unaligned access crash!
* SLC_SESSION_PROCESS gets the last j bytes and moves them overriding bytes at index i, then "shrinks" the storage by j.
* SLC_SESSION_MARK sets marker
*
* @param op Defines which operation the caller wants.
* @param i Used on SESSION_GET and is the accessor index, in case of SESSIN_ALLOC it is the amount to allocate.

26
examples/do.slc Normal file
View File

@ -0,0 +1,26 @@
/*
Parses this:
do {
...code...
} (...cond...)
Into this:
...code...
while(...cond...) {
...code...
}
*/
// -> [bytecodes]
::builtin do @body
"" parseblock {} // "" means no entry into the symbol table (unnamed function) - equals to 0
do@body(.) // (.) does not removes - only (_) no need for gencode because
gen(while) // address top bit 1 when builtin from C
genopen() // equal to gencode(1)
"" parseblock ()
genclose()
genopen{}
do@body
genclose{}

4
examples/dup.slc Normal file
View File

@ -0,0 +1,4 @@
:builtin dup @t
dup@t(_)
dup@t
dup@t

13
examples/for.slc Normal file
View File

@ -0,0 +1,13 @@
::builtin for
parseword drop // TODO: error handling
"" ";" parse
gen(while)
genopen()
"" ";" parse
genclose()
genopen{}
"" ")" parse()
"" parseblock {}
swap
genclose{}
;

8
examples/gen.slc Normal file
View File

@ -0,0 +1,8 @@
: gen
if(1 !=) {
dup 1 -
gen
}
;
5 gen

1
examples/gen_str Normal file
View File

@ -0,0 +1 @@
"word_name" gen_str // -> session storage-be

12
examples/if.slc Normal file
View File

@ -0,0 +1,12 @@
// prefixed builtins with '#'
// when writing a compiler - like a c-like language
:if
()
#asm(jz else$)
{}
#asm(jmp ifend$)
#asm(else$:)
[]
#asm(ifend$:)
;

23
examples/strings.slc Normal file
View File

@ -0,0 +1,23 @@
"word_name" gen_str // -> session storage-be - amúgy ez utf8
"abc" "df" strcon
/*
abc0
0004
df00
0002
*/
// TODO: implement as C builtin instead.. but doable this way
:builtin strcon @off1 @mod1 @
dup
7 + 4 /
strcon@off1(_) // (_) azt jelenti, hogy pop és változóba tesz, (.) means peek and put in var
dup
4 %
strcon@mod1(_)
strcon@off1
popat
+
;

View File

@ -0,0 +1,7 @@
// a b -> b a
:builtin swap @tmp1 @tmp2
swap@tmp1(_)
swap@tmp2(_)
swap@tmp1
swap@tmp2
;

2
examples/variable.slc Normal file
View File

@ -0,0 +1,2 @@
: player_data @xpos @ypos @health
;

22
main.c
View File

@ -46,18 +46,18 @@ char testcoderead() {
static int i = 0;
static const char *code =
"// This is a small test program to push(42); print\n"
"/*#push(21)\n"
"#push(21)\n"
"#add*/\n"
" #push(42)\n"
" #print\n"
"/*#21\n"
"#21\n"
"#+*/\n"
" #42\n"
" #printnum\n"
"\n"
"#: word_test\n"
" #push(21)\n"
" #push(21)\n"
" #add\n"
" #print\n"
"\n"
" #21\n"
" #21\n"
" #+\n"
" #printnum\n"
";\n"
"word_test\n";
return code[i++];
}
@ -80,7 +80,7 @@ int main(int argc, const char **argv) {
"/*", // multiline_comment_opener
"*/", // multiline_comment_closer,
"#", // prefix
"#", // ender
";", // ender
"@" // varprefix
);