-
Notifications
You must be signed in to change notification settings - Fork 23
Implicit Compilation in zeptoforth
Note: this is a new feature that is first available in release 1.5.0.
Implicit compilation is a feature of zeptoforth that allows the evaluation of code from the REPL through compiling it in RAM as a temporary anonymous word which is then executed, and afterwards immediately forgotten. It is initiated by evaluating any one of if
, begin
, do
, ?do
, or case
while in interpretation mode, and execution is triggered by compiling a matching then
, for if
; a matching end
, until
, repeat
(if while
had been specified), or again
for begin
; a matching loop
or +loop
for do
or ?do
, or a matching endcase
or endcasestr
for case
. Once it has been started no code aside from immediate words is evaluated until the outermost block is matched. All operations permissible during ordinary compilation are permissible during implicit compilation except for ones which require space alloted in the RAM dictionary to remain valid afterwards.
Examples of implicit compilation in action include:
begin 2,0 3,0 4,0 5,0 { D: a D: b D: c D: x } a x 2 fi** f* b x f* d+ c d+ f. end 69,0 ok
true if false if ." *A*" else ." *B*" then else ." *C*" then *B* ok
10 0 do i . loop 0 1 2 3 4 5 6 7 8 9 ok
10 begin ?dup while rng::random . 1- repeat -483139579 65935751 618859148 -1719283164 1699951016 1297462867 894730864 1042467505 831079856 56931937 ok
begin ." *" 250 ms key? until ******************** ok
1 case 0 of ." foo" endof 1 of ." bar" endof 2 of ." baz" endof endcase bar ok
Note that it is not safe to carry out operations which allot RAM dictionary space which is meant to persist outside of the implicitly compiled block, because all the RAM dictionary space alloted from the point that implicit compilation is initiated is reclaimed afterwards and thus is very likely to be overwritten in the future. E.g. the following is unsafe:
begin 0 [: begin ." *" 1000 ms again ;] 320 128 512 task::spawn task::run end
This is because the lambda block defined during implicit compilation will be referenced past its conclusion as it will be executing as part of a persistent task, and thus will be most likely corrupted by any future usage of the main task's RAM dictionary.
For this reason [:
does not start implicit compilation by itself, because the lambda defined by it would immediately go out of scope with the matching ;]
. If one wishes to use [:
during implicit compilation, one must contain it in some other block, such as a begin
... end
block to give the lambda so defined a scope in which it is valid.
Defining lambdas and generally alloting space in the RAM dictionary during implicit compilation is safe as long as they do not persist outside its scope, as in the following example:
begin 256 [: { addr } 256 0 do i addr i + c! loop addr addr 256 + dump ;] with-allot end
200192A0 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F |................|
200192B0 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F |................|
200192C0 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F | !"#$%&'()*+,-./|
200192D0 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F |0123456789:;<=>?|
200192E0 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F |@ABCDEFGHIJKLMNO|
200192F0 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F |PQRSTUVWXYZ[\]^_|
20019300 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F |`abcdefghijklmno|
20019310 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F |pqrstuvwxyz{|}~.|
20019320 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F |................|
20019330 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F |................|
20019340 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF |................|
20019350 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF |................|
20019360 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF |................|
20019370 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF |................|
20019380 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF |................|
20019390 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF |................|
ok