compiler: make space for compiling into the VM bytecode

This commit is contained in:
2026-04-25 20:44:37 +03:00
parent fe3ad80826
commit 190ec94e14
2 changed files with 20 additions and 2 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
(library (library
(name compiler) (name compiler)
(libraries parser)) (libraries parser vm))
+19 -1
View File
@@ -39,6 +39,24 @@ type expression =
| Set of variable * expression | Set of variable * expression
| Begin of expression list | Begin of expression list
(* IMPORTANT:
This is a predefined global table.
Some symbols in the standard library have special importance, so
they must have "special" values that exist before the program is
even compiled.
For example, the print function is always global. It must always
be global number 0. Most other primitives have similar assignments.
The runtime is not stable as it is now, so a program compiled with
a current version of the compiler may not remain functional with
later versions of the runtime. The source program should remain
good though.
*)
let default_global_table =
SymbolTable.of_list [
("print", 0);
("add", 1)
]
(* extract all defined global symbols, given the top-level expressions (* extract all defined global symbols, given the top-level expressions
and definitions of a program and definitions of a program
@@ -57,7 +75,7 @@ let extract_globals (top : Core_ast.top_level list) =
aux (SymbolTable.add sym (id ()) tbl) rest aux (SymbolTable.add sym (id ()) tbl) rest
| Expr _ :: rest -> | Expr _ :: rest ->
aux tbl rest aux tbl rest
in aux SymbolTable.empty top in aux default_global_table top
(* The current lexical scope is simply a linked list of entries, (* The current lexical scope is simply a linked list of entries,
and each symbol access will be resolved as an access to an index and each symbol access will be resolved as an access to an index