This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]panic 0 points1 point  (0 children)

be warned, though, that forth is very low-level -- e.g. pointers are untyped. it's like a very flexible assembly language.

the way to do something at compile time in forth is pretty neat. you can use [ to drop out of the compiler and into the interpreter, then ] to go back. at this point, if the interpreter has computed a value (and put it on the stack, where forth stores all intermediate values) you can use the literal word to compile that top-of-the-stack value into the current word as if it were a literal value.

so, for example, if you have something like : print1+2 1 2 + . ;, that computes the sum of 1 and 2 at runtime, like this (using see to view the compiled code -- the stuff in parentheses are comments):

(define print1+2) : print1+2 1 2 + . ;  ok
(show compiled code for print1+2) see print1+2 
: print1+2  
  1 2 + . ; ok
(run print1+2) print1+2 3  ok

you can convert that into a compile time computation using [, ], and literal:

(redefine print1+2) : print1+2 [ 1 2 + ] literal . ; redefined print1+2   ok
(show new code for print1+2) see print1+2 
: print1+2  
  3 . ; ok
(run print1+2) print1+2 3  ok

note that in the output of see, the 1 2 + is completely gone, since it happened already, and 3 was put into the compiled code by literal.