From: Mike D. Lowis Date: Tue, 1 Apr 2014 01:43:14 +0000 (-0400) Subject: added some basic built-in words X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=df570a4dbc7338a61a8b51ff383ce27ab87773e7;p=proto%2Fsclpl.git added some basic built-in words --- diff --git a/SConstruct b/SConstruct index edb8440..2303401 100644 --- a/SConstruct +++ b/SConstruct @@ -72,33 +72,33 @@ scheme.PrependENVPath('PATH', './build') #------------------------------------------------------------------------------ # SOF Shared Library -c_cpp.SharedLibrary('build/sof', find_files('source/libsof/','*.c')) +#c_cpp.SharedLibrary('build/sof', find_files('source/libsof/','*.c')) # SBC Shared Library -c_cpp.SharedLibrary('build/sbc', find_files('source/libsbc/','*.c')) +#c_cpp.SharedLibrary('build/sbc', find_files('source/libsbc/','*.c')) # readsof Command Line Utility -readsof = c_cpp.Clone( - CPPPATH = [ 'source/libsof/' ], - LIBS = [ 'sof' ], - LIBPATH = [ 'build' ]) -readsof.Program('build/readsof', find_files('source/readsof/','*.c')) -readsof.Depends('readsof', 'sof') - -# SCLPL Compiler -SchemeBuildAndTest( 'build/slc', - find_files('source/slc/','*.scm'), - find_files('tests/slc/','*.scm') ) - -# SCLPL Package Manager -SchemeBuildAndTest( 'build/slpkg', - find_files('source/slpkg/','*.scm'), - find_files('tests/slpkg/','*.scm') ) - -# SCLPL Assembler -SchemeBuildAndTest( 'build/slas', - find_files('source/slas/','*.scm'), - find_files('tests/slas/','*.scm') ) +#readsof = c_cpp.Clone( +# CPPPATH = [ 'source/libsof/' ], +# LIBS = [ 'sof' ], +# LIBPATH = [ 'build' ]) +#readsof.Program('build/readsof', find_files('source/readsof/','*.c')) +#readsof.Depends('readsof', 'sof') + +## SCLPL Compiler +#SchemeBuildAndTest( 'build/slc', +# find_files('source/slc/','*.scm'), +# find_files('tests/slc/','*.scm') ) +# +## SCLPL Package Manager +#SchemeBuildAndTest( 'build/slpkg', +# find_files('source/slpkg/','*.scm'), +# find_files('tests/slpkg/','*.scm') ) +# +## SCLPL Assembler +#SchemeBuildAndTest( 'build/slas', +# find_files('source/slas/','*.scm'), +# find_files('tests/slas/','*.scm') ) # SCLPL Virtual Machine c_cpp.Program('build/slvm', find_files('source/slvm/','*.c')) diff --git a/source/slvm/main.c b/source/slvm/main.c index d56b5af..dbcffb8 100644 --- a/source/slvm/main.c +++ b/source/slvm/main.c @@ -1,11 +1,51 @@ #include #include +#include #include "wordlist.h" +static word_t* find_word(char* name); +static void exec_word(word_t* word); + long* ArgStackPtr; +long* InstructionPtr; int main(int argc, char** argv) { - puts("Hello, World!"); + long stack[] = {0,42,0,0}; + ArgStackPtr = &stack[1]; + + //exec_word(find_word("dupdup")); + exec_word(find_word("dup")); + + printf("stack[3] = %d\n", (int)stack[3]); + printf("stack[2] = %d\n", (int)stack[2]); + printf("stack[1] = %d\n", (int)stack[1]); + printf("stack[0] = %d\n", (int)stack[0]); return 0; } + +static word_t* find_word(char* name) { + word_t* curr = LatestWord; + while(curr) + { + if (0 == strcmp(curr->name,name)) + { + break; + } + curr = curr->link; + } + return curr; +} + +static void exec_word(word_t* word) { + word->codeword(word->code); +} + +void exec_word_def(long* code) { + while(*code) + { + exec_word( (word_t*)(*code) ); + code++; + } +} + diff --git a/source/slvm/wordlist.c b/source/slvm/wordlist.c index 12404ea..2a0d075 100644 --- a/source/slvm/wordlist.c +++ b/source/slvm/wordlist.c @@ -2,20 +2,22 @@ extern long* ArgStackPtr; +extern void exec_word_def(long* code); + /** * Define a built-in word that executes native code */ #define defcode(name_str,c_name,flags,prev) \ - extern void c_name##_code(void); \ - extern char c_name##_str[]; \ - word_t c_name = { \ - prev, \ - flags, \ - c_name##_str, \ - &c_name##_code, \ - 0 \ - }; \ - char c_name##_str[] = name_str; \ - void c_name##_code(void) \ + extern void c_name##_code(long* code); \ + extern char c_name##_str[]; \ + word_t c_name = { \ + prev, \ + flags, \ + c_name##_str, \ + &c_name##_code, \ + 0 \ + }; \ + char c_name##_str[] = name_str; \ + void c_name##_code(long* inst_ptr) \ /** * Define a built-in word that is defined by references to other words. */ @@ -26,13 +28,16 @@ extern long* ArgStackPtr; prev, \ flags, \ c_name##_str, \ - &do_colon, \ + &exec_word_def, \ c_name##_code \ }; \ char c_name##_str[] = name_str; \ long c_name##_code[] = -/*****************************************************************************/ +#define w(name) (long)&name + +/* Built-in Primitive Words + *****************************************************************************/ defcode("drop", drop, 0, 0){ ArgStackPtr--; } @@ -67,6 +72,98 @@ defcode("-rot", nrot, 0, &rot){ *(ArgStackPtr) = temp; } -/*****************************************************************************/ -word_t* LatestWord = &nrot; +defcode("2drop", twodrop, 0, &nrot){} + +defcode("2dup", twodup, 0, &twodrop){} + +defcode("2swap", twoswap, 0, &twodup){} + +defcode("?dup", qdup, 0, &twoswap){} + +defcode("1+", incr, 0, &qdup){} + +defcode("1-", decr, 0, &incr){} + +defcode("4+", incr4, 0, &decr){} + +defcode("4-", decr4, 0, &incr4){} + +defcode("+", add, 0, &decr4){} + +defcode("-", sub, 0, &add){} + +defcode("*", mul, 0, &sub){} + +defcode("/", div, 0, &mul){} + +defcode("/mod", divmod, 0, &div){} + +defcode("=", equal, 0, &divmod){} + +defcode("!=", notequal, 0, &equal){} + +defcode("<", lessthan, 0, ¬equal){} + +defcode(">", greaterthan, 0, &lessthan){} + +defcode("<=", lessthaneq, 0, &greaterthan){} + +defcode(">=", greaterthaneq, 0, &lessthaneq){} + +defcode("0=", zeroeq, 0, &greaterthaneq){} + +defcode("0!=", zeroneq, 0, &zeroeq){} + +defcode("0<", zerolt, 0, &zeroneq){} + +defcode("0>", zerogt, 0, &zerolt){} + +defcode("0<=", zerolte, 0, &zerogt){} + +defcode("0>=", zerogte, 0, &zerolte){} + +defcode("and", and, 0, &zerogte){} + +defcode("or", or, 0, &and){} + +defcode("not", not, 0, &or){} + +defcode("band", band, 0, ¬){} + +defcode("bor", bor, 0, &band){} + +defcode("bxor", bxor, 0, &bor){} + +defcode("bnot", bnot, 0, &bxor){} + +defcode("lit", lit, 0, &bnot){} + +defcode("!", store, 0, &lit){} + +defcode("@", fetch, 0, &store){} + +defcode("+!", addstore, 0, &fetch){} + +defcode("-!", substore, 0, &addstore){} + +defcode("b!", bytestore, 0, &substore){} + +defcode("b@", bytefetch, 0, &bytestore){} + +defcode("b@b!", bytecopy, 0, &bytefetch){} + +defcode("bmove", bytemove, 0, &bytecopy){} + +/* Built-in Variables + *****************************************************************************/ + +/* Built-in Constants + *****************************************************************************/ + +/* Built-in Defined Words + *****************************************************************************/ + +/* Latest Defined Word + *****************************************************************************/ +word_t* LatestWord = &bytemove; diff --git a/source/slvm/wordlist.h b/source/slvm/wordlist.h index 49fa6f9..4171735 100644 --- a/source/slvm/wordlist.h +++ b/source/slvm/wordlist.h @@ -7,7 +7,7 @@ #ifndef WORDLIST_H #define WORDLIST_H -typedef void (*codeword_t)(void); +typedef void (*codeword_t)(long*); typedef struct word_t { struct word_t* link;