From 5e02f07c089b3dad6bb8ddf64a1d197c1e8a6686 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 1 Apr 2014 16:46:41 -0400 Subject: [PATCH] Implemented arithmetic and bitwise operations as well as some basic stack shuffling operations --- source/slvm/main.c | 31 ++-- source/slvm/wordlist.c | 325 +++++++++++++++++++++++++++++++++-------- source/slvm/wordlist.h | 14 +- 3 files changed, 292 insertions(+), 78 deletions(-) diff --git a/source/slvm/main.c b/source/slvm/main.c index dbcffb8..17ac1c1 100644 --- a/source/slvm/main.c +++ b/source/slvm/main.c @@ -3,19 +3,19 @@ #include #include "wordlist.h" -static word_t* find_word(char* name); -static void exec_word(word_t* word); +static void fetch_and_exec(void); +static word_t const* find_word(char const* name); +static void exec_word(word_t const* word); -long* ArgStackPtr; -long* InstructionPtr; +long const* ArgStackPtr; int main(int argc, char** argv) { long stack[] = {0,42,0,0}; ArgStackPtr = &stack[1]; - //exec_word(find_word("dupdup")); - exec_word(find_word("dup")); + //exec_word(find_word("dup")); + fetch_and_exec(); printf("stack[3] = %d\n", (int)stack[3]); printf("stack[2] = %d\n", (int)stack[2]); @@ -24,8 +24,17 @@ int main(int argc, char** argv) return 0; } -static word_t* find_word(char* name) { - word_t* curr = LatestWord; +static void fetch_and_exec(void) +{ + char buffer[1024]; + printf("> "); + fgets(buffer, 1024, stdin); + find_word(buffer); + puts(buffer); +} + +static word_t const* find_word(char const* name) { + word_t const* curr = LatestWord; while(curr) { if (0 == strcmp(curr->name,name)) @@ -37,14 +46,14 @@ static word_t* find_word(char* name) { return curr; } -static void exec_word(word_t* word) { +static void exec_word(word_t const* word) { word->codeword(word->code); } -void exec_word_def(long* code) { +void exec_word_def(long const* code) { while(*code) { - exec_word( (word_t*)(*code) ); + exec_word( (word_t const*)(*code) ); code++; } } diff --git a/source/slvm/wordlist.c b/source/slvm/wordlist.c index 2a0d075..d609430 100644 --- a/source/slvm/wordlist.c +++ b/source/slvm/wordlist.c @@ -2,40 +2,46 @@ extern long* ArgStackPtr; -extern void exec_word_def(long* code); +extern void exec_word_def(long const* code); /** * Define a built-in word that executes native code */ #define defcode(name_str,c_name,flags,prev) \ - extern void c_name##_code(long* code); \ - extern char c_name##_str[]; \ - word_t c_name = { \ + extern void c_name##_code(long const* code); \ + extern char const c_name##_str[]; \ + word_t const c_name = { \ prev, \ flags, \ c_name##_str, \ &c_name##_code, \ 0 \ }; \ - char c_name##_str[] = name_str; \ - void c_name##_code(long* inst_ptr) \ + char const c_name##_str[] = name_str; \ + void c_name##_code(long const* inst_ptr) \ /** * Define a built-in word that is defined by references to other words. */ #define defword(name_str,c_name,flags,prev) \ - extern long c_name##_code[]; \ - extern char c_name##_str[]; \ - word_t c_name = { \ - prev, \ - flags, \ - c_name##_str, \ - &exec_word_def, \ - c_name##_code \ - }; \ - char c_name##_str[] = name_str; \ - long c_name##_code[] = + extern long const c_name##_code[]; \ + extern char const c_name##_str[]; \ + word_t const c_name = { \ + prev, \ + flags, \ + c_name##_str, \ + &exec_word_def, \ + c_name##_code \ + }; \ + char const c_name##_str[] = name_str; \ + long const c_name##_code[] = + +#define defvar() + +#define defconst() #define w(name) (long)&name +#define NEXT 0u + /* Built-in Primitive Words *****************************************************************************/ defcode("drop", drop, 0, 0){ @@ -72,98 +78,297 @@ defcode("-rot", nrot, 0, &rot){ *(ArgStackPtr) = temp; } -defcode("2drop", twodrop, 0, &nrot){} +defcode("2drop", twodrop, 0, &nrot){ + ArgStackPtr -= 2; +} + +defcode("2dup", twodup, 0, &twodrop){ + ArgStackPtr += 2; + *(ArgStackPtr-1) = *(ArgStackPtr-3); + *(ArgStackPtr) = *(ArgStackPtr-2); +} -defcode("2dup", twodup, 0, &twodrop){} +defcode("2swap", twoswap, 0, &twodup){ +} -defcode("2swap", twoswap, 0, &twodup){} +defcode("?dup", qdup, 0, &twoswap){ +} -defcode("?dup", qdup, 0, &twoswap){} +defcode("1+", incr, 0, &qdup){ + *(ArgStackPtr) += 1; +} -defcode("1+", incr, 0, &qdup){} +defcode("1-", decr, 0, &incr){ + *(ArgStackPtr) -= 1; +} -defcode("1-", decr, 0, &incr){} +defcode("4+", incr4, 0, &decr){ + *(ArgStackPtr) += 1; +} -defcode("4+", incr4, 0, &decr){} +defcode("4-", decr4, 0, &incr4){ + *(ArgStackPtr) -= 1; +} -defcode("4-", decr4, 0, &incr4){} +defcode("+", add, 0, &decr4){ + *(ArgStackPtr-1) += *(ArgStackPtr); + ArgStackPtr--; +} -defcode("+", add, 0, &decr4){} +defcode("-", sub, 0, &add){ + *(ArgStackPtr-1) -= *(ArgStackPtr); + ArgStackPtr--; +} -defcode("-", sub, 0, &add){} +defcode("*", mul, 0, &sub){ + *(ArgStackPtr-1) *= *(ArgStackPtr); + ArgStackPtr--; +} -defcode("*", mul, 0, &sub){} +defcode("/", div, 0, &mul){ + *(ArgStackPtr-1) /= *(ArgStackPtr); + ArgStackPtr--; +} -defcode("/", div, 0, &mul){} +defcode("%", mod, 0, &div){ + *(ArgStackPtr-1) %= *(ArgStackPtr); + ArgStackPtr--; +} -defcode("/mod", divmod, 0, &div){} +defcode("/%", divmod, 0, &mod){ +} -defcode("=", equal, 0, &divmod){} +defcode("=", equal, 0, &divmod){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) == *(ArgStackPtr); + ArgStackPtr--; +} -defcode("!=", notequal, 0, &equal){} +defcode("!=", notequal, 0, &equal){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) != *(ArgStackPtr); + ArgStackPtr--; +} -defcode("<", lessthan, 0, ¬equal){} +defcode("<", lessthan, 0, ¬equal){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) < *(ArgStackPtr); + ArgStackPtr--; +} -defcode(">", greaterthan, 0, &lessthan){} +defcode(">", greaterthan, 0, &lessthan){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) > *(ArgStackPtr); + ArgStackPtr--; +} -defcode("<=", lessthaneq, 0, &greaterthan){} +defcode("<=", lessthaneq, 0, &greaterthan){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) <= *(ArgStackPtr); + ArgStackPtr--; +} -defcode(">=", greaterthaneq, 0, &lessthaneq){} +defcode(">=", greaterthaneq, 0, &lessthaneq){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) >= *(ArgStackPtr); + ArgStackPtr--; +} -defcode("0=", zeroeq, 0, &greaterthaneq){} +defcode("0=", zeroeq, 0, &greaterthaneq){ + *(ArgStackPtr) = *(ArgStackPtr) == 0; +} -defcode("0!=", zeroneq, 0, &zeroeq){} +defcode("0!=", zeroneq, 0, &zeroeq){ + *(ArgStackPtr) = *(ArgStackPtr) != 0; +} -defcode("0<", zerolt, 0, &zeroneq){} +defcode("0<", zerolt, 0, &zeroneq){ + *(ArgStackPtr) = *(ArgStackPtr) < 0; +} -defcode("0>", zerogt, 0, &zerolt){} +defcode("0>", zerogt, 0, &zerolt){ + *(ArgStackPtr) = *(ArgStackPtr) > 0; +} -defcode("0<=", zerolte, 0, &zerogt){} +defcode("0<=", zerolte, 0, &zerogt){ + *(ArgStackPtr) = *(ArgStackPtr) <= 0; +} -defcode("0>=", zerogte, 0, &zerolte){} +defcode("0>=", zerogte, 0, &zerolte){ + *(ArgStackPtr) = *(ArgStackPtr) >= 0; +} -defcode("and", and, 0, &zerogte){} +defcode("and", and, 0, &zerogte){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) && *(ArgStackPtr); + ArgStackPtr--; +} -defcode("or", or, 0, &and){} +defcode("or", or, 0, &and){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) || *(ArgStackPtr); + ArgStackPtr--; +} -defcode("not", not, 0, &or){} +defcode("not", not, 0, &or){ + *(ArgStackPtr) = !(*(ArgStackPtr)); +} -defcode("band", band, 0, ¬){} +defcode("band", band, 0, ¬){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) & *(ArgStackPtr); + ArgStackPtr--; +} -defcode("bor", bor, 0, &band){} +defcode("bor", bor, 0, &band){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) | *(ArgStackPtr); + ArgStackPtr--; +} -defcode("bxor", bxor, 0, &bor){} +defcode("bxor", bxor, 0, &bor){ + *(ArgStackPtr-1) = *(ArgStackPtr-1) ^ *(ArgStackPtr); + ArgStackPtr--; +} -defcode("bnot", bnot, 0, &bxor){} +defcode("bnot", bnot, 0, &bxor){ + *(ArgStackPtr) = ~(*(ArgStackPtr)); +} -defcode("lit", lit, 0, &bnot){} +defcode("lit", lit, 0, &bnot){ +} -defcode("!", store, 0, &lit){} +defcode("!", store, 0, &lit){ +} -defcode("@", fetch, 0, &store){} +defcode("@", fetch, 0, &store){ +} -defcode("+!", addstore, 0, &fetch){} +defcode("+!", addstore, 0, &fetch){ +} -defcode("-!", substore, 0, &addstore){} +defcode("-!", substore, 0, &addstore){ +} -defcode("b!", bytestore, 0, &substore){} +defcode("b!", bytestore, 0, &substore){ +} -defcode("b@", bytefetch, 0, &bytestore){} +defcode("b@", bytefetch, 0, &bytestore){ +} -defcode("b@b!", bytecopy, 0, &bytefetch){} +defcode("b@b!", bytecopy, 0, &bytefetch){ +} -defcode("bmove", bytemove, 0, &bytecopy){} +defcode("bmove", bytemove, 0, &bytecopy){ +} /* Built-in Variables *****************************************************************************/ +//defvar("state", , 0, &){ +//} +// +//defvar("here", , 0, &){ +//} +// +//defvar("latest", , 0, &){ +//} +// +//defvar("tos", , 0, &){ +//} +// +//defvar("base", , 0, &){ +//} /* Built-in Constants *****************************************************************************/ +//defconst("version", , 0, &){ +//} +// +//defconst("docol", , 0, &){ +//} +// +//defconst("f_immed", , 0, &){ +//} +// +//defconst("f_hidden", , 0, &){ +//} + +/* Input/Output Words + *****************************************************************************/ +//defcode("getc", , 0, &){ +//} +// +//defcode("putc", , 0, &){ +//} +// +//defcode("getw", , 0, &){ +//} +// +//defcode("getn", , 0, &){ +//} + +/* Compiler Words + *****************************************************************************/ +//defcode("findw", , 0, &){ +//} +// +//defcode("wcwa", , 0, &){ +//} +// +//defcode("wcda", , 0, &){ +//} +// +//defcode("create", , 0, &){ +//} +// +//defcode(",", , 0, &){ +//} +// +//defcode("[", , 0, &){ +//} +// +//defcode("]", , 0, &){ +//} + +//defword(":", , 0, &){ +//} +// +//defword(";", , 0, &){ +//} + +//defcode("immediate", , 0, &){ +//} +// +//defcode("hidden", , 0, &){ +//} +// +//defcode("hide", , 0, &){ +//} +// +//defcode("'", , 0, &){ +//} + +/* Branching Words + *****************************************************************************/ +//defcode("branch", , 0, &){ +//} +// +//defcode("0branch", , 0, &){ +//} + +/* String Literal Words + *****************************************************************************/ +//defcode("litstring", , 0, &){ +//} +// +//defcode("puts", , 0, &){ +//} -/* Built-in Defined Words +/* Interpreter Words *****************************************************************************/ +//defcode("quit", , 0, &){ +//} +// +//defcode("interpret", , 0, &){ +//} +// +//defcode("char", , 0, &){ +//} +// +//defcode("execute", , 0, &){ +//} /* Latest Defined Word *****************************************************************************/ -word_t* LatestWord = &bytemove; +word_t const* LatestWord = &bytemove; diff --git a/source/slvm/wordlist.h b/source/slvm/wordlist.h index 4171735..e41526d 100644 --- a/source/slvm/wordlist.h +++ b/source/slvm/wordlist.h @@ -7,16 +7,16 @@ #ifndef WORDLIST_H #define WORDLIST_H -typedef void (*codeword_t)(long*); +typedef void (*codeword_t)(long const*); typedef struct word_t { - struct word_t* link; - long flags; - char* name; - codeword_t codeword; - long* code; + struct word_t const* link; + long const flags; + char const* name; + codeword_t const codeword; + long const* code; } word_t; -extern word_t* LatestWord; +extern word_t const* LatestWord; #endif /* WORDLIST_H */ -- 2.49.0