]> git.mdlowis.com Git - projs/onward.git/commitdiff
Combined standalone interpreter into onward.c with a build option
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 2 Dec 2014 03:19:38 +0000 (22:19 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 2 Dec 2014 03:19:38 +0000 (22:19 -0500)
build.rb
source/main.c [deleted file]
source/onward.c [moved from source/onward/onward.c with 80% similarity]
source/onward.ft [moved from source/onward/onward.ft with 100% similarity]
source/onward.h [moved from source/onward/onward.h with 100% similarity]
source/onward_sys.h [moved from source/onward/onward_sys.h with 100% similarity]
source/syscall.c [deleted file]
source/syscall.h [deleted file]

index c7c3dc593e8e91b81081846f544d089b69a16105..f48b8d630dcf1fb65015883a75141283c948c7b0 100755 (executable)
--- a/build.rb
+++ b/build.rb
@@ -34,8 +34,7 @@ end
 #------------------------------------------------------------------------------
 unless Opts[:profile].include? "no-tests"
   test_env.Program('onward-tests', [
-      'source/onward/onward.c',
-      'source/syscall.c',
+      'source/onward.c',
       'modules/atf/source/atf.c'] +
       Dir['tests/**/*.c'])
   test_env.Command('Unit Tests', ['./onward-tests'], 'CMD' => ['./onward-tests'])
@@ -44,6 +43,7 @@ end
 #------------------------------------------------------------------------------
 # Release Build Targets
 #------------------------------------------------------------------------------
-main_env.Library('libonward.a', FileList['source/onward/*.c'])
-main_env.Program('onward', ['source/main.c', 'libonward.a'])
+main_env.Library('libonward.a', FileList['source/*.c'])
+main_env.Program('onward', FileList['source/*.c'],
+                 'CFLAGS' => main_env['CFLAGS'] + ['-DSTANDALONE'])
 
diff --git a/source/main.c b/source/main.c
deleted file mode 100755 (executable)
index 6644411..0000000
+++ /dev/null
@@ -1,109 +0,0 @@
-#include "onward_sys.h"
-#include "syscall.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
-
-defcode("dumpw", dumpw, LATEST_BUILTIN, 0u) {
-    word_t* word = (word_t*)onward_aspop();
-    printf("name:\t'%s'\n", word->name);
-    printf("flags:\t%#lx\n", word->flags);
-    printf("link:\t%p\n", word->link);
-    /* Print the word's instructions */
-    if (word->flags & F_PRIMITIVE_MSK) {
-        printf("code:\t%p\n", word->code);
-    } else {
-        printf("code:");
-        word_t** code = (word_t**)word->code;
-        while(*code) {
-            printf("\t%s", (*code)->name);
-            if ((*code == &lit) || (*code == &zbr) || (*code == &br))
-                printf(" %zd", (intptr_t)*(++code));
-            code++;
-            puts("");
-        }
-        printf("\tret\n");
-    }
-}
-
-defcode("syscall", syscall, &dumpw, 0u) {
-    System_Calls[onward_aspop()]();
-}
-
-defvar("infile",  infile,  0u, &syscall);
-defvar("outfile", outfile, 0u, &infile_word);
-defvar("errfile", errfile, 0u, &outfile_word);
-
-/*****************************************************************************/
-bool Newline_Consumed = false;
-
-value_t fetch_char(void)
-{
-    value_t ch = (value_t)fgetc((FILE*)infile);
-    if ((char)ch == '\n')
-        Newline_Consumed = true;
-    return ch;
-}
-
-void emit_char(value_t val)
-{
-    fputc((int)val, (FILE*)outfile);
-}
-
-void print_stack(void) {
-    value_t* base = (value_t*)asb;
-    value_t* top  = (value_t*)asp;
-    printf("( ");
-    int i;
-    if (top-5 >= base)
-        printf("... ");
-    for (i = 4; i >= 0; i--) {
-        value_t* curr = top-i;
-        if (curr > base)
-            printf("%zd ", *curr);
-    }
-    puts(")");
-    printf("errcode: %zd\n", errcode);
-    puts(!errcode ? "OK." : "?");
-}
-
-void parse(FILE* file) {
-    value_t old = infile;
-    infile = (value_t)file;
-    if (file == stdin)
-        printf(":> ");
-    while (!feof(file)) {
-        interp_code();
-        if ((file == stdin) && Newline_Consumed) {
-            print_stack();
-            printf(":> ");
-            Newline_Consumed = false;
-            errcode = 0;
-        }
-    }
-    infile = old;
-}
-
-void parse_file(char* fname) {
-    FILE* file = fopen(fname, "r");
-    if (file) {
-        parse(file);
-        fclose(file);
-    }
-}
-
-int main(int argc, char** argv) {
-    int i;
-    /* Initialize implementation specific words */
-    latest  = (value_t)&errfile_word;
-    infile  = (value_t)stdin;
-    outfile = (value_t)stdout;
-    errfile = (value_t)stderr;
-    /* Load any dictionaries specified on the  command line */
-    for (i = 1; i < argc; i++)
-        parse_file(argv[i]);
-    /* Start the REPL */
-    parse(stdin);
-    return 0;
-}
-
similarity index 80%
rename from source/onward/onward.c
rename to source/onward.c
index d196df2129cb8effd4c4198ce29f6da9a6b1d99f..402a2f5a2c4b9ab83c6a763049a4fd967ef792dd 100755 (executable)
@@ -4,7 +4,6 @@
 #include <stdio.h>
 
 static value_t char_oneof(char ch, char* chs);
-static void abort_on_error(value_t cond, value_t errcode);
 
 /** Version number of the implementation */
 defconst("VERSION", VERSION, 0, 0u);
@@ -523,7 +522,6 @@ defcode("~", bnot, &bxor, 0u) {
 
 /* Helper C Functions
  *****************************************************************************/
-
 value_t onward_pcfetch(void) {
     value_t* reg = (value_t*)pc;
     value_t  val = *reg++;
@@ -532,16 +530,8 @@ value_t onward_pcfetch(void) {
 }
 
 void onward_aspush(value_t val) {
-    #ifndef UNSAFE_MODE
-    if ((asp-asb) <= assz) {
-    #endif
-        asp += sizeof(value_t);
-        *((value_t*)asp) = val;
-    #ifndef UNSAFE_MODE
-    } else {
-        abort_on_error(1, ERR_ARG_STACK_OVRFLW);
-    }
-    #endif
+    asp += sizeof(value_t);
+    *((value_t*)asp) = val;
 }
 
 value_t onward_aspeek(value_t val) {
@@ -551,27 +541,17 @@ value_t onward_aspeek(value_t val) {
 value_t onward_aspop(void) {
     value_t val = *((value_t*)asp);
     asp -= sizeof(value_t);
-    abort_on_error(asp < asb, ERR_ARG_STACK_UNDRFLW);
     return val;
 }
 
 void onward_rspush(value_t val) {
-    #ifndef UNSAFE_MODE
-    if ((rsp-rsb) <= rssz) {
-    #endif
-        rsp += sizeof(value_t);
-        *((value_t*)rsp) = val;
-    #ifndef UNSAFE_MODE
-    } else {
-        abort_on_error(1, ERR_RET_STACK_OVRFLW);
-    }
-    #endif
+    rsp += sizeof(value_t);
+    *((value_t*)rsp) = val;
 }
 
 value_t onward_rspop(void) {
     value_t val = *((value_t*)rsp);
     rsp -= sizeof(value_t);
-    abort_on_error(rsp < rsb, ERR_RET_STACK_UNDRFLW);
     return val;
 }
 
@@ -587,11 +567,166 @@ static value_t char_oneof(char ch, char* chs) {
     return ret;
 }
 
-static void abort_on_error(value_t cond, value_t errcode) {
-    #ifndef UNSAFE_MODE
-    if(cond) {
-        errno = errcode;
-        _abort_code();
+/* System Calls
+ *****************************************************************************/
+#ifdef STANDALONE
+#include <stdio.h>
+#include <stdlib.h>
+
+static void syscall_open(void)
+{
+    intptr_t modenum = onward_aspop();
+    char* fname = (char*)onward_aspop();
+    char* mode;
+    switch (modenum) {
+        case 0: mode = "r";   break;
+        case 1: mode = "w";   break;
+        case 2: mode = "a";   break;
+        case 3: mode = "r+";  break;
+        case 4: mode = "w+";  break;
+        case 5: mode = "a+";  break;
+        default: mode = NULL; break;
     }
-    #endif
+    onward_aspush(mode ? (intptr_t)fopen(fname, mode) : 0);
+}
+
+static void syscall_close(void)
+{
+    onward_aspush(fclose((FILE*)onward_aspop()));
+}
+
+static void syscall_read(void)
+{
+    size_t nbytes = (size_t)onward_aspop();
+    FILE* fhndl   = (FILE*)onward_aspop();
+    void* dest    = (void*)onward_aspop();
+    onward_aspush(nbytes != fread(dest, 1u, nbytes, fhndl));
+}
+
+static void syscall_write(void)
+{
+    size_t nbytes = (size_t)onward_aspop();
+    void* src     = (void*)onward_aspop();
+    FILE* fhndl   = (FILE*)onward_aspop();
+    onward_aspush(nbytes != fwrite(src, 1u, nbytes, fhndl));
+}
+
+static void syscall_seek(void)
+{
+    intptr_t nbytes = onward_aspop();
+    intptr_t origin = onward_aspop();
+    FILE* fhndl     = (FILE*)onward_aspop();
+    origin = (origin == 0) ? SEEK_CUR : (origin < 0) ? SEEK_SET : SEEK_END;
+    onward_aspush(fseek(fhndl, nbytes, origin));
+}
+
+static void syscall_alloc(void)
+{
+    onward_aspush((intptr_t)malloc((size_t)onward_aspop()));
 }
+
+static void syscall_free(void)
+{
+    free((void*)onward_aspop());
+}
+
+typedef void (*syscall_fn_t)(void);
+
+static syscall_fn_t System_Calls[7] = {
+    /* File Operations */
+    &syscall_open,
+    &syscall_close,
+    &syscall_read,
+    &syscall_write,
+    &syscall_seek,
+
+    /* Memory Operations */
+    &syscall_alloc,
+    &syscall_free,
+};
+#endif
+
+/* Standalone Interpreter
+ *****************************************************************************/
+#ifdef STANDALONE
+#include <stdbool.h>
+
+defvar("infile",  infile,  0u, LATEST_BUILTIN);
+defvar("outfile", outfile, 0u, &infile_word);
+defvar("errfile", errfile, 0u, &outfile_word);
+defcode("syscall", syscall, &errfile_word, 0u) {
+    System_Calls[onward_aspop()]();
+}
+
+static bool Newline_Consumed = false;
+
+value_t fetch_char(void)
+{
+    value_t ch = (value_t)fgetc((FILE*)infile);
+    if ((char)ch == '\n')
+        Newline_Consumed = true;
+    return ch;
+}
+
+void emit_char(value_t val)
+{
+    fputc((int)val, (FILE*)outfile);
+}
+
+void print_stack(void) {
+    value_t* base = (value_t*)asb;
+    value_t* top  = (value_t*)asp;
+    printf("( ");
+    int i;
+    if (top-5 >= base)
+        printf("... ");
+    for (i = 4; i >= 0; i--) {
+        value_t* curr = top-i;
+        if (curr > base)
+            printf("%zd ", *curr);
+    }
+    puts(")");
+    printf("errcode: %zd\n", errcode);
+    puts(!errcode ? "OK." : "?");
+}
+
+void parse(FILE* file) {
+    value_t old = infile;
+    infile = (value_t)file;
+    if (file == stdin)
+        printf(":> ");
+    while (!feof(file)) {
+        interp_code();
+        if ((file == stdin) && Newline_Consumed) {
+            print_stack();
+            printf(":> ");
+            Newline_Consumed = false;
+            errcode = 0;
+        }
+    }
+    infile = old;
+}
+
+void parse_file(char* fname) {
+    FILE* file = fopen(fname, "r");
+    if (file) {
+        parse(file);
+        fclose(file);
+    }
+}
+
+int main(int argc, char** argv) {
+    int i;
+    /* Initialize implementation specific words */
+    latest  = (value_t)&syscall;
+    infile  = (value_t)stdin;
+    outfile = (value_t)stdout;
+    errfile = (value_t)stderr;
+    /* Load any dictionaries specified on the  command line */
+    for (i = 1; i < argc; i++)
+        parse_file(argv[i]);
+    /* Start the REPL */
+    parse(stdin);
+    return 0;
+}
+#endif
similarity index 100%
rename from source/onward/onward.ft
rename to source/onward.ft
similarity index 100%
rename from source/onward/onward.h
rename to source/onward.h
diff --git a/source/syscall.c b/source/syscall.c
deleted file mode 100644 (file)
index 434aab9..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-#include "syscall.h"
-#include "onward.h"
-#include <stdio.h>
-#include <stdlib.h>
-
-void syscall_open(void)
-{
-    intptr_t modenum = onward_aspop();
-    char* fname = (char*)onward_aspop();
-    char* mode;
-    switch (modenum) {
-        case 0: mode = "r";   break;
-        case 1: mode = "w";   break;
-        case 2: mode = "a";   break;
-        case 3: mode = "r+";  break;
-        case 4: mode = "w+";  break;
-        case 5: mode = "a+";  break;
-        default: mode = NULL; break;
-    }
-    onward_aspush(mode ? (intptr_t)fopen(fname, mode) : 0);
-}
-
-void syscall_close(void)
-{
-    onward_aspush(fclose((FILE*)onward_aspop()));
-}
-
-void syscall_read(void)
-{
-    size_t nbytes = (size_t)onward_aspop();
-    FILE* fhndl   = (FILE*)onward_aspop();
-    void* dest    = (void*)onward_aspop();
-    onward_aspush(nbytes != fread(dest, 1u, nbytes, fhndl));
-}
-
-void syscall_write(void)
-{
-    size_t nbytes = (size_t)onward_aspop();
-    void* src     = (void*)onward_aspop();
-    FILE* fhndl   = (FILE*)onward_aspop();
-    onward_aspush(nbytes != fwrite(src, 1u, nbytes, fhndl));
-}
-
-void syscall_seek(void)
-{
-    intptr_t nbytes = onward_aspop();
-    intptr_t origin = onward_aspop();
-    FILE* fhndl     = (FILE*)onward_aspop();
-    origin = (origin == 0) ? SEEK_CUR : (origin < 0) ? SEEK_SET : SEEK_END;
-    onward_aspush(fseek(fhndl, nbytes, origin));
-}
-
-void syscall_alloc(void)
-{
-    onward_aspush((intptr_t)malloc((size_t)onward_aspop()));
-}
-
-void syscall_free(void)
-{
-    free((void*)onward_aspop());
-}
-
-syscall_fn_t System_Calls[7] = {
-    /* File Operations */
-    &syscall_open,
-    &syscall_close,
-    &syscall_read,
-    &syscall_write,
-    &syscall_seek,
-
-    /* Memory Operations */
-    &syscall_alloc,
-    &syscall_free,
-};
-
diff --git a/source/syscall.h b/source/syscall.h
deleted file mode 100644 (file)
index b244f33..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-/**
-  @file sycall.h
-  @brief TODO: Describe this file
-  $Revision$
-  $HeadURL$
-*/
-#ifndef SYCALL_H
-#define SYCALL_H
-
-typedef void (*syscall_fn_t)(void);
-
-syscall_fn_t System_Calls[7];
-
-#endif /* SYCALL_H */