]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Implemented arithmetic and bitwise operations as well as some basic stack shuffling...
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 1 Apr 2014 20:46:41 +0000 (16:46 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 1 Apr 2014 20:46:41 +0000 (16:46 -0400)
source/slvm/main.c
source/slvm/wordlist.c
source/slvm/wordlist.h

index dbcffb8049a14b5bdcf2062a91daf9f79fdb449a..17ac1c1ec4fc22545d22a384b9c8a54356f6ee5f 100644 (file)
@@ -3,19 +3,19 @@
 #include <string.h>
 #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++;
     }
 }
index 2a0d075f2794fb2e1492e237fe837a85098af280..d6094303da5d1f2ca63d14e9efd6569044253d26 100644 (file)
@@ -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, &notequal){}
+defcode("<", lessthan, 0, &notequal){
+    *(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, &not){}
+defcode("band", band, 0, &not){
+    *(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;
 
index 4171735a894e511eb48864c6aa7add929ed0742e..e41526d93e1c8ea3392f46f306fc3db6421804f0 100644 (file)
@@ -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 */