From 3e5462fc7454801d60cce38a3717535755990c5a Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Wed, 16 Dec 2020 15:00:26 -0800 Subject: [PATCH] add ulisp --- ulisp/LICENSE | 21 + ulisp/Makefile | 32 + ulisp/README.md | 13 + ulisp/include/README | 39 + ulisp/lib/README | 46 + ulisp/platformio.ini | 14 + ulisp/src/ulisp.cc | 5551 ++++++++++++++++++++++++++++++++++++++++++ ulisp/test/README | 11 + 8 files changed, 5727 insertions(+) create mode 100644 ulisp/LICENSE create mode 100644 ulisp/Makefile create mode 100644 ulisp/README.md create mode 100644 ulisp/include/README create mode 100644 ulisp/lib/README create mode 100644 ulisp/platformio.ini create mode 100644 ulisp/src/ulisp.cc create mode 100644 ulisp/test/README diff --git a/ulisp/LICENSE b/ulisp/LICENSE new file mode 100644 index 0000000..6f02b75 --- /dev/null +++ b/ulisp/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 David Johnson-Davies + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/ulisp/Makefile b/ulisp/Makefile new file mode 100644 index 0000000..2bdca72 --- /dev/null +++ b/ulisp/Makefile @@ -0,0 +1,32 @@ +BOARD := adafruit_feather_m4 +FIRMWARE := .pioenvs/$(BOARD)/firmware.bin +SOURCES := \ + src/ulisp.cc + +PIO := pio run -e $(BOARD) + +$(FIRMWARE): $(SOURCES) + $(PIO) + +PHONY: all +all: $(FIRMWARE) + +.PHONY: upload +upload: $(FIRMWARE) + $(PIO) -t upload + +.PHONY: monitor +monitor: + $(PIO) -t monitor + +.PHONY: deploy +deploy: $(FIRMWARE) + $(PIO) -t upload && sleep 0.5 && $(PIO) -t monitor + +.PHONY: clean +clean: + $(PIO) -t clean + +.PHONY: cloc +cloc: + cloc include lib src diff --git a/ulisp/README.md b/ulisp/README.md new file mode 100644 index 0000000..e846741 --- /dev/null +++ b/ulisp/README.md @@ -0,0 +1,13 @@ +# ulisp-arm +A version of the Lisp programming language for boards based on the ARM processor: + +* Arduino Zero and MKRZero. +* Adafruit ItsyBitsy M0, Feather M0, and Gemma M0. +* Adafruit Metro M4, ItsyBitsy M4, Feather M4, and Grand Central M4. +* Adafruit PyBadge and PyGamer. +* Adafruit CLUE and ItsyBitsy nRF52840. +* BBC Micro Bit. +* Maxim MAX32620FTHR. +* Teensy 4.0/4.1. + +For more information see: http://www.ulisp.com/ diff --git a/ulisp/include/README b/ulisp/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/ulisp/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/ulisp/lib/README b/ulisp/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/ulisp/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/ulisp/platformio.ini b/ulisp/platformio.ini new file mode 100644 index 0000000..50208f0 --- /dev/null +++ b/ulisp/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:adafruit_feather_m4] +platform = atmelsam +board = adafruit_feather_m4 +framework = arduino diff --git a/ulisp/src/ulisp.cc b/ulisp/src/ulisp.cc new file mode 100644 index 0000000..71e643c --- /dev/null +++ b/ulisp/src/ulisp.cc @@ -0,0 +1,5551 @@ +/* uLisp ARM 3.3a - www.ulisp.com + David Johnson-Davies - www.technoblogy.com - 1st July 2020 + + Licensed under the MIT license: https://opensource.org/licenses/MIT +*/ + +#include + +// Lisp Library +const char LispLibrary[] PROGMEM = ""; + +// Compile options + +// #define resetautorun +#define printfreespace +// #define printgcs +// #define sdcardsupport +// #define gfxsupport +// #define lisplibrary +#define assemblerlist +// #define lineeditor +// #define vt100 + +// Includes + +// #include "LispLibrary.h" +#include +#include +#include +#include + +#if defined(gfxsupport) +#include // Core graphics library +#include // Hardware-specific library for ST7735 +#define COLOR_WHITE 0xffff +#define COLOR_BLACK 0 + +// Adafruit PyBadge/PyGamer +#define TFT_CS 44 // Chip select +#define TFT_RST 46 // Display reset +#define TFT_DC 45 // Display data/command select +#define TFT_BACKLIGHT 47 // Display backlight pin +#define TFT_MOSI 41 // Data out +#define TFT_SCLK 42 // Clock out +Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); +#endif + +#if defined(sdcardsupport) +#include +#define SDSIZE 172 +#else +#define SDSIZE 0 +#endif + +// C Macros + +#define nil NULL +#define car(x) (((object *) (x))->car) +#define cdr(x) (((object *) (x))->cdr) + +#define first(x) (((object *) (x))->car) +#define second(x) (car(cdr(x))) +#define cddr(x) (cdr(cdr(x))) +#define third(x) (car(cdr(cdr(x)))) + +#define push(x, y) ((y) = cons((x),(y))) +#define pop(y) ((y) = cdr(y)) + +#define integerp(x) ((x) != NULL && (x)->type == NUMBER) +#define floatp(x) ((x) != NULL && (x)->type == FLOAT) +#define symbolp(x) ((x) != NULL && (x)->type == SYMBOL) +#define stringp(x) ((x) != NULL && (x)->type == STRING) +#define characterp(x) ((x) != NULL && (x)->type == CHARACTER) +#define arrayp(x) ((x) != NULL && (x)->type == ARRAY) +#define streamp(x) ((x) != NULL && (x)->type == STREAM) + +#define mark(x) (car(x) = (object *)(((uintptr_t)(car(x))) | MARKBIT)) +#define unmark(x) (car(x) = (object *)(((uintptr_t)(car(x))) & ~MARKBIT)) +#define marked(x) ((((uintptr_t)(car(x))) & MARKBIT) != 0) +#define MARKBIT 1 + +#define setflag(x) (Flags = Flags | 1<<(x)) +#define clrflag(x) (Flags = Flags & ~(1<<(x))) +#define tstflag(x) (Flags & 1<<(x)) + +#define issp(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t') + +// Code marker stores start and end of code block +#define startblock(x) ((x->integer) & 0xFFFF) +#define endblock(x) ((x->integer) >> 16 & 0xFFFF) + +// Constants + +const int TRACEMAX = 3; // Number of traced functions +enum type { ZZERO=0, SYMBOL=2, CODE=4, NUMBER=6, STREAM=8, CHARACTER=10, FLOAT=12, ARRAY=14, STRING=16, PAIR=18 }; // ARRAY STRING and PAIR must be last +enum token { UNUSED, BRA, KET, QUO, DOT }; +enum stream { SERIALSTREAM, I2CSTREAM, SPISTREAM, SDSTREAM, STRINGSTREAM, GFXSTREAM }; + +// Stream names used by printobject +const char serialstream[] PROGMEM = "serial"; +const char i2cstream[] PROGMEM = "i2c"; +const char spistream[] PROGMEM = "spi"; +const char sdstream[] PROGMEM = "sd"; +const char stringstream[] PROGMEM = "string"; +const char gfxstream[] PROGMEM = "gfx"; +const char *const streamname[] PROGMEM = {serialstream, i2cstream, spistream, sdstream, stringstream, gfxstream}; + +enum function { NIL, TEE, NOTHING, OPTIONAL, INITIALELEMENT, ELEMENTTYPE, BIT, AMPREST, LAMBDA, LET, +LETSTAR, CLOSURE, SPECIAL_FORMS, QUOTE, DEFUN, DEFVAR, SETQ, LOOP, RETURN, PUSH, POP, INCF, DECF, SETF, +DOLIST, DOTIMES, TRACE, UNTRACE, FORMILLIS, WITHOUTPUTTOSTRING, WITHSERIAL, WITHI2C, WITHSPI, WITHSDCARD, +WITHGFX, DEFCODE, TAIL_FORMS, PROGN, IF, COND, WHEN, UNLESS, CASE, AND, OR, FUNCTIONS, NOT, NULLFN, CONS, +ATOM, LISTP, CONSP, SYMBOLP, ARRAYP, BOUNDP, SETFN, STREAMP, EQ, CAR, FIRST, CDR, REST, CAAR, CADR, +SECOND, CDAR, CDDR, CAAAR, CAADR, CADAR, CADDR, THIRD, CDAAR, CDADR, CDDAR, CDDDR, LENGTH, +ARRAYDIMENSIONS, LIST, MAKEARRAY, REVERSE, NTH, AREF, ASSOC, MEMBER, APPLY, FUNCALL, APPEND, MAPC, MAPCAR, +MAPCAN, ADD, SUBTRACT, MULTIPLY, DIVIDE, MOD, ONEPLUS, ONEMINUS, ABS, RANDOM, MAXFN, MINFN, NOTEQ, NUMEQ, +LESS, LESSEQ, GREATER, GREATEREQ, PLUSP, MINUSP, ZEROP, ODDP, EVENP, INTEGERP, NUMBERP, FLOATFN, FLOATP, +SIN, COS, TAN, ASIN, ACOS, ATAN, SINH, COSH, TANH, EXP, SQRT, LOG, EXPT, CEILING, FLOOR, TRUNCATE, ROUND, +CHAR, CHARCODE, CODECHAR, CHARACTERP, STRINGP, STRINGEQ, STRINGLESS, STRINGGREATER, SORT, STRINGFN, +CONCATENATE, SUBSEQ, READFROMSTRING, PRINCTOSTRING, PRIN1TOSTRING, LOGAND, LOGIOR, LOGXOR, LOGNOT, ASH, +LOGBITP, EVAL, GLOBALS, LOCALS, MAKUNBOUND, BREAK, READ, PRIN1, PRINT, PRINC, TERPRI, READBYTE, READLINE, +WRITEBYTE, WRITESTRING, WRITELINE, RESTARTI2C, GC, ROOM, SAVEIMAGE, LOADIMAGE, CLS, PINMODE, DIGITALREAD, +DIGITALWRITE, ANALOGREAD, ANALOGWRITE, DELAY, MILLIS, SLEEP, NOTE, EDIT, PPRINT, PPRINTALL, FORMAT, +REQUIRE, LISTLIBRARY, DRAWPIXEL, DRAWLINE, DRAWRECT, FILLRECT, DRAWCIRCLE, FILLCIRCLE, DRAWROUNDRECT, +FILLROUNDRECT, DRAWTRIANGLE, FILLTRIANGLE, DRAWCHAR, SETCURSOR, SETTEXTCOLOR, SETTEXTSIZE, SETTEXTWRAP, +FILLSCREEN, SETROTATION, INVERTDISPLAY, ENDFUNCTIONS }; + +// Typedefs + +typedef unsigned int symbol_t; + +typedef struct sobject { + union { + struct { + sobject *car; + sobject *cdr; + }; + struct { + unsigned int type; + union { + symbol_t name; + int integer; + int chars; // For strings + float single_float; + }; + }; + }; +} object; + +typedef object *(*fn_ptr_type)(object *, object *); +typedef void (*mapfun_t)(object *, object **); +typedef int (*intfn_ptr_type)(int w, int x, int y, int z); + +typedef struct { + const char *string; + fn_ptr_type fptr; + uint8_t minmax; +} tbl_entry_t; + +typedef int (*gfun_t)(); +typedef void (*pfun_t)(char); +typedef int PinMode; + +// Workspace +#define WORDALIGNED __attribute__((aligned (4))) +#define BUFFERSIZE 34 // Number of bits+2 +#define RAMFUNC __attribute__ ((section (".ramfunctions"))) +#define MEMBANK + +#if defined(ARDUINO_ITSYBITSY_M0) || defined(ARDUINO_SAMD_FEATHER_M0_EXPRESS) + #define WORKSPACESIZE 2816-SDSIZE /* Objects (8*bytes) */ + #define DATAFLASHSIZE 2048000 /* 2 MBytes */ + #define SYMBOLTABLESIZE 512 /* Bytes */ + #define CODESIZE 128 /* Bytes */ + #define SDCARD_SS_PIN 4 + #define STACKDIFF 320 + #define CPU_ATSAMD21 + +#elif defined(ARDUINO_GEMMA_M0) + #define WORKSPACESIZE 2816-SDSIZE /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 512 /* Bytes */ + #define CODESIZE 128 /* Bytes */ + #define STACKDIFF 320 + #define CPU_ATSAMD21 + +#elif defined(ARDUINO_METRO_M4) || defined(ARDUINO_ITSYBITSY_M4) || defined(ARDUINO_FEATHER_M4) || defined(ARDUINO_PYBADGE_M4) || defined(ARDUINO_PYGAMER_M4) + #define WORKSPACESIZE 20480-SDSIZE /* Objects (8*bytes) */ + #define DATAFLASHSIZE 2048000 /* 2 MBytes */ + #define SYMBOLTABLESIZE 1024 /* Bytes */ + #define CODESIZE 256 /* Bytes */ + #define SDCARD_SS_PIN 10 + #define STACKDIFF 400 + #define CPU_ATSAMD51 + +#elif defined(ARDUINO_GRAND_CENTRAL_M4) + #define WORKSPACESIZE 28672-SDSIZE /* Objects (8*bytes) */ + #define DATAFLASHSIZE 8192000 /* 8 MBytes */ + #define SYMBOLTABLESIZE 1024 /* Bytes */ + #define CODESIZE 256 /* Bytes */ + #define STACKDIFF 400 + #define CPU_ATSAMD51P20 + +#elif defined(ARDUINO_SAMD_MKRZERO) + #define WORKSPACESIZE 2816-SDSIZE /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 512 /* Bytes */ + #define CODESIZE 128 /* Bytes */ + #define STACKDIFF 840 + #define CPU_ATSAMD21 + +#elif defined(ARDUINO_SAMD_ZERO) /* Put this last, otherwise overrides the Adafruit boards */ + #define WORKSPACESIZE 2816-SDSIZE /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 512 /* Bytes */ + #define CODESIZE 128 /* Bytes */ + #define SDCARD_SS_PIN 10 + #define STACKDIFF 320 + #define CPU_ATSAMD21 + +#elif defined(ARDUINO_BBC_MICROBIT) + #define WORKSPACESIZE 1280 /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 512 /* Bytes */ + #define CODESIZE 64 /* Bytes */ + #define STACKDIFF 320 + #define CPU_NRF51822 + +#elif defined(ARDUINO_CALLIOPE_MINI) + #define WORKSPACESIZE 3328 /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 512 /* Bytes */ + #define CODESIZE 64 /* Bytes */ + #define STACKDIFF 320 + #define CPU_NRF51822 + +#elif defined(ARDUINO_SINOBIT) + #define WORKSPACESIZE 1280 /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 512 /* Bytes */ + #define CODESIZE 64 /* Bytes */ + #define STACKDIFF 320 + #define CPU_NRF51822 + +#elif defined(ARDUINO_NRF52840_ITSYBITSY) + #define WORKSPACESIZE 20992-SDSIZE /* Objects (8*bytes) */ + #define DATAFLASHSIZE 2048000 /* 2 MBytes */ + #define SYMBOLTABLESIZE 1024 /* Bytes */ + #define CODESIZE 256 /* Bytes */ + #define STACKDIFF 1200 + #define CPU_NRF52840 + +#elif defined(ARDUINO_NRF52840_CLUE) + #define WORKSPACESIZE 19456-SDSIZE /* Objects (8*bytes) */ + #define DATAFLASHSIZE 2048000 /* 2 MBytes */ + #define SYMBOLTABLESIZE 1024 /* Bytes */ + #define CODESIZE 256 /* Bytes */ + #define STACKDIFF 0 + #define CPU_NRF52840 + +#elif defined(MAX32620) + #define WORKSPACESIZE 24576-SDSIZE /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 1024 /* Bytes */ + #define CODESIZE 256 /* Bytes */ + #define STACKDIFF 320 + +#elif defined(ARDUINO_FEATHER_F405) + #define WORKSPACESIZE 11840-SDSIZE /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 1024 /* Bytes */ + #define CODESIZE 256 /* Bytes */ + #define STACKDIFF 320 + +#elif defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) + #define WORKSPACESIZE 60000 /* Objects (8*bytes) */ + #define SYMBOLTABLESIZE 1024 /* Bytes */ + #define CODESIZE 256 /* Bytes */ + #define STACKDIFF 15000 + #define SDCARD_SS_PIN BUILTIN_SDCARD + #define BitOrder uint8_t + #undef RAMFUNC + #define RAMFUNC FASTRUN + #undef MEMBANK + #define MEMBANK DMAMEM + +#else +#error "Board not supported!" +#endif + +object Workspace[WORKSPACESIZE] WORDALIGNED MEMBANK; +char SymbolTable[SYMBOLTABLESIZE]; +#if defined(CODESIZE) +RAMFUNC uint8_t MyCode[CODESIZE] WORDALIGNED; +#endif + +// Global variables + +jmp_buf exception; +unsigned int Freespace = 0; +object *Freelist; +char *SymbolTop = SymbolTable; +unsigned int I2CCount; +unsigned int TraceFn[TRACEMAX]; +unsigned int TraceDepth[TRACEMAX]; + +object *GlobalEnv; +object *GCStack = NULL; +object *GlobalString; +int GlobalStringIndex = 0; +uint8_t PrintCount = 0; +uint8_t BreakLevel = 0; +char LastChar = 0; +char LastPrint = 0; + +// Flags +enum flag { PRINTREADABLY, RETURNFLAG, ESCAPE, EXITEDITOR, LIBRARYLOADED, NOESC }; +volatile char Flags = 0b00001; // PRINTREADABLY set by default + +// Forward references +object *tee; +object *tf_progn (object *form, object *env); +object *eval (object *form, object *env); +object *read (gfun_t gfun); +void repl (object *env); +void printobject (object *form, pfun_t pfun); +char *lookupbuiltin (symbol_t name); +intptr_t lookupfn (symbol_t name); +int builtin (char* n); + +// Error handling + +void errorsub (symbol_t fname, PGM_P string) { + pfl(pserial); pfstring(PSTR("Error: "), pserial); + if (fname) { + pserial('\''); + pstring(symbolname(fname), pserial); + pfstring(PSTR("' "), pserial); + } + pfstring(string, pserial); +} + +void error (symbol_t fname, PGM_P string, object *symbol) { + errorsub(fname, string); + pfstring(PSTR(": "), pserial); printobject(symbol, pserial); + pln(pserial); + GCStack = NULL; + longjmp(exception, 1); +} + +void error2 (symbol_t fname, PGM_P string) { + errorsub(fname, string); + pln(pserial); + GCStack = NULL; + longjmp(exception, 1); +} + +// Save space as these are used multiple times +const char notanumber[] PROGMEM = "argument is not a number"; +const char notaninteger[] PROGMEM = "argument is not an integer"; +const char notastring[] PROGMEM = "argument is not a string"; +const char notalist[] PROGMEM = "argument is not a list"; +const char notasymbol[] PROGMEM = "argument is not a symbol"; +const char notproper[] PROGMEM = "argument is not a proper list"; +const char toomanyargs[] PROGMEM = "too many arguments"; +const char toofewargs[] PROGMEM = "too few arguments"; +const char noargument[] PROGMEM = "missing argument"; +const char nostream[] PROGMEM = "missing stream argument"; +const char overflow[] PROGMEM = "arithmetic overflow"; +const char invalidarg[] PROGMEM = "invalid argument"; +const char invalidpin[] PROGMEM = "invalid pin"; +const char resultproper[] PROGMEM = "result is not a proper list"; +const char oddargs[] PROGMEM = "odd number of arguments"; + +// Set up workspace + +void initworkspace () { + Freelist = NULL; + for (int i=WORKSPACESIZE-1; i>=0; i--) { + object *obj = &Workspace[i]; + car(obj) = NULL; + cdr(obj) = Freelist; + Freelist = obj; + Freespace++; + } +} + +object *myalloc () { + if (Freespace == 0) error2(0, PSTR("no room")); + object *temp = Freelist; + Freelist = cdr(Freelist); + Freespace--; + return temp; +} + +inline void myfree (object *obj) { + car(obj) = NULL; + cdr(obj) = Freelist; + Freelist = obj; + Freespace++; +} + +// Make each type of object + +object *number (int n) { + object *ptr = myalloc(); + ptr->type = NUMBER; + ptr->integer = n; + return ptr; +} + +object *makefloat (float f) { + object *ptr = myalloc(); + ptr->type = FLOAT; + ptr->single_float = f; + return ptr; +} + +object *character (char c) { + object *ptr = myalloc(); + ptr->type = CHARACTER; + ptr->integer = c; + return ptr; +} + +object *cons (object *arg1, object *arg2) { + object *ptr = myalloc(); + ptr->car = arg1; + ptr->cdr = arg2; + return ptr; +} + +object *symbol (symbol_t name) { + object *ptr = myalloc(); + ptr->type = SYMBOL; + ptr->name = name; + return ptr; +} + +object *codehead (int entry) { + object *ptr = myalloc(); + ptr->type = CODE; + ptr->integer = entry; + return ptr; +} + +object *newsymbol (symbol_t name) { + for (int i=WORKSPACESIZE-1; i>=0; i--) { + object *obj = &Workspace[i]; + if (obj->type == SYMBOL && obj->name == name) return obj; + } + return symbol(name); +} + +object *stream (unsigned char streamtype, unsigned char address) { + object *ptr = myalloc(); + ptr->type = STREAM; + ptr->integer = streamtype<<8 | address; + return ptr; +} + +// Garbage collection + +void markobject (object *obj) { + MARK: + if (obj == NULL) return; + if (marked(obj)) return; + + object* arg = car(obj); + unsigned int type = obj->type; + mark(obj); + + if (type >= PAIR || type == ZZERO) { // cons + markobject(arg); + obj = cdr(obj); + goto MARK; + } + + if (type == ARRAY) { + obj = cdr(obj); + goto MARK; + } + + if (type == STRING) { + obj = cdr(obj); + while (obj != NULL) { + arg = car(obj); + mark(obj); + obj = arg; + } + } +} + +void sweep () { + Freelist = NULL; + Freespace = 0; + for (int i=WORKSPACESIZE-1; i>=0; i--) { + object *obj = &Workspace[i]; + if (!marked(obj)) myfree(obj); else unmark(obj); + } +} + +void gc (object *form, object *env) { + #if defined(printgcs) + int start = Freespace; + #endif + markobject(tee); + markobject(GlobalEnv); + markobject(GCStack); + markobject(form); + markobject(env); + sweep(); + #if defined(printgcs) + pfl(pserial); pserial('{'); pint(Freespace - start, pserial); pserial('}'); + #endif +} + +// Compact image + +void movepointer (object *from, object *to) { + for (int i=0; itype) & ~MARKBIT; + if (marked(obj) && (type >= ARRAY || type==ZZERO)) { + if (car(obj) == (object *)((uintptr_t)from | MARKBIT)) + car(obj) = (object *)((uintptr_t)to | MARKBIT); + if (cdr(obj) == from) cdr(obj) = to; + } + } + // Fix strings + for (int i=0; itype) & ~MARKBIT) == STRING) { + obj = cdr(obj); + while (obj != NULL) { + if (cdr(obj) == to) cdr(obj) = from; + obj = (object *)((uintptr_t)(car(obj)) & ~MARKBIT); + } + } + } +} + +uintptr_t compactimage (object **arg) { + markobject(tee); + markobject(GlobalEnv); + markobject(GCStack); + object *firstfree = Workspace; + while (marked(firstfree)) firstfree++; + object *obj = &Workspace[WORKSPACESIZE-1]; + while (firstfree < obj) { + if (marked(obj)) { + car(firstfree) = car(obj); + cdr(firstfree) = cdr(obj); + unmark(obj); + movepointer(obj, firstfree); + if (GlobalEnv == obj) GlobalEnv = firstfree; + if (GCStack == obj) GCStack = firstfree; + if (*arg == obj) *arg = firstfree; + while (marked(firstfree)) firstfree++; + } + obj--; + } + sweep(); + return firstfree - Workspace; +} + +// Make SD card filename + +char *MakeFilename (object *arg) { + char *buffer = SymbolTop; + int max = maxbuffer(buffer); + int i = 0; + do { + char c = nthchar(arg, i); + if (c == '\0') break; + buffer[i++] = c; + } while (i>8 & 0xFF); + file.write(data>>16 & 0xFF); file.write(data>>24 & 0xFF); +} +#elif defined(DATAFLASHSIZE) +// Winbond DataFlash support for Adafruit M4 Express boards +#define PAGEPROG 0x02 +#define READSTATUS 0x05 +#define READDATA 0x03 +#define WRITEENABLE 0x06 +#define BLOCK64K 0xD8 +#define READID 0x90 + +// Arduino pins used for dataflash +#if defined(ARDUINO_ITSYBITSY_M0) || defined(ARDUINO_SAMD_FEATHER_M0_EXPRESS) +const int sck = 38, ssel = 39, mosi = 37, miso = 36; +#elif defined(EXTERNAL_FLASH_USE_QSPI) +const int sck = PIN_QSPI_SCK, ssel = PIN_QSPI_CS, mosi = PIN_QSPI_IO0, miso = PIN_QSPI_IO1; +#endif + +boolean FlashSetup () { + uint8_t manID, devID; + digitalWrite(ssel, HIGH); pinMode(ssel, OUTPUT); + pinMode(sck, OUTPUT); + pinMode(mosi, OUTPUT); + pinMode(miso, INPUT); + digitalWrite(sck, LOW); digitalWrite(mosi, HIGH); + digitalWrite(ssel, LOW); + FlashWrite(READID); + for(uint8_t i=0; i<4; i++) manID = FlashRead(); + devID = FlashRead(); + digitalWrite(ssel, HIGH); + return (devID == 0x14 || devID == 0x15 || devID == 0x16); // Found correct device +} + +inline void FlashWrite (uint8_t data) { + shiftOut(mosi, sck, MSBFIRST, data); +} + +void FlashBusy () { + digitalWrite(ssel, 0); + FlashWrite(READSTATUS); + while (FlashRead() & 1 != 0); + digitalWrite(ssel, 1); +} + +void FlashWriteEnable () { + digitalWrite(ssel, 0); + FlashWrite(WRITEENABLE); + digitalWrite(ssel, 1); +} + +void FlashBeginRead () { + FlashBusy(); + digitalWrite(ssel, 0); + FlashWrite(READDATA); + FlashWrite(0); FlashWrite(0); FlashWrite(0); +} + +inline uint8_t FlashRead () { + int data; + return shiftIn(miso, sck, MSBFIRST); +} + +inline void FlashEndRead(void) { + digitalWrite(ssel, 1); +} + +void FlashBeginWrite (int blocks) { + // Erase 64K + for (int b=0; b>16); + FlashWrite((*addr)>>8); + FlashWrite(0); + } + FlashWrite(data); + (*addr)++; +} + +inline void FlashEndWrite (void) { + digitalWrite(ssel, 1); + FlashBusy(); +} + +void FlashWriteInt (uint32_t *addr, int data) { + FlashWriteByte(addr, data & 0xFF); FlashWriteByte(addr, data>>8 & 0xFF); + FlashWriteByte(addr, data>>16 & 0xFF); FlashWriteByte(addr, data>>24 & 0xFF); +} +#endif + +int saveimage (object *arg) { +#if defined(sdcardsupport) + unsigned int imagesize = compactimage(&arg); + SD.begin(SDCARD_SS_PIN); + File file; + if (stringp(arg)) { + file = SD.open(MakeFilename(arg), O_RDWR | O_CREAT | O_TRUNC); + arg = NULL; + } else if (arg == NULL || listp(arg)) file = SD.open("ULISP.IMG", O_RDWR | O_CREAT | O_TRUNC); + else error(SAVEIMAGE, invalidarg, arg); + if (!file) error2(SAVEIMAGE, PSTR("problem saving to SD card")); + SDWriteInt(file, (uintptr_t)arg); + SDWriteInt(file, imagesize); + SDWriteInt(file, (uintptr_t)GlobalEnv); + SDWriteInt(file, (uintptr_t)GCStack); + #if SYMBOLTABLESIZE > BUFFERSIZE + SDWriteInt(file, (uintptr_t)SymbolTop); + for (int i=0; i DATAFLASHSIZE) error(SAVEIMAGE, PSTR("image size too large"), number(imagesize)); + uint32_t addr = 0; + FlashBeginWrite((bytesneeded+65535)/65536); + FlashWriteInt(&addr, (uintptr_t)arg); + FlashWriteInt(&addr, imagesize); + FlashWriteInt(&addr, (uintptr_t)GlobalEnv); + FlashWriteInt(&addr, (uintptr_t)GCStack); + #if SYMBOLTABLESIZE > BUFFERSIZE + FlashWriteInt(&addr, (uintptr_t)SymbolTop); + for (int i=0; i BUFFERSIZE + SymbolTop = (char *)SDReadInt(file); + for (int i=0; i BUFFERSIZE + SymbolTop = (char *)FlashReadInt(); + for (int i=0; itype; + return type >= PAIR || type == ZZERO; +} + +bool atom (object *x) { + if (x == NULL) return true; + unsigned int type = x->type; + return type < PAIR && type != ZZERO; +} + +bool listp (object *x) { + if (x == NULL) return true; + unsigned int type = x->type; + return type >= PAIR || type == ZZERO; +} + +bool improperp (object *x) { + if (x == NULL) return false; + unsigned int type = x->type; + return type < PAIR && type != ZZERO; +} + +object *quote (object *arg) { + return cons(symbol(QUOTE), cons(arg,NULL)); +} + +// Radix 40 encoding + +#define MAXSYMBOL 4096000000 + +int toradix40 (char ch) { + if (ch == 0) return 0; + if (ch >= '0' && ch <= '9') return ch-'0'+30; + if (ch == '$') return 27; if (ch == '*') return 28; if (ch == '-') return 29; + ch = ch | 0x20; + if (ch >= 'a' && ch <= 'z') return ch-'a'+1; + return -1; // Invalid +} + +int fromradix40 (int n) { + if (n >= 1 && n <= 26) return 'a'+n-1; + if (n == 27) return '$'; if (n == 28) return '*'; if (n == 29) return '-'; + if (n >= 30 && n <= 39) return '0'+n-30; + return 0; +} + +int pack40 (char *buffer) { + int x = 0; + for (int i=0; i<6; i++) x = x * 40 + toradix40(buffer[i]); + return x; +} + +bool valid40 (char *buffer) { + for (int i=0; i<6; i++) if (toradix40(buffer[i]) == -1) return false; + return true; +} + +char *symbolname (symbol_t x) { + if (x < ENDFUNCTIONS) return lookupbuiltin(x); + else if (x >= MAXSYMBOL) return lookupsymbol(x); + char *buffer = SymbolTop; + buffer[3] = '\0'; buffer[4] = '\0'; buffer[5] = '\0'; buffer[6] = '\0'; + for (int n=5; n>=0; n--) { + buffer[n] = fromradix40(x % 40); + x = x / 40; + } + return buffer; +} + +int digitvalue (char d) { + if (d>='0' && d<='9') return d-'0'; + d = d | 0x20; + if (d>='a' && d<='f') return d-'a'+10; + return 16; +} + +int checkinteger (symbol_t name, object *obj) { + if (!integerp(obj)) error(name, notaninteger, obj); + return obj->integer; +} + +int checkbitvalue (symbol_t name, object *obj) { + if (!integerp(obj)) error(name, notaninteger, obj); + int n = obj->integer; + if (n & ~1) error(name, PSTR("argument is not a bit value"), obj); + return n; +} + +float checkintfloat (symbol_t name, object *obj){ + if (integerp(obj)) return obj->integer; + if (!floatp(obj)) error(name, notanumber, obj); + return obj->single_float; +} + +int checkchar (symbol_t name, object *obj) { + if (!characterp(obj)) error(name, PSTR("argument is not a character"), obj); + return obj->chars; +} + +int isstream (object *obj){ + if (!streamp(obj)) error(0, PSTR("not a stream"), obj); + return obj->integer; +} + +int issymbol (object *obj, symbol_t n) { + return symbolp(obj) && obj->name == n; +} + +void checkargs (symbol_t name, object *args) { + int nargs = listlength(name, args); + if (name >= ENDFUNCTIONS) error(0, PSTR("not valid here"), symbol(name)); + checkminmax(name, nargs); +} + +int eq (object *arg1, object *arg2) { + if (arg1 == arg2) return true; // Same object + if ((arg1 == nil) || (arg2 == nil)) return false; // Not both values + if (arg1->cdr != arg2->cdr) return false; // Different values + if (symbolp(arg1) && symbolp(arg2)) return true; // Same symbol + if (integerp(arg1) && integerp(arg2)) return true; // Same integer + if (floatp(arg1) && floatp(arg2)) return true; // Same float + if (characterp(arg1) && characterp(arg2)) return true; // Same character + return false; +} + +int listlength (symbol_t name, object *list) { + int length = 0; + while (list != NULL) { + if (improperp(list)) error2(name, notproper); + list = cdr(list); + length++; + } + return length; +} + +// Association lists + +object *assoc (object *key, object *list) { + while (list != NULL) { + if (improperp(list)) error(ASSOC, notproper, list); + object *pair = first(list); + if (!listp(pair)) error(ASSOC, PSTR("element is not a list"), pair); + if (pair != NULL && eq(key,car(pair))) return pair; + list = cdr(list); + } + return nil; +} + +object *delassoc (object *key, object **alist) { + object *list = *alist; + object *prev = NULL; + while (list != NULL) { + object *pair = first(list); + if (eq(key,car(pair))) { + if (prev == NULL) *alist = cdr(list); + else cdr(prev) = cdr(list); + return key; + } + prev = list; + list = cdr(list); + } + return nil; +} + +// Array utilities + +int nextpower2 (int n) { + n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; + n |= n >> 8; n |= n >> 16; n++; + return n<2 ? 2 : n; +} + +object *buildarray (int n, int s, object *def) { + int s2 = s>>1; + if (s2 == 1) { + if (n == 2) return cons(def, def); + else if (n == 1) return cons(def, NULL); + else return NULL; + } else if (n >= s2) return cons(buildarray(s2, s2, def), buildarray(n - s2, s2, def)); + else return cons(buildarray(n, s2, def), nil); +} + +object *makearray (symbol_t name, object *dims, object *def, bool bitp) { + int size = 1; + object *dimensions = dims; + while (dims != NULL) { + int d = car(dims)->integer; + if (d < 0) error2(MAKEARRAY, PSTR("dimension can't be negative")); + size = size * d; + dims = cdr(dims); + } + // Bit array identified by making first dimension negative + if (bitp) { size = (size + 31)/32; car(dimensions) = number(-(car(dimensions)->integer)); } + object *ptr = myalloc(); + ptr->type = ARRAY; + object *tree = nil; + if (size != 0) tree = buildarray(size, nextpower2(size), def); + ptr->cdr = cons(tree, dimensions); + return ptr; +} + +object **arrayref (object *array, int index, int size) { + int mask = nextpower2(size)>>1; + object **p = &car(cdr(array)); + while (mask) { + if ((index & mask) == 0) p = &(car(*p)); else p = &(cdr(*p)); + mask = mask>>1; + } + return p; +} + +object **getarray (symbol_t name, object *array, object *subs, object *env, int *bit) { + int index = 0, size = 1, s; + *bit = -1; + bool bitp = false; + object *dims = cddr(array); + while (dims != NULL && subs != NULL) { + int d = car(dims)->integer; + if (d < 0) { d = -d; bitp = true; } + if (env) s = checkinteger(name, eval(car(subs), env)); else s = checkinteger(name, car(subs)); + if (s < 0 || s >= d) error(name, PSTR("subscript out of range"), car(subs)); + size = size * d; + index = index * d + s; + dims = cdr(dims); subs = cdr(subs); + } + if (dims != NULL) error2(name, PSTR("too few subscripts")); + if (subs != NULL) error2(name, PSTR("too many subscripts")); + if (bitp) { + size = (size + 31)/32; + *bit = index & 0x1F; index = index>>5; + } + return arrayref(array, index, size); +} + +void rslice (object *array, int size, int slice, object *dims, object *args) { + int d = first(dims)->integer; + for (int i = 0; i < d; i++) { + int index = slice * d + i; + if (!consp(args)) error2(0, PSTR("initial contents don't match array type")); + if (cdr(dims) == NULL) { + object **p = arrayref(array, index, size); + *p = car(args); + } else rslice(array, size, index, cdr(dims), car(args)); + args = cdr(args); + } +} + +object *readarray (int d, object *args) { + object *list = args; + object *dims = NULL; object *head = NULL; + int size = 1; + for (int i = 0; i < d; i++) { + if (!listp(list)) error2(0, PSTR("initial contents don't match array type")); + int l = listlength(0, list); + if (dims == NULL) { dims = cons(number(l), NULL); head = dims; } + else { cdr(dims) = cons(number(l), NULL); dims = cdr(dims); } + size = size * l; + if (list != NULL) list = car(list); + } + object *array = makearray(0, head, NULL, false); + rslice(array, size, 0, head, args); + return array; +} + +object *readbitarray (gfun_t gfun) { + char ch = gfun(); + object *head = NULL; + object *tail = NULL; + while (!issp(ch) && ch != ')' && ch != '(') { + if (ch != '0' && ch != '1') error2(0, PSTR("illegal character in bit array")); + object *cell = cons(number(ch - '0'), NULL); + if (head == NULL) head = cell; + else tail->cdr = cell; + tail = cell; + ch = gfun(); + } + LastChar = ch; + int size = listlength(0, head); + object *array = makearray(0, cons(number(size), NULL), 0, true); + size = (size + 31) / 32; + int index = 0; + while (head != NULL) { + object **loc = arrayref(array, index>>5, size); + int bit = index & 0x1F; + *loc = number((((*loc)->integer) & ~(1<integer)<integer; + if (d < 0) d = -d; + for (int i = 0; i < d; i++) { + if (i && spaces) pfun(' '); + int index = slice * d + i; + if (cdr(dims) == NULL) { + if (bitp) pint(((*arrayref(array, index>>5, size))->integer)>>(index & 0x1f) & 1, pfun); + else printobject(*arrayref(array, index, size), pfun); + } else { pfun('('); pslice(array, size, index, cdr(dims), pfun, bitp); pfun(')'); } + } +} + +void printarray (object *array, pfun_t pfun) { + object *dimensions = cddr(array); + object *dims = dimensions; + bool bitp = false; + int size = 1, n = 0; + while (dims != NULL) { + int d = car(dims)->integer; + if (d < 0) { bitp = true; d = -d; } + size = size * d; + dims = cdr(dims); n++; + } + if (bitp) size = (size+31)/32; + pfun('#'); + if (n == 1 && bitp) { pfun('*'); pslice(array, size, -1, dimensions, pfun, bitp); } + else { + if (n > 1) { pint(n, pfun); pfun('A'); } + pfun('('); pslice(array, size, 0, dimensions, pfun, bitp); pfun(')'); + } +} + +// String utilities + +void indent (uint8_t spaces, char ch, pfun_t pfun) { + for (uint8_t i=0; itype = STRING; + GlobalString = NULL; + GlobalStringIndex = 0; + return string; +} + +void buildstring (char ch, int *chars, object **head) { + static object* tail; + static uint8_t shift; + if (*chars == 0) { + shift = (sizeof(int)-1)*8; + *chars = ch<car = cell; + cell->car = NULL; + cell->chars = *chars; + tail = cell; + } else { + shift = shift - 8; + *chars = *chars | ch<chars = *chars; + if (shift == 0) *chars = 0; + } +} + +object *readstring (char delim, gfun_t gfun) { + object *obj = myalloc(); + obj->type = STRING; + int ch = gfun(); + if (ch == -1) return nil; + object *head = NULL; + int chars = 0; + while ((ch != delim) && (ch != -1)) { + if (ch == '\\') ch = gfun(); + buildstring(ch, &chars, &head); + ch = gfun(); + } + obj->cdr = head; + return obj; +} + +int stringlength (object *form) { + int length = 0; + form = cdr(form); + while (form != NULL) { + int chars = form->chars; + for (int i=(sizeof(int)-1)*8; i>=0; i=i-8) { + if (chars>>i & 0xFF) length++; + } + form = car(form); + } + return length; +} + +char nthchar (object *string, int n) { + object *arg = cdr(string); + int top; + if (sizeof(int) == 4) { top = n>>2; n = 3 - (n&3); } + else { top = n>>1; n = 1 - (n&1); } + for (int i=0; ichars)>>(n*8) & 0xFF; +} + +int gstr () { + if (LastChar) { + char temp = LastChar; + LastChar = 0; + return temp; + } + char c = nthchar(GlobalString, GlobalStringIndex++); + if (c != 0) return c; + return '\n'; // -1? +} + +void pstr (char c) { + buildstring(c, &GlobalStringIndex, &GlobalString); +} + +// Lookup variable in environment + +object *value (symbol_t n, object *env) { + while (env != NULL) { + object *pair = car(env); + if (pair != NULL && car(pair)->name == n) return pair; + env = cdr(env); + } + return nil; +} + +bool boundp (object *var, object *env) { + symbol_t varname = var->name; + if (value(varname, env) != NULL) return true; + if (value(varname, GlobalEnv) != NULL) return true; + return false; +} + +object *findvalue (object *var, object *env) { + symbol_t varname = var->name; + object *pair = value(varname, env); + if (pair == NULL) pair = value(varname, GlobalEnv); + if (pair == NULL) error(0, PSTR("unknown variable"), var); + return pair; +} + +// Handling closures + +object *closure (int tc, symbol_t name, object *state, object *function, object *args, object **env) { + int trace = 0; + if (name) trace = tracing(name); + if (trace) { + indent(TraceDepth[trace-1]<<1, ' ', pserial); + pint(TraceDepth[trace-1]++, pserial); + pserial(':'); pserial(' '); pserial('('); pstring(symbolname(name), pserial); + } + object *params = first(function); + function = cdr(function); + // Dropframe + if (tc) { + if (*env != NULL && car(*env) == NULL) { + pop(*env); + while (*env != NULL && car(*env) != NULL) pop(*env); + } else push(nil, *env); + } + // Push state + while (state != NULL) { + object *pair = first(state); + push(pair, *env); + state = cdr(state); + } + // Add arguments to environment + bool optional = false; + while (params != NULL) { + object *value; + object *var = first(params); + if (symbolp(var) && var->name == OPTIONAL) optional = true; + else { + if (consp(var)) { + if (!optional) error(name, PSTR("invalid default value"), var); + if (args == NULL) value = eval(second(var), *env); + else { value = first(args); args = cdr(args); } + var = first(var); + if (!symbolp(var)) error(name, PSTR("illegal optional parameter"), var); + } else if (!symbolp(var)) { + error2(name, PSTR("illegal parameter")); + } else if (var->name == AMPREST) { + params = cdr(params); + var = first(params); + value = args; + args = NULL; + } else { + if (args == NULL) { + if (optional) value = nil; + else error2(name, toofewargs); + } else { value = first(args); args = cdr(args); } + } + push(cons(var,value), *env); + if (trace) { pserial(' '); printobject(value, pserial); } + } + params = cdr(params); + } + if (args != NULL) error2(name, toomanyargs); + if (trace) { pserial(')'); pln(pserial); } + // Do an implicit progn + if (tc) push(nil, *env); + return tf_progn(function, *env); +} + +object *apply (symbol_t name, object *function, object *args, object *env) { + if (symbolp(function)) { + symbol_t fname = function->name; + checkargs(fname, args); + return ((fn_ptr_type)lookupfn(fname))(args, env); + } + if (consp(function) && issymbol(car(function), LAMBDA)) { + function = cdr(function); + object *result = closure(0, 0, NULL, function, args, &env); + return eval(result, env); + } + if (consp(function) && issymbol(car(function), CLOSURE)) { + function = cdr(function); + object *result = closure(0, 0, car(function), cdr(function), args, &env); + return eval(result, env); + } + error(name, PSTR("illegal function"), function); + return NULL; +} + +// In-place operations + +object **place (symbol_t name, object *args, object *env, int *bit) { + *bit = -1; + if (atom(args)) return &cdr(findvalue(args, env)); + object* function = first(args); + if (symbolp(function)) { + symbol_t fname = function->name; + if (fname == CAR || fname == FIRST) { + object *value = eval(second(args), env); + if (!listp(value)) error(name, PSTR("can't take car"), value); + return &car(value); + } + if (fname == CDR || fname == REST) { + object *value = eval(second(args), env); + if (!listp(value)) error(name, PSTR("can't take cdr"), value); + return &cdr(value); + } + if (fname == NTH) { + int index = checkinteger(NTH, eval(second(args), env)); + object *list = eval(third(args), env); + if (atom(list)) error(name, PSTR("second argument to nth is not a list"), list); + while (index > 0) { + list = cdr(list); + if (list == NULL) error2(name, PSTR("index to nth is out of range")); + index--; + } + return &car(list); + } + if (fname == AREF) { + object *array = eval(second(args), env); + if (!arrayp(array)) error(AREF, PSTR("first argument is not an array"), array); + return getarray(AREF, array, cddr(args), env, bit); + } + } + error2(name, PSTR("illegal place")); + return nil; +} + +// Checked car and cdr + +object *carx (object *arg) { + if (!listp(arg)) error(0, PSTR("can't take car"), arg); + if (arg == nil) return nil; + return car(arg); +} + +object *cdrx (object *arg) { + if (!listp(arg)) error(0, PSTR("can't take cdr"), arg); + if (arg == nil) return nil; + return cdr(arg); +} + +// I2C interface + +void I2Cinit (bool enablePullup) { + (void) enablePullup; + Wire.begin(); +} + +int I2Cread () { + return Wire.read(); +} + +void I2Cwrite (uint8_t data) { + Wire.write(data); +} + +bool I2Cstart (uint8_t address, uint8_t read) { + int ok = true; + if (read == 0) { + Wire.beginTransmission(address); + ok = (Wire.endTransmission(true) == 0); + Wire.beginTransmission(address); + } + else Wire.requestFrom(address, I2CCount); + return ok; +} + +bool I2Crestart (uint8_t address, uint8_t read) { + int error = (Wire.endTransmission(false) != 0); + if (read == 0) Wire.beginTransmission(address); + else Wire.requestFrom(address, I2CCount); + return error ? false : true; +} + +void I2Cstop (uint8_t read) { + if (read == 0) Wire.endTransmission(); // Check for error? +} + +// Streams + +inline int spiread () { return SPI.transfer(0); } +#if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_GRAND_CENTRAL_M4) || defined(ARDUINO_PYBADGE_M4) || defined(ARDUINO_PYGAMER_M4) || defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) +inline int spi1read () { return SPI1.transfer(0); } +#endif +#if defined(ARDUINO_SAM_DUE) || defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) +inline int serial1read () { while (!Serial1.available()) testescape(); return Serial1.read(); } +inline int serial2read () { while (!Serial2.available()) testescape(); return Serial2.read(); } +inline int serial3read () { while (!Serial3.available()) testescape(); return Serial3.read(); } +#elif !defined(CPU_NRF51822) && !defined(ARDUINO_FEATHER_F405) +inline int serial1read () { while (!Serial1.available()) testescape(); return Serial1.read(); } +#endif +#if defined(sdcardsupport) +File SDpfile, SDgfile; +inline int SDread () { + if (LastChar) { + char temp = LastChar; + LastChar = 0; + return temp; + } + return SDgfile.read(); +} +#endif + +void serialbegin (int address, int baud) { + #if defined(CPU_NRF51822) || defined(ARDUINO_FEATHER_F405) + error(WITHSERIAL, PSTR("port not supported"), number(address)); + #elif defined(ARDUINO_SAM_DUE) || defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) + if (address == 1) Serial1.begin((long)baud*100); + else if (address == 2) Serial2.begin((long)baud*100); + else if (address == 3) Serial3.begin((long)baud*100); + else error(WITHSERIAL, PSTR("port not supported"), number(address)); + #else + if (address == 1) Serial1.begin((long)baud*100); + else error(WITHSERIAL, PSTR("port not supported"), number(address)); + #endif +} + +void serialend (int address) { + #if defined(ARDUINO_SAM_DUE) + if (address == 1) {Serial1.flush(); Serial1.end(); } + else if (address == 2) {Serial2.flush(); Serial2.end(); } + else if (address == 3) {Serial3.flush(); Serial3.end(); } + #elif !defined(CPU_NRF51822) && !defined(ARDUINO_FEATHER_F405) + if (address == 1) {Serial1.flush(); Serial1.end(); } + #endif +} + +gfun_t gstreamfun (object *args) { + int streamtype = SERIALSTREAM; + int address = 0; + gfun_t gfun = gserial; + if (args != NULL) { + int stream = isstream(first(args)); + streamtype = stream>>8; address = stream & 0xFF; + } + if (streamtype == I2CSTREAM) gfun = (gfun_t)I2Cread; + else if (streamtype == SPISTREAM) { + if (address < 128) gfun = spiread; + #if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_GRAND_CENTRAL_M4) || defined(ARDUINO_PYBADGE_M4) || defined(ARDUINO_PYGAMER_M4) + else gfun = spi1read; + #endif + } + else if (streamtype == SERIALSTREAM) { + if (address == 0) gfun = gserial; + #if defined(ARDUINO_SAM_DUE) + else if (address == 1) gfun = serial1read; + else if (address == 2) gfun = serial2read; + else if (address == 3) gfun = serial3read; + #elif !defined(CPU_NRF51822) && !defined(ARDUINO_FEATHER_F405) + else if (address == 1) gfun = serial1read; + #endif + } + #if defined(sdcardsupport) + else if (streamtype == SDSTREAM) gfun = (gfun_t)SDread; + #endif + else error2(0, PSTR("unknown stream type")); + return gfun; +} + +inline void spiwrite (char c) { SPI.transfer(c); } +#if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_GRAND_CENTRAL_M4) || defined(ARDUINO_PYBADGE_M4) || defined(ARDUINO_PYGAMER_M4) || defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) +inline void spi1write (char c) { SPI1.transfer(c); } +#endif +#if defined(ARDUINO_SAM_DUE) || defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) +inline void serial1write (char c) { Serial1.write(c); } +inline void serial2write (char c) { Serial2.write(c); } +inline void serial3write (char c) { Serial3.write(c); } +#elif !defined(CPU_NRF51822) +inline void serial1write (char c) { Serial1.write(c); } +#endif +#if defined(sdcardsupport) +inline void SDwrite (char c) { SDpfile.write(c); } +#endif +#if defined(gfxsupport) +inline void gfxwrite (char c) { tft.write(c); } +#endif + +pfun_t pstreamfun (object *args) { + int streamtype = SERIALSTREAM; + int address = 0; + pfun_t pfun = pserial; + if (args != NULL && first(args) != NULL) { + int stream = isstream(first(args)); + streamtype = stream>>8; address = stream & 0xFF; + } + if (streamtype == I2CSTREAM) pfun = (pfun_t)I2Cwrite; + else if (streamtype == SPISTREAM) { + if (address < 128) pfun = spiwrite; + #if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_GRAND_CENTRAL_M4) || defined(ARDUINO_PYBADGE_M4) || defined(ARDUINO_PYGAMER_M4)|| defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) + else pfun = spi1write; + #endif + } + else if (streamtype == SERIALSTREAM) { + if (address == 0) pfun = pserial; + #if defined(ARDUINO_SAM_DUE) || defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) + else if (address == 1) pfun = serial1write; + else if (address == 2) pfun = serial2write; + else if (address == 3) pfun = serial3write; + #elif !defined(CPU_NRF51822) && !defined(ARDUINO_FEATHER_F405) + else if (address == 1) pfun = serial1write; + #endif + } + else if (streamtype == STRINGSTREAM) { + pfun = pstr; + } + #if defined(sdcardsupport) + else if (streamtype == SDSTREAM) pfun = (pfun_t)SDwrite; + #endif + #if defined(gfxsupport) + else if (streamtype == GFXSTREAM) pfun = (pfun_t)gfxwrite; + #endif + else error2(0, PSTR("unknown stream type")); + return pfun; +} + +// Check pins + +void checkanalogread (int pin) { +#if defined(ARDUINO_SAM_DUE) + if (!(pin>=54 && pin<=65)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_SAMD_ZERO) + if (!(pin>=14 && pin<=19)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_SAMD_MKRZERO) + if (!(pin>=15 && pin<=21)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_ITSYBITSY_M0) + if (!(pin>=14 && pin<=25)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_GEMMA_M0) + if (!(pin>=8 && pin<=10)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_METRO_M4) + if (!(pin>=14 && pin<=21)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_ITSYBITSY_M4) + if (!(pin>=14 && pin<=19)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_FEATHER_M4) + if (!(pin>=14 && pin<=19)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_GRAND_CENTRAL_M4) + if (!((pin>=67 && pin<=74) || (pin>=54 && pin<=61))) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_BBC_MICROBIT) + if (!((pin>=0 && pin<=4) || pin==10)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_CALLIOPE_MINI) + if (!(pin==1 || pin==2 || (pin>=4 && pin<=6) || pin==21)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_SINOBIT) + if (!((pin>=0 && pin<=4) || pin==10)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_NRF52840_ITSYBITSY) + if (!(pin>=14 && pin<=20)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_NRF52840_CLUE) + if (!((pin>=0 && pin<=4) || pin==10 || pin==12 || pin==16)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(MAX32620) + if (!(pin>=49 && pin<=52)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_TEENSY40) + if (!((pin>=14 && pin<=27))) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_TEENSY41) + if (!((pin>=14 && pin<=27) || (pin>=38 && pin<=41))) error(ANALOGREAD, invalidpin, number(pin)); +#endif +} + +void checkanalogwrite (int pin) { +#if defined(ARDUINO_SAM_DUE) + if (!((pin>=2 && pin<=13) || pin==66 || pin==67)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_SAMD_ZERO) + if (!((pin>=3 && pin<=6) || (pin>=8 && pin<=13) || pin==14)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_SAMD_MKRZERO) + if (!((pin>=0 && pin<=8) || pin==10 || pin==18 || pin==19)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_ITSYBITSY_M0) + if (!((pin>=3 && pin<=6) || (pin>=8 && pin<=13) || (pin>=15 && pin<=16) || (pin>=22 && pin<=25))) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_GEMMA_M0) + if (!(pin==0 || pin==2 || pin==9 || pin==10)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_METRO_M4) + if (!(pin>=0 && pin<=15)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_ITSYBITSY_M4) + if (!(pin==0 || pin==1 || pin==4 || pin==5 || pin==7 || (pin>=9 && pin<=15) || pin==21 || pin==22)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_FEATHER_M4) + if (!(pin==0 || pin==1 || (pin>=4 && pin<=6) || (pin>=9 && pin<=13) || pin==14 || pin==15 || pin==17 || pin==21 || pin==22)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_GRAND_CENTRAL_M4) + if (!((pin>=2 && pin<=9) || pin==11 || (pin>=13 && pin<=45) || pin==48 || (pin>=50 && pin<=53) || pin==58 || pin==61 || pin==68 || pin==69)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_BBC_MICROBIT) + if (!(pin>=0 && pin<=32)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_CALLIOPE_MINI) + if (!(pin>=0 && pin<=30)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_SINOBIT) + if (!(pin>=0 && pin<=32)) error(ANALOGREAD, invalidpin, number(pin)); +#elif defined(ARDUINO_NRF52840_ITSYBITSY) + if (!(pin>=0 && pin<=25)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_NRF52840_CLUE) + if (!(pin>=0 && pin<=46)) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(MAX32620) + if (!((pin>=20 && pin<=29) || pin==32 || (pin>=40 && pin<=48))) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_TEENSY40) + if (!((pin>=0 && pin<=15) || (pin>=18 && pin<=19) || (pin>=22 && pin<=25) || (pin>=28 && pin<=29) || (pin>=33 && pin<=39))) error(ANALOGWRITE, invalidpin, number(pin)); +#elif defined(ARDUINO_TEENSY41) + if (!((pin>=0 && pin<=15) || (pin>=18 && pin<=19) || (pin>=22 && pin<=25) || (pin>=28 && pin<=29) || pin==33 || (pin>=36 && pin<=37))) error(ANALOGWRITE, invalidpin, number(pin)); +#endif +} + +// Note + +const int scale[] PROGMEM = {4186,4435,4699,4978,5274,5588,5920,6272,6645,7040,7459,7902}; + +void playnote (int pin, int note, int octave) { +#if defined(ARDUINO_NRF52840_CLUE) + int prescaler = 8 - octave - note/12; + if (prescaler<0 || prescaler>8) error(NOTE, PSTR("octave out of range"), number(prescaler)); + tone(pin, scale[note%12]>>prescaler); +#endif +} + +void nonote (int pin) { +#if defined(ARDUINO_NRF52840_CLUE) + noTone(pin); +#endif +} + +// Sleep + +#if defined(CPU_ATSAMD21) +void WDT_Handler(void) { + // ISR for watchdog early warning + WDT->CTRL.bit.ENABLE = 0; // Disable watchdog + while(WDT->STATUS.bit.SYNCBUSY); // Sync CTRL write + WDT->INTFLAG.bit.EW = 1; // Clear interrupt flag +} +#endif + +void initsleep () { +#if defined(CPU_ATSAMD21) + // One-time initialization of watchdog timer. + + // Generic clock generator 2, divisor = 32 (2^(DIV+1)) + GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(4); + // Enable clock generator 2 using low-power 32KHz oscillator. + // With /32 divisor above, this yields 1024Hz clock. + GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(2) | + GCLK_GENCTRL_GENEN | + GCLK_GENCTRL_SRC_OSCULP32K | + GCLK_GENCTRL_DIVSEL; + while(GCLK->STATUS.bit.SYNCBUSY); + // WDT clock = clock gen 2 + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_WDT | + GCLK_CLKCTRL_CLKEN | + GCLK_CLKCTRL_GEN_GCLK2; + + // Enable WDT early-warning interrupt + NVIC_DisableIRQ(WDT_IRQn); + NVIC_ClearPendingIRQ(WDT_IRQn); + NVIC_SetPriority(WDT_IRQn, 0); // Top priority + NVIC_EnableIRQ(WDT_IRQn); +#endif +} + +void sleep (int secs) { +#if defined(CPU_ATSAMD21) + WDT->CTRL.reg = 0; // Disable watchdog for config + while(WDT->STATUS.bit.SYNCBUSY); + WDT->INTENSET.bit.EW = 1; // Enable early warning interrupt + WDT->CONFIG.bit.PER = 0xB; // Period = max + WDT->CONFIG.bit.WINDOW = 0x7; // Set time of interrupt = 1024 cycles = 1 sec + WDT->CTRL.bit.WEN = 1; // Enable window mode + while(WDT->STATUS.bit.SYNCBUSY); // Sync CTRL write + + SysTick->CTRL = 0; // Stop SysTick interrupts + + while (secs > 0) { + WDT->CLEAR.reg = WDT_CLEAR_CLEAR_KEY;// Clear watchdog interval + while(WDT->STATUS.bit.SYNCBUSY); + WDT->CTRL.bit.ENABLE = 1; // Start watchdog now! + while(WDT->STATUS.bit.SYNCBUSY); + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; // Deepest sleep + __DSB(); + __WFI(); // Wait for interrupt + secs--; + } + SysTick->CTRL = 7; // Restart SysTick interrupts +#else + delay(1000*secs); +#endif +} + +// Prettyprint + +const int PPINDENT = 2; +const int PPWIDTH = 80; +const int GFXPPWIDTH = 52; // 320 pixel wide screen +int ppwidth = PPWIDTH; + +void pcount (char c) { + if (c == '\n') PrintCount++; + PrintCount++; +} + +uint8_t atomwidth (object *obj) { + PrintCount = 0; + printobject(obj, pcount); + return PrintCount; +} + +uint8_t hexwidth (object *obj) { + PrintCount = 0; + pinthex(obj->integer, pcount); + return PrintCount; +} + +boolean quoted (object *obj) { + return (consp(obj) && car(obj) != NULL && car(obj)->name == QUOTE && consp(cdr(obj)) && cddr(obj) == NULL); +} + +int subwidth (object *obj, int w) { + if (atom(obj)) return w - atomwidth(obj); + if (quoted(obj)) return subwidthlist(car(cdr(obj)), w - 1); + return subwidthlist(obj, w - 1); +} + +int subwidthlist (object *form, int w) { + while (form != NULL && w >= 0) { + if (atom(form)) return w - (2 + atomwidth(form)); + w = subwidth(car(form), w - 1); + form = cdr(form); + } + return w; +} + +void superprint (object *form, int lm, pfun_t pfun) { + if (atom(form)) { + if (symbolp(form) && form->name == NOTHING) pstring(symbolname(form->name), pfun); + else printobject(form, pfun); + } + else if (quoted(form)) { pfun('\''); superprint(car(cdr(form)), lm + 1, pfun); } + else if (subwidth(form, ppwidth - lm) >= 0) supersub(form, lm + PPINDENT, 0, pfun); + else supersub(form, lm + PPINDENT, 1, pfun); +} + +const int ppspecials = 18; +const char ppspecial[ppspecials] PROGMEM = + { DOTIMES, DOLIST, IF, SETQ, TEE, LET, LETSTAR, LAMBDA, WHEN, UNLESS, WITHI2C, WITHSERIAL, WITHSPI, WITHSDCARD, WITHGFX, WITHOUTPUTTOSTRING, FORMILLIS }; + +void supersub (object *form, int lm, int super, pfun_t pfun) { + int special = 0, separate = 1; + object *arg = car(form); + if (symbolp(arg)) { + int name = arg->name; + if (name == DEFUN || name == DEFCODE) special = 2; + else for (int i=0; iinteger; + else param[i] = (uintptr_t)arg; + args = cdr(args); + } + int w = ((intfn_ptr_type)&MyCode[entry])(param[0], param[1], param[2], param[3]); + return number(w); +#else + return nil; +#endif +} + +void putcode (object *arg, int origin, int pc) { +#if defined(CODESIZE) + int code = checkinteger(DEFCODE, arg); + MyCode[origin+pc] = code & 0xff; + MyCode[origin+pc+1] = (code>>8) & 0xff; + #if defined(assemblerlist) + printhex4(pc, pserial); + printhex4(code, pserial); + #endif +#endif +} + +int assemble (int pass, int origin, object *entries, object *env, object *pcpair) { + int pc = 0; cdr(pcpair) = number(pc); + while (entries != NULL) { + object *arg = first(entries); + if (symbolp(arg)) { + if (pass == 2) { + #if defined(assemblerlist) + printhex4(pc, pserial); + pfstring(PSTR(" "), pserial); + printobject(arg, pserial); pln(pserial); + #endif + } else { + object *pair = findvalue(arg, env); + cdr(pair) = number(pc); + } + } else { + object *argval = eval(arg, env); + if (listp(argval)) { + object *arglist = argval; + while (arglist != NULL) { + if (pass == 2) { + putcode(first(arglist), origin, pc); + #if defined(assemblerlist) + if (arglist == argval) superprint(arg, 0, pserial); + pln(pserial); + #endif + } + pc = pc + 2; + cdr(pcpair) = number(pc); + arglist = cdr(arglist); + } + } else if (integerp(argval)) { + if (pass == 2) { + putcode(argval, origin, pc); + #if defined(assemblerlist) + superprint(arg, 0, pserial); pln(pserial); + #endif + } + pc = pc + 2; + cdr(pcpair) = number(pc); + } else error(DEFCODE, PSTR("illegal entry"), arg); + } + entries = cdr(entries); + } + // Round up to multiple of 4 to give code size + if (pc%4 != 0) pc = pc + 4 - pc%4; + return pc; +} + +// Special forms + +object *sp_quote (object *args, object *env) { + (void) env; + checkargs(QUOTE, args); + return first(args); +} + +object *sp_defun (object *args, object *env) { + (void) env; + checkargs(DEFUN, args); + object *var = first(args); + if (!symbolp(var)) error(DEFUN, notasymbol, var); + object *val = cons(symbol(LAMBDA), cdr(args)); + object *pair = value(var->name,GlobalEnv); + if (pair != NULL) cdr(pair) = val; + else push(cons(var, val), GlobalEnv); + return var; +} + +object *sp_defvar (object *args, object *env) { + checkargs(DEFVAR, args); + object *var = first(args); + if (!symbolp(var)) error(DEFVAR, notasymbol, var); + object *val = NULL; + args = cdr(args); + if (args != NULL) { setflag(NOESC); val = eval(first(args), env); clrflag(NOESC); } + object *pair = value(var->name, GlobalEnv); + if (pair != NULL) cdr(pair) = val; + else push(cons(var, val), GlobalEnv); + return var; +} + +object *sp_setq (object *args, object *env) { + object *arg = nil; + while (args != NULL) { + if (cdr(args) == NULL) error2(SETQ, oddargs); + object *pair = findvalue(first(args), env); + arg = eval(second(args), env); + cdr(pair) = arg; + args = cddr(args); + } + return arg; +} + +object *sp_loop (object *args, object *env) { + object *start = args; + for (;;) { + args = start; + while (args != NULL) { + object *result = eval(car(args),env); + if (tstflag(RETURNFLAG)) { + clrflag(RETURNFLAG); + return result; + } + args = cdr(args); + } + } +} + +object *sp_return (object *args, object *env) { + object *result = eval(tf_progn(args,env), env); + setflag(RETURNFLAG); + return result; +} + +object *sp_push (object *args, object *env) { + int bit; + checkargs(PUSH, args); + object *item = eval(first(args), env); + object **loc = place(PUSH, second(args), env, &bit); + push(item, *loc); + return *loc; +} + +object *sp_pop (object *args, object *env) { + int bit; + checkargs(POP, args); + object **loc = place(POP, first(args), env, &bit); + object *result = car(*loc); + pop(*loc); + return result; +} + +// Accessors + +object *sp_incf (object *args, object *env) { + int bit; + checkargs(INCF, args); + object **loc = place(INCF, first(args), env, &bit); + args = cdr(args); + + object *x = *loc; + object *inc = (args != NULL) ? eval(first(args), env) : NULL; + + if (bit != -1) { + int increment; + if (inc == NULL) increment = 1; else increment = checkbitvalue(INCF, inc); + int newvalue = (((*loc)->integer)>>bit & 1) + increment; + + if (newvalue & ~1) error2(INCF, PSTR("result is not a bit value")); + *loc = number((((*loc)->integer) & ~(1<integer; + + if (inc == NULL) increment = 1; else increment = inc->integer; + + if (increment < 1) { + if (INT_MIN - increment > value) *loc = makefloat((float)value + (float)increment); + else *loc = number(value + increment); + } else { + if (INT_MAX - increment < value) *loc = makefloat((float)value + (float)increment); + else *loc = number(value + increment); + } + } else error2(INCF, notanumber); + return *loc; +} + +object *sp_decf (object *args, object *env) { + int bit; + checkargs(DECF, args); + object **loc = place(DECF, first(args), env, &bit); + args = cdr(args); + + object *x = *loc; + object *dec = (args != NULL) ? eval(first(args), env) : NULL; + + if (bit != -1) { + int decrement; + if (dec == NULL) decrement = 1; else decrement = checkbitvalue(DECF, dec); + int newvalue = (((*loc)->integer)>>bit & 1) - decrement; + + if (newvalue & ~1) error2(INCF, PSTR("result is not a bit value")); + *loc = number((((*loc)->integer) & ~(1<integer; + + if (dec == NULL) decrement = 1; else decrement = dec->integer; + + if (decrement < 1) { + if (INT_MAX + decrement < value) *loc = makefloat((float)value - (float)decrement); + else *loc = number(value - decrement); + } else { + if (INT_MIN + decrement > value) *loc = makefloat((float)value - (float)decrement); + else *loc = number(value - decrement); + } + } else error2(DECF, notanumber); + return *loc; +} + +object *sp_setf (object *args, object *env) { + int bit; + object *arg = nil; + while (args != NULL) { + if (cdr(args) == NULL) error2(SETF, oddargs); + object **loc = place(SETF, first(args), env, &bit); + arg = eval(second(args), env); + if (bit == -1) *loc = arg; + else *loc = number((checkinteger(SETF,*loc) & ~(1<name); + args = cdr(args); + } + int i = 0; + while (i < TRACEMAX) { + if (TraceFn[i] != 0) args = cons(symbol(TraceFn[i]), args); + i++; + } + return args; +} + +object *sp_untrace (object *args, object *env) { + (void) env; + if (args == NULL) { + int i = 0; + while (i < TRACEMAX) { + if (TraceFn[i] != 0) args = cons(symbol(TraceFn[i]), args); + TraceFn[i] = 0; + i++; + } + } else { + while (args != NULL) { + object *var = first(args); + if (!symbolp(var)) error(UNTRACE, notasymbol, var); + untrace(var->name); + args = cdr(args); + } + } + return args; +} + +object *sp_formillis (object *args, object *env) { + object *param = first(args); + unsigned long start = millis(); + unsigned long now, total = 0; + if (param != NULL) total = checkinteger(FORMILLIS, eval(first(param), env)); + eval(tf_progn(cdr(args),env), env); + do { + now = millis() - start; + testescape(); + } while (now < total); + if (now <= INT_MAX) return number(now); + return nil; +} + +object *sp_withoutputtostring (object *args, object *env) { + object *params = first(args); + if (params == NULL) error2(WITHOUTPUTTOSTRING, nostream); + object *var = first(params); + object *pair = cons(var, stream(STRINGSTREAM, 0)); + push(pair,env); + object *string = startstring(WITHOUTPUTTOSTRING); + object *forms = cdr(args); + eval(tf_progn(forms,env), env); + string->cdr = GlobalString; + GlobalString = NULL; + return string; +} + +object *sp_withserial (object *args, object *env) { + object *params = first(args); + if (params == NULL) error2(WITHSERIAL, nostream); + object *var = first(params); + int address = checkinteger(WITHSERIAL, eval(second(params), env)); + params = cddr(params); + int baud = 96; + if (params != NULL) baud = checkinteger(WITHSERIAL, eval(first(params), env)); + object *pair = cons(var, stream(SERIALSTREAM, address)); + push(pair,env); + serialbegin(address, baud); + object *forms = cdr(args); + object *result = eval(tf_progn(forms,env), env); + serialend(address); + return result; +} + +object *sp_withi2c (object *args, object *env) { + object *params = first(args); + if (params == NULL) error2(WITHI2C, nostream); + object *var = first(params); + int address = checkinteger(WITHI2C, eval(second(params), env)); + params = cddr(params); + int read = 0; // Write + I2CCount = 0; + if (params != NULL) { + object *rw = eval(first(params), env); + if (integerp(rw)) I2CCount = rw->integer; + read = (rw != NULL); + } + I2Cinit(1); // Pullups + object *pair = cons(var, (I2Cstart(address, read)) ? stream(I2CSTREAM, address) : nil); + push(pair,env); + object *forms = cdr(args); + object *result = eval(tf_progn(forms,env), env); + I2Cstop(read); + return result; +} + +object *sp_withspi (object *args, object *env) { + object *params = first(args); + if (params == NULL) error2(WITHSPI, nostream); + object *var = first(params); + params = cdr(params); + if (params == NULL) error2(WITHSPI, nostream); + int pin = checkinteger(WITHSPI, eval(car(params), env)); + pinMode(pin, OUTPUT); + digitalWrite(pin, HIGH); + params = cdr(params); + int clock = 4000, mode = SPI_MODE0, address = 0; // Defaults + BitOrder bitorder = MSBFIRST; + if (params != NULL) { + clock = checkinteger(WITHSPI, eval(car(params), env)); + params = cdr(params); + if (params != NULL) { + bitorder = (checkinteger(WITHSPI, eval(car(params), env)) == 0) ? LSBFIRST : MSBFIRST; + params = cdr(params); + if (params != NULL) { + int modeval = checkinteger(WITHSPI, eval(car(params), env)); + mode = (modeval == 3) ? SPI_MODE3 : (modeval == 2) ? SPI_MODE2 : (modeval == 1) ? SPI_MODE1 : SPI_MODE0; + params = cdr(params); + if (params != NULL) { + address = checkinteger(WITHSPI, eval(car(params), env)); + } + } + } + } + object *pair = cons(var, stream(SPISTREAM, pin + 128*address)); + push(pair,env); + SPIClass *spiClass = &SPI; + #if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_GRAND_CENTRAL_M4) || defined(ARDUINO_PYBADGE_M4) || defined(ARDUINO_PYGAMER_M4) || defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) + if (address == 1) spiClass = &SPI1; + #endif + (*spiClass).begin(); + (*spiClass).beginTransaction(SPISettings(((unsigned long)clock * 1000), bitorder, mode)); + digitalWrite(pin, LOW); + object *forms = cdr(args); + object *result = eval(tf_progn(forms,env), env); + digitalWrite(pin, HIGH); + (*spiClass).endTransaction(); + return result; +} + +object *sp_withsdcard (object *args, object *env) { +#if defined(sdcardsupport) + object *params = first(args); + if (params == NULL) error2(WITHSDCARD, nostream); + object *var = first(params); + object *filename = eval(second(params), env); + params = cddr(params); + SD.begin(SDCARD_SS_PIN); + int mode = 0; + if (params != NULL && first(params) != NULL) mode = checkinteger(WITHSDCARD, first(params)); + int oflag = O_READ; + if (mode == 1) oflag = O_RDWR | O_CREAT | O_APPEND; else if (mode == 2) oflag = O_RDWR | O_CREAT | O_TRUNC; + if (mode >= 1) { + SDpfile = SD.open(MakeFilename(filename), oflag); + if (!SDpfile) error2(WITHSDCARD, PSTR("problem writing to SD card")); + } else { + SDgfile = SD.open(MakeFilename(filename), oflag); + if (!SDgfile) error2(WITHSDCARD, PSTR("problem reading from SD card")); + } + object *pair = cons(var, stream(SDSTREAM, 1)); + push(pair,env); + object *forms = cdr(args); + object *result = eval(tf_progn(forms,env), env); + if (mode >= 1) SDpfile.close(); else SDgfile.close(); + return result; +#else + (void) args, (void) env; + error2(WITHSDCARD, PSTR("not supported")); + return nil; +#endif +} + +object *sp_withgfx (object *args, object *env) { +#if defined(gfxsupport) + object *params = first(args); + object *var = first(params); + object *pair = cons(var, stream(GFXSTREAM, 1)); + push(pair,env); + object *forms = cdr(args); + object *result = eval(tf_progn(forms,env), env); + return result; +#else + (void) args, (void) env; + error2(WITHGFX, PSTR("not supported")); + return nil; +#endif +} + +// Assembler + +object *sp_defcode (object *args, object *env) { +#if defined(CODESIZE) + setflag(NOESC); + checkargs(DEFCODE, args); + object *var = first(args); + object *params = second(args); + if (!symbolp(var)) error(DEFCODE, PSTR("not a symbol"), var); + + // Make parameters into synonyms for registers r0, r1, etc + int regn = 0; + while (params != NULL) { + if (regn > 3) error(DEFCODE, PSTR("more than 4 parameters"), var); + object *regpair = cons(car(params), newsymbol((18*40+30+regn)*2560000)); // Symbol for r0 etc + push(regpair,env); + regn++; + params = cdr(params); + } + + // Make *pc* a local variable + object *pcpair = cons(newsymbol(pack40((char*)"*pc*\0\0")), number(0)); + push(pcpair,env); + args = cdr(args); + + // Make labels into local variables + object *entries = cdr(args); + while (entries != NULL) { + object *arg = first(entries); + if (symbolp(arg)) { + object *pair = cons(arg,number(0)); + push(pair,env); + } + entries = cdr(entries); + } + + // First pass + int origin = 0; + int codesize = assemble(1, origin, cdr(args), env, pcpair); + + // See if it will fit + object *globals = GlobalEnv; + while (globals != NULL) { + object *pair = car(globals); + if (pair != NULL && car(pair) != var) { // Exclude me if I already exist + object *codeid = second(pair); + if (codeid->type == CODE) { + codesize = codesize + endblock(codeid) - startblock(codeid); + } + } + globals = cdr(globals); + } + if (codesize > CODESIZE) error(DEFCODE, PSTR("not enough room for code"), var); + + // Compact the code block, removing gaps + origin = 0; + object *block; + int smallest; + + do { + smallest = CODESIZE; + globals = GlobalEnv; + while (globals != NULL) { + object *pair = car(globals); + if (pair != NULL && car(pair) != var) { // Exclude me if I already exist + object *codeid = second(pair); + if (codeid->type == CODE) { + if (startblock(codeid) < smallest && startblock(codeid) >= origin) { + smallest = startblock(codeid); + block = codeid; + } + } + } + globals = cdr(globals); + } + + // Compact fragmentation if necessary + if (smallest == origin) origin = endblock(block); // No gap + else if (smallest < CODESIZE) { // Slide block down + int target = origin; + for (int i=startblock(block); iinteger = target<<16 | origin; + origin = target; + } + + } while (smallest < CODESIZE); + + // Second pass - origin is first free location + codesize = assemble(2, origin, cdr(args), env, pcpair); + + object *val = cons(codehead((origin+codesize)<<16 | origin), args); + object *pair = value(var->name, GlobalEnv); + if (pair != NULL) cdr(pair) = val; + else push(cons(var, val), GlobalEnv); + clrflag(NOESC); + return var; +#else + error2(DEFCODE, PSTR("not available")); + return nil; +#endif +} + +// Tail-recursive forms + +object *tf_progn (object *args, object *env) { + if (args == NULL) return nil; + object *more = cdr(args); + while (more != NULL) { + object *result = eval(car(args),env); + if (tstflag(RETURNFLAG)) return result; + args = more; + more = cdr(args); + } + return car(args); +} + +object *tf_if (object *args, object *env) { + if (args == NULL || cdr(args) == NULL) error2(IF, PSTR("missing argument(s)")); + if (eval(first(args), env) != nil) return second(args); + args = cddr(args); + return (args != NULL) ? first(args) : nil; +} + +object *tf_cond (object *args, object *env) { + while (args != NULL) { + object *clause = first(args); + if (!consp(clause)) error(COND, PSTR("illegal clause"), clause); + object *test = eval(first(clause), env); + object *forms = cdr(clause); + if (test != nil) { + if (forms == NULL) return quote(test); else return tf_progn(forms, env); + } + args = cdr(args); + } + return nil; +} + +object *tf_when (object *args, object *env) { + if (args == NULL) error2(WHEN, noargument); + if (eval(first(args), env) != nil) return tf_progn(cdr(args),env); + else return nil; +} + +object *tf_unless (object *args, object *env) { + if (args == NULL) error2(UNLESS, noargument); + if (eval(first(args), env) != nil) return nil; + else return tf_progn(cdr(args),env); +} + +object *tf_case (object *args, object *env) { + object *test = eval(first(args), env); + args = cdr(args); + while (args != NULL) { + object *clause = first(args); + if (!consp(clause)) error(CASE, PSTR("illegal clause"), clause); + object *key = car(clause); + object *forms = cdr(clause); + if (consp(key)) { + while (key != NULL) { + if (eq(test,car(key))) return tf_progn(forms, env); + key = cdr(key); + } + } else if (eq(test,key) || eq(key,tee)) return tf_progn(forms, env); + args = cdr(args); + } + return nil; +} + +object *tf_and (object *args, object *env) { + if (args == NULL) return tee; + object *more = cdr(args); + while (more != NULL) { + if (eval(car(args), env) == NULL) return nil; + args = more; + more = cdr(args); + } + return car(args); +} + +object *tf_or (object *args, object *env) { + while (args != NULL) { + if (eval(car(args), env) != NULL) return car(args); + args = cdr(args); + } + return nil; +} + +// Core functions + +object *fn_not (object *args, object *env) { + (void) env; + return (first(args) == nil) ? tee : nil; +} + +object *fn_cons (object *args, object *env) { + (void) env; + return cons(first(args), second(args)); +} + +object *fn_atom (object *args, object *env) { + (void) env; + return atom(first(args)) ? tee : nil; +} + +object *fn_listp (object *args, object *env) { + (void) env; + return listp(first(args)) ? tee : nil; +} + +object *fn_consp (object *args, object *env) { + (void) env; + return consp(first(args)) ? tee : nil; +} + +object *fn_symbolp (object *args, object *env) { + (void) env; + object *arg = first(args); + return symbolp(arg) ? tee : nil; +} + +object *fn_arrayp (object *args, object *env) { + (void) env; + return arrayp(first(args)) ? tee : nil; +} + +object *fn_boundp (object *args, object *env) { + (void) env; + object *var = first(args); + if (!symbolp(var)) error(BOUNDP, notasymbol, var); + return boundp(var, env) ? tee : nil; +} + +object *fn_setfn (object *args, object *env) { + object *arg = nil; + while (args != NULL) { + if (cdr(args) == NULL) error2(SETFN, oddargs); + object *pair = findvalue(first(args), env); + arg = second(args); + cdr(pair) = arg; + args = cddr(args); + } + return arg; +} + +object *fn_streamp (object *args, object *env) { + (void) env; + object *arg = first(args); + return streamp(arg) ? tee : nil; +} + +object *fn_eq (object *args, object *env) { + (void) env; + return eq(first(args), second(args)) ? tee : nil; +} + +// List functions + +object *fn_car (object *args, object *env) { + (void) env; + return carx(first(args)); +} + +object *fn_cdr (object *args, object *env) { + (void) env; + return cdrx(first(args)); +} + +object *fn_caar (object *args, object *env) { + (void) env; + return carx(carx(first(args))); +} + +object *fn_cadr (object *args, object *env) { + (void) env; + return carx(cdrx(first(args))); +} + +object *fn_cdar (object *args, object *env) { + (void) env; + return cdrx(carx(first(args))); +} + +object *fn_cddr (object *args, object *env) { + (void) env; + return cdrx(cdrx(first(args))); +} + +object *fn_caaar (object *args, object *env) { + (void) env; + return carx(carx(carx(first(args)))); +} + +object *fn_caadr (object *args, object *env) { + (void) env; + return carx(carx(cdrx(first(args)))); +} + +object *fn_cadar (object *args, object *env) { + (void) env; + return carx(cdrx(carx(first(args)))); +} + +object *fn_caddr (object *args, object *env) { + (void) env; + return carx(cdrx(cdrx(first(args)))); +} + +object *fn_cdaar (object *args, object *env) { + (void) env; + return cdrx(carx(carx(first(args)))); +} + +object *fn_cdadr (object *args, object *env) { + (void) env; + return cdrx(carx(cdrx(first(args)))); +} + +object *fn_cddar (object *args, object *env) { + (void) env; + return cdrx(cdrx(carx(first(args)))); +} + +object *fn_cdddr (object *args, object *env) { + (void) env; + return cdrx(cdrx(cdrx(first(args)))); +} + +object *fn_length (object *args, object *env) { + (void) env; + object *arg = first(args); + if (listp(arg)) return number(listlength(LENGTH, arg)); + if (stringp(arg)) return number(stringlength(arg)); + if (!(arrayp(arg) && cdr(cddr(arg)) == NULL)) error(LENGTH, PSTR("argument is not a list, 1d array, or string"), arg); + return number(-(first(cddr(arg))->integer)); +} + +object *fn_arraydimensions (object *args, object *env) { + object *array = first(args); + if (!arrayp(array)) error(ARRAYDIMENSIONS, PSTR("argument is not an array"), array); + object *dimensions = cddr(array); + return (first(dimensions)->integer < 0) ? cons(number(-(first(dimensions)->integer)), cdr(dimensions)) : dimensions; +} + +object *fn_list (object *args, object *env) { + (void) env; + return args; +} + +object *fn_makearray (object *args, object *env) { + (void) env; + object *def = nil; + bool bitp = false; + object *dims = first(args); + if (dims == NULL) error2(MAKEARRAY, PSTR("dimensions can't be nil")); + else if (atom(dims)) dims = cons(dims, NULL); + args = cdr(args); + while (args != NULL && cdr(args) != NULL) { + object *var = first(args); + if (issymbol(first(args), INITIALELEMENT)) def = second(args); + else if (issymbol(first(args), ELEMENTTYPE) && issymbol(second(args), BIT)) bitp = true; + else error(MAKEARRAY, PSTR("argument not recognised"), var); + args = cddr(args); + } + if (bitp) { + if (def == nil) def = number(0); + else def = number(-checkbitvalue(MAKEARRAY, def)); // 1 becomes all ones + } + return makearray(MAKEARRAY, dims, def, bitp); +} + +object *fn_reverse (object *args, object *env) { + (void) env; + object *list = first(args); + object *result = NULL; + while (list != NULL) { + if (improperp(list)) error(REVERSE, notproper, list); + push(first(list),result); + list = cdr(list); + } + return result; +} + +object *fn_nth (object *args, object *env) { + (void) env; + int n = checkinteger(NTH, first(args)); + object *list = second(args); + while (list != NULL) { + if (improperp(list)) error(NTH, notproper, list); + if (n == 0) return car(list); + list = cdr(list); + n--; + } + return nil; +} + +object *fn_aref (object *args, object *env) { + int bit; + object *array = first(args); + if (!arrayp(array)) error(AREF, PSTR("first argument is not an array"), array); + object *loc = *getarray(AREF, array, cdr(args), 0, &bit); + if (bit == -1) return loc; + else return number((loc->integer)>>bit & 1); +} + +object *fn_assoc (object *args, object *env) { + (void) env; + object *key = first(args); + object *list = second(args); + return assoc(key,list); +} + +object *fn_member (object *args, object *env) { + (void) env; + object *item = first(args); + object *list = second(args); + while (list != NULL) { + if (improperp(list)) error(MEMBER, notproper, list); + if (eq(item,car(list))) return list; + list = cdr(list); + } + return nil; +} + +object *fn_apply (object *args, object *env) { + object *previous = NULL; + object *last = args; + while (cdr(last) != NULL) { + previous = last; + last = cdr(last); + } + object *arg = car(last); + if (!listp(arg)) error(APPLY, notalist, arg); + cdr(previous) = arg; + return apply(APPLY, first(args), cdr(args), env); +} + +object *fn_funcall (object *args, object *env) { + return apply(FUNCALL, first(args), cdr(args), env); +} + +object *fn_append (object *args, object *env) { + (void) env; + object *head = NULL; + object *tail; + while (args != NULL) { + object *list = first(args); + if (!listp(list)) error(APPEND, notalist, list); + while (consp(list)) { + object *obj = cons(car(list), cdr(list)); + if (head == NULL) head = obj; + else cdr(tail) = obj; + tail = obj; + list = cdr(list); + if (cdr(args) != NULL && improperp(list)) error(APPEND, notproper, first(args)); + } + args = cdr(args); + } + return head; +} + +object *fn_mapc (object *args, object *env) { + object *function = first(args); + args = cdr(args); + object *result = first(args); + object *params = cons(NULL, NULL); + push(params,GCStack); + // Make parameters + while (true) { + object *tailp = params; + object *lists = args; + while (lists != NULL) { + object *list = car(lists); + if (list == NULL) { + pop(GCStack); + return result; + } + if (improperp(list)) error(MAPC, notproper, list); + object *obj = cons(first(list),NULL); + car(lists) = cdr(list); + cdr(tailp) = obj; tailp = obj; + lists = cdr(lists); + } + apply(MAPC, function, cdr(params), env); + } +} + +void mapcarfun (object *result, object **tail) { + object *obj = cons(result,NULL); + cdr(*tail) = obj; *tail = obj; +} + +void mapcanfun (object *result, object **tail) { + while (consp(result)) { + cdr(*tail) = result; *tail = result; + result = cdr(result); + } + if (result != NULL) error(MAPCAN, resultproper, result); +} + +object *mapcarcan (symbol_t name, object *args, object *env, mapfun_t fun) { + object *function = first(args); + args = cdr(args); + object *params = cons(NULL, NULL); + push(params,GCStack); + object *head = cons(NULL, NULL); + push(head,GCStack); + object *tail = head; + // Make parameters + while (true) { + object *tailp = params; + object *lists = args; + while (lists != NULL) { + object *list = car(lists); + if (list == NULL) { + pop(GCStack); + pop(GCStack); + return cdr(head); + } + if (improperp(list)) error(name, notproper, list); + object *obj = cons(first(list),NULL); + car(lists) = cdr(list); + cdr(tailp) = obj; tailp = obj; + lists = cdr(lists); + } + object *result = apply(name, function, cdr(params), env); + fun(result, &tail); + } +} + +object *fn_mapcar (object *args, object *env) { + return mapcarcan(MAPCAR, args, env, mapcarfun); +} + +object *fn_mapcan (object *args, object *env) { + return mapcarcan(MAPCAN, args, env, mapcanfun); +} + +// Arithmetic functions + +object *add_floats (object *args, float fresult) { + while (args != NULL) { + object *arg = car(args); + fresult = fresult + checkintfloat(ADD, arg); + args = cdr(args); + } + return makefloat(fresult); +} + +object *fn_add (object *args, object *env) { + (void) env; + int result = 0; + while (args != NULL) { + object *arg = car(args); + if (floatp(arg)) return add_floats(args, (float)result); + else if (integerp(arg)) { + int val = arg->integer; + if (val < 1) { if (INT_MIN - val > result) return add_floats(args, (float)result); } + else { if (INT_MAX - val < result) return add_floats(args, (float)result); } + result = result + val; + } else error(ADD, notanumber, arg); + args = cdr(args); + } + return number(result); +} + +object *subtract_floats (object *args, float fresult) { + while (args != NULL) { + object *arg = car(args); + fresult = fresult - checkintfloat(SUBTRACT, arg); + args = cdr(args); + } + return makefloat(fresult); +} + +object *negate (object *arg) { + if (integerp(arg)) { + int result = arg->integer; + if (result == INT_MIN) return makefloat(-result); + else return number(-result); + } else if (floatp(arg)) return makefloat(-(arg->single_float)); + else error(SUBTRACT, notanumber, arg); + return nil; +} + +object *fn_subtract (object *args, object *env) { + (void) env; + object *arg = car(args); + args = cdr(args); + if (args == NULL) return negate(arg); + else if (floatp(arg)) return subtract_floats(args, arg->single_float); + else if (integerp(arg)) { + int result = arg->integer; + while (args != NULL) { + arg = car(args); + if (floatp(arg)) return subtract_floats(args, result); + else if (integerp(arg)) { + int val = (car(args))->integer; + if (val < 1) { if (INT_MAX + val < result) return subtract_floats(args, result); } + else { if (INT_MIN + val > result) return subtract_floats(args, result); } + result = result - val; + } else error(SUBTRACT, notanumber, arg); + args = cdr(args); + } + return number(result); + } else error(SUBTRACT, notanumber, arg); + return nil; +} + +object *multiply_floats (object *args, float fresult) { + while (args != NULL) { + object *arg = car(args); + fresult = fresult * checkintfloat(MULTIPLY, arg); + args = cdr(args); + } + return makefloat(fresult); +} + +object *fn_multiply (object *args, object *env) { + (void) env; + int result = 1; + while (args != NULL){ + object *arg = car(args); + if (floatp(arg)) return multiply_floats(args, result); + else if (integerp(arg)) { + int64_t val = result * (int64_t)(arg->integer); + if ((val > INT_MAX) || (val < INT_MIN)) return multiply_floats(args, result); + result = val; + } else error(MULTIPLY, notanumber, arg); + args = cdr(args); + } + return number(result); +} + +object *divide_floats (object *args, float fresult) { + while (args != NULL) { + object *arg = car(args); + float f = checkintfloat(DIVIDE, arg); + if (f == 0.0) error2(DIVIDE, PSTR("division by zero")); + fresult = fresult / f; + args = cdr(args); + } + return makefloat(fresult); +} + +object *fn_divide (object *args, object *env) { + (void) env; + object* arg = first(args); + args = cdr(args); + // One argument + if (args == NULL) { + if (floatp(arg)) { + float f = arg->single_float; + if (f == 0.0) error2(DIVIDE, PSTR("division by zero")); + return makefloat(1.0 / f); + } else if (integerp(arg)) { + int i = arg->integer; + if (i == 0) error2(DIVIDE, PSTR("division by zero")); + else if (i == 1) return number(1); + else return makefloat(1.0 / i); + } else error(DIVIDE, notanumber, arg); + } + // Multiple arguments + if (floatp(arg)) return divide_floats(args, arg->single_float); + else if (integerp(arg)) { + int result = arg->integer; + while (args != NULL) { + arg = car(args); + if (floatp(arg)) { + return divide_floats(args, result); + } else if (integerp(arg)) { + int i = arg->integer; + if (i == 0) error2(DIVIDE, PSTR("division by zero")); + if ((result % i) != 0) return divide_floats(args, result); + if ((result == INT_MIN) && (i == -1)) return divide_floats(args, result); + result = result / i; + args = cdr(args); + } else error(DIVIDE, notanumber, arg); + } + return number(result); + } else error(DIVIDE, notanumber, arg); + return nil; +} + +object *fn_mod (object *args, object *env) { + (void) env; + object *arg1 = first(args); + object *arg2 = second(args); + if (integerp(arg1) && integerp(arg2)) { + int divisor = arg2->integer; + if (divisor == 0) error2(MOD, PSTR("division by zero")); + int dividend = arg1->integer; + int remainder = dividend % divisor; + if ((dividend<0) != (divisor<0)) remainder = remainder + divisor; + return number(remainder); + } else { + float fdivisor = checkintfloat(MOD, arg2); + if (fdivisor == 0.0) error2(MOD, PSTR("division by zero")); + float fdividend = checkintfloat(MOD, arg1); + float fremainder = fmod(fdividend , fdivisor); + if ((fdividend<0) != (fdivisor<0)) fremainder = fremainder + fdivisor; + return makefloat(fremainder); + } +} + +object *fn_oneplus (object *args, object *env) { + (void) env; + object* arg = first(args); + if (floatp(arg)) return makefloat((arg->single_float) + 1.0); + else if (integerp(arg)) { + int result = arg->integer; + if (result == INT_MAX) return makefloat((arg->integer) + 1.0); + else return number(result + 1); + } else error(ONEPLUS, notanumber, arg); + return nil; +} + +object *fn_oneminus (object *args, object *env) { + (void) env; + object* arg = first(args); + if (floatp(arg)) return makefloat((arg->single_float) - 1.0); + else if (integerp(arg)) { + int result = arg->integer; + if (result == INT_MIN) return makefloat((arg->integer) - 1.0); + else return number(result - 1); + } else error(ONEMINUS, notanumber, arg); + return nil; +} + +object *fn_abs (object *args, object *env) { + (void) env; + object *arg = first(args); + if (floatp(arg)) return makefloat(abs(arg->single_float)); + else if (integerp(arg)) { + int result = arg->integer; + if (result == INT_MIN) return makefloat(abs((float)result)); + else return number(abs(result)); + } else error(ABS, notanumber, arg); + return nil; +} + +object *fn_random (object *args, object *env) { + (void) env; + object *arg = first(args); + if (integerp(arg)) return number(random(arg->integer)); + else if (!floatp(arg)) return makefloat((float)rand()/(float)(RAND_MAX/(arg->single_float))); + else error(RANDOM, notanumber, arg); + return nil; +} + +object *fn_maxfn (object *args, object *env) { + (void) env; + object* result = first(args); + args = cdr(args); + while (args != NULL) { + object *arg = car(args); + if (integerp(result) && integerp(arg)) { + if ((arg->integer) > (result->integer)) result = arg; + } else if ((checkintfloat(MAXFN, arg) > checkintfloat(MAXFN, result))) result = arg; + args = cdr(args); + } + return result; +} + +object *fn_minfn (object *args, object *env) { + (void) env; + object* result = first(args); + args = cdr(args); + while (args != NULL) { + object *arg = car(args); + if (integerp(result) && integerp(arg)) { + if ((arg->integer) < (result->integer)) result = arg; + } else if ((checkintfloat(MINFN, arg) < checkintfloat(MINFN, result))) result = arg; + args = cdr(args); + } + return result; +} + +// Arithmetic comparisons + +object *fn_noteq (object *args, object *env) { + (void) env; + while (args != NULL) { + object *nargs = args; + object *arg1 = first(nargs); + nargs = cdr(nargs); + while (nargs != NULL) { + object *arg2 = first(nargs); + if (integerp(arg1) && integerp(arg2)) { + if ((arg1->integer) == (arg2->integer)) return nil; + } else if ((checkintfloat(NOTEQ, arg1) == checkintfloat(NOTEQ, arg2))) return nil; + nargs = cdr(nargs); + } + args = cdr(args); + } + return tee; +} + +object *fn_numeq (object *args, object *env) { + (void) env; + object *arg1 = first(args); + args = cdr(args); + while (args != NULL) { + object *arg2 = first(args); + if (integerp(arg1) && integerp(arg2)) { + if (!((arg1->integer) == (arg2->integer))) return nil; + } else if (!(checkintfloat(NUMEQ, arg1) == checkintfloat(NUMEQ, arg2))) return nil; + arg1 = arg2; + args = cdr(args); + } + return tee; +} + +object *fn_less (object *args, object *env) { + (void) env; + object *arg1 = first(args); + args = cdr(args); + while (args != NULL) { + object *arg2 = first(args); + if (integerp(arg1) && integerp(arg2)) { + if (!((arg1->integer) < (arg2->integer))) return nil; + } else if (!(checkintfloat(LESS, arg1) < checkintfloat(LESS, arg2))) return nil; + arg1 = arg2; + args = cdr(args); + } + return tee; +} + +object *fn_lesseq (object *args, object *env) { + (void) env; + object *arg1 = first(args); + args = cdr(args); + while (args != NULL) { + object *arg2 = first(args); + if (integerp(arg1) && integerp(arg2)) { + if (!((arg1->integer) <= (arg2->integer))) return nil; + } else if (!(checkintfloat(LESSEQ, arg1) <= checkintfloat(LESSEQ, arg2))) return nil; + arg1 = arg2; + args = cdr(args); + } + return tee; +} + +object *fn_greater (object *args, object *env) { + (void) env; + object *arg1 = first(args); + args = cdr(args); + while (args != NULL) { + object *arg2 = first(args); + if (integerp(arg1) && integerp(arg2)) { + if (!((arg1->integer) > (arg2->integer))) return nil; + } else if (!(checkintfloat(GREATER, arg1) > checkintfloat(GREATER, arg2))) return nil; + arg1 = arg2; + args = cdr(args); + } + return tee; +} + +object *fn_greatereq (object *args, object *env) { + (void) env; + object *arg1 = first(args); + args = cdr(args); + while (args != NULL) { + object *arg2 = first(args); + if (integerp(arg1) && integerp(arg2)) { + if (!((arg1->integer) >= (arg2->integer))) return nil; + } else if (!(checkintfloat(GREATEREQ, arg1) >= checkintfloat(GREATEREQ, arg2))) return nil; + arg1 = arg2; + args = cdr(args); + } + return tee; +} + +object *fn_plusp (object *args, object *env) { + (void) env; + object *arg = first(args); + if (floatp(arg)) return ((arg->single_float) > 0.0) ? tee : nil; + else if (integerp(arg)) return ((arg->integer) > 0) ? tee : nil; + else error(PLUSP, notanumber, arg); + return nil; +} + +object *fn_minusp (object *args, object *env) { + (void) env; + object *arg = first(args); + if (floatp(arg)) return ((arg->single_float) < 0.0) ? tee : nil; + else if (integerp(arg)) return ((arg->integer) < 0) ? tee : nil; + else error(MINUSP, notanumber, arg); + return nil; +} + +object *fn_zerop (object *args, object *env) { + (void) env; + object *arg = first(args); + if (floatp(arg)) return ((arg->single_float) == 0.0) ? tee : nil; + else if (integerp(arg)) return ((arg->integer) == 0) ? tee : nil; + else error(ZEROP, notanumber, arg); + return nil; +} + +object *fn_oddp (object *args, object *env) { + (void) env; + int arg = checkinteger(ODDP, first(args)); + return ((arg & 1) == 1) ? tee : nil; +} + +object *fn_evenp (object *args, object *env) { + (void) env; + int arg = checkinteger(EVENP, first(args)); + return ((arg & 1) == 0) ? tee : nil; +} + +// Number functions + +object *fn_integerp (object *args, object *env) { + (void) env; + return integerp(first(args)) ? tee : nil; +} + +object *fn_numberp (object *args, object *env) { + (void) env; + object *arg = first(args); + return (integerp(arg) || floatp(arg)) ? tee : nil; +} + +// Floating-point functions + +object *fn_floatfn (object *args, object *env) { + (void) env; + object *arg = first(args); + return (floatp(arg)) ? arg : makefloat((float)(arg->integer)); +} + +object *fn_floatp (object *args, object *env) { + (void) env; + return floatp(first(args)) ? tee : nil; +} + +object *fn_sin (object *args, object *env) { + (void) env; + return makefloat(sin(checkintfloat(SIN, first(args)))); +} + +object *fn_cos (object *args, object *env) { + (void) env; + return makefloat(cos(checkintfloat(COS, first(args)))); +} + +object *fn_tan (object *args, object *env) { + (void) env; + return makefloat(tan(checkintfloat(TAN, first(args)))); +} + +object *fn_asin (object *args, object *env) { + (void) env; + return makefloat(asin(checkintfloat(ASIN, first(args)))); +} + +object *fn_acos (object *args, object *env) { + (void) env; + return makefloat(acos(checkintfloat(ACOS, first(args)))); +} + +object *fn_atan (object *args, object *env) { + (void) env; + object *arg = first(args); + float div = 1.0; + args = cdr(args); + if (args != NULL) div = checkintfloat(ATAN, first(args)); + return makefloat(atan2(checkintfloat(ATAN, arg), div)); +} + +object *fn_sinh (object *args, object *env) { + (void) env; + return makefloat(sinh(checkintfloat(SINH, first(args)))); +} + +object *fn_cosh (object *args, object *env) { + (void) env; + return makefloat(cosh(checkintfloat(COSH, first(args)))); +} + +object *fn_tanh (object *args, object *env) { + (void) env; + return makefloat(tanh(checkintfloat(TANH, first(args)))); +} + +object *fn_exp (object *args, object *env) { + (void) env; + return makefloat(exp(checkintfloat(EXP, first(args)))); +} + +object *fn_sqrt (object *args, object *env) { + (void) env; + return makefloat(sqrt(checkintfloat(SQRT, first(args)))); +} + +object *fn_log (object *args, object *env) { + (void) env; + object *arg = first(args); + float fresult = log(checkintfloat(LOG, arg)); + args = cdr(args); + if (args == NULL) return makefloat(fresult); + else return makefloat(fresult / log(checkintfloat(LOG, first(args)))); +} + +int intpower (int base, int exp) { + int result = 1; + while (exp) { + if (exp & 1) result = result * base; + exp = exp / 2; + base = base * base; + } + return result; +} + +object *fn_expt (object *args, object *env) { + (void) env; + object *arg1 = first(args); object *arg2 = second(args); + float float1 = checkintfloat(EXPT, arg1); + float value = log(abs(float1)) * checkintfloat(EXPT, arg2); + if (integerp(arg1) && integerp(arg2) && ((arg2->integer) > 0) && (abs(value) < 21.4875)) + return number(intpower(arg1->integer, arg2->integer)); + if (float1 < 0) error2(EXPT, PSTR("invalid result")); + return makefloat(exp(value)); +} + +object *fn_ceiling (object *args, object *env) { + (void) env; + object *arg = first(args); + args = cdr(args); + if (args != NULL) return number(ceil(checkintfloat(CEILING, arg) / checkintfloat(CEILING, first(args)))); + else return number(ceil(checkintfloat(CEILING, arg))); +} + +object *fn_floor (object *args, object *env) { + (void) env; + object *arg = first(args); + args = cdr(args); + if (args != NULL) return number(floor(checkintfloat(FLOOR, arg) / checkintfloat(FLOOR, first(args)))); + else return number(floor(checkintfloat(FLOOR, arg))); +} + +object *fn_truncate (object *args, object *env) { + (void) env; + object *arg = first(args); + args = cdr(args); + if (args != NULL) return number((int)(checkintfloat(TRUNCATE, arg) / checkintfloat(TRUNCATE, first(args)))); + else return number((int)(checkintfloat(TRUNCATE, arg))); +} + +int myround (float number) { + return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); +} + +object *fn_round (object *args, object *env) { + (void) env; + object *arg = first(args); + args = cdr(args); + if (args != NULL) return number(myround(checkintfloat(ROUND, arg) / checkintfloat(ROUND, first(args)))); + else return number(myround(checkintfloat(ROUND, arg))); +} + +// Characters + +object *fn_char (object *args, object *env) { + (void) env; + object *arg = first(args); + if (!stringp(arg)) error(CHAR, notastring, arg); + char c = nthchar(arg, checkinteger(CHAR, second(args))); + if (c == 0) error2(CHAR, PSTR("index out of range")); + return character(c); +} + +object *fn_charcode (object *args, object *env) { + (void) env; + return number(checkchar(CHARCODE, first(args))); +} + +object *fn_codechar (object *args, object *env) { + (void) env; + return character(checkinteger(CODECHAR, first(args))); +} + +object *fn_characterp (object *args, object *env) { + (void) env; + return characterp(first(args)) ? tee : nil; +} + +// Strings + +object *fn_stringp (object *args, object *env) { + (void) env; + return stringp(first(args)) ? tee : nil; +} + +bool stringcompare (symbol_t name, object *args, bool lt, bool gt, bool eq) { + object *arg1 = first(args); if (!stringp(arg1)) error(name, notastring, arg1); + object *arg2 = second(args); if (!stringp(arg2)) error(name, notastring, arg2); + arg1 = cdr(arg1); + arg2 = cdr(arg2); + while ((arg1 != NULL) || (arg2 != NULL)) { + if (arg1 == NULL) return lt; + if (arg2 == NULL) return gt; + if (arg1->chars < arg2->chars) return lt; + if (arg1->chars > arg2->chars) return gt; + arg1 = car(arg1); + arg2 = car(arg2); + } + return eq; +} + +object *fn_stringeq (object *args, object *env) { + (void) env; + return stringcompare(STRINGEQ, args, false, false, true) ? tee : nil; +} + +object *fn_stringless (object *args, object *env) { + (void) env; + return stringcompare(STRINGLESS, args, true, false, false) ? tee : nil; +} + +object *fn_stringgreater (object *args, object *env) { + (void) env; + return stringcompare(STRINGGREATER, args, false, true, false) ? tee : nil; +} + +object *fn_sort (object *args, object *env) { + if (first(args) == NULL) return nil; + object *list = cons(nil,first(args)); + push(list,GCStack); + object *predicate = second(args); + object *compare = cons(NULL, cons(NULL, NULL)); + push(compare,GCStack); + object *ptr = cdr(list); + while (cdr(ptr) != NULL) { + object *go = list; + while (go != ptr) { + car(compare) = car(cdr(ptr)); + car(cdr(compare)) = car(cdr(go)); + if (apply(SORT, predicate, compare, env)) break; + go = cdr(go); + } + if (go != ptr) { + object *obj = cdr(ptr); + cdr(ptr) = cdr(obj); + cdr(obj) = cdr(go); + cdr(go) = obj; + } else ptr = cdr(ptr); + } + pop(GCStack); pop(GCStack); + return cdr(list); +} + +object *fn_stringfn (object *args, object *env) { + (void) env; + object *arg = first(args); + int type = arg->type; + if (type == STRING) return arg; + object *obj = myalloc(); + obj->type = STRING; + if (type == CHARACTER) { + object *cell = myalloc(); + cell->car = NULL; + uint8_t shift = (sizeof(int)-1)*8; + cell->chars = (arg->chars)<cdr = cell; + } else if (type == SYMBOL) { + char *s = symbolname(arg->name); + char ch = *s++; + object *head = NULL; + int chars = 0; + while (ch) { + if (ch == '\\') ch = *s++; + buildstring(ch, &chars, &head); + ch = *s++; + } + obj->cdr = head; + } else error(STRINGFN, PSTR("can't convert to string"), arg); + return obj; +} + +object *fn_concatenate (object *args, object *env) { + (void) env; + object *arg = first(args); + symbol_t name = arg->name; + if (name != STRINGFN) error2(CONCATENATE, PSTR("only supports strings")); + args = cdr(args); + object *result = myalloc(); + result->type = STRING; + object *head = NULL; + int chars = 0; + while (args != NULL) { + object *obj = first(args); + if (!stringp(obj)) error(CONCATENATE, notastring, obj); + obj = cdr(obj); + while (obj != NULL) { + int quad = obj->chars; + while (quad != 0) { + char ch = quad>>((sizeof(int)-1)*8) & 0xFF; + buildstring(ch, &chars, &head); + quad = quad<<8; + } + obj = car(obj); + } + args = cdr(args); + } + result->cdr = head; + return result; +} + +object *fn_subseq (object *args, object *env) { + (void) env; + object *arg = first(args); + if (!stringp(arg)) error(SUBSEQ, notastring, arg); + int start = checkinteger(SUBSEQ, second(args)); + int end; + args = cddr(args); + if (args != NULL) end = checkinteger(SUBSEQ, car(args)); else end = stringlength(arg); + object *result = myalloc(); + result->type = STRING; + object *head = NULL; + int chars = 0; + for (int i=start; icdr = head; + return result; +} + +object *fn_readfromstring (object *args, object *env) { + (void) env; + object *arg = first(args); + if (!stringp(arg)) error(READFROMSTRING, notastring, arg); + GlobalString = arg; + GlobalStringIndex = 0; + return read(gstr); +} + +object *fn_princtostring (object *args, object *env) { + (void) env; + object *arg = first(args); + object *obj = startstring(PRINCTOSTRING); + prin1object(arg, pstr); + obj->cdr = GlobalString; + return obj; +} + +object *fn_prin1tostring (object *args, object *env) { + (void) env; + object *arg = first(args); + object *obj = startstring(PRIN1TOSTRING); + printobject(arg, pstr); + obj->cdr = GlobalString; + return obj; +} + +// Bitwise operators + +object *fn_logand (object *args, object *env) { + (void) env; + int result = -1; + while (args != NULL) { + result = result & checkinteger(LOGAND, first(args)); + args = cdr(args); + } + return number(result); +} + +object *fn_logior (object *args, object *env) { + (void) env; + int result = 0; + while (args != NULL) { + result = result | checkinteger(LOGIOR, first(args)); + args = cdr(args); + } + return number(result); +} + +object *fn_logxor (object *args, object *env) { + (void) env; + int result = 0; + while (args != NULL) { + result = result ^ checkinteger(LOGXOR, first(args)); + args = cdr(args); + } + return number(result); +} + +object *fn_lognot (object *args, object *env) { + (void) env; + int result = checkinteger(LOGNOT, car(args)); + return number(~result); +} + +object *fn_ash (object *args, object *env) { + (void) env; + int value = checkinteger(ASH, first(args)); + int count = checkinteger(ASH, second(args)); + if (count >= 0) return number(value << count); + else return number(value >> abs(count)); +} + +object *fn_logbitp (object *args, object *env) { + (void) env; + int index = checkinteger(LOGBITP, first(args)); + int value = checkinteger(LOGBITP, second(args)); + return (bitRead(value, index) == 1) ? tee : nil; +} + +// System functions + +object *fn_eval (object *args, object *env) { + return eval(first(args), env); +} + +object *fn_globals (object *args, object *env) { + (void) args; + if (GlobalEnv == NULL) return nil; + return fn_mapcar(cons(symbol(CAR),cons(GlobalEnv,nil)), env); +} + +object *fn_locals (object *args, object *env) { + (void) args; + return env; +} + +object *fn_makunbound (object *args, object *env) { + (void) env; + object *var = first(args); + if (!symbolp(var)) error(MAKUNBOUND, notasymbol, var); + delassoc(var, &GlobalEnv); + return var; +} + +object *fn_break (object *args, object *env) { + (void) args; + pfstring(PSTR("\nBreak!\n"), pserial); + BreakLevel++; + repl(env); + BreakLevel--; + return nil; +} + +object *fn_read (object *args, object *env) { + (void) env; + gfun_t gfun = gstreamfun(args); + return read(gfun); +} + +object *fn_prin1 (object *args, object *env) { + (void) env; + object *obj = first(args); + pfun_t pfun = pstreamfun(cdr(args)); + printobject(obj, pfun); + return obj; +} + +object *fn_print (object *args, object *env) { + (void) env; + object *obj = first(args); + pfun_t pfun = pstreamfun(cdr(args)); + pln(pfun); + printobject(obj, pfun); + pfun(' '); + return obj; +} + +object *fn_princ (object *args, object *env) { + (void) env; + object *obj = first(args); + pfun_t pfun = pstreamfun(cdr(args)); + prin1object(obj, pfun); + return obj; +} + +object *fn_terpri (object *args, object *env) { + (void) env; + pfun_t pfun = pstreamfun(args); + pln(pfun); + return nil; +} + +object *fn_readbyte (object *args, object *env) { + (void) env; + gfun_t gfun = gstreamfun(args); + int c = gfun(); + return (c == -1) ? nil : number(c); +} + +object *fn_readline (object *args, object *env) { + (void) env; + gfun_t gfun = gstreamfun(args); + return readstring('\n', gfun); +} + +object *fn_writebyte (object *args, object *env) { + (void) env; + int value = checkinteger(WRITEBYTE, first(args)); + pfun_t pfun = pstreamfun(cdr(args)); + (pfun)(value); + return nil; +} + +object *fn_writestring (object *args, object *env) { + (void) env; + object *obj = first(args); + pfun_t pfun = pstreamfun(cdr(args)); + char temp = Flags; + clrflag(PRINTREADABLY); + printstring(obj, pfun); + Flags = temp; + return nil; +} + +object *fn_writeline (object *args, object *env) { + (void) env; + object *obj = first(args); + pfun_t pfun = pstreamfun(cdr(args)); + char temp = Flags; + clrflag(PRINTREADABLY); + printstring(obj, pfun); + pln(pfun); + Flags = temp; + return nil; +} + +object *fn_restarti2c (object *args, object *env) { + (void) env; + int stream = first(args)->integer; + args = cdr(args); + int read = 0; // Write + I2CCount = 0; + if (args != NULL) { + object *rw = first(args); + if (integerp(rw)) I2CCount = rw->integer; + read = (rw != NULL); + } + int address = stream & 0xFF; + if (stream>>8 != I2CSTREAM) error2(RESTARTI2C, PSTR("not an i2c stream")); + return I2Crestart(address, read) ? tee : nil; +} + +object *fn_gc (object *obj, object *env) { + int initial = Freespace; + unsigned long start = micros(); + gc(obj, env); + unsigned long elapsed = micros() - start; + pfstring(PSTR("Space: "), pserial); + pint(Freespace - initial, pserial); + pfstring(PSTR(" bytes, Time: "), pserial); + pint(elapsed, pserial); + pfstring(PSTR(" us\n"), pserial); + return nil; +} + +object *fn_room (object *args, object *env) { + (void) args, (void) env; + return number(Freespace); +} + +object *fn_saveimage (object *args, object *env) { + if (args != NULL) args = eval(first(args), env); + return number(saveimage(args)); +} + +object *fn_loadimage (object *args, object *env) { + (void) env; + if (args != NULL) args = first(args); + return number(loadimage(args)); +} + +object *fn_cls (object *args, object *env) { + (void) args, (void) env; + pserial(12); + return nil; +} + +// Arduino procedures + +object *fn_pinmode (object *args, object *env) { + (void) env; + int pin = checkinteger(PINMODE, first(args)); + PinMode pm = INPUT; + object *mode = second(args); + if (integerp(mode)) { + int nmode = mode->integer; + if (nmode == 1) pm = OUTPUT; else if (nmode == 2) pm = INPUT_PULLUP; + #if defined(INPUT_PULLDOWN) + else if (nmode == 4) pm = INPUT_PULLDOWN; + #endif + } else if (mode != nil) pm = OUTPUT; + pinMode(pin, pm); + return nil; +} + +object *fn_digitalread (object *args, object *env) { + (void) env; + int pin = checkinteger(DIGITALREAD, first(args)); + if (digitalRead(pin) != 0) return tee; else return nil; +} + +object *fn_digitalwrite (object *args, object *env) { + (void) env; + int pin = checkinteger(DIGITALWRITE, first(args)); + object *mode = second(args); + if (integerp(mode)) digitalWrite(pin, mode->integer ? HIGH : LOW); + else digitalWrite(pin, (mode != nil) ? HIGH : LOW); + return mode; +} + +object *fn_analogread (object *args, object *env) { + (void) env; + int pin = checkinteger(ANALOGREAD, first(args)); + checkanalogread(pin); + return number(analogRead(pin)); +} + +object *fn_analogwrite (object *args, object *env) { + (void) env; + int pin = checkinteger(ANALOGWRITE, first(args)); + checkanalogwrite(pin); + object *value = second(args); + analogWrite(pin, checkinteger(ANALOGWRITE, value)); + return value; +} + +object *fn_delay (object *args, object *env) { + (void) env; + object *arg1 = first(args); + delay(checkinteger(DELAY, arg1)); + return arg1; +} + +object *fn_millis (object *args, object *env) { + (void) args, (void) env; + return number(millis()); +} + +object *fn_sleep (object *args, object *env) { + (void) env; + object *arg1 = first(args); + sleep(checkinteger(SLEEP, arg1)); + return arg1; +} + +object *fn_note (object *args, object *env) { + (void) env; + static int pin = 255; + if (args != NULL) { + pin = checkinteger(NOTE, first(args)); + int note = 0; + if (cddr(args) != NULL) note = checkinteger(NOTE, second(args)); + int octave = 0; + if (cddr(args) != NULL) octave = checkinteger(NOTE, third(args)); + playnote(pin, note, octave); + } else nonote(pin); + return nil; +} + +// Tree Editor + +object *fn_edit (object *args, object *env) { + object *fun = first(args); + object *pair = findvalue(fun, env); + clrflag(EXITEDITOR); + object *arg = edit(eval(fun, env)); + cdr(pair) = arg; + return arg; +} + +object *edit (object *fun) { + while (1) { + if (tstflag(EXITEDITOR)) return fun; + char c = gserial(); + if (c == 'q') setflag(EXITEDITOR); + else if (c == 'b') return fun; + else if (c == 'r') fun = read(gserial); + else if (c == '\n') { pfl(pserial); superprint(fun, 0, pserial); pln(pserial); } + else if (c == 'c') fun = cons(read(gserial), fun); + else if (atom(fun)) pserial('!'); + else if (c == 'd') fun = cons(car(fun), edit(cdr(fun))); + else if (c == 'a') fun = cons(edit(car(fun)), cdr(fun)); + else if (c == 'x') fun = cdr(fun); + else pserial('?'); + } +} + +// Pretty printer + +object *fn_pprint (object *args, object *env) { + (void) env; + object *obj = first(args); + pfun_t pfun = pstreamfun(cdr(args)); +#if defined(gfxsupport) + if (pfun == gfxwrite) ppwidth = GFXPPWIDTH; +#endif + pln(pfun); + superprint(obj, 0, pfun); + ppwidth = PPWIDTH; + return symbol(NOTHING); +} + +object *fn_pprintall (object *args, object *env) { + (void) env; + pfun_t pfun = pstreamfun(args); +#if defined(gfxsupport) + if (pfun == gfxwrite) ppwidth = GFXPPWIDTH; +#endif + object *globals = GlobalEnv; + while (globals != NULL) { + object *pair = first(globals); + object *var = car(pair); + object *val = cdr(pair); + pln(pfun); + if (consp(val) && symbolp(car(val)) && car(val)->name == LAMBDA) { + superprint(cons(symbol(DEFUN), cons(var, cdr(val))), 0, pfun); + } else if (consp(val) && car(val)->type == CODE) { + superprint(cons(symbol(DEFCODE), cons(var, cdr(val))), 0, pfun); + } else { + superprint(cons(symbol(DEFVAR), cons(var, cons(quote(val), NULL))), 0, pserial); + } + pln(pfun); + testescape(); + globals = cdr(globals); + } + ppwidth = PPWIDTH; + return symbol(NOTHING); +} + +// Format + +void formaterr (object *formatstr, PGM_P string, uint8_t p) { + pln(pserial); indent(4, ' ', pserial); printstring(formatstr, pserial); pln(pserial); + indent(p+5, ' ', pserial); pserial('^'); + errorsub(FORMAT, string); + pln(pserial); + GCStack = NULL; + longjmp(exception, 1); +} + +object *fn_format (object *args, object *env) { + (void) env; + pfun_t pfun = pserial; + object *output = first(args); + object *obj; + if (output == nil) { obj = startstring(FORMAT); pfun = pstr; } + else if (output != tee) pfun = pstreamfun(args); + object *formatstr = second(args); + if (!stringp(formatstr)) error(FORMAT, notastring, formatstr); + object *save = NULL; + args = cddr(args); + int len = stringlength(formatstr); + uint8_t n = 0, width = 0, w, bra = 0; + char pad = ' '; + bool tilde = false, mute = false, comma, quote; + while (n < len) { + char ch = nthchar(formatstr, n); + char ch2 = ch & ~0x20; // force to upper case + if (tilde) { + if (ch == '}') { + if (save == NULL) formaterr(formatstr, PSTR("no matching ~{"), n); + if (args == NULL) { args = cdr(save); save = NULL; } else n = bra; + mute = false; tilde = false; + } + else if (!mute) { + if (comma && quote) { pad = ch; comma = false, quote = false; } + else if (ch == '\'') { + if (comma) quote = true; + else formaterr(formatstr, PSTR("quote not valid"), n); + } + else if (ch == '~') { pfun('~'); tilde = false; } + else if (ch >= '0' && ch <= '9') width = width*10 + ch - '0'; + else if (ch == ',') comma = true; + else if (ch == '%') { pln(pfun); tilde = false; } + else if (ch == '&') { pfl(pfun); tilde = false; } + else if (ch == '^') { + if (save != NULL && args == NULL) mute = true; + tilde = false; + } + else if (ch == '{') { + if (save != NULL) formaterr(formatstr, PSTR("can't nest ~{"), n); + if (args == NULL) formaterr(formatstr, noargument, n); + if (!listp(first(args))) formaterr(formatstr, notalist, n); + save = args; args = first(args); bra = n; tilde = false; + } + else if (ch2 == 'A' || ch2 == 'S' || ch2 == 'D' || ch2 == 'G' || ch2 == 'X') { + if (args == NULL) formaterr(formatstr, noargument, n); + object *arg = first(args); args = cdr(args); + uint8_t aw = atomwidth(arg); + if (width < aw) w = 0; else w = width-aw; + tilde = false; + if (ch2 == 'A') { prin1object(arg, pfun); indent(w, pad, pfun); } + else if (ch2 == 'S') { printobject(arg, pfun); indent(w, pad, pfun); } + else if (ch2 == 'D' || ch2 == 'G') { indent(w, pad, pfun); prin1object(arg, pfun); } + else if (ch2 == 'X' && integerp(arg)) { + uint8_t hw = hexwidth(arg); if (width < hw) w = 0; else w = width-hw; + indent(w, pad, pfun); pinthex(arg->integer, pfun); + } else if (ch2 == 'X') { indent(w, pad, pfun); prin1object(arg, pfun); } + tilde = false; + } else formaterr(formatstr, PSTR("invalid directive"), n); + } + } else { + if (ch == '~') { tilde = true; pad = ' '; width = 0; comma = false; quote = false; } + else if (!mute) pfun(ch); + } + n++; + } + if (output == nil) { obj->cdr = GlobalString; return obj; } + else return nil; +} + +// LispLibrary + +object *fn_require (object *args, object *env) { + object *arg = first(args); + object *globals = GlobalEnv; + if (!symbolp(arg)) error(REQUIRE, notasymbol, arg); + while (globals != NULL) { + object *pair = first(globals); + object *var = car(pair); + if (symbolp(var) && var == arg) return nil; + globals = cdr(globals); + } + GlobalStringIndex = 0; + object *line = read(glibrary); + while (line != NULL) { + // Is this the definition we want + int fname = first(line)->name; + if ((fname == DEFUN || fname == DEFVAR) && symbolp(second(line)) && second(line)->name == arg->name) { + eval(line, env); + return tee; + } + line = read(glibrary); + } + return nil; +} + +object *fn_listlibrary (object *args, object *env) { + (void) args, (void) env; + GlobalStringIndex = 0; + object *line = read(glibrary); + while (line != NULL) { + int fname = first(line)->name; + if (fname == DEFUN || fname == DEFVAR) { + pstring(symbolname(second(line)->name), pserial); pserial(' '); + } + line = read(glibrary); + } + return symbol(NOTHING); +} + +// Graphics functions + +object *fn_drawpixel (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t colour = COLOR_WHITE; + if (cddr(args) != NULL) colour = checkinteger(DRAWPIXEL, third(args)); + tft.drawPixel(checkinteger(DRAWPIXEL, first(args)), checkinteger(DRAWPIXEL, second(args)), colour); +#endif + return nil; +} + +object *fn_drawline (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[4], colour = COLOR_WHITE; + for (int i=0; i<4; i++) { params[i] = checkinteger(DRAWLINE, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(DRAWLINE, car(args)); + tft.drawLine(params[0], params[1], params[2], params[3], colour); +#endif + return nil; +} + +object *fn_drawrect (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[4], colour = COLOR_WHITE; + for (int i=0; i<4; i++) { params[i] = checkinteger(DRAWRECT, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(DRAWRECT, car(args)); + tft.drawRect(params[0], params[1], params[2], params[3], colour); +#endif + return nil; +} + +object *fn_fillrect (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[4], colour = COLOR_WHITE; + for (int i=0; i<4; i++) { params[i] = checkinteger(FILLRECT, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(FILLRECT, car(args)); + tft.fillRect(params[0], params[1], params[2], params[3], colour); +#endif + return nil; +} + +object *fn_drawcircle (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[3], colour = COLOR_WHITE; + for (int i=0; i<3; i++) { params[i] = checkinteger(DRAWCIRCLE, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(DRAWCIRCLE, car(args)); + tft.drawCircle(params[0], params[1], params[2], colour); +#endif + return nil; +} + +object *fn_fillcircle (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[3], colour = COLOR_WHITE; + for (int i=0; i<3; i++) { params[i] = checkinteger(FILLCIRCLE, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(FILLCIRCLE, car(args)); + tft.fillCircle(params[0], params[1], params[2], colour); +#endif + return nil; +} + +object *fn_drawroundrect (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[5], colour = COLOR_WHITE; + for (int i=0; i<5; i++) { params[i] = checkinteger(DRAWROUNDRECT, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(DRAWROUNDRECT, car(args)); + tft.drawRoundRect(params[0], params[1], params[2], params[3], params[4], colour); +#endif + return nil; +} + +object *fn_fillroundrect (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[5], colour = COLOR_WHITE; + for (int i=0; i<5; i++) { params[i] = checkinteger(FILLROUNDRECT, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(FILLROUNDRECT, car(args)); + tft.fillRoundRect(params[0], params[1], params[2], params[3], params[4], colour); +#endif + return nil; +} + +object *fn_drawtriangle (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[6], colour = COLOR_WHITE; + for (int i=0; i<6; i++) { params[i] = checkinteger(DRAWTRIANGLE, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(DRAWTRIANGLE, car(args)); + tft.drawTriangle(params[0], params[1], params[2], params[3], params[4], params[5], colour); +#endif + return nil; +} + +object *fn_filltriangle (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t params[6], colour = COLOR_WHITE; + for (int i=0; i<6; i++) { params[i] = checkinteger(FILLTRIANGLE, car(args)); args = cdr(args); } + if (args != NULL) colour = checkinteger(FILLTRIANGLE, car(args)); + tft.fillTriangle(params[0], params[1], params[2], params[3], params[4], params[5], colour); +#endif + return nil; +} + +object *fn_drawchar (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t colour = COLOR_WHITE, bg = COLOR_BLACK, size = 1; + object *more = cdr(cddr(args)); + if (more != NULL) { + colour = checkinteger(DRAWCHAR, car(more)); + more = cdr(more); + if (more != NULL) { + bg = checkinteger(DRAWCHAR, car(more)); + more = cdr(more); + if (more != NULL) size = checkinteger(DRAWCHAR, car(more)); + } + } + tft.drawChar(checkinteger(DRAWCHAR, first(args)), checkinteger(DRAWCHAR, second(args)), checkchar(DRAWCHAR, third(args)), + colour, bg, size); +#endif + return nil; +} + +object *fn_setcursor (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + tft.setCursor(checkinteger(SETCURSOR, first(args)), checkinteger(SETCURSOR, second(args))); +#endif + return nil; +} + +object *fn_settextcolor (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + if (cdr(args) != NULL) tft.setTextColor(checkinteger(SETTEXTCOLOR, first(args)), checkinteger(SETTEXTCOLOR, second(args))); + else tft.setTextColor(checkinteger(SETTEXTCOLOR, first(args))); +#endif + return nil; +} + +object *fn_settextsize (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + tft.setTextSize(checkinteger(SETTEXTSIZE, first(args))); +#endif + return nil; +} + +object *fn_settextwrap (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + tft.setTextWrap(first(args) != NULL); +#endif + return nil; +} + +object *fn_fillscreen (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + uint16_t colour = COLOR_BLACK; + if (args != NULL) colour = checkinteger(FILLSCREEN, first(args)); + tft.fillScreen(colour); +#endif + return nil; +} + +object *fn_setrotation (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + tft.setRotation(checkinteger(SETROTATION, first(args))); +#endif + return nil; +} + +object *fn_invertdisplay (object *args, object *env) { +#if defined(gfxsupport) + (void) env; + tft.invertDisplay(first(args) != NULL); +#endif + return nil; +} + +// Insert your own function definitions here + +// Built-in procedure names - stored in PROGMEM + +const char string0[] PROGMEM = "nil"; +const char string1[] PROGMEM = "t"; +const char string2[] PROGMEM = "nothing"; +const char string3[] PROGMEM = "&optional"; +const char string4[] PROGMEM = ":initial-element"; +const char string5[] PROGMEM = ":element-type"; +const char string6[] PROGMEM = "bit"; +const char string7[] PROGMEM = "&rest"; +const char string8[] PROGMEM = "lambda"; +const char string9[] PROGMEM = "let"; +const char string10[] PROGMEM = "let*"; +const char string11[] PROGMEM = "closure"; +const char string12[] PROGMEM = ""; +const char string13[] PROGMEM = "quote"; +const char string14[] PROGMEM = "defun"; +const char string15[] PROGMEM = "defvar"; +const char string16[] PROGMEM = "setq"; +const char string17[] PROGMEM = "loop"; +const char string18[] PROGMEM = "return"; +const char string19[] PROGMEM = "push"; +const char string20[] PROGMEM = "pop"; +const char string21[] PROGMEM = "incf"; +const char string22[] PROGMEM = "decf"; +const char string23[] PROGMEM = "setf"; +const char string24[] PROGMEM = "dolist"; +const char string25[] PROGMEM = "dotimes"; +const char string26[] PROGMEM = "trace"; +const char string27[] PROGMEM = "untrace"; +const char string28[] PROGMEM = "for-millis"; +const char string29[] PROGMEM = "with-output-to-string"; +const char string30[] PROGMEM = "with-serial"; +const char string31[] PROGMEM = "with-i2c"; +const char string32[] PROGMEM = "with-spi"; +const char string33[] PROGMEM = "with-sd-card"; +const char string34[] PROGMEM = "with-gfx"; +const char string35[] PROGMEM = "defcode"; +const char string36[] PROGMEM = ""; +const char string37[] PROGMEM = "progn"; +const char string38[] PROGMEM = "if"; +const char string39[] PROGMEM = "cond"; +const char string40[] PROGMEM = "when"; +const char string41[] PROGMEM = "unless"; +const char string42[] PROGMEM = "case"; +const char string43[] PROGMEM = "and"; +const char string44[] PROGMEM = "or"; +const char string45[] PROGMEM = ""; +const char string46[] PROGMEM = "not"; +const char string47[] PROGMEM = "null"; +const char string48[] PROGMEM = "cons"; +const char string49[] PROGMEM = "atom"; +const char string50[] PROGMEM = "listp"; +const char string51[] PROGMEM = "consp"; +const char string52[] PROGMEM = "symbolp"; +const char string53[] PROGMEM = "arrayp"; +const char string54[] PROGMEM = "boundp"; +const char string55[] PROGMEM = "setfn"; +const char string56[] PROGMEM = "streamp"; +const char string57[] PROGMEM = "eq"; +const char string58[] PROGMEM = "car"; +const char string59[] PROGMEM = "first"; +const char string60[] PROGMEM = "cdr"; +const char string61[] PROGMEM = "rest"; +const char string62[] PROGMEM = "caar"; +const char string63[] PROGMEM = "cadr"; +const char string64[] PROGMEM = "second"; +const char string65[] PROGMEM = "cdar"; +const char string66[] PROGMEM = "cddr"; +const char string67[] PROGMEM = "caaar"; +const char string68[] PROGMEM = "caadr"; +const char string69[] PROGMEM = "cadar"; +const char string70[] PROGMEM = "caddr"; +const char string71[] PROGMEM = "third"; +const char string72[] PROGMEM = "cdaar"; +const char string73[] PROGMEM = "cdadr"; +const char string74[] PROGMEM = "cddar"; +const char string75[] PROGMEM = "cdddr"; +const char string76[] PROGMEM = "length"; +const char string77[] PROGMEM = "array-dimensions"; +const char string78[] PROGMEM = "list"; +const char string79[] PROGMEM = "make-array"; +const char string80[] PROGMEM = "reverse"; +const char string81[] PROGMEM = "nth"; +const char string82[] PROGMEM = "aref"; +const char string83[] PROGMEM = "assoc"; +const char string84[] PROGMEM = "member"; +const char string85[] PROGMEM = "apply"; +const char string86[] PROGMEM = "funcall"; +const char string87[] PROGMEM = "append"; +const char string88[] PROGMEM = "mapc"; +const char string89[] PROGMEM = "mapcar"; +const char string90[] PROGMEM = "mapcan"; +const char string91[] PROGMEM = "+"; +const char string92[] PROGMEM = "-"; +const char string93[] PROGMEM = "*"; +const char string94[] PROGMEM = "/"; +const char string95[] PROGMEM = "mod"; +const char string96[] PROGMEM = "1+"; +const char string97[] PROGMEM = "1-"; +const char string98[] PROGMEM = "abs"; +const char string99[] PROGMEM = "random"; +const char string100[] PROGMEM = "max"; +const char string101[] PROGMEM = "min"; +const char string102[] PROGMEM = "/="; +const char string103[] PROGMEM = "="; +const char string104[] PROGMEM = "<"; +const char string105[] PROGMEM = "<="; +const char string106[] PROGMEM = ">"; +const char string107[] PROGMEM = ">="; +const char string108[] PROGMEM = "plusp"; +const char string109[] PROGMEM = "minusp"; +const char string110[] PROGMEM = "zerop"; +const char string111[] PROGMEM = "oddp"; +const char string112[] PROGMEM = "evenp"; +const char string113[] PROGMEM = "integerp"; +const char string114[] PROGMEM = "numberp"; +const char string115[] PROGMEM = "float"; +const char string116[] PROGMEM = "floatp"; +const char string117[] PROGMEM = "sin"; +const char string118[] PROGMEM = "cos"; +const char string119[] PROGMEM = "tan"; +const char string120[] PROGMEM = "asin"; +const char string121[] PROGMEM = "acos"; +const char string122[] PROGMEM = "atan"; +const char string123[] PROGMEM = "sinh"; +const char string124[] PROGMEM = "cosh"; +const char string125[] PROGMEM = "tanh"; +const char string126[] PROGMEM = "exp"; +const char string127[] PROGMEM = "sqrt"; +const char string128[] PROGMEM = "log"; +const char string129[] PROGMEM = "expt"; +const char string130[] PROGMEM = "ceiling"; +const char string131[] PROGMEM = "floor"; +const char string132[] PROGMEM = "truncate"; +const char string133[] PROGMEM = "round"; +const char string134[] PROGMEM = "char"; +const char string135[] PROGMEM = "char-code"; +const char string136[] PROGMEM = "code-char"; +const char string137[] PROGMEM = "characterp"; +const char string138[] PROGMEM = "stringp"; +const char string139[] PROGMEM = "string="; +const char string140[] PROGMEM = "string<"; +const char string141[] PROGMEM = "string>"; +const char string142[] PROGMEM = "sort"; +const char string143[] PROGMEM = "string"; +const char string144[] PROGMEM = "concatenate"; +const char string145[] PROGMEM = "subseq"; +const char string146[] PROGMEM = "read-from-string"; +const char string147[] PROGMEM = "princ-to-string"; +const char string148[] PROGMEM = "prin1-to-string"; +const char string149[] PROGMEM = "logand"; +const char string150[] PROGMEM = "logior"; +const char string151[] PROGMEM = "logxor"; +const char string152[] PROGMEM = "lognot"; +const char string153[] PROGMEM = "ash"; +const char string154[] PROGMEM = "logbitp"; +const char string155[] PROGMEM = "eval"; +const char string156[] PROGMEM = "globals"; +const char string157[] PROGMEM = "locals"; +const char string158[] PROGMEM = "makunbound"; +const char string159[] PROGMEM = "break"; +const char string160[] PROGMEM = "read"; +const char string161[] PROGMEM = "prin1"; +const char string162[] PROGMEM = "print"; +const char string163[] PROGMEM = "princ"; +const char string164[] PROGMEM = "terpri"; +const char string165[] PROGMEM = "read-byte"; +const char string166[] PROGMEM = "read-line"; +const char string167[] PROGMEM = "write-byte"; +const char string168[] PROGMEM = "write-string"; +const char string169[] PROGMEM = "write-line"; +const char string170[] PROGMEM = "restart-i2c"; +const char string171[] PROGMEM = "gc"; +const char string172[] PROGMEM = "room"; +const char string173[] PROGMEM = "save-image"; +const char string174[] PROGMEM = "load-image"; +const char string175[] PROGMEM = "cls"; +const char string176[] PROGMEM = "pinmode"; +const char string177[] PROGMEM = "digitalread"; +const char string178[] PROGMEM = "digitalwrite"; +const char string179[] PROGMEM = "analogread"; +const char string180[] PROGMEM = "analogwrite"; +const char string181[] PROGMEM = "delay"; +const char string182[] PROGMEM = "millis"; +const char string183[] PROGMEM = "sleep"; +const char string184[] PROGMEM = "note"; +const char string185[] PROGMEM = "edit"; +const char string186[] PROGMEM = "pprint"; +const char string187[] PROGMEM = "pprintall"; +const char string188[] PROGMEM = "format"; +const char string189[] PROGMEM = "require"; +const char string190[] PROGMEM = "list-library"; +const char string191[] PROGMEM = "draw-pixel"; +const char string192[] PROGMEM = "draw-line"; +const char string193[] PROGMEM = "draw-rect"; +const char string194[] PROGMEM = "fill-rect"; +const char string195[] PROGMEM = "draw-circle"; +const char string196[] PROGMEM = "fill-circle"; +const char string197[] PROGMEM = "draw-round-rect"; +const char string198[] PROGMEM = "fill-round-rect"; +const char string199[] PROGMEM = "draw-triangle"; +const char string200[] PROGMEM = "fill-triangle"; +const char string201[] PROGMEM = "draw-char"; +const char string202[] PROGMEM = "set-cursor"; +const char string203[] PROGMEM = "set-text-color"; +const char string204[] PROGMEM = "set-text-size"; +const char string205[] PROGMEM = "set-text-wrap"; +const char string206[] PROGMEM = "fill-screen"; +const char string207[] PROGMEM = "set-rotation"; +const char string208[] PROGMEM = "invert-display"; + +// Third parameter is no. of arguments; 1st hex digit is min, 2nd hex digit is max, 0xF is unlimited +const tbl_entry_t lookup_table[] PROGMEM = { + { string0, NULL, 0x00 }, + { string1, NULL, 0x00 }, + { string2, NULL, 0x00 }, + { string3, NULL, 0x00 }, + { string4, NULL, 0x00 }, + { string5, NULL, 0x00 }, + { string6, NULL, 0x00 }, + { string7, NULL, 0x00 }, + { string8, NULL, 0x0F }, + { string9, NULL, 0x0F }, + { string10, NULL, 0x0F }, + { string11, NULL, 0x0F }, + { string12, NULL, 0x00 }, + { string13, sp_quote, 0x11 }, + { string14, sp_defun, 0x2F }, + { string15, sp_defvar, 0x12 }, + { string16, sp_setq, 0x2F }, + { string17, sp_loop, 0x0F }, + { string18, sp_return, 0x0F }, + { string19, sp_push, 0x22 }, + { string20, sp_pop, 0x11 }, + { string21, sp_incf, 0x12 }, + { string22, sp_decf, 0x12 }, + { string23, sp_setf, 0x2F }, + { string24, sp_dolist, 0x1F }, + { string25, sp_dotimes, 0x1F }, + { string26, sp_trace, 0x01 }, + { string27, sp_untrace, 0x01 }, + { string28, sp_formillis, 0x1F }, + { string29, sp_withoutputtostring, 0x1F }, + { string30, sp_withserial, 0x1F }, + { string31, sp_withi2c, 0x1F }, + { string32, sp_withspi, 0x1F }, + { string33, sp_withsdcard, 0x2F }, + { string34, sp_withgfx, 0x1F }, + { string35, sp_defcode, 0x0F }, + { string36, NULL, 0x00 }, + { string37, tf_progn, 0x0F }, + { string38, tf_if, 0x23 }, + { string39, tf_cond, 0x0F }, + { string40, tf_when, 0x1F }, + { string41, tf_unless, 0x1F }, + { string42, tf_case, 0x1F }, + { string43, tf_and, 0x0F }, + { string44, tf_or, 0x0F }, + { string45, NULL, 0x00 }, + { string46, fn_not, 0x11 }, + { string47, fn_not, 0x11 }, + { string48, fn_cons, 0x22 }, + { string49, fn_atom, 0x11 }, + { string50, fn_listp, 0x11 }, + { string51, fn_consp, 0x11 }, + { string52, fn_symbolp, 0x11 }, + { string53, fn_arrayp, 0x11 }, + { string54, fn_boundp, 0x11 }, + { string55, fn_setfn, 0x2F }, + { string56, fn_streamp, 0x11 }, + { string57, fn_eq, 0x22 }, + { string58, fn_car, 0x11 }, + { string59, fn_car, 0x11 }, + { string60, fn_cdr, 0x11 }, + { string61, fn_cdr, 0x11 }, + { string62, fn_caar, 0x11 }, + { string63, fn_cadr, 0x11 }, + { string64, fn_cadr, 0x11 }, + { string65, fn_cdar, 0x11 }, + { string66, fn_cddr, 0x11 }, + { string67, fn_caaar, 0x11 }, + { string68, fn_caadr, 0x11 }, + { string69, fn_cadar, 0x11 }, + { string70, fn_caddr, 0x11 }, + { string71, fn_caddr, 0x11 }, + { string72, fn_cdaar, 0x11 }, + { string73, fn_cdadr, 0x11 }, + { string74, fn_cddar, 0x11 }, + { string75, fn_cdddr, 0x11 }, + { string76, fn_length, 0x11 }, + { string77, fn_arraydimensions, 0x11 }, + { string78, fn_list, 0x0F }, + { string79, fn_makearray, 0x15 }, + { string80, fn_reverse, 0x11 }, + { string81, fn_nth, 0x22 }, + { string82, fn_aref, 0x2F }, + { string83, fn_assoc, 0x22 }, + { string84, fn_member, 0x22 }, + { string85, fn_apply, 0x2F }, + { string86, fn_funcall, 0x1F }, + { string87, fn_append, 0x0F }, + { string88, fn_mapc, 0x2F }, + { string89, fn_mapcar, 0x2F }, + { string90, fn_mapcan, 0x2F }, + { string91, fn_add, 0x0F }, + { string92, fn_subtract, 0x1F }, + { string93, fn_multiply, 0x0F }, + { string94, fn_divide, 0x1F }, + { string95, fn_mod, 0x22 }, + { string96, fn_oneplus, 0x11 }, + { string97, fn_oneminus, 0x11 }, + { string98, fn_abs, 0x11 }, + { string99, fn_random, 0x11 }, + { string100, fn_maxfn, 0x1F }, + { string101, fn_minfn, 0x1F }, + { string102, fn_noteq, 0x1F }, + { string103, fn_numeq, 0x1F }, + { string104, fn_less, 0x1F }, + { string105, fn_lesseq, 0x1F }, + { string106, fn_greater, 0x1F }, + { string107, fn_greatereq, 0x1F }, + { string108, fn_plusp, 0x11 }, + { string109, fn_minusp, 0x11 }, + { string110, fn_zerop, 0x11 }, + { string111, fn_oddp, 0x11 }, + { string112, fn_evenp, 0x11 }, + { string113, fn_integerp, 0x11 }, + { string114, fn_numberp, 0x11 }, + { string115, fn_floatfn, 0x11 }, + { string116, fn_floatp, 0x11 }, + { string117, fn_sin, 0x11 }, + { string118, fn_cos, 0x11 }, + { string119, fn_tan, 0x11 }, + { string120, fn_asin, 0x11 }, + { string121, fn_acos, 0x11 }, + { string122, fn_atan, 0x12 }, + { string123, fn_sinh, 0x11 }, + { string124, fn_cosh, 0x11 }, + { string125, fn_tanh, 0x11 }, + { string126, fn_exp, 0x11 }, + { string127, fn_sqrt, 0x11 }, + { string128, fn_log, 0x12 }, + { string129, fn_expt, 0x22 }, + { string130, fn_ceiling, 0x12 }, + { string131, fn_floor, 0x12 }, + { string132, fn_truncate, 0x12 }, + { string133, fn_round, 0x12 }, + { string134, fn_char, 0x22 }, + { string135, fn_charcode, 0x11 }, + { string136, fn_codechar, 0x11 }, + { string137, fn_characterp, 0x11 }, + { string138, fn_stringp, 0x11 }, + { string139, fn_stringeq, 0x22 }, + { string140, fn_stringless, 0x22 }, + { string141, fn_stringgreater, 0x22 }, + { string142, fn_sort, 0x22 }, + { string143, fn_stringfn, 0x11 }, + { string144, fn_concatenate, 0x1F }, + { string145, fn_subseq, 0x23 }, + { string146, fn_readfromstring, 0x11 }, + { string147, fn_princtostring, 0x11 }, + { string148, fn_prin1tostring, 0x11 }, + { string149, fn_logand, 0x0F }, + { string150, fn_logior, 0x0F }, + { string151, fn_logxor, 0x0F }, + { string152, fn_lognot, 0x11 }, + { string153, fn_ash, 0x22 }, + { string154, fn_logbitp, 0x22 }, + { string155, fn_eval, 0x11 }, + { string156, fn_globals, 0x00 }, + { string157, fn_locals, 0x00 }, + { string158, fn_makunbound, 0x11 }, + { string159, fn_break, 0x00 }, + { string160, fn_read, 0x01 }, + { string161, fn_prin1, 0x12 }, + { string162, fn_print, 0x12 }, + { string163, fn_princ, 0x12 }, + { string164, fn_terpri, 0x01 }, + { string165, fn_readbyte, 0x02 }, + { string166, fn_readline, 0x01 }, + { string167, fn_writebyte, 0x12 }, + { string168, fn_writestring, 0x12 }, + { string169, fn_writeline, 0x12 }, + { string170, fn_restarti2c, 0x12 }, + { string171, fn_gc, 0x00 }, + { string172, fn_room, 0x00 }, + { string173, fn_saveimage, 0x01 }, + { string174, fn_loadimage, 0x01 }, + { string175, fn_cls, 0x00 }, + { string176, fn_pinmode, 0x22 }, + { string177, fn_digitalread, 0x11 }, + { string178, fn_digitalwrite, 0x22 }, + { string179, fn_analogread, 0x11 }, + { string180, fn_analogwrite, 0x22 }, + { string181, fn_delay, 0x11 }, + { string182, fn_millis, 0x00 }, + { string183, fn_sleep, 0x11 }, + { string184, fn_note, 0x03 }, + { string185, fn_edit, 0x11 }, + { string186, fn_pprint, 0x12 }, + { string187, fn_pprintall, 0x01 }, + { string188, fn_format, 0x2F }, + { string189, fn_require, 0x11 }, + { string190, fn_listlibrary, 0x00 }, + { string191, fn_drawpixel, 0x23 }, + { string192, fn_drawline, 0x45 }, + { string193, fn_drawrect, 0x45 }, + { string194, fn_fillrect, 0x45 }, + { string195, fn_drawcircle, 0x34 }, + { string196, fn_fillcircle, 0x34 }, + { string197, fn_drawroundrect, 0x56 }, + { string198, fn_fillroundrect, 0x56 }, + { string199, fn_drawtriangle, 0x67 }, + { string200, fn_filltriangle, 0x67 }, + { string201, fn_drawchar, 0x36 }, + { string202, fn_setcursor, 0x22 }, + { string203, fn_settextcolor, 0x12 }, + { string204, fn_settextsize, 0x11 }, + { string205, fn_settextwrap, 0x11 }, + { string206, fn_fillscreen, 0x01 }, + { string207, fn_setrotation, 0x11 }, + { string208, fn_invertdisplay, 0x11 }, +}; + +// Table lookup functions + +int builtin (char* n) { + int entry = 0; + while (entry < ENDFUNCTIONS) { + if (strcasecmp(n, (char*)lookup_table[entry].string) == 0) + return entry; + entry++; + } + return ENDFUNCTIONS; +} + +int longsymbol (char *buffer) { + char *p = SymbolTable; + int i = 0; + while (strcasecmp(p, buffer) != 0) {p = p + strlen(p) + 1; i++; } + if (p == buffer) { + // Add to symbol table? + char *newtop = SymbolTop + strlen(p) + 1; + if (SYMBOLTABLESIZE - (newtop - SymbolTable) < BUFFERSIZE) error2(0, PSTR("symbol table full")); + SymbolTop = newtop; + } + return i + MAXSYMBOL; // First number unused by radix40 +} + +intptr_t lookupfn (symbol_t name) { + return (intptr_t)lookup_table[name].fptr; +} + +void checkminmax (symbol_t name, int nargs) { + uint8_t minmax = lookup_table[name].minmax; + if (nargs<(minmax >> 4)) error2(name, toofewargs); + if ((minmax & 0x0f) != 0x0f && nargs>(minmax & 0x0f)) error2(name, toomanyargs); +} + +char *lookupbuiltin (symbol_t name) { + char *buffer = SymbolTop; + strcpy(buffer, (char *)lookup_table[name].string); + return buffer; +} + +char *lookupsymbol (symbol_t name) { + char *p = SymbolTable; + int i = name - MAXSYMBOL; + while (i > 0 && p < SymbolTop) {p = p + strlen(p) + 1; i--; } + if (p == SymbolTop) return NULL; else return p; +} + +void deletesymbol (symbol_t name) { + char *p = lookupsymbol(name); + if (p == NULL) return; + char *q = p + strlen(p) + 1; + *p = '\0'; p++; + while (q < SymbolTop) *(p++) = *(q++); + SymbolTop = p; +} + +void testescape () { + if (Serial.read() == '~') error2(0, PSTR("escape!")); +} + +// Main evaluator +#if defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) +#define ENDSTACK _ebss +#else +#define ENDSTACK end +#endif + +extern uint32_t ENDSTACK; // Bottom of stack + +object *eval (object *form, object *env) { + register int *sp asm ("r13"); + int TC=0; + EVAL: + // Enough space? + // Serial.println((uint32_t)sp - (uint32_t)&ENDSTACK); // Find best STACKDIFF value + if (((uint32_t)sp - (uint32_t)&ENDSTACK) < STACKDIFF) error2(0, PSTR("stack overflow")); + if (Freespace <= WORKSPACESIZE>>4) gc(form, env); + // Escape + if (tstflag(ESCAPE)) { clrflag(ESCAPE); error2(0, PSTR("escape!"));} + if (!tstflag(NOESC)) testescape(); + + if (form == NULL) return nil; + + if (form->type >= NUMBER && form->type <= STRING) return form; + + if (symbolp(form)) { + symbol_t name = form->name; + object *pair = value(name, env); + if (pair != NULL) return cdr(pair); + pair = value(name, GlobalEnv); + if (pair != NULL) return cdr(pair); + else if (name <= ENDFUNCTIONS) return form; + error(0, PSTR("undefined"), form); + } + + if (form->type == CODE) error2(0, PSTR("can't evaluate CODE header")); + + // It's a list + object *function = car(form); + object *args = cdr(form); + + if (function == NULL) error(0, PSTR("illegal function"), nil); + if (!listp(args)) error(0, PSTR("can't evaluate a dotted pair"), args); + + // List starts with a symbol? + if (symbolp(function)) { + symbol_t name = function->name; + + if ((name == LET) || (name == LETSTAR)) { + int TCstart = TC; + object *assigns = first(args); + if (!listp(assigns)) error(name, notalist, assigns); + object *forms = cdr(args); + object *newenv = env; + push(newenv, GCStack); + while (assigns != NULL) { + object *assign = car(assigns); + if (!consp(assign)) push(cons(assign,nil), newenv); + else if (cdr(assign) == NULL) push(cons(first(assign),nil), newenv); + else push(cons(first(assign),eval(second(assign),env)), newenv); + car(GCStack) = newenv; + if (name == LETSTAR) env = newenv; + assigns = cdr(assigns); + } + env = newenv; + pop(GCStack); + form = tf_progn(forms,env); + TC = TCstart; + goto EVAL; + } + + if (name == LAMBDA) { + if (env == NULL) return form; + object *envcopy = NULL; + while (env != NULL) { + object *pair = first(env); + if (pair != NULL) push(pair, envcopy); + env = cdr(env); + } + return cons(symbol(CLOSURE), cons(envcopy,args)); + } + + if ((name > SPECIAL_FORMS) && (name < TAIL_FORMS)) { + return ((fn_ptr_type)lookupfn(name))(args, env); + } + + if ((name > TAIL_FORMS) && (name < FUNCTIONS)) { + form = ((fn_ptr_type)lookupfn(name))(args, env); + TC = 1; + goto EVAL; + } + + if (name < SPECIAL_FORMS) error2(name, PSTR("can't be used as a function")); + } + + // Evaluate the parameters - result in head + object *fname = car(form); + int TCstart = TC; + object *head = cons(eval(fname, env), NULL); + push(head, GCStack); // Don't GC the result list + object *tail = head; + form = cdr(form); + int nargs = 0; + + while (form != NULL){ + object *obj = cons(eval(car(form),env),NULL); + cdr(tail) = obj; + tail = obj; + form = cdr(form); + nargs++; + } + + function = car(head); + args = cdr(head); + + if (symbolp(function)) { + symbol_t name = function->name; + if (name >= ENDFUNCTIONS) error(0, PSTR("not valid here"), fname); + checkminmax(name, nargs); + object *result = ((fn_ptr_type)lookupfn(name))(args, env); + pop(GCStack); + return result; + } + + if (consp(function)) { + + if (issymbol(car(function), LAMBDA)) { + form = closure(TCstart, fname->name, NULL, cdr(function), args, &env); + pop(GCStack); + int trace = tracing(fname->name); + if (trace) { + object *result = eval(form, env); + indent((--(TraceDepth[trace-1]))<<1, ' ', pserial); + pint(TraceDepth[trace-1], pserial); + pserial(':'); pserial(' '); + printobject(fname, pserial); pfstring(PSTR(" returned "), pserial); + printobject(result, pserial); pln(pserial); + return result; + } else { + TC = 1; + goto EVAL; + } + } + + if (issymbol(car(function), CLOSURE)) { + function = cdr(function); + form = closure(TCstart, fname->name, car(function), cdr(function), args, &env); + pop(GCStack); + TC = 1; + goto EVAL; + } + + if (car(function)->type == CODE) { + int n = listlength(DEFCODE, second(function)); + if (nargsname, toofewargs); + if (nargs>n) error2(fname->name, toomanyargs); + uint32_t entry = startblock(car(function)) + 1; + pop(GCStack); + return call(entry, n, args, env); + } + + } + error(0, PSTR("illegal function"), fname); return nil; +} + +// Print functions + +inline int maxbuffer (char *buffer) { + return SYMBOLTABLESIZE-(buffer-SymbolTable)-1; +} + +void pserial (char c) { + LastPrint = c; + if (c == '\n') Serial.write('\r'); + Serial.write(c); +} + +const char ControlCodes[] PROGMEM = "Null\0SOH\0STX\0ETX\0EOT\0ENQ\0ACK\0Bell\0Backspace\0Tab\0Newline\0VT\0" +"Page\0Return\0SO\0SI\0DLE\0DC1\0DC2\0DC3\0DC4\0NAK\0SYN\0ETB\0CAN\0EM\0SUB\0Escape\0FS\0GS\0RS\0US\0Space\0"; + +void pcharacter (char c, pfun_t pfun) { + if (!tstflag(PRINTREADABLY)) pfun(c); + else { + pfun('#'); pfun('\\'); + if (c > 32) pfun(c); + else { + const char *p = ControlCodes; + while (c > 0) {p = p + strlen(p) + 1; c--; } + pfstring(p, pfun); + } + } +} + +void pstring (char *s, pfun_t pfun) { + while (*s) pfun(*s++); +} + +void printstring (object *form, pfun_t pfun) { + if (tstflag(PRINTREADABLY)) pfun('"'); + form = cdr(form); + while (form != NULL) { + int chars = form->chars; + for (int i=(sizeof(int)-1)*8; i>=0; i=i-8) { + char ch = chars>>i & 0xFF; + if (tstflag(PRINTREADABLY) && (ch == '"' || ch == '\\')) pfun('\\'); + if (ch) pfun(ch); + } + form = car(form); + } + if (tstflag(PRINTREADABLY)) pfun('"'); +} + +void pfstring (const char *s, pfun_t pfun) { + int p = 0; + while (1) { + char c = s[p++]; + if (c == 0) return; + pfun(c); + } +} + +void pint (int i, pfun_t pfun) { + int lead = 0; + #if INT_MAX == 32767 + int p = 10000; + #else + int p = 1000000000; + #endif + if (i<0) pfun('-'); + for (int d=p; d>0; d=d/10) { + int j = i/d; + if (j!=0 || lead || d==1) { pfun(abs(j)+'0'); lead=1;} + i = i - j*d; + } +} + +void pinthex (uint32_t i, pfun_t pfun) { + int lead = 0; + #if INT_MAX == 32767 + uint32_t p = 0x1000; + #else + uint32_t p = 0x10000000; + #endif + for (uint32_t d=p; d>0; d=d/16) { + uint32_t j = i/d; + if (j!=0 || lead || d==1) { pfun((j<10) ? j+'0' : j+'W'); lead=1;} + i = i - j*d; + } +} + +void printhex4 (int i, pfun_t pfun) { + int p = 0x1000; + for (int d=p; d>0; d=d/16) { + int j = i/d; + pfun((j<10) ? j+'0' : j + 'W'); + i = i - j*d; + } + pfun(' '); +} + +void pmantissa (float f, pfun_t pfun) { + int sig = floor(log10(f)); + int mul = pow(10, 5 - sig); + int i = round(f * mul); + bool point = false; + if (i == 1000000) { i = 100000; sig++; } + if (sig < 0) { + pfun('0'); pfun('.'); point = true; + for (int j=0; j < - sig - 1; j++) pfun('0'); + } + mul = 100000; + for (int j=0; j<7; j++) { + int d = (int)(i / mul); + pfun(d + '0'); + i = i - d * mul; + if (i == 0) { + if (!point) { + for (int k=j; k= 0) { pfun('.'); point = true; } + mul = mul / 10; + } +} + +void pfloat (float f, pfun_t pfun) { + if (isnan(f)) { pfstring(PSTR("NaN"), pfun); return; } + if (f == 0.0) { pfun('0'); return; } + if (isinf(f)) { pfstring(PSTR("Inf"), pfun); return; } + if (f < 0) { pfun('-'); f = -f; } + // Calculate exponent + int e = 0; + if (f < 1e-3 || f >= 1e5) { + e = floor(log(f) / 2.302585); // log10 gives wrong result + f = f / pow(10, e); + } + + pmantissa (f, pfun); + + // Exponent + if (e != 0) { + pfun('e'); + pint(e, pfun); + } +} + +inline void pln (pfun_t pfun) { + pfun('\n'); +} + +void pfl (pfun_t pfun) { + if (LastPrint != '\n') pfun('\n'); +} + +void plist (object *form, pfun_t pfun) { + pfun('('); + printobject(car(form), pfun); + form = cdr(form); + while (form != NULL && listp(form)) { + pfun(' '); + printobject(car(form), pfun); + form = cdr(form); + } + if (form != NULL) { + pfstring(PSTR(" . "), pfun); + printobject(form, pfun); + } + pfun(')'); +} + +void pstream (object *form, pfun_t pfun) { + pfun('<'); + pfstring(streamname[(form->integer)>>8], pfun); + pfstring(PSTR("-stream "), pfun); + pint(form->integer & 0xFF, pfun); + pfun('>'); +} + +void printobject (object *form, pfun_t pfun) { + if (form == NULL) pfstring(PSTR("nil"), pfun); + else if (listp(form) && issymbol(car(form), CLOSURE)) pfstring(PSTR(""), pfun); + else if (listp(form)) plist(form, pfun); + else if (integerp(form)) pint(form->integer, pfun); + else if (floatp(form)) pfloat(form->single_float, pfun); + else if (symbolp(form)) { if (form->name != NOTHING) pstring(symbolname(form->name), pfun); } + else if (characterp(form)) pcharacter(form->chars, pfun); + else if (stringp(form)) printstring(form, pfun); + else if (arrayp(form)) printarray(form, pfun); + else if (form->type == CODE) pfstring(PSTR("code"), pfun); + else if (streamp(form)) pstream(form, pfun); + else error2(0, PSTR("error in print")); +} + +void prin1object (object *form, pfun_t pfun) { + char temp = Flags; + clrflag(PRINTREADABLY); + printobject(form, pfun); + Flags = temp; +} + +// Read functions + +int glibrary () { + if (LastChar) { + char temp = LastChar; + LastChar = 0; + return temp; + } + char c = LispLibrary[GlobalStringIndex++]; + return (c != 0) ? c : -1; // -1? +} + +void loadfromlibrary (object *env) { + GlobalStringIndex = 0; + object *line = read(glibrary); + while (line != NULL) { + eval(line, env); + line = read(glibrary); + } +} + +// For line editor +const int TerminalWidth = 80; +volatile int WritePtr = 0, ReadPtr = 0; +const int KybdBufSize = 333; // 42*8 - 3 +char KybdBuf[KybdBufSize]; +volatile uint8_t KybdAvailable = 0; + +// Parenthesis highlighting +void esc (int p, char c) { + Serial.write('\e'); Serial.write('['); + Serial.write((char)('0'+ p/100)); + Serial.write((char)('0'+ (p/10) % 10)); + Serial.write((char)('0'+ p % 10)); + Serial.write(c); +} + +void hilight (char c) { + Serial.write('\e'); Serial.write('['); Serial.write(c); Serial.write('m'); +} + +void Highlight (int p, int wp, uint8_t invert) { + wp = wp + 2; // Prompt +#if defined (printfreespace) + int f = Freespace; + while (f) { wp++; f=f/10; } +#endif + int line = wp/TerminalWidth; + int col = wp%TerminalWidth; + int targetline = (wp - p)/TerminalWidth; + int targetcol = (wp - p)%TerminalWidth; + int up = line-targetline, left = col-targetcol; + if (p) { + if (up) esc(up, 'A'); + if (col > targetcol) esc(left, 'D'); else esc(-left, 'C'); + if (invert) hilight('7'); + Serial.write('('); Serial.write('\b'); + // Go back + if (up) esc(up, 'B'); // Down + if (col > targetcol) esc(left, 'C'); else esc(-left, 'D'); + Serial.write('\b'); Serial.write(')'); + if (invert) hilight('0'); + } +} + +void processkey (char c) { + if (c == 27) { setflag(ESCAPE); return; } // Escape key +#if defined(vt100) + static int parenthesis = 0, wp = 0; + // Undo previous parenthesis highlight + Highlight(parenthesis, wp, 0); + parenthesis = 0; +#endif + // Edit buffer + if (c == '\n' || c == '\r') { + pserial('\n'); + KybdAvailable = 1; + ReadPtr = 0; + return; + } + if (c == 8 || c == 0x7f) { // Backspace key + if (WritePtr > 0) { + WritePtr--; + Serial.write(8); Serial.write(' '); Serial.write(8); + if (WritePtr) c = KybdBuf[WritePtr-1]; + } + } else if (WritePtr < KybdBufSize) { + KybdBuf[WritePtr++] = c; + Serial.write(c); + } +#if defined(vt100) + // Do new parenthesis highlight + if (c == ')') { + int search = WritePtr-1, level = 0; + while (search >= 0 && parenthesis == 0) { + c = KybdBuf[search--]; + if (c == ')') level++; + if (c == '(') { + level--; + if (level == 0) {parenthesis = WritePtr-search-1; wp = WritePtr; } + } + } + Highlight(parenthesis, wp, 1); + } +#endif + return; +} + +int gserial () { + if (LastChar) { + char temp = LastChar; + LastChar = 0; + return temp; + } +#if defined(lineeditor) + while (!KybdAvailable) { + while (!Serial.available()); + char temp = Serial.read(); + processkey(temp); + } + if (ReadPtr != WritePtr) return KybdBuf[ReadPtr++]; + KybdAvailable = 0; + WritePtr = 0; + return '\n'; +#else + while (!Serial.available()); + char temp = Serial.read(); + if (temp != '\n') pserial(temp); + return temp; +#endif +} + +#define issp(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t') + +object *nextitem (gfun_t gfun) { + int ch = gfun(); + while(issp(ch)) ch = gfun(); + + if (ch == ';') { + while(ch != '(') ch = gfun(); + ch = '('; + } + if (ch == '\n') ch = gfun(); + if (ch == -1) return nil; + if (ch == ')') return (object *)KET; + if (ch == '(') return (object *)BRA; + if (ch == '\'') return (object *)QUO; + + // Parse string + if (ch == '"') return readstring('"', gfun); + + // Parse symbol, character, or number + int index = 0, base = 10, sign = 1; + char *buffer = SymbolTop; + int bufmax = maxbuffer(buffer); // Max index + unsigned int result = 0; + bool isfloat = false; + float fresult = 0.0; + + if (ch == '+') { + buffer[index++] = ch; + ch = gfun(); + } else if (ch == '-') { + sign = -1; + buffer[index++] = ch; + ch = gfun(); + } else if (ch == '.') { + buffer[index++] = ch; + ch = gfun(); + if (ch == ' ') return (object *)DOT; + isfloat = true; + } + + // Parse reader macros + else if (ch == '#') { + ch = gfun(); + char ch2 = ch & ~0x20; // force to upper case + if (ch == '\\') { // Character + base = 0; ch = gfun(); + if (issp(ch) || ch == ')' || ch == '(') return character(ch); + else LastChar = ch; + } else if (ch == '|') { + do { while (gfun() != '|'); } + while (gfun() != '#'); + return nextitem(gfun); + } else if (ch2 == 'B') base = 2; + else if (ch2 == 'O') base = 8; + else if (ch2 == 'X') base = 16; + else if (ch == '\'') return nextitem(gfun); + else if (ch == '.') { + setflag(NOESC); + object *result = eval(read(gfun), NULL); + clrflag(NOESC); + return result; + } + else if (ch == '(') { LastChar = ch; return readarray(1, read(gfun)); } + else if (ch == '*') return readbitarray(gfun); + else if (ch >= '1' && ch <= '9' && (gfun() & ~0x20) == 'A') return readarray(ch - '0', read(gfun)); + else error2(0, PSTR("illegal character after #")); + ch = gfun(); + } + int valid; // 0=undecided, -1=invalid, +1=valid + if (ch == '.') valid = 0; else if (digitvalue(ch) ((unsigned int)INT_MAX+(1-sign)/2)) + return makefloat((float)result*sign); + return number(result*sign); + } else if (base == 0) { + if (index == 1) return character(buffer[0]); + const char* p = ControlCodes; char c = 0; + while (c < 33) { + if (strcasecmp(buffer, p) == 0) return character(c); + p = p + strlen(p) + 1; c++; + } + error2(0, PSTR("unknown character")); + } + + int x = builtin(buffer); + if (x == NIL) return nil; + if (x < ENDFUNCTIONS) return newsymbol(x); + else if (index <= 6 && valid40(buffer)) return newsymbol(pack40(buffer)); + else return newsymbol(longsymbol(buffer)); +} + +object *readrest (gfun_t gfun) { + object *item = nextitem(gfun); + object *head = NULL; + object *tail = NULL; + + while (item != (object *)KET) { + if (item == (object *)BRA) { + item = readrest(gfun); + } else if (item == (object *)QUO) { + item = cons(symbol(QUOTE), cons(read(gfun), NULL)); + } else if (item == (object *)DOT) { + tail->cdr = read(gfun); + if (readrest(gfun) != NULL) error2(0, PSTR("malformed list")); + return head; + } else { + object *cell = cons(item, NULL); + if (head == NULL) head = cell; + else tail->cdr = cell; + tail = cell; + item = nextitem(gfun); + } + } + return head; +} + +object *read (gfun_t gfun) { + object *item = nextitem(gfun); + if (item == (object *)KET) error2(0, PSTR("incomplete list")); + if (item == (object *)BRA) return readrest(gfun); + if (item == (object *)DOT) return read(gfun); + if (item == (object *)QUO) return cons(symbol(QUOTE), cons(read(gfun), NULL)); + return item; +} + +// Setup + +void initgfx () { +#if defined(gfxsupport) + tft.initR(INITR_BLACKTAB); + tft.setRotation(1); + pinMode(TFT_BACKLIGHT, OUTPUT); + digitalWrite(TFT_BACKLIGHT, HIGH); + tft.fillScreen(ST77XX_BLACK); +#endif +} + +void initenv () { + GlobalEnv = NULL; + tee = symbol(TEE); +} + +void setup () { + Serial.begin(9600); + int start = millis(); + while ((millis() - start) < 5000) { if (Serial) break; } + initworkspace(); + initenv(); + initsleep(); + initgfx(); + pfstring(PSTR("uLisp 3.3 "), pserial); pln(pserial); +} + +// Read/Evaluate/Print loop + +void repl (object *env) { + for (;;) { + randomSeed(micros()); + gc(NULL, env); + #if defined (printfreespace) + pint(Freespace, pserial); + #endif + if (BreakLevel) { + pfstring(PSTR(" : "), pserial); + pint(BreakLevel, pserial); + } + pfstring(PSTR("> "), pserial); + object *line = read(gserial); + if (BreakLevel && line == nil) { pln(pserial); return; } + if (line == (object *)KET) error2(0, PSTR("unmatched right bracket")); + push(line, GCStack); + pfl(pserial); + line = eval(line, env); + pfl(pserial); + printobject(line, pserial); + pop(GCStack); + pfl(pserial); + pln(pserial); + } +} + +void loop () { + if (!setjmp(exception)) { + #if defined(resetautorun) + volatile int autorun = 12; // Fudge to keep code size the same + #else + volatile int autorun = 13; + #endif + if (autorun == 12) autorunimage(); + } + // Come here after error + delay(100); while (Serial.available()) Serial.read(); + clrflag(NOESC); BreakLevel = 0; + for (int i=0; i