Log In  

This cart is a barebones Forth implementation in 279 tokens. It could be smaller, it could be more usable, but I haven't really touched it in weeks, so here you go. Probably full of bugs.

Features

  • eval() function
  • Interpret and compile modes - extend syntax in Forth!
  • if/then (implemented in Forth)
  • A few useful comments, including some commented-out utility functions and basic smoke test.

Non-features / shortcomings

  • else (you can add this in Forth)
  • Arithmetic (you can add this for 11 tokens per binop, or 9 tokens if you're willing to use valid Lua identifiers as operator names)
  • Looping (you can probably add this for the cost of a few arithmetic/comparison operators, then add the syntax in Forth for no token cost)
  • Passing args or returning values when calling Lua functions from Forth (depending on how fancy you want to get, probably takes 20-50 tokens for a reasonable wrapper)
  • [ and ] are spelled _lb and _rb for some reason.
  • The inner interpreter works in a non-standard way that will make it hard to call arbitrary Forth from Lua outside of eval - this is probably worth rethinking.

A few ways to make it smaller

  • A few functions could probably be moved from Lua to the initial Forth definitions as-is, although since : and ; depend on them this will be a bit messy.
  • With the right Forth/Lua binding util, even more functions could probably be moved.
  • With some internal representation changes, even _interp and eval could probably be (mostly) boostrapped.

Anyway, enjoy. This may not be useful for very much, but the question of how little material you need to bootstrap a reasonable eval is still kind of a fun question.

Cart #bbforth-1 | 2023-07-11 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
5

P#131852 2023-07-11 21:17 ( Edited 2023-07-11 21:23)


[Please log in to post a comment]