Trying to build a forth runtime in C
Ondřej Hruška 9c44e0fb29 add fac example 6 days ago
include add ACCEPT and KEY 6 days ago
src add ACCEPT and KEY 6 days ago
testfiles add fac example 6 days ago
.gitignore new words 2 weeks ago
CMakeLists.txt convert input parsing to pluggable specs 1 week ago
README.md add ACCEPT and KEY 6 days ago
build.sh move to folders, more cleaning and fixes 2 weeks ago

README.md

Forth

This is a new implementation of Forth 2012 in C.

Building & use

Use cmake or build.sh. It probably needs GCC with --std=gnu99.

You get a binary with the forth runtime. Run it to get a prompt. Use -h for help.

Goals

  • Comfy to use and extend
  • No dynamic allocation where possible
  • Compact and embeddable so it can be ported to ARM-CortexM or similar later
  • Implement the CORE and CORE-EXT word sets, for a start
  • Pass the Forth2012 test suite, at least for the word sets that are implemented
  • Maybe eventually have some terminal-based implementation of (a subset of) the Facility word set, like AT-XY and keyboard input
  • Try to keep added custom words and quirks at a minimum
  • Comment stuff, maybe

Non-Goals

  • I don't really care about implementing some of the word sets
  • Performance is not so critical for a hobby Forth like this
  • Probably won't accept merge requests, this is my Forth
  • I also don't care about someone telling me to add a license file

Custom words

  • .\" - like ." but allows escaping, this is essentially S\" blah" TYPE, but without allocation (unless compiled)
  • ?? - debug dump of the stacks
  • reset - everything should become like if you restarted the program
  • debug ( flag -- ) - enable or disable verbose logging

For debug, there is see ( "word" -- ) that lets you inspect and disassemble dictionary entries.

Implementation status

(this section may be outdated)

Implemented and tested:

CORE:
! ' ( * */ */MOD + +! +LOOP , - . ." # #> #S <# >BODY >NUMBER / /mod 0< 0= 1+ 1- 2! 2* 2/ 2@ 2DROP 2DUP 2OVER 2SWAP
: ; < = > >IN >R ?DUP @ ACCEPT ABORT ABORT" ABS ALIGN ALIGNED ALLOT AND BASE BEGIN BL C! C, C@ CELL CELL+ CELLS CHAR CHAR+
CHARS CONSTANT COUNT CR CREATE DECIMAL DEPTH DO DOES> DROP DUP ELSE EMIT ENVIRONMENT? EVALUATE EXECUTE EXIT FILL FM/MOD FIND
HERE HOLD I IF IMMEDIATE INVERT J KEY LEAVE LITERAL LOOP LSHIFT M* MAX MIN MOD MOVE NEGATE OR OVER POSTPONE QUIT R> R@ RECURSE
REPEAT ROT RSHIFT S>D S" SIGN SM/REM SOURCE SPACE SPACES STATE SWAP THEN TYPE U. U< UNTIL UM* UM/MOD UNLOOP VARIABLE WHILE WORD XOR [ ['] [CHAR] ] 

CORE-EXT:
.( .R :NONAME 0<> 0> 2>R 2R> 2R@ <> ?DO ACTION-OF AGAIN BUFFER: C" CASE COMPILE, DEFER DEFER! DEFER@ ENDCASE ENDOF ERASE FALSE HEX HOLDS IS MARKER NIP OF PAD PARSE PARSE-NAME PICK REFILL RESTORE-INPUT SAVE-INPUT SOURCE-ID ROLL S\" TO TRUE TUCK U.R U> UNUSED VALUE WITHIN
\

Other sets:
FORGET SEE BYE INCLUDE INCLUDED

Missing:

CORE-EXT:
[COMPILE]

.