(rontolisp) docs

Compiled eval Limitations

eval works in all three backends. In the interpreter it is the full tree-walking evaluator. The WASM and JVM compilers each emit a small tree-walking interpreter into their output (_eval/_apply/_store plus the helpers _envLookup/_lookup) that runs the form at runtime, so no separate evaluator or parser is needed.

The compiled eval (WASM and JVM) implements a lexical environment plus a persistent global environment, and aims for parity with the interpreter: self-evaluating atoms, variable references, closures, the special forms and higher-order functions (let, lambda, cond, while, setq, setf, push, pop, funcall, apply, mapcar, mapc, reduce, nested eval, ...), the built-in macros, and application of any function or interpreted closure all behave as in the interpreter. Rather than enumerate everything, the differences are listed below.

Compiled eval limitations

The compiled eval (WASM and JVM) differs from the interpreter only in these cases:

  • A lambda built at runtime does not parse lambda-list keywords. Compiled defun/lambda forms support &optional/&rest/&key (desugared at compile time), and calling such a compiled variadic function from eval works. But a lambda that only exists inside an eval'd form, e.g. (eval '(funcall (lambda (&rest r) r) 1 2)), binds its parameters positionally — &rest is treated as an ordinary parameter name — and its argument count is not checked. A runtime lambda without lambda-list keywords reports a wrong argument count as a program-error, as the interpreter does. A definition handed to compile by the program's own top-level definitions is compiled ahead and is not subject to this.
  • Global variables are shared between compiled code and eval. A setq/defvar/defparameter/defconstant mirrors its value into the runtime eval global environment, so an eval'd expression can read a global the compiled program defined (e.g. (setq add10 (make-adder 10)) then (eval '(funcall add10 100)) returns 110). An eval'd assignment (setq, setf, push, pop) of such a variable writes the compiled variable as well, so compiled code sees it. A special variable (defvar/defparameter, in the program or in an eval'd form) is read and assigned through its current dynamic binding, as compiled code does, and an eval'd let of one binds it dynamically: a function it calls sees the binding. A (set ...) -- or (setf (symbol-value ...) ...) -- writes both as well. Only global variables are mirrored: a lexical of a top-level form -- a let/loop/do variable -- is not, matching Common Lisp, where eval evaluates in the null lexical environment ((let ((x 5)) (eval 'x)) reads the global x, and signals unbound-variable when there is none, on every backend).
  • pprint-logical-block is not supported. The other built-in macros are, along with block, return-from, tagbody, go, catch, throw, unwind-protect, progv, flet and labels: a table of run-time expansions covers them, and an eval'd defvar makes its name special. pprint-logical-block signals undefined-function naming it, as any operator no function answers does. In a file read by a runtime load, in-package, defpackage and declaim are not processed and answer nil.
  • A symbol macro an eval'd form defines exists only for eval'd forms, and a program's own top-level define-symbol-macro only for its compiled code. An eval'd define-symbol-macro is read and assigned through its expansion by later eval'd forms. A top-level one of the program is consumed when the program compiles, like defmacro, so an eval'd form does not see it. An &environment parameter of an eval'd macrolet is nil.
  • setf of a place reaches car, cdr and their compositions, first ... tenth, rest, nth, gethash, aref, svref, row-major-aref, elt, char, schar, getf, get, slot-value, symbol-value and any place with a (setf name) function the program defines, a defclass accessor among them. An eval'd form assigning any other place, a structure accessor or a (setf name) function only an eval'd defun defines among them, assigns nothing.
  • The standard functions, variables and macros a form can reach depend on how the program builds it. Functions resolve through the registry of the functions compiled into the output. Most are always there; typep, coerce, aref, format, error, sqrt and the like, and the run-time expansion of a macro, are compiled in only for a program that names them, and a form the program spells as data names them: (eval '(typep x 'integer)), (eval (list 'aref v 0)) and (eval '(dolist (x l) (print x))) work. A form read or built at run time ((eval (read)), (eval (intern name)), a runtime load) can name anything, so such a program carries every standard function and variable, which makes it larger. The exceptions are sleep, which on --component puts the module in async mode, and load, which keeps every library definition from being pruned: the program has to name them.
  • defmacro and backquote are compile-time only. On the compilation path, user macros are fully expanded (and their definitions consumed) before the compilers run, and backquote templates are expanded by the reader; the runtime eval/read of a compiled program recognizes neither defmacro nor the backquote character. The same holds for macroexpand/macroexpand-1: a call with a literal quoted argument is folded to its expansion at compile time, and in the runtime eval these functions signal an error (gensym, by contrast, works — it has a first-class wrapper).
  • defstruct is compile-time only. A top-level defstruct is expanded into its generated functions before compilation, so calling a constructor/accessor/predicate from eval works, but an eval'd form can neither define a new structure nor use an accessor as a setf place. A #S(...) literal is likewise resolved before compilation, so it is not recognized inside eval either.
  • The CLOS subset is compile-time only. Like defstruct, top-level defclass/defgeneric/defmethod forms are expanded before compilation — calling a generic function, a reader/accessor, or a constructor from eval works, but an eval'd form cannot define classes or methods.
  • The rontolisp package functions are not supported. rontolisp:version, rontolisp:fetch, rontolisp:http-handler, rontolisp:await, rontolisp:futurep, rontolisp:json-parse and rontolisp:json-stringify are compiled directly (constants, inline calls or spliced-in library functions); in the runtime eval/load they signal undefined-function.

These differences come from the design: the runtime eval resolves operators by name against a compile-time registry of the functions that were actually compiled into the output, and built-in functions are shared with the compiled code.