38 lines
600 B
Forth
38 lines
600 B
Forth
\ nsforth a small and slow implementation of a subset of Forth.
|
|
|
|
: sq ( n -- n ) dup * ;
|
|
: foo dup * 10 + ;
|
|
|
|
5 foo . 42 .s 3 zero? .
|
|
see foo \ see foo definition
|
|
' foo .
|
|
' foo 32 .s swap .s
|
|
create array1 4 allot \ 4 elements
|
|
create array? 1 , 2 ,
|
|
|
|
: inc ( n1 -- n1 ) 1 + ;
|
|
: dec ( n1 -- n1 ) 1 - ;
|
|
4 inc .
|
|
4 dec .
|
|
|
|
\ Forth is a concatenative language, that is:
|
|
: fn1 2 + ;
|
|
: fn2 3 * ;
|
|
: fn3 4 * ;
|
|
|
|
3 fn1 fn2 fn3 .
|
|
forget fn1 forget fn2 forget fn3
|
|
\ 4 fn1 <- error
|
|
: let create ;
|
|
|
|
cst
|
|
-4 3 - .
|
|
\ <cond> if <body> end
|
|
2 3 + 10 < if
|
|
3 sq .
|
|
end
|
|
|
|
let var1 2 ,
|
|
\ <cond> while <body> end
|
|
|
|
\ import file.fs
|