/*- Author: Brian Tiffin Dedicated to the public domain Date: October 2016 Modified: 2016-12-09/06:37-0500 Ficl License and Disclaimer: Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ /* unificl-v1.c a Forth interpreter in Unicon with loadfunc tectonics: gcc -o unificl.so -shared -fpic unificl-v1.c -lficl */ #include #include #include "ficl.h" #include "icall.h" /* Global variables to track VM across calls */ int unificlLoaded = 0; ficlVm *unificlVm = NULL; ficlSystem *unificlSystem = NULL; /* Unicon calling ficl */ int unificl(int argc, descriptor *argv) { int returnValue = 0; char buffer[256]; /* Need a string of code parameter */ if (argc < 1) Error(103); ArgString(1); /* start up the ficl VM */ if (!unificlLoaded) { unificlSystem = ficlSystemCreate(NULL); ficlSystemCompileExtras(unificlSystem); unificlVm = ficlSystemCreateVm(unificlSystem); //returnValue = ficlVmEvaluate(unificlVm, // ".ver .( " __DATE__ " ) cr quit"); unificlLoaded = 1; } /* Run the Forth code and get an integer status */ returnValue = ficlVmEvaluate(unificlVm, StringVal(argv[1])); /* return to Unicon */ RetInteger(returnValue); } /* run down the ficl VM, return a 0 to Unicon */ int unificlRundown(int argc, descriptor *argv) { ficlSystemDestroy(unificlSystem); unificlVm = NULL; unificlSystem = NULL; unificlLoaded = 0; RetInteger(0); } /* Return the stack */ int unificlStack(int argc, descriptor *argv) { int depth; int i; listblock *list; if (!unificlLoaded) { Error(117); /* report engine not loaded, missing main procedure */ } depth = ficlStackDepth(unificlVm->dataStack); int *integers = malloc(sizeof(int) * depth); for (i = 0; i < depth; i++) { integers[i] = ficlStackFetch(unificlVm->dataStack, i).i; } list = mkIlist(integers, depth); /* return to Unicon */ free(integers); RetList(list); } /* Return the floating point stack */ int unificlFloatStack(int argc, descriptor *argv) { int depth; int i; listblock *list; if (!unificlLoaded) { Error(117); /* report engine not loaded, missing main procedure */ } depth = ficlStackDepth(unificlVm->floatStack); double *doubles = malloc(sizeof(double) * depth); for (i = 0; i < depth; i++) { doubles[i] = ficlStackFetch(unificlVm->floatStack, i).f; } list = mkRlist(doubles, depth); /* return to Unicon */ free(doubles); RetList(list); }