2021-01-04 14:23:54 +01:00
|
|
|
h@l@h@!h@C+h!k1k0-h@$k:k0-h@k1k0-+$h@C+h!ih@!h@C+h!kefh@!h@C+h!l!
|
2020-12-30 13:17:09 +01:00
|
|
|
h@l@h@!h@C+h!k1k0-h@$k h@k1k0-+$h@C+h!ih@!h@C+h!kefh@!h@C+h!l!
|
2020-12-30 12:09:11 +01:00
|
|
|
|
2020-12-30 16:52:36 +01:00
|
|
|
h@l@ h@!h@C+h! k1k0-h@$ k\h@k1k0-+$ h@C+h!
|
2020-12-30 14:00:20 +01:00
|
|
|
i h@!h@C+h!
|
|
|
|
kkf h@!h@C+h!
|
|
|
|
kLf h@!h@C+h!
|
|
|
|
k:k0- h@!h@C+h!
|
|
|
|
k=f h@!h@C+h!
|
|
|
|
kJf h@!h@C+h!
|
|
|
|
k0k5-C* h@!h@C+h!
|
|
|
|
kef h@!h@C+h!
|
|
|
|
l!
|
|
|
|
|
|
|
|
\ **Now we can use single-line comments!**
|
|
|
|
|
|
|
|
\ planckforth -
|
2021-01-02 05:29:46 +01:00
|
|
|
\ Copyright (C) 2021 nineties
|
2020-12-30 14:00:20 +01:00
|
|
|
|
2021-01-02 05:14:16 +01:00
|
|
|
\ This project aims to bootstrap a Forth interpreter
|
|
|
|
\ from hand-written tiny ELF binary.
|
2020-12-30 14:38:27 +01:00
|
|
|
|
|
|
|
\ In the 1st stage, only single character words are registered
|
|
|
|
\ in the dictionary.
|
|
|
|
\ List of builtin words:
|
2021-01-02 01:35:07 +01:00
|
|
|
\ 'Q' ( -- ) Exit the process
|
|
|
|
\ 'C' ( -- n ) The size of Cells
|
|
|
|
\ 'h' ( -- a-addr ) The address of 'here' cell
|
|
|
|
\ 'l' ( -- a-addr ) The address of 'latest' cell
|
|
|
|
\ 'k' ( -- c ) Read character
|
|
|
|
\ 't' ( c -- ) Print character
|
|
|
|
\ 'j' ( -- ) Unconditional branch
|
|
|
|
\ 'J' ( n -- ) Jump if a == 0
|
|
|
|
\ 'f' ( c -- xt ) Get execution token of c
|
|
|
|
\ 'x' ( xt -- ... ) Run the execution token
|
|
|
|
\ '@' ( a-addr -- w ) Load value from addr
|
|
|
|
\ '!' ( w a-addr -- ) Store value to addr
|
|
|
|
\ '?' ( c-addr -- c ) Load byte from addr
|
|
|
|
\ '$' ( c c-addr -- ) Store byte to addr
|
|
|
|
\ 'd' ( -- a-addr ) Get data stack pointer
|
|
|
|
\ 'D' ( a-addr -- ) Set data stack pointer
|
|
|
|
\ 'r' ( -- a-addr ) Get return stack pointer
|
|
|
|
\ 'R' ( a-addr -- ) Set return stack pointer
|
|
|
|
\ 'i' ( -- a-addr ) Get the interpreter function
|
|
|
|
\ 'e' ( -- ) Exit current function
|
|
|
|
\ 'L' ( -- u ) Load immediate
|
2021-01-04 14:21:48 +01:00
|
|
|
\ 'S' ( -- c-addr ) Load string literal
|
2021-01-02 01:35:07 +01:00
|
|
|
\ '+' ( a b -- c ) c = (a + b)
|
|
|
|
\ '-' ( a b -- c ) c = (a - b)
|
|
|
|
\ '*' ( a b -- c ) c = (a * b)
|
|
|
|
\ '/' ( a b -- c ) c = (a / b)
|
|
|
|
\ '%' ( a b -- c ) c = (a % b)
|
|
|
|
\ '&' ( a b -- c ) c = (a & b)
|
|
|
|
\ '|' ( a b -- c ) c = (a | b)
|
|
|
|
\ '^' ( a b -- c ) c = (a ^ b)
|
|
|
|
\ '<' ( a b -- c ) c = (a < b)
|
2021-01-02 05:14:16 +01:00
|
|
|
\ '=' ( a b -- c ) c = (a == b)
|
2020-12-30 14:38:27 +01:00
|
|
|
|
|
|
|
\ The 1st stage interpreter repeats execution of k, f and x.
|
|
|
|
\ There following line is an example program of planckforth
|
|
|
|
\ which prints "Hello World!\n"
|
|
|
|
\ --
|
|
|
|
\ kHtketkltkltkotk tkWtkotkrtkltkdtk!tk:k0-tQ
|
|
|
|
\ --
|
|
|
|
\ This code repeats that 'k' reads a character and 't' prints it.
|
|
|
|
\ Note that ':' (58) minus '0' (48) is '\n' (10).
|
|
|
|
|
2021-01-02 05:14:16 +01:00
|
|
|
\ The structure of the dictionary.
|
2020-12-30 14:38:27 +01:00
|
|
|
\ +------+----------+---------+------------+---------------+
|
|
|
|
\ | link | len+flag | name... | padding... | code field ...|
|
|
|
|
\ +------+----------+---------+------------+---------------+
|
|
|
|
\ - link pointer to the previous entry (CELL byte)
|
2021-01-01 10:37:41 +01:00
|
|
|
\ - length of the name (6 bits)
|
|
|
|
\ - smudge bit (1 bit)
|
|
|
|
\ - immediate bit (1 bit)
|
2020-12-30 14:38:27 +01:00
|
|
|
\ - characters of the name (N bits)
|
|
|
|
\ - padding to align CELL boundary if necessary.
|
|
|
|
\ - codewords and datawords (CELL-bye aligned)
|
|
|
|
|
|
|
|
\ The code group at the beginning of this file
|
|
|
|
\ defines ' ' and '\n' as no-op operation and
|
|
|
|
\ '\' to read following characters until '\n'.
|
|
|
|
\ Since I couldn't write a comment at the beginning,
|
|
|
|
\ I repost the definition of '\' for explanation.
|
|
|
|
\ --
|
|
|
|
\ h@ ( save addr of new entry )
|
|
|
|
\ l@ h@!h@C+h! ( set link pointer. *here++ = latest )
|
2020-12-30 14:41:49 +01:00
|
|
|
\ k1k0-h@$ k\h@k1k0-+$ h@C+h! ( write the name '\' and its length )
|
2020-12-30 14:38:27 +01:00
|
|
|
\ i h@!h@C+h! ( docol )
|
|
|
|
\ kkf h@!h@C+h! ( key )
|
|
|
|
\ kLf h@!h@C+h! ( lit )
|
|
|
|
\ k:k0- h@!h@C+h! ( '\n' )
|
|
|
|
\ k=f h@!h@C+h! ( = )
|
|
|
|
\ kJf h@!h@C+h! ( branch )
|
|
|
|
\ k0k5-C* h@!h@C+h! ( -5*CELL )
|
|
|
|
\ kef h@!h@C+h! ( exit )
|
|
|
|
\ l! ( set latest to this new entry. )
|
|
|
|
\ --
|
|
|
|
|
2020-12-30 14:48:07 +01:00
|
|
|
\ That's all for the brief explanation. Let's restart bootstrap!
|
|
|
|
|
2020-12-30 14:51:29 +01:00
|
|
|
\ The COMMA operator
|
|
|
|
\ ',' ( a -- ) Store a to 'here' and increment 'here' CELL bytes.
|
2020-12-30 16:52:36 +01:00
|
|
|
h@l@ h@!h@C+h! k1k0-h@$ k,h@k1k0-+$ h@C+h!
|
2020-12-30 14:51:29 +01:00
|
|
|
i h@!h@C+h! \ docol
|
2020-12-30 15:23:07 +01:00
|
|
|
\ store 'a' to here
|
2020-12-30 14:51:29 +01:00
|
|
|
khf h@!h@C+h!
|
|
|
|
k@f h@!h@C+h!
|
2020-12-30 15:23:07 +01:00
|
|
|
k!f h@!h@C+h!
|
|
|
|
\ here <- here + CELL
|
2020-12-30 14:51:29 +01:00
|
|
|
khf h@!h@C+h!
|
|
|
|
k@f h@!h@C+h!
|
|
|
|
kCf h@!h@C+h!
|
2020-12-30 15:23:07 +01:00
|
|
|
k+f h@!h@C+h!
|
2020-12-30 14:51:29 +01:00
|
|
|
khf h@!h@C+h!
|
2020-12-30 15:23:07 +01:00
|
|
|
k!f h@!h@C+h!
|
|
|
|
\ exit
|
|
|
|
kef h@!h@C+h!
|
2020-12-30 14:51:29 +01:00
|
|
|
l!
|
|
|
|
|
2020-12-30 15:42:38 +01:00
|
|
|
\ TICK-like operator
|
|
|
|
\ '\'' ( "c" -- xt ) Get execution token of following character
|
|
|
|
\ NB: This definition is different from the usual definition of tick
|
|
|
|
\ because it does not skip leading spaces and can read only a single
|
|
|
|
\ character. It will be redefined in later stage.
|
2020-12-30 16:52:36 +01:00
|
|
|
h@l@, k1k0-h@$ k'h@k1k0-+$ h@C+h!
|
2020-12-30 15:42:38 +01:00
|
|
|
i, kkf, kff, kef,
|
|
|
|
l!
|
|
|
|
|
2020-12-30 16:52:53 +01:00
|
|
|
\ Utility for defining a word
|
|
|
|
\ 'c' ( "c" -- w )
|
|
|
|
\ Read character, create new word then push its address.
|
|
|
|
\ 'latest' will not be updated.
|
|
|
|
h@l@, k1k0-h@$ kch@k1k0-+$ h@C+h!
|
|
|
|
i, 'h, '@, 'l, '@, ',,
|
2021-01-04 13:57:52 +01:00
|
|
|
'L, k1k0-, 'h, '@, '$, \ fill 1
|
|
|
|
'k, 'h, '@, 'L, k1k0-, '+, '$, \ fill "c"
|
|
|
|
'L, k0k0-, 'h, '@, 'L, k2k0-, '+, '$, \ fill "\0"
|
2020-12-31 00:26:06 +01:00
|
|
|
'h, '@, 'C, '+, 'h, '!,
|
|
|
|
'e, l!
|
2020-12-30 16:52:53 +01:00
|
|
|
|
2020-12-30 16:59:24 +01:00
|
|
|
\ '_' ( a -- ) DROP
|
|
|
|
c_ i, 'd, 'C, '+, 'D, 'e, l!
|
|
|
|
|
2020-12-30 22:25:40 +01:00
|
|
|
\ '#' ( a -- a a ) DUP
|
|
|
|
c# i, 'd, '@, 'e, l!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\ Implementations of TOR and FROMR are a bit tricky.
|
|
|
|
\ Since return-address will be placed at the top of return stack,
|
|
|
|
\ the code in the body of these function have to manipulate
|
|
|
|
\ 2nd element of the stack.
|
|
|
|
|
|
|
|
\ '{' ( a -- R:a ) TOR
|
|
|
|
\ Move value from data stack to return stack.
|
2020-12-31 00:26:06 +01:00
|
|
|
c{ i,
|
2020-12-30 22:25:40 +01:00
|
|
|
'r, 'r, '@, \ ( a rsp ret )
|
2020-12-30 22:36:20 +01:00
|
|
|
'r, 'C, '-, '#, \ ( a rsp ret rsp-1 rsp-1 )
|
|
|
|
'R, \ ( a rsp+1 ret rsp ) extend return stack
|
|
|
|
'!, \ ( a rsp+1 ) store return address to the top
|
2020-12-30 22:25:40 +01:00
|
|
|
'!, \ store a to the 2nd
|
2020-12-31 00:26:06 +01:00
|
|
|
'e, l!
|
2020-12-30 22:25:40 +01:00
|
|
|
|
|
|
|
\ '}' ( R:a -- a ) FROMR
|
|
|
|
\ Move value from return stack to data stack.
|
2020-12-31 00:26:06 +01:00
|
|
|
c} i,
|
2020-12-30 22:25:40 +01:00
|
|
|
'r, 'C, '+, '@, \ ( a ) load 2nd value
|
|
|
|
'r, '@, \ ( a ret ) load return addr
|
2020-12-30 22:36:20 +01:00
|
|
|
'r, 'C, '+, '#, \ ( a ret rsp+1 rsp+1 )
|
2020-12-30 22:25:40 +01:00
|
|
|
'R, \ ( a ret rsp ) reduce return stack
|
|
|
|
'!, \ ( a , R:ret ) store return addr to top of return stack
|
2020-12-31 00:26:06 +01:00
|
|
|
'e, l!
|
2020-12-30 22:25:40 +01:00
|
|
|
|
2020-12-30 22:39:43 +01:00
|
|
|
\ 'o' ( a b -- a b a ) OVER
|
|
|
|
co i, 'd, 'C, '+, '@, 'e, l!
|
2020-12-30 22:37:08 +01:00
|
|
|
|
|
|
|
\ '~' ( a b -- b a ) SWAP
|
|
|
|
c~ i,
|
2020-12-30 22:39:43 +01:00
|
|
|
'o, \ ( a b a )
|
2020-12-30 22:37:08 +01:00
|
|
|
'{, \ ( a b , R:a )
|
|
|
|
'd, 'C, '+, \ ( a b sp+1 , R:a )
|
|
|
|
'!, \ ( b , R:a )
|
|
|
|
'}, \ ( b a )
|
2020-12-31 00:26:06 +01:00
|
|
|
'e, l!
|
2020-12-30 22:37:08 +01:00
|
|
|
|
2020-12-30 23:50:02 +01:00
|
|
|
\ 'B' ( c -- ) C-COMMA
|
|
|
|
\ Store byte 'c' to here and increment it
|
2020-12-31 01:40:29 +01:00
|
|
|
cB i, 'h, '@, '$, 'h, '@, 'L, k1k0-, '+, 'h, '!, 'e, l!
|
2020-12-30 22:39:43 +01:00
|
|
|
|
2021-01-02 10:01:01 +01:00
|
|
|
\ 'm' ( c-addr u -- ) CMOVE,
|
2020-12-31 11:36:45 +01:00
|
|
|
\ Copy u bytes from c-addr to here,
|
|
|
|
\ increment here u bytes.
|
2020-12-31 05:10:18 +01:00
|
|
|
cm i,
|
|
|
|
\ <loop>
|
2020-12-31 11:36:45 +01:00
|
|
|
'#, 'J, k>k0-C*, \ goto <exit> if u=0
|
2020-12-31 05:10:18 +01:00
|
|
|
'{, \ preserve u
|
2020-12-31 11:36:45 +01:00
|
|
|
'#, '?, 'B, \ copy byte
|
|
|
|
'L, k1k0-, '+, \ increment c-addr
|
2020-12-31 05:10:18 +01:00
|
|
|
'}, 'L, k1k0-, '-, \ decrement u
|
2020-12-31 11:36:45 +01:00
|
|
|
'j, k0k?-C*, \ goto <loop>
|
2020-12-31 05:10:18 +01:00
|
|
|
\ <exit>
|
2020-12-31 11:36:45 +01:00
|
|
|
'_, '_,
|
2020-12-31 05:10:18 +01:00
|
|
|
'e, l!
|
|
|
|
|
2020-12-31 00:09:22 +01:00
|
|
|
\ 'a' ( c-addr -- a-addr ) ALIGNED
|
2020-12-31 16:42:30 +01:00
|
|
|
\ Round up to a nearlest multiple of CELL
|
2020-12-31 00:09:22 +01:00
|
|
|
ca i,
|
2020-12-31 00:13:42 +01:00
|
|
|
'L, Ck1k0--, '+, \ ( a+CELL-1 )
|
|
|
|
'L, k0k0-C-, \ ( a+CELL-1 ~(CELL-1) )
|
2020-12-31 00:26:06 +01:00
|
|
|
'&,
|
|
|
|
'e, l!
|
2020-12-31 00:09:22 +01:00
|
|
|
|
|
|
|
\ 'A' ( -- ) ALIGN
|
2020-12-31 16:42:30 +01:00
|
|
|
\ Round up 'here' to a nearlest multiple of CELL
|
2020-12-31 00:09:22 +01:00
|
|
|
cA i, 'h, '@, 'a, 'h, '!, 'e, l!
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
\ 'E' ( c-addr1 c-addr2 -- flag ) STR=
|
|
|
|
\ Compate null-terminated strings.
|
2020-12-31 01:40:42 +01:00
|
|
|
\ Return 1 if they are same 0 otherwise.
|
|
|
|
cE i,
|
|
|
|
\ <loop>
|
2021-01-04 13:57:52 +01:00
|
|
|
'o, '?, 'o, '?, \ ( c-addr1 c-addr2 c1 c2 )
|
|
|
|
'o, '=, 'J, k=k0-C*, \ goto <not_equal> if c1<>c2
|
|
|
|
'J, kAk0-C*, \ goto <equal> if c1==0
|
|
|
|
'L, k1k0-, '+, '~, \ increment c-addr2
|
|
|
|
'L, k1k0-, '+, '~, \ increment c-addr1
|
|
|
|
'j, k0kC-C*, \ goto <loop>
|
2020-12-31 01:40:42 +01:00
|
|
|
\ <not_equal>
|
|
|
|
'_, '_, '_, 'L, k0k0-, 'e,
|
2021-01-04 13:57:52 +01:00
|
|
|
\ <equal>
|
|
|
|
'_, '_, 'L, k1k0-, 'e,
|
|
|
|
l!
|
|
|
|
|
|
|
|
\ 'z' ( c-addr -- u ) STRLEN
|
|
|
|
\ Calculate length of string
|
|
|
|
cz i,
|
|
|
|
'L, k0k0-, \ 0
|
|
|
|
\ <loop>
|
|
|
|
'o, '?, 'J, k;k0-C*, \ goto <exit> if '\0'
|
|
|
|
'L, k1k0-, '+, '~, \ increment u
|
|
|
|
'L, k1k0-, '+, '~, \ increment c-addr
|
|
|
|
'j, k0k=-C*, \ goto <loop>
|
|
|
|
\ <exit>
|
|
|
|
'~, '_, 'e,
|
2020-12-31 01:40:42 +01:00
|
|
|
l!
|
|
|
|
|
2020-12-31 03:59:09 +01:00
|
|
|
\ 's' ( c -- n)
|
|
|
|
\ Return 1 if c==' ' or c=='\n', 0 otherwise.
|
|
|
|
cs i, '#, 'L, k , '=, '~, 'L, k:k0-, '=, '|, 'e, l!
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
\ 'W' ( "name" -- c-addr )
|
2020-12-31 03:59:09 +01:00
|
|
|
\ Skip leading spaces (' ' and '\n'),
|
|
|
|
\ Read name, then return its address and length.
|
2021-01-01 10:37:41 +01:00
|
|
|
\ The maximum length of the name is 63. The behavior is undefined
|
|
|
|
\ when the name exceeds 63 characters,
|
2020-12-31 03:59:09 +01:00
|
|
|
\ Note that it returns the address of statically allocated buffer,
|
|
|
|
\ so the content will be overwritten each time 'w' executed.
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
\ Allocate buffer of 63+1 bytes or more,
|
2020-12-31 03:59:09 +01:00
|
|
|
\ push the address for compilation of 'w'
|
2021-01-04 13:57:52 +01:00
|
|
|
h@ # kpk0-+ h! A
|
2020-12-31 06:44:39 +01:00
|
|
|
cW~
|
2020-12-31 03:59:09 +01:00
|
|
|
i,
|
|
|
|
\ skip leading spaces
|
|
|
|
'k, '#, 's, 'J, k4k0-C*, '_, 'j, k0k7-C*,
|
|
|
|
\ p=address of buffer
|
|
|
|
'L, #, '~,
|
|
|
|
\ <loop>
|
|
|
|
\ ( p c )
|
|
|
|
'o, '$, \ store c to p
|
|
|
|
'L, k1k0-, '+, \ increment p
|
|
|
|
'k, '#, 's, 'J, k0k9-C*, \ goto <loop> if c is not space
|
2021-01-04 13:57:52 +01:00
|
|
|
'_,
|
|
|
|
'L, k0k0-, 'o, '$, \ fill \0
|
|
|
|
'_, 'L, , \ return buf
|
2020-12-31 03:59:09 +01:00
|
|
|
'e, l!
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
\ 'F' ( c-addr -- w )
|
2020-12-31 05:27:25 +01:00
|
|
|
\ Lookup multi-character word from dictionary.
|
2020-12-31 07:10:54 +01:00
|
|
|
\ Return 0 if the word is not found.
|
2021-01-01 10:37:41 +01:00
|
|
|
\ Entries with smudge-bit=1 are ignored.
|
2020-12-31 05:27:25 +01:00
|
|
|
cF i,
|
2020-12-31 06:45:02 +01:00
|
|
|
'l, '@,
|
2021-01-04 13:57:52 +01:00
|
|
|
\ <loop> ( addr it )
|
|
|
|
'#, 'J, kEk0-C*, \ goto <exit> if it=NULL
|
|
|
|
'#, 'C, '+, '?, \ ( addr it len+flag )
|
2021-01-01 10:37:41 +01:00
|
|
|
'L, k@, '&, \ test smudge-bit of it
|
|
|
|
'J, k4k0-C*,
|
2020-12-31 05:27:25 +01:00
|
|
|
\ <1>
|
2021-01-01 10:37:41 +01:00
|
|
|
\ smudge-bit=1
|
|
|
|
'@, \ load link
|
|
|
|
'j, k0k>-C*, \ goto <loop>
|
|
|
|
\ <2>
|
|
|
|
\ smudge-bit=0
|
2021-01-04 13:57:52 +01:00
|
|
|
'o, 'o, \ ( addr it addr it )
|
|
|
|
'L, Ck1k0-+, '+, \ address of name
|
|
|
|
\ ( addr1 it addr1 addr2 )
|
|
|
|
'E, 'J, k0k:-C*, \ goto <1> if different name
|
2020-12-31 05:27:25 +01:00
|
|
|
\ <exit>
|
2021-01-04 13:57:52 +01:00
|
|
|
'{, '_, '}, \ Drop addr, return it
|
2020-12-31 05:27:25 +01:00
|
|
|
'e, l!
|
2020-12-31 05:10:18 +01:00
|
|
|
|
2020-12-31 07:10:54 +01:00
|
|
|
\ 'G' ( w -- xt )
|
|
|
|
\ Get CFA of the word
|
|
|
|
cG i,
|
|
|
|
'C, '+, '#, '?, \ ( addr len+flag )
|
2021-01-01 10:37:41 +01:00
|
|
|
'L, kok0-, '&, \ take length
|
2020-12-31 07:10:54 +01:00
|
|
|
'+, \ add length to the addr
|
2021-01-04 13:57:52 +01:00
|
|
|
'L, k2k0-, '+, \ add 2 to the addr (len+field and \0)
|
2020-12-31 07:10:54 +01:00
|
|
|
'a, \ align
|
|
|
|
'e, l!
|
2020-12-31 07:52:39 +01:00
|
|
|
|
2021-01-04 14:37:28 +01:00
|
|
|
\ 'M' ( -- a-addr)
|
2020-12-31 09:46:11 +01:00
|
|
|
\ The state variable
|
2020-12-31 07:52:39 +01:00
|
|
|
\ 0: immediate mode
|
|
|
|
\ 1: compile mode
|
2020-12-31 09:46:11 +01:00
|
|
|
h@ k0k0-, \ allocate 1 cell and fill 0
|
|
|
|
cM~ i, 'L, , 'e, l!
|
2020-12-31 07:52:39 +01:00
|
|
|
|
2020-12-31 10:04:08 +01:00
|
|
|
\ 'I'
|
|
|
|
\ The 2nd Stage Interpreter
|
|
|
|
cI i,
|
|
|
|
\ <loop>
|
2020-12-31 10:23:02 +01:00
|
|
|
'W, \ read name from input
|
|
|
|
'F, \ find word
|
|
|
|
'M, '@, \ read state
|
|
|
|
'J, kAk0-C*, \ goto <immediate> if state=0
|
|
|
|
\ <compile>
|
|
|
|
'#, 'C, '+, '?, \ ( w len+flag )
|
|
|
|
'L, k@k@+, '&, \ test immediate bit
|
|
|
|
'L, k0k0-, '=,
|
|
|
|
'J, k5k0-C*, \ goto <immediate> if immediate-bit=1
|
2021-01-02 06:09:18 +01:00
|
|
|
'G, ',, \ compile
|
2020-12-31 12:22:51 +01:00
|
|
|
'j, k0kE-C*, \ goto <loop>
|
2020-12-31 10:23:02 +01:00
|
|
|
\ <immediate>
|
2021-01-02 06:09:18 +01:00
|
|
|
'G, 'x, \ execute
|
2020-12-31 10:23:02 +01:00
|
|
|
'j, k0kI-C*, \ goto <loop>
|
2020-12-31 10:04:08 +01:00
|
|
|
l!
|
|
|
|
|
2020-12-31 11:01:57 +01:00
|
|
|
I \ Enter 2nd Stage
|
|
|
|
|
|
|
|
\ === 2nd Stage Interpreter ===
|
|
|
|
|
|
|
|
r C + R \ Drop 1st stage interpreter from call stack
|
|
|
|
|
2020-12-31 13:06:22 +01:00
|
|
|
\ '\'' ( "name" -- xt )
|
|
|
|
\ Redefine existing '\'' which uses 'k' and 'f'
|
|
|
|
\ to use 'W' and 'F'.
|
|
|
|
c ' i , ' W , ' F , ' G , ' e , l !
|
|
|
|
|
|
|
|
\ [ immediate ( -- )
|
|
|
|
\ Switch to immediate mode
|
|
|
|
c [ i , ' L , k 0 k 0 - , ' M , ' ! , ' e , l !
|
2020-12-31 15:32:07 +01:00
|
|
|
\ Set immediate-bit of [
|
2020-12-31 13:06:22 +01:00
|
|
|
l @ C + # { ? k @ k @ + | } $
|
|
|
|
|
|
|
|
\ ] ( -- )
|
|
|
|
\ Switch to compile mode
|
|
|
|
c ] i , ' L , k 1 k 0 - , ' M , ' ! , ' e , l !
|
2020-12-31 12:23:08 +01:00
|
|
|
|
2021-01-01 10:37:41 +01:00
|
|
|
\ : ( "name" -- ) COLON
|
|
|
|
\ Read name, create word with smudge=1,
|
2020-12-31 12:23:08 +01:00
|
|
|
\ compile 'docol' and enter compile mode.
|
|
|
|
c : i ,
|
2021-01-04 13:57:52 +01:00
|
|
|
' A , \ align here
|
2020-12-31 16:23:27 +01:00
|
|
|
' h , ' @ ,
|
2021-01-04 13:57:52 +01:00
|
|
|
' l , ' @ , ' , , \ fill link
|
|
|
|
' l , ' ! , \ update latest
|
|
|
|
' W , \ read name ( addr )
|
|
|
|
' # , ' z , ' # , \ ( addr len len )
|
|
|
|
' L , k @ , ' | , \ set smudge-bit
|
|
|
|
' B , \ fill length + smudge-bit
|
|
|
|
' m , \ fill name
|
|
|
|
' L , k 0 k 0 - , ' B , \ fill \0
|
|
|
|
' A , \ align here
|
|
|
|
' i , ' , , \ compile docol
|
|
|
|
' ] , \ enter compile mode
|
2020-12-31 12:23:08 +01:00
|
|
|
' e , l !
|
|
|
|
|
2021-01-01 10:37:41 +01:00
|
|
|
\ ; ( -- ) SEMICOLON
|
|
|
|
\ Compile 'exit', unsmudge latest, and enter immediate mode.
|
2020-12-31 12:23:08 +01:00
|
|
|
c ; i ,
|
2021-01-02 07:48:47 +01:00
|
|
|
' A , \ align here
|
2020-12-31 12:23:08 +01:00
|
|
|
' L , ' e , ' , , \ compile exit
|
2021-01-01 10:37:41 +01:00
|
|
|
' l , ' @ ,
|
|
|
|
' C , ' + , ' # , ' ? ,
|
|
|
|
' L , k [ k d + , \ 0xbf
|
|
|
|
' & , ' ~ , ' $ , \ unsmudge
|
|
|
|
' [ , \ enter immediate mode
|
2020-12-31 12:23:08 +01:00
|
|
|
' e , l !
|
2020-12-31 15:32:07 +01:00
|
|
|
\ Set immediate-bit of ';'
|
2020-12-31 12:23:08 +01:00
|
|
|
l @ C + # { ? k @ k @ + | } $
|
|
|
|
|
2021-01-01 10:37:41 +01:00
|
|
|
: immediate-bit [ ' L , k @ k @ + , ] ; \ 0x80
|
|
|
|
: smudge-bit [ ' L , k @ , ] ; \ 0x40
|
|
|
|
: length-mask [ ' L , k o k 0 - , ] ; \ 0x3f
|
2020-12-31 15:34:56 +01:00
|
|
|
|
|
|
|
\ ( "name" -- )
|
|
|
|
: set-immediate
|
|
|
|
W F C + # { ? immediate-bit | } $
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Set immediate-bit of single-line comment word \
|
|
|
|
\ so that we can write comments in compile-mode.
|
|
|
|
set-immediate \
|
2020-12-31 15:47:37 +01:00
|
|
|
|
2021-01-01 10:37:41 +01:00
|
|
|
\ Set immediate-bit of 'latest'
|
2021-01-01 00:23:45 +01:00
|
|
|
: immediate
|
2021-01-01 10:37:41 +01:00
|
|
|
l @ C + # { ? immediate-bit | } $
|
2021-01-01 00:23:45 +01:00
|
|
|
;
|
|
|
|
|
2020-12-31 15:47:37 +01:00
|
|
|
: alias-builtin \ ( "name-new" "name-old" -- )
|
|
|
|
\ Create new word "name-new".
|
|
|
|
\ Copy code pointer of builtin word "name-old" to
|
|
|
|
\ the new word "name-new".
|
|
|
|
\ "name-old" must not be a FORTH word.
|
2021-01-04 13:57:52 +01:00
|
|
|
A h @ l @ , l ! \ fill link, update latest
|
|
|
|
W # z # B m \ fill length and chars of "name-new"
|
|
|
|
[ ' L , k 0 k 0 - , ] B \ fill \0
|
|
|
|
A
|
|
|
|
W F G @ , \ fill code-pointer of "name-old"
|
2020-12-31 15:47:37 +01:00
|
|
|
;
|
|
|
|
|
2020-12-31 16:04:21 +01:00
|
|
|
\ Add new names to builtin primities.
|
|
|
|
\ Instead of defining as a new FORTH word like shown below,
|
|
|
|
\ the aliases ared created by copying their code-pointer.
|
|
|
|
\ : new-name old-name ;
|
|
|
|
\ Primitive operators which manipulate program counter and return stack
|
|
|
|
\ can not be defined as a FORTH word.
|
|
|
|
|
2020-12-31 15:47:37 +01:00
|
|
|
alias-builtin bye Q
|
|
|
|
alias-builtin cell C
|
2021-01-02 01:40:40 +01:00
|
|
|
alias-builtin &here h
|
|
|
|
alias-builtin &latest l
|
2020-12-31 15:47:37 +01:00
|
|
|
alias-builtin branch j
|
|
|
|
alias-builtin 0branch J
|
|
|
|
alias-builtin execute x
|
|
|
|
alias-builtin c@ ?
|
|
|
|
alias-builtin c! $
|
|
|
|
alias-builtin sp@ d
|
|
|
|
alias-builtin sp! D
|
|
|
|
alias-builtin rp@ r
|
|
|
|
alias-builtin rp! R
|
|
|
|
alias-builtin docol i
|
|
|
|
alias-builtin exit e
|
|
|
|
alias-builtin lit L
|
2021-01-03 01:36:41 +01:00
|
|
|
alias-builtin litstring S
|
2020-12-31 15:47:37 +01:00
|
|
|
alias-builtin div /
|
|
|
|
alias-builtin mod %
|
|
|
|
alias-builtin and &
|
|
|
|
alias-builtin or |
|
|
|
|
alias-builtin xor ^
|
|
|
|
|
2021-01-09 10:20:37 +01:00
|
|
|
\ One-step indirect access for key and emit. Since k and t are
|
2021-01-09 10:20:09 +01:00
|
|
|
\ temporary implementations, we will allow them to be replaced later.
|
|
|
|
&here @ # cell + &here !
|
|
|
|
: key-func [ ' L , , ] ;
|
|
|
|
' k key-func !
|
|
|
|
: key key-func @ execute ;
|
|
|
|
|
|
|
|
&here @ # cell + &here !
|
|
|
|
: emit-func [ ' L , , ] ;
|
|
|
|
' t emit-func !
|
|
|
|
: emit emit-func @ execute ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-01 10:37:41 +01:00
|
|
|
\ Rename existing FORTH words
|
2021-01-01 18:42:59 +01:00
|
|
|
: word W ;
|
|
|
|
: find F ;
|
|
|
|
: >cfa G ;
|
|
|
|
: c, B ;
|
2021-01-09 09:13:51 +01:00
|
|
|
: memcpy, m ;
|
2021-01-04 13:57:52 +01:00
|
|
|
: strlen z ;
|
2021-01-09 09:16:29 +01:00
|
|
|
: streq E ;
|
2021-01-02 02:25:16 +01:00
|
|
|
: state M ;
|
2021-01-03 04:52:39 +01:00
|
|
|
: aligned a ;
|
|
|
|
: align A ;
|
2021-01-01 10:37:41 +01:00
|
|
|
|
2021-01-02 01:40:40 +01:00
|
|
|
: here &here @ ;
|
|
|
|
: latest &latest @ ;
|
2021-01-05 15:22:25 +01:00
|
|
|
: >dfa >cfa cell + ;
|
2021-01-02 01:40:40 +01:00
|
|
|
|
2021-01-01 10:37:41 +01:00
|
|
|
\ === Compilers ===
|
|
|
|
|
|
|
|
\ compile: ( n -- )
|
|
|
|
\ runtime: ( -- n )
|
|
|
|
: literal
|
|
|
|
lit lit , \ compile lit
|
|
|
|
, \ compile n
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( "name" -- )
|
|
|
|
\ '[compile] word' compiles word *now* even if it is immediate
|
|
|
|
: [compile]
|
|
|
|
' ,
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ ( xt -- )
|
|
|
|
\ postpone compilation of xt
|
2021-01-05 17:54:45 +01:00
|
|
|
: (compile)
|
2021-01-01 10:37:41 +01:00
|
|
|
[compile] literal \ compile 'literal'
|
|
|
|
[ ' , ] literal , \ compile ,
|
|
|
|
;
|
|
|
|
|
|
|
|
\ compile: ( "name" -- )
|
|
|
|
\ 'compile word' compiles word *later* even if it is immediate
|
|
|
|
: compile
|
2021-01-05 17:54:45 +01:00
|
|
|
' (compile)
|
2021-01-01 10:37:41 +01:00
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ ( -- xt )
|
|
|
|
: :noname
|
2021-01-03 04:52:39 +01:00
|
|
|
align
|
2021-01-02 01:40:40 +01:00
|
|
|
here
|
2021-01-01 10:37:41 +01:00
|
|
|
[ docol ] literal , \ compile docol
|
|
|
|
] \ enter compile mode
|
|
|
|
;
|
|
|
|
|
|
|
|
\ ( "name" -- xt )
|
|
|
|
\ compile time tick
|
|
|
|
: [']
|
|
|
|
' \ read name and get xt
|
|
|
|
[compile] literal \ call literal
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ === Constants ===
|
|
|
|
|
2021-01-01 10:54:42 +01:00
|
|
|
\ Since we don't have integer literals yet,
|
|
|
|
\ define small integer words for convenience
|
|
|
|
\ and readability.
|
2021-01-02 14:01:44 +01:00
|
|
|
: 0 [ key 0 key 0 - ] literal ;
|
|
|
|
: 1 [ key 1 key 0 - ] literal ;
|
|
|
|
: 2 [ key 2 key 0 - ] literal ;
|
|
|
|
: 3 [ key 3 key 0 - ] literal ;
|
2021-01-02 15:52:47 +01:00
|
|
|
: 4 [ key 4 key 0 - ] literal ;
|
2021-01-02 14:01:44 +01:00
|
|
|
: 10 [ key : key 0 - ] literal ;
|
|
|
|
: 16 [ key @ key 0 - ] literal ;
|
|
|
|
: -1 [ key 0 key 1 - ] literal ;
|
2021-01-01 10:37:41 +01:00
|
|
|
|
|
|
|
: true 1 ;
|
|
|
|
: false 0 ;
|
|
|
|
|
|
|
|
\ === Address Arithmetic ===
|
|
|
|
|
|
|
|
: cell+ cell + ;
|
|
|
|
: cell- cell - ;
|
|
|
|
: cells cell * ;
|
|
|
|
|
|
|
|
\ === Stack Manipulation ===
|
|
|
|
|
|
|
|
: drop sp@ cell+ sp! ; \ ( w -- )
|
|
|
|
: dup sp@ @ ; \ ( w -- w w )
|
|
|
|
|
|
|
|
: >r rp@ rp@ @ rp@ cell - dup rp! ! ! ; \ ( w -- R:w )
|
|
|
|
: r> rp@ cell + @ rp@ @ rp@ cell + dup rp! ! ; \ ( R:w -- w)
|
|
|
|
|
|
|
|
: swap sp@ cell + dup @ >r ! r> ; \ ( a b -- b a )
|
|
|
|
: rot >r swap r> swap ; \ ( a b c -- b c a )
|
|
|
|
: -rot swap >r swap r> ; \ ( a b c -- c a b )
|
2021-01-09 09:19:51 +01:00
|
|
|
: nip swap drop ; \ ( a b -- b )
|
2021-01-01 10:37:41 +01:00
|
|
|
: over >r dup r> swap ; \ ( a b -- a b a )
|
|
|
|
: tuck dup -rot ; \ ( a b -- b a b )
|
2021-01-05 10:19:21 +01:00
|
|
|
: pick cells sp@ + cell + @ ; \ ( wu ... x0 u -- xu ... x0 xu )
|
2021-01-01 10:37:41 +01:00
|
|
|
|
|
|
|
: 2drop drop drop ; \ ( a b -- )
|
2021-01-07 20:27:50 +01:00
|
|
|
: 3drop 2drop drop ; \ ( a b c -- )
|
2021-01-01 10:37:41 +01:00
|
|
|
: 2dup over over ; \ ( a b -- a b a b )
|
2021-01-07 20:27:50 +01:00
|
|
|
: 3dup 2 pick 2 pick 2 pick ; \ ( a b c -- a b c a b c )
|
2021-01-01 10:37:41 +01:00
|
|
|
: 2swap >r -rot r> -rot ; \ ( a b c d -- c d a b )
|
|
|
|
: 2nip 2swap 2drop ; \ ( a b c d -- c d )
|
|
|
|
: 2over 3 pick 3 pick ; \ ( a b c d -- a b c d a b )
|
|
|
|
: 2tuck 2swap 2over ; \ ( a b c d -- c d a b c d )
|
|
|
|
: 2rot >r >r 2swap r> r> 2swap ; \ ( a b c d e f -- c d e f a b )
|
|
|
|
: -2rot 2swap >r >r 2swap r> r> ; \ ( a b c d e f -- e f a b c d )
|
|
|
|
|
|
|
|
: rdrop r> rp@ ! ; \ ( R:w -- )
|
|
|
|
|
2021-01-05 10:19:26 +01:00
|
|
|
\ ( R xu ... x0 u -- xu ... x0 xu )
|
|
|
|
: rpick
|
|
|
|
cells rp@ + cell + @
|
|
|
|
;
|
|
|
|
|
2021-01-01 10:37:41 +01:00
|
|
|
\ ( -- a-addr )
|
|
|
|
\ The bottom address of stacks.
|
|
|
|
\ sp@ and rp@ points bottom if implementation so far is correct.
|
2021-01-01 10:54:42 +01:00
|
|
|
: sp0 [ sp@ ] literal ;
|
|
|
|
: rp0 [ rp@ ] literal ;
|
2021-01-01 10:37:41 +01:00
|
|
|
|
|
|
|
\ === Integer Arithmetic ===
|
|
|
|
|
|
|
|
: 1+ 1 + ;
|
|
|
|
: 1- 1 - ;
|
|
|
|
|
2021-01-01 12:01:29 +01:00
|
|
|
\ ( a b -- (a mod b) (a / b) )
|
|
|
|
: /mod 2dup mod -rot / ;
|
2021-01-01 10:37:41 +01:00
|
|
|
|
|
|
|
\ ( n -- -n )
|
2021-01-01 12:01:29 +01:00
|
|
|
: negate 0 swap - ;
|
|
|
|
|
|
|
|
\ ( n1 -- n2 )
|
|
|
|
: not false = ;
|
|
|
|
|
2021-01-04 01:31:24 +01:00
|
|
|
\ ( n1 -- n2 )
|
|
|
|
\ bitwise invert
|
|
|
|
: invert -1 xor ;
|
|
|
|
|
2021-01-01 12:01:29 +01:00
|
|
|
: > swap < ;
|
|
|
|
: <= > not ;
|
|
|
|
: >= < not ;
|
|
|
|
: <> = not ;
|
|
|
|
|
|
|
|
: 0= 0 = ;
|
|
|
|
: 0<> 0 <> ;
|
|
|
|
: 0< 0 < ;
|
|
|
|
: 0> 0 > ;
|
|
|
|
: 0<= 0 <= ;
|
|
|
|
: 0>= 0 >= ;
|
|
|
|
|
|
|
|
\ ( a b c -- (a<=c & c<b) )
|
|
|
|
: within tuck > -rot <= and ;
|
|
|
|
|
|
|
|
\ === Conditional Branch ===
|
|
|
|
\ <condition> if <if-true> then
|
|
|
|
\ <condition> if <if-true> else <if-false> then
|
|
|
|
\ <condition> unless <if-false> then
|
|
|
|
\ <condition> unless <if-false> else <if-true> then
|
|
|
|
|
|
|
|
\ compile: ( -- orig )
|
|
|
|
\ runtime: ( n -- )
|
|
|
|
: if
|
|
|
|
compile 0branch
|
2021-01-02 01:40:40 +01:00
|
|
|
here 0 , \ save location of offset, fill dummy
|
2021-01-01 12:01:29 +01:00
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( orig -- )
|
|
|
|
\ runtime: ( -- )
|
|
|
|
: then
|
2021-01-02 01:40:40 +01:00
|
|
|
here \ ( orig dest )
|
2021-01-01 12:01:29 +01:00
|
|
|
over - \ ( orig offset )
|
|
|
|
swap ! \ fill offset to orig
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( orig1 -- orig2 )
|
|
|
|
\ runtime: ( -- )
|
|
|
|
: else
|
|
|
|
compile branch
|
2021-01-02 01:40:40 +01:00
|
|
|
here 0 , \ save location of offset, fill dummy
|
2021-01-01 12:01:29 +01:00
|
|
|
swap
|
|
|
|
\ fill offset, here-orig1, to orig1
|
2021-01-02 01:40:40 +01:00
|
|
|
here
|
2021-01-01 12:01:29 +01:00
|
|
|
over -
|
|
|
|
swap !
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( -- orig )
|
|
|
|
\ runtime: ( n -- )
|
|
|
|
: unless
|
|
|
|
compile not
|
|
|
|
[compile] if
|
|
|
|
; immediate
|
|
|
|
|
2021-01-01 12:47:08 +01:00
|
|
|
\ ( n -- n n | n )
|
|
|
|
\ duplicate if n<>0
|
|
|
|
: ?dup dup if dup then ;
|
|
|
|
|
2021-01-01 12:01:29 +01:00
|
|
|
\ === Loops ===
|
|
|
|
\ begin <body> <condition> until
|
|
|
|
\ begin <body> again
|
|
|
|
\ begin <condition> while <body> repeat
|
|
|
|
|
|
|
|
\ compile: ( -- dest )
|
|
|
|
\ runtime: ( -- )
|
|
|
|
: begin
|
2021-01-02 01:40:40 +01:00
|
|
|
here \ save location
|
2021-01-01 12:01:29 +01:00
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( dest -- )
|
|
|
|
\ runtime: ( n -- )
|
|
|
|
: until
|
|
|
|
compile 0branch
|
2021-01-02 01:40:40 +01:00
|
|
|
here - , \ fill offset
|
2021-01-01 12:01:29 +01:00
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( dest -- )
|
|
|
|
\ runtime: ( -- )
|
|
|
|
: again
|
|
|
|
compile branch
|
2021-01-02 01:40:40 +01:00
|
|
|
here - , \ fill offset
|
2021-01-01 12:01:29 +01:00
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( dest -- dest orig )
|
|
|
|
\ runtime: ( n -- )
|
|
|
|
\ dest=location of begin
|
|
|
|
\ orig=location of while
|
|
|
|
: while
|
|
|
|
compile 0branch
|
2021-01-02 01:40:40 +01:00
|
|
|
here 0 , \ save location, fill dummy
|
2021-01-01 12:01:29 +01:00
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( dest orig -- )
|
|
|
|
\ runtime: ( -- )
|
|
|
|
\ dest=location of begin
|
|
|
|
\ orig=location of while
|
|
|
|
: repeat
|
|
|
|
swap
|
|
|
|
compile branch
|
2021-01-02 01:40:40 +01:00
|
|
|
here - , \ fill offset from here to begin
|
|
|
|
here over - swap ! \ backfill offset from while to here
|
2021-01-01 12:01:29 +01:00
|
|
|
; immediate
|
|
|
|
|
2021-01-01 12:11:47 +01:00
|
|
|
\ === Recursive Call ===
|
2021-01-01 10:37:41 +01:00
|
|
|
|
2021-01-01 12:18:45 +01:00
|
|
|
\ recursive call.
|
|
|
|
\ compiles xt of current definition
|
2021-01-01 12:11:47 +01:00
|
|
|
: recurse
|
2021-01-02 01:40:40 +01:00
|
|
|
latest >cfa ,
|
2021-01-01 12:11:47 +01:00
|
|
|
; immediate
|
2021-01-01 10:37:41 +01:00
|
|
|
|
2021-01-01 13:40:59 +01:00
|
|
|
\ === Case ===
|
|
|
|
|
|
|
|
\ ---
|
|
|
|
\ <value> case
|
|
|
|
\ <value1> of <case1> endof
|
|
|
|
\ <value2> of <case2> endof
|
|
|
|
\ ...
|
|
|
|
\ <default case>
|
|
|
|
\ endcase
|
|
|
|
\ ---
|
|
|
|
\ This is equivalent to
|
|
|
|
\ ---
|
|
|
|
\ <value>
|
|
|
|
\ <value1> over = if drop <case1> else
|
|
|
|
\ <value2> over = if drop <case2> else
|
|
|
|
\ ...
|
|
|
|
\ <default case>
|
|
|
|
\ then ... then then
|
|
|
|
\ ---
|
|
|
|
|
|
|
|
|
|
|
|
\ compile: ( -- 0 )
|
|
|
|
\ runtime: ( n -- )
|
|
|
|
: case
|
|
|
|
0 \ push 0 to indicate there is no more case
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ compile: ( -- orig )
|
|
|
|
: of
|
|
|
|
compile over
|
|
|
|
compile =
|
|
|
|
[compile] if
|
|
|
|
compile drop
|
|
|
|
; immediate
|
|
|
|
|
2021-01-01 14:12:45 +01:00
|
|
|
\ <value> a b rangeof <body> endof
|
|
|
|
\ Execute <body> when
|
|
|
|
\ a <= <value> and <value> <= b
|
|
|
|
: rangeof
|
|
|
|
compile 2
|
|
|
|
compile pick
|
|
|
|
compile >=
|
|
|
|
compile swap
|
|
|
|
compile 2
|
|
|
|
compile pick
|
|
|
|
compile <=
|
|
|
|
compile and
|
|
|
|
[compile] if
|
|
|
|
compile drop
|
|
|
|
; immediate
|
|
|
|
|
2021-01-01 13:40:59 +01:00
|
|
|
\ compile: ( orig1 -- orig2 )
|
|
|
|
: endof
|
|
|
|
[compile] else
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
: endcase
|
|
|
|
compile drop
|
|
|
|
begin ?dup while
|
|
|
|
[compile] then
|
|
|
|
repeat
|
|
|
|
; immediate
|
|
|
|
|
2021-01-09 09:20:09 +01:00
|
|
|
\ === Integer Arithmetic (that require control flow words) ===
|
|
|
|
\ ( a b -- c )
|
|
|
|
: max 2dup > if drop else nip then ;
|
|
|
|
: min 2dup < if drop else nip then ;
|
|
|
|
|
2021-01-01 12:35:09 +01:00
|
|
|
\ === Multiline Comment ===
|
|
|
|
|
|
|
|
: '(' [ key ( ] literal ;
|
|
|
|
: ')' [ key ) ] literal ;
|
|
|
|
|
|
|
|
: (
|
|
|
|
1 \ depth counter
|
2021-01-01 13:41:15 +01:00
|
|
|
begin ?dup while
|
|
|
|
key case
|
|
|
|
'(' of 1+ endof \ increment depth
|
|
|
|
')' of 1- endof \ decrement depth
|
|
|
|
endcase
|
|
|
|
repeat
|
2021-01-01 12:35:09 +01:00
|
|
|
; immediate
|
2021-01-01 12:18:45 +01:00
|
|
|
|
2021-01-01 12:35:09 +01:00
|
|
|
(
|
|
|
|
Now we can use multiline comment with ( nests. )
|
|
|
|
)
|
2021-01-01 12:18:45 +01:00
|
|
|
|
2021-01-01 18:44:57 +01:00
|
|
|
( === Memory Operation === )
|
|
|
|
|
|
|
|
: +! ( n a-addr -- ) tuck @ + swap ! ;
|
2021-01-05 10:19:36 +01:00
|
|
|
: -! ( n a-addr -- ) tuck @ swap - swap ! ;
|
2021-01-01 18:44:57 +01:00
|
|
|
|
|
|
|
\ allocate n bytes
|
|
|
|
: allot ( n -- c-addr )
|
2021-01-02 01:40:40 +01:00
|
|
|
here swap
|
|
|
|
&here +!
|
2021-01-01 18:44:57 +01:00
|
|
|
;
|
|
|
|
|
2021-01-01 18:45:27 +01:00
|
|
|
( === create and does> === )
|
|
|
|
|
|
|
|
\ no-operation
|
|
|
|
: nop ;
|
|
|
|
|
|
|
|
\ ( "name" -- )
|
|
|
|
\ Read name and create new dictionary entry.
|
|
|
|
\ When the word is executed, it pushs value of here
|
|
|
|
\ at the end of the entry.
|
|
|
|
: create
|
2021-01-02 07:48:47 +01:00
|
|
|
align
|
2021-01-04 13:57:52 +01:00
|
|
|
latest , \ fill link
|
|
|
|
here cell- &latest ! \ update latest
|
2021-01-04 22:59:58 +01:00
|
|
|
word dup strlen
|
2021-01-09 09:13:51 +01:00
|
|
|
dup c, memcpy, 0 c, align \ fill length, name and \0
|
2021-01-04 13:57:52 +01:00
|
|
|
docol , \ compile docol
|
2021-01-01 18:45:27 +01:00
|
|
|
['] lit ,
|
2021-01-04 13:57:52 +01:00
|
|
|
here 3 cells + , \ compile the address
|
|
|
|
['] nop , \ does>, if any, will fill this cell
|
|
|
|
['] exit , \ compile exit
|
2021-01-01 18:45:27 +01:00
|
|
|
;
|
|
|
|
|
2021-01-05 17:54:45 +01:00
|
|
|
: (does>)
|
2021-01-02 01:40:40 +01:00
|
|
|
latest >cfa
|
2021-01-03 09:51:07 +01:00
|
|
|
3 cells + ! \ replace nop
|
2021-01-01 18:45:27 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
: does>
|
2021-01-02 07:48:47 +01:00
|
|
|
align
|
2021-01-01 18:45:27 +01:00
|
|
|
0 [compile] literal \ literal for xt
|
2021-01-02 01:40:40 +01:00
|
|
|
here cell- \ save addr of xt
|
2021-01-01 18:45:27 +01:00
|
|
|
|
2021-01-03 09:51:07 +01:00
|
|
|
\ replace nop with xt at runtime
|
2021-01-05 17:54:45 +01:00
|
|
|
compile (does>)
|
2021-01-01 18:45:27 +01:00
|
|
|
|
|
|
|
[compile] ; \ finish compilation of initialization part
|
|
|
|
:noname \ start compilation of does> part
|
|
|
|
swap ! \ backfill xt to the operand of literal
|
|
|
|
; immediate
|
|
|
|
|
2021-01-02 01:08:59 +01:00
|
|
|
( === Variable and Constant === )
|
|
|
|
|
|
|
|
\ ( "name" -- )
|
|
|
|
: variable create 0 , ;
|
|
|
|
|
|
|
|
\ ( n "name" -- )
|
2021-01-01 18:45:27 +01:00
|
|
|
: constant create , does> @ ;
|
|
|
|
|
2021-01-02 12:03:40 +01:00
|
|
|
( === Throw and Catch === )
|
|
|
|
|
|
|
|
\ 'xt catch' saves data stack pointer and a marker
|
|
|
|
\ to indicate where to return on return stack
|
|
|
|
\ then execute 'xt'.
|
|
|
|
\ When 'n throw' is executed, the catch statement returns
|
|
|
|
\ 'n'. If no throw is executed, returns 0.
|
|
|
|
|
|
|
|
\ At the beginning of execution of 'xt', return stack
|
|
|
|
\ contains following information.
|
|
|
|
\ +-------------------------+
|
|
|
|
\ | original return address |
|
|
|
|
\ | saved stack pointer |
|
|
|
|
\ | exception marker | <- top of return stack
|
|
|
|
\ +-------------------------+
|
|
|
|
\ If no 'throw' is called, after execution of 'xt'
|
|
|
|
\ program goes to the exception-marker because it is
|
|
|
|
\ on the top of return stack.
|
|
|
|
\ The exception-marker drops 'saved stack pointer',
|
|
|
|
\ push 0 to indicate no error and return to the
|
|
|
|
\ 'original return address'.
|
|
|
|
\ When 'n throw' is called, it scans return stack
|
|
|
|
\ to find the exception-marker, restore return stack pointer
|
|
|
|
\ and data stack pointer, push error code, and returns to
|
|
|
|
\ the 'original return address'
|
|
|
|
|
|
|
|
create exception-marker
|
|
|
|
' rdrop , \ drop saved stack pointer
|
|
|
|
0 literal \ push 0 to indicate no-error
|
|
|
|
' exit ,
|
|
|
|
|
|
|
|
: catch ( xt -- n )
|
|
|
|
sp@ cell+ >r \ save stack pointer
|
|
|
|
exception-marker >r \ push exception marker
|
|
|
|
execute
|
|
|
|
;
|
|
|
|
|
2021-01-04 07:42:14 +01:00
|
|
|
: success 0 ;
|
2021-01-04 07:35:55 +01:00
|
|
|
|
2021-01-02 12:03:40 +01:00
|
|
|
: throw ( w -- )
|
|
|
|
?dup unless exit then \ do nothing if no error
|
|
|
|
rp@
|
|
|
|
begin
|
|
|
|
dup rp0 cell- < \ rp < rp0
|
|
|
|
while
|
|
|
|
dup @ \ load return stack entry
|
|
|
|
exception-marker = if
|
|
|
|
rp! \ restore return stack pointer
|
|
|
|
rdrop \ drop exception marker
|
|
|
|
|
|
|
|
\ Reserve enough working space of data stack since
|
|
|
|
\ following code manipulates data stack pointer
|
|
|
|
\ and write value to data stack directly via
|
|
|
|
\ address.
|
|
|
|
dup dup dup dup
|
|
|
|
|
|
|
|
r> \ original stack pointer
|
|
|
|
\ ( n sp )
|
|
|
|
cell- \ allocate space for error code
|
|
|
|
tuck ! \ store error code of top of stack
|
|
|
|
sp! \ restore data stack pointer
|
|
|
|
exit
|
|
|
|
then
|
|
|
|
cell+
|
|
|
|
repeat
|
|
|
|
drop
|
|
|
|
;
|
|
|
|
|
2021-01-02 18:01:26 +01:00
|
|
|
( === Printing Numbers === )
|
2021-01-02 17:56:02 +01:00
|
|
|
|
|
|
|
\ Skip reading spaces, read characters and returns first character
|
2021-01-04 13:57:52 +01:00
|
|
|
: char ( <spces>ccc -- c ) word c@ ;
|
2021-01-02 17:56:02 +01:00
|
|
|
|
|
|
|
\ compile-time version of char
|
|
|
|
: [char] ( compile: <spaces>ccc -- ; runtime: --- c )
|
|
|
|
char
|
|
|
|
[compile] literal
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
|
|
|
|
: '\n' [ key : key 0 - ] literal ; \ neline (10)
|
|
|
|
: bl [ key P key 0 - ] literal ; \ space (32)
|
|
|
|
: '"' [char] "" ;
|
|
|
|
|
|
|
|
: cr '\n' emit ;
|
|
|
|
: space bl emit ;
|
|
|
|
|
2021-01-02 14:01:44 +01:00
|
|
|
|
|
|
|
variable base \ number base
|
|
|
|
: decimal 10 base ! ;
|
|
|
|
: hex 16 base ! ;
|
|
|
|
|
|
|
|
decimal \ set default to decimal
|
|
|
|
|
2021-01-02 23:56:27 +01:00
|
|
|
: '0' [char] 0 ;
|
|
|
|
: '9' [char] 9 ;
|
|
|
|
: 'a' [char] a ;
|
|
|
|
: 'x' [char] x ;
|
|
|
|
: 'z' [char] z ;
|
|
|
|
: 'A' [char] A ;
|
|
|
|
: 'Z' [char] Z ;
|
|
|
|
: '-' [char] - ;
|
|
|
|
: '&' [char] & ;
|
|
|
|
: '#' [char] # ;
|
|
|
|
: '%' [char] % ;
|
|
|
|
: '$' [char] $ ;
|
|
|
|
: '\'' [char] ' ;
|
2021-01-02 14:01:44 +01:00
|
|
|
|
|
|
|
\ Display unsigned integer u2 with number base u1.
|
|
|
|
: print-uint ( u1 u2 -- )
|
|
|
|
over /mod ( base mod quot )
|
|
|
|
?dup if
|
|
|
|
\ mod base quot base
|
|
|
|
>r over r>
|
|
|
|
recurse
|
|
|
|
then
|
|
|
|
dup 10 < if '0' + else 10 - 'a' + then emit
|
|
|
|
drop
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Display signed integer n with number base u.
|
|
|
|
: print-int ( u n -- )
|
|
|
|
dup 0< if '-' emit negate then
|
|
|
|
print-uint
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Display unsigned integer followed by a space.
|
|
|
|
: u. ( u -- ) base @ swap print-uint space ;
|
|
|
|
|
|
|
|
\ Display n followed by a space.
|
|
|
|
: . ( n -- ) base @ swap print-int space ;
|
|
|
|
|
|
|
|
\ Display n as a signed decimal number followed by a space.
|
|
|
|
: dec. ( n -- ) 10 swap print-int space ;
|
|
|
|
|
|
|
|
\ Display u as an unsigned hex number prefixed with $
|
|
|
|
\ and followed by a space.
|
|
|
|
: hex. ( u -- ) '$' emit 16 swap print-uint space ;
|
|
|
|
|
|
|
|
\ Number of characters of u in 'base'
|
|
|
|
: uwidth ( u -- u )
|
|
|
|
base @ /
|
|
|
|
?dup if recurse 1+ else 1 then
|
|
|
|
;
|
|
|
|
|
|
|
|
: spaces ( n -- )
|
|
|
|
begin dup 0> while space 1- repeat drop
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Display unsigned integer u right aligned in n characters.
|
|
|
|
: u.r ( u n -- )
|
|
|
|
over uwidth
|
2021-01-04 01:31:40 +01:00
|
|
|
- spaces base @ swap print-uint
|
2021-01-02 14:01:44 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
\ Display signed integer n1 right aligned in n2 characters.
|
|
|
|
: .r ( n1 n2 -- )
|
|
|
|
over 0>= if
|
|
|
|
u.r
|
|
|
|
else
|
|
|
|
swap negate
|
|
|
|
dup uwidth 1+
|
|
|
|
rot swap - spaces
|
|
|
|
'-' emit
|
2021-01-04 01:31:40 +01:00
|
|
|
base @ swap print-uint
|
2021-01-02 14:01:44 +01:00
|
|
|
then
|
|
|
|
;
|
2021-01-02 15:52:47 +01:00
|
|
|
|
|
|
|
( === Parsing Numbers === )
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
\ Parse string c-addr as an unsigned integer with base u
|
2021-01-02 15:52:47 +01:00
|
|
|
\ and return n. f represents the conversion is success or not.
|
2021-01-04 13:57:52 +01:00
|
|
|
: parse-uint ( u c-addr -- n f )
|
2021-01-02 15:52:47 +01:00
|
|
|
0 \ accumulator
|
2021-01-04 13:57:52 +01:00
|
|
|
begin over c@ while
|
|
|
|
\ ( base addr acc )
|
2021-01-02 15:52:47 +01:00
|
|
|
>r \ save acc
|
|
|
|
dup c@ swap 1+ >r \ load char, increment addr and save
|
|
|
|
dup case
|
|
|
|
'0' '9' rangeof '0' - endof
|
|
|
|
'a' 'z' rangeof 'a' - 10 + endof
|
|
|
|
'A' 'Z' rangeof 'A' - 10 + endof
|
|
|
|
\ failed to convert
|
2021-01-09 09:19:51 +01:00
|
|
|
2drop r> r> nip false
|
2021-01-02 15:52:47 +01:00
|
|
|
exit
|
|
|
|
endcase
|
|
|
|
2dup
|
|
|
|
\ ( base n base n )
|
|
|
|
0 -rot
|
|
|
|
\ ( base n 0 base n )
|
|
|
|
within unless
|
|
|
|
\ failed to convert
|
2021-01-09 09:19:51 +01:00
|
|
|
2drop r> r> nip false
|
2021-01-02 15:52:47 +01:00
|
|
|
exit
|
|
|
|
then
|
2021-01-04 13:57:52 +01:00
|
|
|
\ ( base addr n acc )
|
|
|
|
r> swap r>
|
|
|
|
3 pick * +
|
2021-01-02 15:52:47 +01:00
|
|
|
repeat
|
|
|
|
\ success
|
2021-01-09 09:19:51 +01:00
|
|
|
nip nip true
|
2021-01-02 15:52:47 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
\ Parse string as number.
|
|
|
|
\ This function interprets prefixes that specifies number base.
|
2021-01-04 13:57:52 +01:00
|
|
|
: >number ( c-addr -- n f )
|
|
|
|
dup c@ unless
|
|
|
|
drop
|
2021-01-02 15:52:47 +01:00
|
|
|
0 false
|
|
|
|
exit
|
|
|
|
then
|
2021-01-04 13:57:52 +01:00
|
|
|
dup c@ case
|
2021-01-02 15:52:47 +01:00
|
|
|
'-' of
|
2021-01-04 13:57:52 +01:00
|
|
|
1+
|
2021-01-02 15:52:47 +01:00
|
|
|
recurse if
|
|
|
|
negate true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
then
|
|
|
|
endof
|
2021-01-04 13:57:52 +01:00
|
|
|
'&' of 1+ 10 swap parse-uint endof
|
|
|
|
'#' of 1+ 10 swap parse-uint endof
|
|
|
|
'%' of 1+ 2 swap parse-uint endof
|
2021-01-02 15:52:47 +01:00
|
|
|
'0' of
|
|
|
|
\ hexadecimal
|
2021-01-04 13:57:52 +01:00
|
|
|
\ ( addr )
|
|
|
|
1+
|
|
|
|
dup c@ unless
|
|
|
|
drop 0 true exit
|
2021-01-02 15:52:47 +01:00
|
|
|
then
|
2021-01-04 13:57:52 +01:00
|
|
|
dup c@ 'x' = if
|
|
|
|
1+ 16 swap parse-uint exit
|
2021-01-02 15:52:47 +01:00
|
|
|
then
|
2021-01-04 13:57:52 +01:00
|
|
|
drop 0 false exit
|
2021-01-02 15:52:47 +01:00
|
|
|
endof
|
|
|
|
'\'' of
|
|
|
|
\ character code
|
2021-01-04 13:57:52 +01:00
|
|
|
\ ( addr )
|
|
|
|
1+
|
|
|
|
dup c@ unless
|
|
|
|
drop 0 false exit
|
|
|
|
then
|
|
|
|
dup c@ swap 1+
|
|
|
|
c@ case
|
|
|
|
0 of true exit endof
|
|
|
|
'\'' of true exit endof
|
2021-01-02 15:52:47 +01:00
|
|
|
drop 0 false
|
|
|
|
endcase
|
|
|
|
endof
|
|
|
|
\ default case
|
2021-01-04 13:57:52 +01:00
|
|
|
\ ( addr base )
|
|
|
|
drop base @ swap parse-uint
|
|
|
|
dup \ need this because endcase drops top of stack
|
2021-01-02 15:52:47 +01:00
|
|
|
endcase
|
|
|
|
;
|
2021-01-02 18:01:26 +01:00
|
|
|
|
|
|
|
( === String === )
|
|
|
|
|
2021-01-07 20:28:25 +01:00
|
|
|
\ c-addr2 = c-addr1+n
|
|
|
|
\ u2 = u1-n
|
|
|
|
: succ-buffer ( c-addr1 u1 n -- c-addr2 u2 )
|
|
|
|
dup -rot - >r + r>
|
|
|
|
;
|
|
|
|
|
2021-01-03 09:24:34 +01:00
|
|
|
\ ( c-from c-to u -- )
|
|
|
|
\ Copy u bytes from c-from to c-to.
|
|
|
|
\ The memory regions must not be overlapped.
|
2021-01-09 09:13:51 +01:00
|
|
|
: memcpy
|
2021-01-03 09:24:34 +01:00
|
|
|
begin dup 0> while
|
|
|
|
1- >r \ decrement u, save
|
|
|
|
over c@
|
|
|
|
over c! \ copy character
|
|
|
|
1+ >r \ increment c-to, save
|
|
|
|
1+ \ increment c-from
|
|
|
|
r> r>
|
2021-01-07 20:28:42 +01:00
|
|
|
repeat 3drop
|
2021-01-03 09:24:34 +01:00
|
|
|
;
|
|
|
|
|
2021-01-09 09:13:51 +01:00
|
|
|
\ we already have memcpy,
|
2021-01-03 09:24:34 +01:00
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
\ ( c-from c-to -- )
|
|
|
|
\ copy nul terminated string from c-from to c-to
|
|
|
|
: strcpy
|
|
|
|
begin over c@ dup while
|
|
|
|
\ ( c-from c-to c )
|
|
|
|
over c!
|
|
|
|
1+ swap 1+ swap
|
2021-01-05 01:56:59 +01:00
|
|
|
repeat
|
|
|
|
over c!
|
2021-01-07 20:28:42 +01:00
|
|
|
2drop
|
2021-01-03 09:24:34 +01:00
|
|
|
;
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
\ ( c-addr -- )
|
|
|
|
\ copy string to here including \0
|
|
|
|
: strcpy,
|
|
|
|
begin dup c@ dup while
|
|
|
|
c, 1+
|
2021-01-04 15:00:16 +01:00
|
|
|
repeat 2drop
|
2021-01-04 13:57:52 +01:00
|
|
|
0 c,
|
|
|
|
;
|
2021-01-03 09:24:34 +01:00
|
|
|
|
2021-01-02 18:01:26 +01:00
|
|
|
\ Print string
|
2021-01-04 13:57:52 +01:00
|
|
|
: type ( c-addr -- )
|
|
|
|
begin dup c@ dup while \ while c<>\0
|
|
|
|
emit 1+
|
2021-01-02 18:01:26 +01:00
|
|
|
repeat
|
|
|
|
2drop
|
|
|
|
;
|
|
|
|
|
2021-01-02 23:56:47 +01:00
|
|
|
\ Allocate a buffer for string literal
|
2021-01-07 20:28:53 +01:00
|
|
|
bl bl * constant s-buffer-size \ 1024
|
2021-01-03 09:51:30 +01:00
|
|
|
create s-buffer s-buffer-size allot drop
|
2021-01-02 23:56:47 +01:00
|
|
|
|
|
|
|
\ Will define the error message corresponds to this error later
|
|
|
|
\ because we can't write string literal yet.
|
2021-01-04 03:10:46 +01:00
|
|
|
char 0 char B - constant STRING-OVERFLOW-ERROR \ -18
|
2021-01-02 23:56:47 +01:00
|
|
|
|
2021-01-02 18:01:26 +01:00
|
|
|
\ Parse string delimited by "
|
|
|
|
\ compile mode: the string is stored as operand of 'string' operator.
|
|
|
|
\ immediate mode: the string is stored to temporary buffer.
|
|
|
|
: s"
|
|
|
|
state @ if
|
2021-01-03 01:36:41 +01:00
|
|
|
compile litstring
|
2021-01-02 18:01:26 +01:00
|
|
|
here 0 , \ save location of length and fill dummy
|
2021-01-05 01:18:53 +01:00
|
|
|
0 \ length of the string + 1 (\0)
|
2021-01-02 18:01:26 +01:00
|
|
|
begin key dup '"' <> while
|
|
|
|
c, \ store character
|
|
|
|
1+ \ increment length
|
|
|
|
repeat drop
|
2021-01-04 13:57:52 +01:00
|
|
|
0 c, \ store \0
|
2021-01-05 01:18:53 +01:00
|
|
|
1+
|
2021-01-02 18:01:26 +01:00
|
|
|
swap ! \ back-fill length
|
|
|
|
align
|
|
|
|
else
|
2021-01-02 23:56:47 +01:00
|
|
|
s-buffer dup \ save start address
|
2021-01-02 18:01:26 +01:00
|
|
|
begin key dup '"' <> while
|
2021-01-07 20:28:53 +01:00
|
|
|
( buf pos c pos-buf )
|
|
|
|
over 3 pick - s-buffer-size 1- >= if
|
|
|
|
STRING-OVERFLOW-ERROR throw
|
2021-01-02 23:56:47 +01:00
|
|
|
then
|
2021-01-02 18:01:26 +01:00
|
|
|
over c! \ store char
|
|
|
|
1+ \ increment address
|
|
|
|
repeat drop
|
2021-01-04 13:57:52 +01:00
|
|
|
0 swap c! \ store \0
|
2021-01-02 18:01:26 +01:00
|
|
|
then
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ Print string delimited by "
|
|
|
|
: ."
|
|
|
|
[compile] s"
|
|
|
|
state @ if
|
|
|
|
compile type
|
|
|
|
else
|
|
|
|
type
|
|
|
|
then
|
|
|
|
; immediate
|
2021-01-03 00:17:41 +01:00
|
|
|
|
|
|
|
( === Error Code and Messages === )
|
|
|
|
|
|
|
|
\ Single linked list of error code and messages.
|
|
|
|
\ Thre structure of each entry:
|
2021-01-04 13:57:52 +01:00
|
|
|
\ | link | code | message ... |
|
2021-01-03 00:17:41 +01:00
|
|
|
variable error-list
|
|
|
|
0 error-list !
|
|
|
|
|
2021-01-03 09:24:34 +01:00
|
|
|
: error>next ( a-addr -- a-addr) @ ;
|
2021-01-04 13:57:52 +01:00
|
|
|
: error>message ( a-addr -- c-addr ) 2 cells + ;
|
2021-01-03 09:24:34 +01:00
|
|
|
: error>code ( a-addr -- n ) cell+ @ ;
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
: add-error ( n c-addr -- )
|
2021-01-03 00:17:41 +01:00
|
|
|
error-list here
|
2021-01-04 15:00:16 +01:00
|
|
|
( n c-addr )
|
2021-01-03 00:17:41 +01:00
|
|
|
over @ , \ fill link
|
|
|
|
swap ! \ update error-list
|
2021-01-04 15:00:16 +01:00
|
|
|
swap , \ fill error-code
|
2021-01-04 13:57:52 +01:00
|
|
|
strcpy, \ fill message
|
2021-01-03 00:17:41 +01:00
|
|
|
;
|
|
|
|
|
2021-01-04 13:57:52 +01:00
|
|
|
: def-error ( n c-addr "name" -- )
|
|
|
|
create over ,
|
2021-01-04 03:10:46 +01:00
|
|
|
add-error
|
|
|
|
does> @
|
|
|
|
;
|
2021-01-03 00:17:41 +01:00
|
|
|
|
2021-01-04 03:10:46 +01:00
|
|
|
decimal
|
|
|
|
STRING-OVERFLOW-ERROR s" Too long string literal" add-error
|
|
|
|
s" -13" >number drop s" Undefined word" def-error UNDEFINED-WORD-ERROR
|
2021-01-03 10:40:30 +01:00
|
|
|
|
2021-01-03 00:17:41 +01:00
|
|
|
variable next-user-error
|
|
|
|
s" -256" >number drop next-user-error !
|
|
|
|
|
|
|
|
\ Create new user defined error and returns error code.
|
2021-01-04 13:57:52 +01:00
|
|
|
: exception ( c-addr -- n )
|
2021-01-05 02:00:16 +01:00
|
|
|
next-user-error @ swap add-error
|
2021-01-03 00:17:41 +01:00
|
|
|
next-user-error @
|
|
|
|
1 next-user-error -!
|
|
|
|
;
|
2021-01-03 10:40:30 +01:00
|
|
|
|
|
|
|
( === 3rd Stage Interpreter === )
|
|
|
|
|
2021-01-04 14:41:22 +01:00
|
|
|
create word-buffer s" 64" >number drop cell+ allot drop
|
2021-01-03 10:40:30 +01:00
|
|
|
|
|
|
|
: interpret
|
2021-01-04 13:57:52 +01:00
|
|
|
word \ read name from input
|
|
|
|
\ ( addr )
|
|
|
|
dup word-buffer strcpy \ save input
|
|
|
|
dup find \ lookup dictionary
|
2021-01-03 10:40:30 +01:00
|
|
|
?dup if
|
|
|
|
\ Found the word
|
2021-01-09 09:19:51 +01:00
|
|
|
nip
|
2021-01-03 10:40:30 +01:00
|
|
|
state @ if
|
|
|
|
\ compile mode
|
|
|
|
dup cell+ c@ immediate-bit and if
|
|
|
|
\ execute immediate word
|
|
|
|
>cfa execute
|
|
|
|
else
|
|
|
|
\ compile the word
|
|
|
|
>cfa ,
|
|
|
|
then
|
|
|
|
else
|
|
|
|
\ immediate mode
|
|
|
|
>cfa execute
|
|
|
|
then
|
|
|
|
else
|
|
|
|
>number unless
|
2021-01-04 03:10:46 +01:00
|
|
|
UNDEFINED-WORD-ERROR throw
|
2021-01-03 10:40:30 +01:00
|
|
|
then
|
|
|
|
\ Not found
|
|
|
|
state @ if
|
|
|
|
\ compile mode
|
|
|
|
[compile] literal
|
|
|
|
then
|
|
|
|
then
|
|
|
|
;
|
|
|
|
|
2021-01-09 09:20:48 +01:00
|
|
|
:noname
|
2021-01-03 10:44:51 +01:00
|
|
|
rdrop \ drop 2nd stage
|
2021-01-03 10:40:30 +01:00
|
|
|
begin
|
|
|
|
['] interpret catch
|
|
|
|
?dup if
|
|
|
|
\ lookup error code
|
|
|
|
error-list @
|
|
|
|
begin ?dup while
|
|
|
|
\ ( error-code error-entry )
|
|
|
|
dup error>code
|
|
|
|
2 pick = if
|
|
|
|
error>message type
|
|
|
|
." : "
|
2021-01-04 13:57:52 +01:00
|
|
|
word-buffer type cr
|
2021-01-03 10:40:30 +01:00
|
|
|
bye
|
|
|
|
then
|
|
|
|
error>next
|
|
|
|
repeat
|
2021-01-05 19:01:24 +01:00
|
|
|
." Unknown error code: "
|
|
|
|
word-buffer type
|
|
|
|
." (" 0 .r ." )" cr
|
2021-01-03 10:40:30 +01:00
|
|
|
bye
|
|
|
|
then
|
|
|
|
again
|
2021-01-09 09:20:48 +01:00
|
|
|
; execute
|
2021-01-04 01:32:16 +01:00
|
|
|
|
2021-01-05 13:33:56 +01:00
|
|
|
( === Error-codes === )
|
|
|
|
|
|
|
|
decimal
|
|
|
|
-1 s" Aborted" def-error ABORTED-ERROR
|
2021-01-05 15:56:14 +01:00
|
|
|
-37 s" File I/O exception" def-error FILE-IO-ERROR
|
2021-01-05 13:33:56 +01:00
|
|
|
-39 s" Unexpected end of file" def-error UNEXPECTED-EOF-ERROR
|
2021-01-05 21:28:30 +01:00
|
|
|
-59 s" ALLOCATE" def-error ALLOCATE-ERROR
|
2021-01-05 19:01:58 +01:00
|
|
|
-62 s" CLOSE-FILE" def-error CLOSE-FILE-ERROR
|
2021-01-05 13:33:56 +01:00
|
|
|
-68 s" FLUSH-FILE" def-error FLUSH-FILE-ERROR
|
2021-01-05 19:01:58 +01:00
|
|
|
-69 s" OPEN-FILE" def-error OPEN-FILE-ERROR
|
2021-01-05 13:33:56 +01:00
|
|
|
-70 s" READ-FILE" def-error READ-FILE-ERROR
|
|
|
|
-71 s" READ-LINE" def-error READ-LINE-ERROR
|
|
|
|
-75 s" WRITE-FILE" def-error WRITE-FILE-ERROR
|
|
|
|
|
2021-01-05 13:53:31 +01:00
|
|
|
: abort ABORTED-ERROR throw ;
|
|
|
|
|
2021-01-09 09:21:21 +01:00
|
|
|
s" Not implemented" exception constant NOT-IMPLEMENTED
|
|
|
|
: not-implemented NOT-IMPLEMENTED throw ;
|
|
|
|
|
|
|
|
( 31 bytes )
|
|
|
|
s" Not reachable here. may be a bug" exception constant NOT-REACHABLE
|
|
|
|
: not-reachable NOT-REACHABLE throw ;
|
|
|
|
|
|
|
|
( === Do-loop === )
|
|
|
|
|
|
|
|
\ limit start do ... loop
|
|
|
|
|
|
|
|
1 constant do-mark
|
|
|
|
2 constant leave-mark
|
|
|
|
|
|
|
|
create do-stack 16 cells allot drop
|
|
|
|
variable do-sp
|
|
|
|
do-stack 16 cells + do-sp !
|
|
|
|
|
|
|
|
: >do ( w -- do: w )
|
|
|
|
cell do-sp -!
|
|
|
|
do-sp @ !
|
2021-01-05 19:33:48 +01:00
|
|
|
;
|
|
|
|
|
2021-01-09 09:21:21 +01:00
|
|
|
: do> ( do: w -- w )
|
|
|
|
do-sp @ @
|
|
|
|
cell do-sp +!
|
|
|
|
;
|
|
|
|
|
|
|
|
: do@ ( do: w -- w, do: w)
|
|
|
|
do-sp @ @
|
|
|
|
;
|
|
|
|
|
|
|
|
\ compile: ( -- dest mark )
|
|
|
|
: do
|
|
|
|
compile 2dup
|
|
|
|
compile >r \ save start
|
|
|
|
compile >r \ save limit
|
|
|
|
\ leave if start >= limit
|
|
|
|
compile >
|
|
|
|
compile 0branch
|
|
|
|
0 ,
|
|
|
|
here >do do-mark >do
|
|
|
|
here cell- >do leave-mark >do
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
: leave ( -- orig mark )
|
|
|
|
compile branch
|
|
|
|
here >do
|
|
|
|
0 , \ fill dummy offset
|
|
|
|
leave-mark >do
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
: backpatch-leave ( dest , do: orig1 mark1 ... -- do: origN markN ... )
|
|
|
|
begin do@ leave-mark = while
|
|
|
|
do> drop do>
|
|
|
|
2dup -
|
|
|
|
swap !
|
|
|
|
repeat
|
|
|
|
drop
|
|
|
|
;
|
|
|
|
|
|
|
|
: loop
|
|
|
|
compile r>
|
|
|
|
compile r>
|
|
|
|
compile 1+
|
|
|
|
compile 2dup
|
|
|
|
compile >r
|
|
|
|
compile >r
|
|
|
|
compile =
|
|
|
|
compile 0branch
|
|
|
|
here cell + backpatch-leave \ leave jumps to here
|
|
|
|
do> drop \ do-mark
|
|
|
|
do> here - ,
|
|
|
|
compile rdrop
|
|
|
|
compile rdrop
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
: i 2 rpick ;
|
|
|
|
: j 4 rpick ;
|
|
|
|
: k 6 rpick ;
|
|
|
|
|
2021-01-04 01:32:16 +01:00
|
|
|
( === Dump of data stack === )
|
2021-01-05 19:33:48 +01:00
|
|
|
|
2021-01-04 01:32:16 +01:00
|
|
|
: .s ( -- )
|
|
|
|
sp0 sp@ - cell- cell / ( depth of the stack )
|
|
|
|
'<' emit 0 u.r '>' emit space
|
|
|
|
sp@ sp0 ( beg end )
|
|
|
|
begin 2dup < while
|
|
|
|
cell- dup @ .
|
|
|
|
repeat 2drop
|
2021-01-05 03:38:21 +01:00
|
|
|
cr
|
2021-01-04 01:32:16 +01:00
|
|
|
;
|
2021-01-04 02:20:51 +01:00
|
|
|
|
|
|
|
( === Data Structure === )
|
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
\ align n1 to u-byte boundary
|
|
|
|
: aligned-by ( n1 u -- n2 )
|
|
|
|
1- dup invert \ ( n1 u-1 ~(u-1) )
|
|
|
|
-rot + and
|
|
|
|
;
|
|
|
|
|
|
|
|
\ align here to u-byte boundary
|
|
|
|
: align-by ( u -- )
|
|
|
|
here swap aligned-by &here !
|
|
|
|
;
|
|
|
|
|
2021-01-04 02:20:51 +01:00
|
|
|
: struct ( -- offset )
|
|
|
|
0
|
|
|
|
;
|
2021-01-04 07:35:55 +01:00
|
|
|
|
|
|
|
\ struct ... end-struct new-word
|
|
|
|
\ defines new-word as a operator
|
|
|
|
\ that returns alignment and size of the struct.
|
|
|
|
\ new-word: ( -- align size )
|
2021-01-04 02:20:51 +01:00
|
|
|
: end-struct ( offset "name" -- )
|
2021-01-04 07:35:55 +01:00
|
|
|
create , does> @ cell swap
|
2021-01-04 02:20:51 +01:00
|
|
|
;
|
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
: cell% ( -- align size ) cell cell ;
|
|
|
|
: char% ( -- align size ) 1 1 ;
|
|
|
|
|
|
|
|
\ allocate user memory
|
|
|
|
: %allot ( align size -- addr )
|
|
|
|
swap align-by allot
|
|
|
|
;
|
2021-01-04 02:20:51 +01:00
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
: field ( offset1 align size "name" -- offset2 )
|
|
|
|
\ align offset with 'align'
|
|
|
|
-rot aligned-by \ ( size offset )
|
2021-01-04 02:20:51 +01:00
|
|
|
create
|
2021-01-04 07:35:55 +01:00
|
|
|
dup , \ fill offset
|
2021-01-04 03:11:56 +01:00
|
|
|
+ \ return new offset
|
2021-01-04 02:20:51 +01:00
|
|
|
does> @ +
|
|
|
|
;
|
2021-01-04 07:35:55 +01:00
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
( === File I/O === )
|
2021-01-04 07:35:55 +01:00
|
|
|
|
2021-01-04 23:25:08 +01:00
|
|
|
-1 constant EOF
|
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
\ file access methods (fam)
|
|
|
|
0x00 constant R/O \ read-only
|
2021-01-05 18:12:47 +01:00
|
|
|
0x01 constant W/O \ write-only
|
|
|
|
0x02 constant R/W \ read-write
|
2021-01-04 07:35:55 +01:00
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
1024 constant BUFSIZE
|
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
\ File
|
|
|
|
struct
|
2021-01-09 09:21:58 +01:00
|
|
|
cell% field file>fd \ file desctipro
|
|
|
|
cell% field file>read ( c-addr u fd -- n )
|
|
|
|
cell% field file>write ( c-addr u fd -- n )
|
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
char% field file>fam
|
2021-01-05 15:56:14 +01:00
|
|
|
cell% field file>name
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
\ read buffer
|
|
|
|
cell% field file>rbuf
|
|
|
|
cell% field file>rbeg \ read head
|
|
|
|
cell% field file>rend
|
|
|
|
|
|
|
|
\ write buffer
|
|
|
|
cell% field file>wbuf
|
|
|
|
cell% field file>wbeg \ write head
|
|
|
|
cell% field file>wend
|
2021-01-04 07:35:55 +01:00
|
|
|
end-struct file%
|
|
|
|
|
|
|
|
: writable? ( file -- f ) file>fam c@ R/O <> ;
|
|
|
|
: readable? ( file -- f ) file>fam c@ W/O <> ;
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
\ Write buffer
|
|
|
|
\ +-------------+-----+
|
|
|
|
\ |aaaaaaaaaaaaa| |
|
|
|
|
\ +-------------+-----+
|
|
|
|
\ ^ ^ ^
|
|
|
|
\ wbuf wbeg wend
|
|
|
|
|
|
|
|
: write-buffer-content ( file -- c-addr u )
|
|
|
|
dup file>wbeg @ swap file>wbuf tuck -
|
|
|
|
;
|
|
|
|
|
|
|
|
: empty-write-buffer ( file -- )
|
|
|
|
dup file>wbuf @ over file>wbeg !
|
|
|
|
dup file>wbuf @ BUFSIZE + over file>wend !
|
|
|
|
drop
|
|
|
|
;
|
|
|
|
|
|
|
|
: succ-write-buffer ( file n -- )
|
|
|
|
swap file>wbeg +!
|
|
|
|
;
|
|
|
|
|
|
|
|
: write-buffer-count ( file -- n )
|
|
|
|
dup file>wbeg @ swap file>wbuf @ -
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Read buffer
|
|
|
|
\ +-------------+-----+
|
|
|
|
\ | |aaaaaaa| |
|
|
|
|
\ +-------------+-----+
|
|
|
|
\ ^ ^ ^
|
|
|
|
\ rbuf rbeg rend
|
|
|
|
|
|
|
|
: read-buffer-content ( file -- c-addr u)
|
|
|
|
dup file>rend @ swap file>rbeg @ tuck -
|
|
|
|
;
|
|
|
|
|
|
|
|
: empty-read-buffer ( file -- )
|
|
|
|
dup file>rbuf @ over file>rbeg !
|
|
|
|
dup file>rbuf @ over file>rend !
|
|
|
|
drop
|
|
|
|
;
|
|
|
|
|
|
|
|
: succ-read-buffer ( file n -- )
|
|
|
|
swap file>rbeg +!
|
|
|
|
;
|
|
|
|
|
|
|
|
: read-buffer-count ( file -- n )
|
|
|
|
dup file>rend @ swap file>rbeg @ -
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Flush output buffer of file, return error-code.
|
|
|
|
: flush-file ( file -- e )
|
|
|
|
dup writable? unless FLUSH-FILE-ERROR exit then
|
|
|
|
begin
|
|
|
|
( file )
|
|
|
|
dup write-buffer-content ( file buf u )
|
|
|
|
dup 0= if 3drop success exit then
|
|
|
|
2 pick file>fd @ 3 pick file>write @ execute
|
|
|
|
( file n )
|
|
|
|
dup 0< if 2drop FLUSH-FILE-ERROR exit then
|
|
|
|
over write-buffer-content
|
|
|
|
( file n u )
|
|
|
|
over > if not-reachable then
|
|
|
|
over swap succ-write-buffer
|
|
|
|
again
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Write bytes from c-addr u to file, return error-code.
|
|
|
|
: write-file ( c-addr u file -- e )
|
|
|
|
dup writable? unless WRITE-FILE-ERROR exit then
|
|
|
|
over 0<= if 3drop WRITE-FILE-ERROR exit then
|
|
|
|
|
|
|
|
dup write-buffer-content BUFSIZE swap - ( space )
|
|
|
|
2 pick ( space u )
|
|
|
|
<= if
|
|
|
|
( c-addr u file )
|
|
|
|
\ enough space, copy data
|
|
|
|
2 pick over file>wbeg @ 3 pick memcpy
|
|
|
|
\ increment wbeg
|
|
|
|
swap succ-write-buffer drop success exit
|
2021-01-04 07:35:55 +01:00
|
|
|
then
|
2021-01-09 09:21:58 +01:00
|
|
|
( c-addr u file )
|
|
|
|
dup flush-file throw
|
|
|
|
|
|
|
|
over BUFSIZE <= if
|
|
|
|
\ fill data to wbuf
|
|
|
|
2 pick over file>wbeg @ 3 pick memcpy
|
|
|
|
swap succ-write-buffer drop success exit
|
|
|
|
then
|
|
|
|
|
|
|
|
\ write large data directly to the file
|
|
|
|
begin
|
|
|
|
( c-addr u file )
|
|
|
|
2 pick 2 pick 2 pick file>fd @ 3 pick file>write @ execute
|
|
|
|
( c-addr u file n )
|
|
|
|
dup 0< if 2drop 2drop WRITE-FILE-ERROR exit then
|
|
|
|
swap >r succ-buffer r>
|
|
|
|
over 0>
|
|
|
|
until
|
|
|
|
empty-write-buffer 2drop success
|
2021-01-04 07:35:55 +01:00
|
|
|
;
|
|
|
|
|
2021-01-04 23:25:08 +01:00
|
|
|
\ Read u1-bytes at most from file, write it to c-addr.
|
|
|
|
\ Return number of bytes read and error-code.
|
2021-01-04 07:35:55 +01:00
|
|
|
: read-file ( c-addr u1 file -- u2 e )
|
2021-01-09 09:21:58 +01:00
|
|
|
dup readable? unless READ-FILE-ERROR exit then
|
|
|
|
over 0<= if 3drop 0 success exit then
|
|
|
|
|
|
|
|
dup read-buffer-count 2 pick ( count u1 )
|
|
|
|
>= if
|
|
|
|
\ enough data in read buffer
|
|
|
|
dup file>rbeg @ 3 pick 3 pick memcpy
|
|
|
|
\ increment rbeg
|
|
|
|
over succ-read-buffer
|
|
|
|
nip success exit
|
2021-01-04 07:35:55 +01:00
|
|
|
then
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
\ copy rbeg..rend to the buffer
|
|
|
|
dup read-buffer-content 4 pick swap memcpy
|
|
|
|
( buf u file )
|
|
|
|
dup read-buffer-count dup >r
|
|
|
|
( buf u file n , R:written )
|
|
|
|
swap >r succ-buffer r>
|
|
|
|
dup empty-read-buffer
|
|
|
|
|
|
|
|
( buf u file , R:count )
|
|
|
|
over BUFSIZE <= if
|
|
|
|
\ read data to rbuf as much as BUFSIZE
|
|
|
|
dup file>rbuf @ BUFSIZE 2 pick file>fd @ 3 pick file>read @ execute
|
|
|
|
dup 0< if 2drop 2drop r> READ-FILE-ERROR exit then
|
|
|
|
( buf u file n , R:count )
|
|
|
|
dup 2 pick file>rend +!
|
|
|
|
2 pick min
|
|
|
|
over file>rbeg @ 4 pick 2 pick memcpy
|
|
|
|
dup 2 pick file>rbeg +!
|
|
|
|
( buf u file n , R:count )
|
|
|
|
>r 3drop r> r> + success
|
2021-01-04 07:35:55 +01:00
|
|
|
else
|
2021-01-09 09:21:58 +01:00
|
|
|
\ read large data directly from the file
|
|
|
|
dup file>fd @ swap file>read @ execute
|
|
|
|
( n , R:count )
|
|
|
|
dup 0< if drop r> READ-FILE-ERROR exit then
|
|
|
|
r> + success
|
2021-01-04 07:35:55 +01:00
|
|
|
then
|
|
|
|
;
|
|
|
|
|
2021-01-04 23:25:08 +01:00
|
|
|
\ Read a character. Return EOF at end of input.
|
2021-01-05 15:56:14 +01:00
|
|
|
: key-file ( file -- c )
|
2021-01-09 09:21:58 +01:00
|
|
|
0 sp@ 1 3 pick read-file throw
|
|
|
|
( file c u )
|
|
|
|
1 = if
|
|
|
|
nip
|
|
|
|
else
|
|
|
|
2drop EOF
|
|
|
|
then
|
2021-01-05 15:56:14 +01:00
|
|
|
;
|
2021-01-04 09:05:48 +01:00
|
|
|
|
2021-01-04 23:25:08 +01:00
|
|
|
\ Read characters from 'file' to the buffer c-addr u1
|
|
|
|
\ until reaches '\n' or end of file.
|
2021-01-09 09:21:58 +01:00
|
|
|
\ '\0' is stored at the last and '\n' is not stored.
|
2021-01-04 23:25:08 +01:00
|
|
|
\ u2 is the number of characters written to the buffer.
|
|
|
|
\ flag=true if it reaches '\n'.
|
|
|
|
\ e is error code.
|
2021-01-04 09:05:48 +01:00
|
|
|
: read-line ( c-addr u1 file -- u2 flag e )
|
2021-01-09 09:21:58 +01:00
|
|
|
over 1- 0 do
|
|
|
|
2 pick i + 1 2 pick read-file
|
|
|
|
dup 0< if >r drop 2drop i false r> leave then
|
|
|
|
drop
|
|
|
|
( c-addr u1 file u2 )
|
|
|
|
0= if 2drop i false success leave then \ EOF
|
|
|
|
2 pick i + c@ = '\n' if 2drop i true success leave then
|
|
|
|
loop
|
|
|
|
( c-addr u2 flag e )
|
|
|
|
>r >r tuck + 0 swap c! r> r>
|
2021-01-04 09:05:48 +01:00
|
|
|
;
|
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
\ Temporary implementation stdin and stdout using 'key' and 'type'
|
|
|
|
|
|
|
|
create stdin_ file% %allot drop
|
|
|
|
R/O stdin_ file>fam c!
|
2021-01-09 09:21:58 +01:00
|
|
|
' not-implemented stdin_ file>write !
|
|
|
|
BUFSIZE allot stdin_ file>rbuf !
|
|
|
|
stdin_ dup file>rbuf @ swap file>rbeg !
|
|
|
|
stdin_ dup file>rbuf @ swap file>rend !
|
2021-01-04 07:35:55 +01:00
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
\ Read just 1 byte from stdin to c-buffer
|
|
|
|
:noname ( c-addr u obj -- n )
|
2021-01-04 07:35:55 +01:00
|
|
|
drop
|
2021-01-09 09:21:58 +01:00
|
|
|
1 < if
|
|
|
|
drop 0
|
|
|
|
else
|
|
|
|
key swap c!
|
|
|
|
1
|
|
|
|
then
|
|
|
|
; stdin_ file>read !
|
2021-01-04 09:38:25 +01:00
|
|
|
|
2021-01-04 07:35:55 +01:00
|
|
|
create stdout_ file% %allot drop
|
|
|
|
W/O stdout_ file>fam c!
|
2021-01-09 09:21:58 +01:00
|
|
|
' not-implemented stdout_ file>read !
|
|
|
|
BUFSIZE allot stdout_ file>wbuf !
|
|
|
|
stdout_ dup file>wbuf @ swap file>wbeg !
|
|
|
|
stdout_ dup file>wbuf @ BUFSIZE + swap file>wend !
|
2021-01-04 07:35:55 +01:00
|
|
|
|
|
|
|
\ Write u byte from c-addr to stdout.
|
2021-01-05 15:56:14 +01:00
|
|
|
:noname ( c-addr u obj -- e )
|
2021-01-04 07:42:14 +01:00
|
|
|
drop type success
|
2021-01-09 09:21:58 +01:00
|
|
|
; stdout_ file>write !
|
2021-01-04 08:14:14 +01:00
|
|
|
|
|
|
|
( === Input Stream === )
|
|
|
|
|
|
|
|
\ input stream stack
|
|
|
|
struct
|
|
|
|
cell% field input>next
|
|
|
|
cell% field input>file
|
|
|
|
cell% field input>lineno
|
2021-01-04 23:00:32 +01:00
|
|
|
end-struct inputstream%
|
2021-01-04 08:14:14 +01:00
|
|
|
|
2021-01-04 23:01:40 +01:00
|
|
|
variable inputstreams
|
|
|
|
0 inputstreams !
|
2021-01-04 08:14:14 +01:00
|
|
|
|
2021-01-04 23:01:40 +01:00
|
|
|
: push-inputstream ( file -- )
|
2021-01-04 23:00:32 +01:00
|
|
|
inputstream% %allot \ addr
|
2021-01-04 08:14:14 +01:00
|
|
|
swap over input>file !
|
|
|
|
0 over input>lineno !
|
2021-01-04 23:01:40 +01:00
|
|
|
inputstreams @ over input>next !
|
|
|
|
inputstreams !
|
2021-01-04 08:14:14 +01:00
|
|
|
;
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: pop-inputstream ( -- file )
|
|
|
|
inputstreams @ dup
|
|
|
|
input>next @ inputstreams !
|
|
|
|
input>file @
|
2021-01-04 08:14:14 +01:00
|
|
|
;
|
|
|
|
|
2021-01-04 23:01:40 +01:00
|
|
|
stdin_ push-inputstream
|
2021-01-04 13:57:52 +01:00
|
|
|
|
2021-01-04 23:51:02 +01:00
|
|
|
\ Rewrite existing functions that reads inputs using inputstream.
|
2021-01-04 23:01:53 +01:00
|
|
|
|
|
|
|
: key ( -- c )
|
|
|
|
inputstreams @ input>file @ key-file
|
|
|
|
;
|
|
|
|
|
2021-01-04 23:51:02 +01:00
|
|
|
\ Read a word from input stream, return address of the string
|
|
|
|
\ and error-code.
|
|
|
|
: word ( -- c-addr e )
|
2021-01-04 23:01:53 +01:00
|
|
|
inputstreams @ input>file @
|
|
|
|
\ skip leading spaces
|
|
|
|
0
|
|
|
|
begin
|
|
|
|
drop
|
|
|
|
dup key-file \ ( file c )
|
|
|
|
dup bl <> over '\n' <> and
|
|
|
|
until
|
2021-01-04 23:51:02 +01:00
|
|
|
dup EOF = if
|
|
|
|
drop word-buffer UNEXPECTED-EOF-ERROR
|
2021-01-09 09:21:58 +01:00
|
|
|
exit
|
2021-01-04 23:51:02 +01:00
|
|
|
then
|
2021-01-04 23:01:53 +01:00
|
|
|
word-buffer tuck c!
|
|
|
|
1+
|
|
|
|
begin
|
|
|
|
\ ( file p )
|
|
|
|
over key-file
|
2021-01-09 09:21:58 +01:00
|
|
|
dup bl = over '\n' = or over EOF = or if
|
2021-01-04 23:01:53 +01:00
|
|
|
drop
|
|
|
|
0 swap c! \ store \0
|
2021-01-04 23:51:02 +01:00
|
|
|
drop word-buffer success
|
|
|
|
exit
|
2021-01-04 23:01:53 +01:00
|
|
|
then
|
|
|
|
over c!
|
|
|
|
1+
|
|
|
|
again
|
|
|
|
;
|
|
|
|
|
2021-01-05 13:46:49 +01:00
|
|
|
: ' ( "name" -- xt )
|
|
|
|
word throw
|
|
|
|
find ?dup if
|
|
|
|
>cfa
|
|
|
|
else
|
|
|
|
UNDEFINED-WORD-ERROR throw
|
|
|
|
then
|
|
|
|
;
|
|
|
|
: [compile] ' , ; immediate
|
2021-01-05 17:54:45 +01:00
|
|
|
: (compile)
|
2021-01-05 13:46:49 +01:00
|
|
|
[compile] literal
|
|
|
|
[ ' , ] literal ,
|
|
|
|
;
|
2021-01-05 17:54:45 +01:00
|
|
|
: compile ' (compile) ; immediate
|
2021-01-05 13:46:49 +01:00
|
|
|
: ['] ' [compile] literal ; immediate
|
2021-01-04 23:01:53 +01:00
|
|
|
|
|
|
|
: : ( "name -- )
|
|
|
|
align
|
|
|
|
here latest , &latest !
|
2021-01-04 23:51:02 +01:00
|
|
|
word throw dup strlen
|
2021-01-04 23:01:53 +01:00
|
|
|
smudge-bit or c,
|
|
|
|
strcpy,
|
|
|
|
align
|
|
|
|
docol ,
|
|
|
|
]
|
|
|
|
;
|
|
|
|
|
|
|
|
: create ( "name" -- )
|
|
|
|
align
|
|
|
|
here latest , &latest !
|
2021-01-04 23:51:02 +01:00
|
|
|
word throw dup strlen c, strcpy,
|
2021-01-04 23:01:53 +01:00
|
|
|
align
|
|
|
|
docol ,
|
|
|
|
compile lit
|
|
|
|
here 3 cells + ,
|
|
|
|
compile nop
|
|
|
|
compile exit
|
|
|
|
;
|
|
|
|
|
2021-01-04 23:51:02 +01:00
|
|
|
: char ( "ccc" -- c ) word throw c@ ;
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: \
|
|
|
|
begin
|
|
|
|
key case
|
|
|
|
'\n' of exit endof
|
|
|
|
EOF of exit endof
|
|
|
|
endcase
|
|
|
|
again
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
: (
|
|
|
|
1 \ depth counter
|
|
|
|
begin ?dup while
|
|
|
|
key case
|
|
|
|
'(' of 1+ endof \ increment depth
|
|
|
|
')' of 1- endof \ decrement depth
|
|
|
|
EOF of UNEXPECTED-EOF-ERROR throw endof
|
|
|
|
endcase
|
|
|
|
repeat
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
: s"
|
|
|
|
state @ if
|
|
|
|
compile litstring
|
|
|
|
here 0 , \ save location of length and fill dummy
|
|
|
|
0 \ length of the string + 1 (\0)
|
|
|
|
begin key dup '"' <> while
|
|
|
|
dup EOF = if UNEXPECTED-EOF-ERROR throw then
|
|
|
|
c, \ store character
|
|
|
|
1+ \ increment length
|
|
|
|
repeat drop
|
|
|
|
0 c, \ store \0
|
|
|
|
1+
|
|
|
|
swap ! \ back-fill length
|
|
|
|
align
|
|
|
|
else
|
|
|
|
s-buffer dup \ save start address
|
|
|
|
begin key dup '"' <> while
|
|
|
|
dup EOF = if UNEXPECTED-EOF-ERROR throw then
|
|
|
|
( buf pos c pos-buf )
|
|
|
|
over 3 pick - s-buffer-size 1- >= if
|
|
|
|
STRING-OVERFLOW-ERROR throw
|
|
|
|
then
|
|
|
|
over c! \ store char
|
|
|
|
1+ \ increment address
|
|
|
|
repeat drop
|
|
|
|
0 swap c! \ store \0
|
|
|
|
then
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ Print string delimited by "
|
|
|
|
: ."
|
|
|
|
[compile] s"
|
|
|
|
state @ if
|
|
|
|
compile type
|
|
|
|
else
|
|
|
|
type
|
|
|
|
then
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ ( "name" -- )
|
|
|
|
: variable create 0 , ;
|
|
|
|
|
|
|
|
\ ( n "name" -- )
|
|
|
|
: constant create , does> @ ;
|
|
|
|
|
|
|
|
: end-struct ( offset "name" -- )
|
|
|
|
create , does> @ cell swap
|
|
|
|
;
|
|
|
|
|
|
|
|
: field ( offset1 align size "name" -- offset2 )
|
|
|
|
\ align offset with 'align'
|
|
|
|
-rot aligned-by \ ( size offset )
|
|
|
|
create
|
|
|
|
dup , \ fill offset
|
|
|
|
+ \ return new offset
|
|
|
|
does> @ +
|
|
|
|
;
|
|
|
|
|
2021-01-04 23:51:02 +01:00
|
|
|
( === 4th Stage Interpreter === )
|
|
|
|
|
|
|
|
-56 s" Bye" def-error QUIT
|
|
|
|
|
|
|
|
: interpret
|
2021-01-05 00:17:54 +01:00
|
|
|
word \ read name from input
|
2021-01-04 23:51:02 +01:00
|
|
|
|
2021-01-05 00:17:54 +01:00
|
|
|
\ EOF at this point is not an error
|
|
|
|
UNEXPECTED-EOF-ERROR = if QUIT throw then
|
2021-01-04 23:51:02 +01:00
|
|
|
|
|
|
|
dup word-buffer strcpy \ save input
|
|
|
|
dup find \ lookup dictionary
|
|
|
|
?dup if
|
|
|
|
\ Found the word
|
2021-01-09 09:19:51 +01:00
|
|
|
nip
|
2021-01-04 23:51:02 +01:00
|
|
|
state @ if
|
|
|
|
\ compile mode
|
|
|
|
dup cell+ c@ immediate-bit and if
|
|
|
|
\ execute immediate word
|
|
|
|
>cfa execute
|
|
|
|
else
|
|
|
|
\ compile the word
|
|
|
|
>cfa ,
|
|
|
|
then
|
|
|
|
else
|
|
|
|
\ immediate mode
|
|
|
|
>cfa execute
|
|
|
|
then
|
|
|
|
else
|
|
|
|
>number unless
|
|
|
|
UNDEFINED-WORD-ERROR throw
|
|
|
|
then
|
|
|
|
\ Not found
|
|
|
|
state @ if
|
|
|
|
\ compile mode
|
|
|
|
[compile] literal
|
|
|
|
then
|
|
|
|
then
|
|
|
|
;
|
|
|
|
|
|
|
|
: interpret-loop
|
|
|
|
begin
|
|
|
|
['] interpret catch
|
|
|
|
?dup if
|
|
|
|
\ lookup error code
|
|
|
|
dup QUIT = if throw then
|
|
|
|
error-list @
|
|
|
|
begin ?dup while
|
|
|
|
\ ( error-code error-entry )
|
|
|
|
dup error>code
|
|
|
|
2 pick = if
|
|
|
|
error>message type
|
|
|
|
." : "
|
|
|
|
word-buffer type cr
|
|
|
|
bye
|
|
|
|
then
|
|
|
|
error>next
|
|
|
|
repeat
|
2021-01-05 19:01:24 +01:00
|
|
|
." Unknown error code: "
|
|
|
|
word-buffer type
|
|
|
|
." (" 0 .r ." )" cr
|
2021-01-04 23:51:02 +01:00
|
|
|
bye
|
|
|
|
then
|
|
|
|
again
|
|
|
|
;
|
|
|
|
|
2021-01-09 09:20:48 +01:00
|
|
|
:noname
|
2021-01-04 23:51:02 +01:00
|
|
|
rdrop \ drop 3rd stage
|
|
|
|
|
2021-01-05 00:17:54 +01:00
|
|
|
['] interpret-loop catch bye
|
2021-01-09 09:20:48 +01:00
|
|
|
; execute
|
2021-01-04 23:01:53 +01:00
|
|
|
|
2021-01-05 01:19:04 +01:00
|
|
|
( === [if]..[else]..[then] === )
|
|
|
|
|
|
|
|
: [if] ( f -- )
|
|
|
|
unless
|
|
|
|
\ skip inputs until corresponding [else] or [then]
|
|
|
|
0 \ depth
|
|
|
|
begin
|
|
|
|
word throw
|
2021-01-09 09:16:29 +01:00
|
|
|
dup s" [if]" streq if
|
2021-01-05 01:19:04 +01:00
|
|
|
drop 1+
|
2021-01-09 09:16:29 +01:00
|
|
|
else dup s" [else]" streq if
|
2021-01-05 01:19:04 +01:00
|
|
|
drop
|
|
|
|
dup 0= if drop exit then
|
2021-01-09 09:16:29 +01:00
|
|
|
else s" [then]" streq if
|
2021-01-05 01:19:04 +01:00
|
|
|
dup 0= if drop exit then
|
|
|
|
1-
|
|
|
|
then then then
|
|
|
|
again
|
|
|
|
then
|
|
|
|
; immediate
|
|
|
|
|
2021-01-06 09:23:29 +01:00
|
|
|
: [unless] ( f -- )
|
|
|
|
not
|
|
|
|
[compile] [if]
|
|
|
|
; immediate
|
|
|
|
|
2021-01-05 01:19:04 +01:00
|
|
|
: [else]
|
|
|
|
\ If the condition is false, [else] is skipped by [if].
|
|
|
|
\ So when the execution reaches [else] it means that
|
|
|
|
\ the condition was true.
|
|
|
|
|
|
|
|
\ skip inputs until corresponding [then]
|
|
|
|
0 \ depth
|
|
|
|
begin
|
|
|
|
word throw
|
2021-01-09 09:16:29 +01:00
|
|
|
dup s" [if]" streq if
|
2021-01-05 01:19:04 +01:00
|
|
|
drop 1+
|
2021-01-09 09:16:29 +01:00
|
|
|
else s" [then]" streq if
|
2021-01-05 01:19:04 +01:00
|
|
|
dup 0= if drop exit then
|
|
|
|
1-
|
|
|
|
then then
|
|
|
|
again
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
: [then] ; immediate \ do nothing
|
|
|
|
|
2021-01-05 17:52:34 +01:00
|
|
|
( === Dictionary === )
|
|
|
|
|
|
|
|
\ print the name of the word
|
|
|
|
: id. ( nt -- )
|
|
|
|
cell+ dup c@ length-mask and
|
|
|
|
begin dup 0> while
|
|
|
|
swap 1+ dup c@ emit swap 1-
|
|
|
|
repeat
|
|
|
|
2drop
|
|
|
|
;
|
|
|
|
|
|
|
|
\ print all visible words
|
|
|
|
: words
|
|
|
|
latest
|
|
|
|
begin ?dup while
|
|
|
|
dup cell+ c@ smudge-bit and unless
|
|
|
|
dup id. space
|
|
|
|
then
|
|
|
|
@
|
|
|
|
repeat
|
|
|
|
cr
|
|
|
|
;
|
2021-01-05 10:45:14 +01:00
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: name>link ( nt -- nt ) @ ;
|
|
|
|
: name>string ( nt -- c-addr ) cell+ 1+ ;
|
|
|
|
|
2021-01-05 10:45:14 +01:00
|
|
|
( === Command-line Arguments === )
|
|
|
|
|
|
|
|
variable argc
|
|
|
|
variable argv
|
|
|
|
v argc ! argv !
|
|
|
|
|
|
|
|
: arg ( u -- c-addr )
|
|
|
|
dup argc @ < if
|
|
|
|
cells argv @ + @
|
|
|
|
else
|
|
|
|
drop 0
|
|
|
|
then
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Remove 1 arg, update argv and argc
|
|
|
|
: shift-args ( -- )
|
|
|
|
argc @ 1 = if exit then
|
|
|
|
argc @ 1 do
|
|
|
|
i 1+ arg \ argv[i+1]
|
|
|
|
i cells argv @ + \ &argv[i]
|
|
|
|
! \ copy argv[i+1] to argv[i]
|
|
|
|
loop
|
2021-01-05 13:34:16 +01:00
|
|
|
1 argc -!
|
2021-01-05 10:45:14 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
\ Take 1 arg and shift arguments
|
|
|
|
: next-arg ( -- c-addr )
|
|
|
|
argc @ 1 = if 0 exit then
|
|
|
|
1 arg
|
|
|
|
shift-args
|
|
|
|
;
|
2021-01-05 13:34:29 +01:00
|
|
|
|
|
|
|
( === Environment-Dependent Code === )
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
\ Parse codegeneration option.
|
|
|
|
\ $ ./planck < bootstrap --i386-linux ...
|
2021-01-05 13:34:29 +01:00
|
|
|
|
|
|
|
variable codegen-target
|
|
|
|
|
|
|
|
\ Parse command-line arguments.
|
2021-01-09 09:21:58 +01:00
|
|
|
:noname ( -- )
|
2021-01-05 13:47:18 +01:00
|
|
|
s" no-codegen" codegen-target !
|
2021-01-05 13:34:29 +01:00
|
|
|
begin argc @ 1 > while
|
|
|
|
1 arg dup c@ '-' <> if drop exit then
|
2021-01-09 09:21:58 +01:00
|
|
|
dup s" --i386-linux" streq if
|
|
|
|
2 + codegen-target !
|
|
|
|
shift-args
|
2021-01-05 13:34:29 +01:00
|
|
|
else
|
|
|
|
." Unknown option: " type cr
|
2021-01-05 13:53:31 +01:00
|
|
|
abort
|
2021-01-05 13:34:29 +01:00
|
|
|
then
|
|
|
|
repeat
|
2021-01-09 09:21:58 +01:00
|
|
|
; execute
|
2021-01-05 13:53:40 +01:00
|
|
|
|
2021-01-09 09:16:29 +01:00
|
|
|
codegen-target @ s" i386-linux" streq [if]
|
2021-01-05 13:53:40 +01:00
|
|
|
|
2021-01-05 15:23:09 +01:00
|
|
|
%000 constant eax immediate
|
|
|
|
%001 constant ecx immediate
|
|
|
|
%010 constant edx immediate
|
|
|
|
%011 constant ebx immediate
|
2021-01-05 21:28:30 +01:00
|
|
|
%100 constant esp immediate
|
|
|
|
%101 constant ebp immediate
|
|
|
|
%110 constant esi immediate
|
|
|
|
%111 constant edi immediate
|
|
|
|
|
|
|
|
: mod-reg-r/m ( mod reg r/m -- u )
|
|
|
|
0
|
|
|
|
swap 0x7 and or
|
|
|
|
swap 0x7 and 8 * or
|
|
|
|
swap 0x3 and 64 * or
|
|
|
|
;
|
|
|
|
|
|
|
|
: scale-index-byte ( scale index byte -- u )
|
|
|
|
0
|
|
|
|
swap 0x7 and or
|
|
|
|
swap 0x7 and 8 * or
|
|
|
|
swap 0x3 and 64 * or
|
|
|
|
;
|
2021-01-05 15:23:09 +01:00
|
|
|
|
|
|
|
\ compile 'pop reg' and 'push reg'
|
|
|
|
: pop ( reg -- ) 0x58 + c, ; immediate
|
|
|
|
: push ( reg -- ) 0x50 + c, ; immediate
|
|
|
|
|
|
|
|
\ lodsl; jmp *(%eax);
|
|
|
|
: next ( -- ) 0xad c, 0xff c, 0x20 c, ; immediate
|
|
|
|
: int80 ( -- ) 0xcd c, 0x80 c, ; immediate
|
|
|
|
|
2021-01-05 21:28:30 +01:00
|
|
|
\ movl disp(reg1), reg2
|
|
|
|
: movmr ( disp reg1 reg2 -- )
|
|
|
|
0x8b c, \ opcode
|
|
|
|
swap dup %100 = if \ if reg1=esp
|
|
|
|
\ ( disp reg2 reg1 )
|
|
|
|
%01 -rot mod-reg-r/m c,
|
|
|
|
%00 %100 %100 scale-index-byte c,
|
|
|
|
else
|
|
|
|
\ ( disp reg2 reg1 )
|
|
|
|
%01 -rot mod-reg-r/m c,
|
|
|
|
then
|
|
|
|
c, \ displacement
|
|
|
|
; immediate
|
|
|
|
|
2021-01-05 15:23:09 +01:00
|
|
|
\ overwrite code field by DFA
|
|
|
|
: ;asm
|
|
|
|
[compile] ; \ finish compilation
|
2021-01-05 15:56:24 +01:00
|
|
|
latest dup >dfa swap >cfa !
|
2021-01-05 15:23:09 +01:00
|
|
|
; immediate
|
2021-01-05 13:53:40 +01:00
|
|
|
|
2021-01-05 19:21:31 +01:00
|
|
|
: syscall0 ( n -- e )
|
2021-01-05 15:23:09 +01:00
|
|
|
eax pop
|
|
|
|
int80
|
|
|
|
eax push
|
|
|
|
next
|
|
|
|
;asm
|
|
|
|
|
2021-01-05 19:21:31 +01:00
|
|
|
: syscall1 ( arg1 n -- e )
|
2021-01-05 15:23:09 +01:00
|
|
|
eax pop
|
|
|
|
ebx pop
|
|
|
|
int80
|
|
|
|
eax push
|
|
|
|
next
|
|
|
|
;asm
|
|
|
|
|
2021-01-05 19:21:31 +01:00
|
|
|
: syscall2 ( arg2 arg1 n -- e )
|
2021-01-05 15:23:09 +01:00
|
|
|
eax pop
|
|
|
|
ebx pop
|
|
|
|
ecx pop
|
|
|
|
int80
|
|
|
|
eax push
|
|
|
|
next
|
|
|
|
;asm
|
|
|
|
|
2021-01-05 19:21:31 +01:00
|
|
|
: syscall3 ( arg3 arg2 arg1 n -- e )
|
2021-01-05 15:23:09 +01:00
|
|
|
eax pop
|
|
|
|
ebx pop
|
|
|
|
ecx pop
|
|
|
|
edx pop
|
|
|
|
int80
|
|
|
|
eax push
|
|
|
|
next
|
|
|
|
;asm
|
|
|
|
|
2021-01-05 21:28:30 +01:00
|
|
|
: syscall4 ( arg4 arg3 arg2 arg1 n -- e )
|
|
|
|
eax pop
|
|
|
|
ebx pop
|
|
|
|
ecx pop
|
|
|
|
edx pop
|
|
|
|
esi push \ save program counter ( arg4 esi )
|
|
|
|
[ 4 ] esp esi movmr \ movl 4(%esp), %esi
|
|
|
|
int80
|
|
|
|
esi pop \ restore esi
|
|
|
|
ebx pop
|
|
|
|
eax push
|
|
|
|
next
|
|
|
|
;asm
|
|
|
|
|
|
|
|
: syscall5 ( arg5 arg4 arg3 arg2 arg1 n -- e )
|
|
|
|
eax pop
|
|
|
|
ebx pop
|
|
|
|
ecx pop
|
|
|
|
edx pop
|
|
|
|
esi push \ save esi ( arg5 arg4 esi )
|
|
|
|
[ 4 ] esp esi movmr
|
|
|
|
[ 8 ] esp edi movmr
|
|
|
|
int80
|
|
|
|
esi pop
|
|
|
|
ebx pop
|
|
|
|
ebx pop
|
|
|
|
eax push
|
|
|
|
next
|
|
|
|
;asm
|
|
|
|
|
|
|
|
: syscall6 ( arg6 arg5 arg4 arg3 arg2 arg1 n -- e )
|
|
|
|
eax pop
|
|
|
|
ebx pop
|
|
|
|
ecx pop
|
|
|
|
edx pop
|
|
|
|
esi push
|
|
|
|
ebp push \ ( arg6 arg5 arg4 esi ebp )
|
|
|
|
[ 8 ] esp esi movmr
|
|
|
|
[ 12 ] esp edi movmr
|
|
|
|
[ 16 ] esp ebp movmr
|
|
|
|
int80
|
|
|
|
ebp pop
|
|
|
|
esi pop
|
|
|
|
ebx pop
|
|
|
|
ebx pop
|
|
|
|
ebx pop
|
|
|
|
eax push
|
|
|
|
next
|
|
|
|
;asm
|
|
|
|
|
|
|
|
( === Heap Memory === )
|
|
|
|
|
|
|
|
192 constant SYS-MMAP2
|
|
|
|
|
|
|
|
0x0 constant PROT-NONE
|
|
|
|
0x1 constant PROT-READ
|
|
|
|
0x2 constant PROT-WRITE
|
|
|
|
0x4 constant PROT-EXEC
|
|
|
|
0x8 constant PROT-SEM
|
|
|
|
|
|
|
|
0x01 constant MAP-SHARED
|
|
|
|
0x02 constant MAP-PRIVATE
|
|
|
|
0x0f constant MAP-TYPE
|
|
|
|
0x10 constant MAP-FIXED
|
|
|
|
0x20 constant MAP-ANONYMOUS
|
|
|
|
|
|
|
|
: mmap2 ( addr1 u -- addr2 e )
|
|
|
|
>r >r \ ( R: u addr1 )
|
|
|
|
0 \ offset
|
|
|
|
-1 \ fd
|
|
|
|
MAP-ANONYMOUS MAP-PRIVATE or \ flags
|
|
|
|
PROT-READ PROT-WRITE or PROT-EXEC or \ prot
|
|
|
|
r> r> swap \ u addr1
|
|
|
|
SYS-MMAP2
|
|
|
|
syscall6
|
2021-01-09 09:21:58 +01:00
|
|
|
dup -1 = if
|
|
|
|
ALLOCATE-ERROR
|
|
|
|
else
|
|
|
|
success
|
|
|
|
then
|
2021-01-05 21:28:30 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
\ Secure a large heap memory block and cut memories from the block.
|
|
|
|
\ The allocated memories are never released until the program exit.
|
|
|
|
0x8000000 constant BLOCK-SIZE ( 128MB )
|
|
|
|
variable block-addr
|
|
|
|
variable next-addr
|
|
|
|
variable remaining-size
|
|
|
|
|
|
|
|
0 BLOCK-SIZE mmap2 throw block-addr !
|
|
|
|
block-addr @ next-addr !
|
|
|
|
BLOCK-SIZE remaining-size !
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: (allocate) ( u -- addr )
|
2021-01-05 21:28:30 +01:00
|
|
|
dup remaining-size @ <= if
|
|
|
|
( u addr )
|
|
|
|
next-addr @
|
|
|
|
swap dup next-addr +! remaining-size -!
|
|
|
|
else
|
2021-01-09 09:21:58 +01:00
|
|
|
drop -1
|
2021-01-05 21:28:30 +01:00
|
|
|
then
|
|
|
|
;
|
|
|
|
|
2021-01-05 17:13:10 +01:00
|
|
|
( === File I/O === )
|
|
|
|
|
2021-01-05 21:28:30 +01:00
|
|
|
3 constant SYS-READ
|
|
|
|
4 constant SYS-WRITE
|
|
|
|
5 constant SYS-OPEN
|
|
|
|
6 constant SYS-CLOSE
|
2021-01-05 17:13:10 +01:00
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: (open) ( c-addr fam -- fd )
|
|
|
|
swap SYS-OPEN syscall2
|
2021-01-05 17:13:10 +01:00
|
|
|
;
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: (close) ( obj -- n )
|
|
|
|
SYS-CLOSE syscall1
|
2021-01-05 19:01:58 +01:00
|
|
|
;
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: (read) ( c-addr u fd -- n )
|
|
|
|
>r swap r> SYS-READ syscall3
|
2021-01-05 19:01:58 +01:00
|
|
|
;
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
: (write) ( c-addr u1 fd -- n )
|
|
|
|
>r swap r> \ ( u1 u1 c-addr fd )
|
2021-01-05 21:28:30 +01:00
|
|
|
SYS-WRITE syscall3 \ ( u1 u2 )
|
2021-01-05 17:13:10 +01:00
|
|
|
;
|
|
|
|
|
2021-01-05 15:23:09 +01:00
|
|
|
[else] \ i386-linux
|
2021-01-05 13:53:40 +01:00
|
|
|
|
2021-01-09 09:16:29 +01:00
|
|
|
codegen-target @ s" no-codegen" streq not [if]
|
2021-01-05 13:53:40 +01:00
|
|
|
." Unknown codegen target: " codegen-target @ type cr
|
|
|
|
abort
|
2021-01-05 21:28:30 +01:00
|
|
|
[then] [then] \ End of environment dependent code
|
2021-01-05 15:22:50 +01:00
|
|
|
|
2021-01-06 09:19:53 +01:00
|
|
|
: defined? ( "name" -- f )
|
|
|
|
word throw find <> 0
|
|
|
|
;
|
2021-01-05 17:13:10 +01:00
|
|
|
|
2021-01-05 19:01:58 +01:00
|
|
|
: need-defined ( "name" -- )
|
|
|
|
word throw dup find unless
|
|
|
|
." Implementation of " type ." is missing." cr
|
|
|
|
." Please implement it or use --gen <target> option." cr
|
|
|
|
UNDEFINED-WORD-ERROR throw
|
|
|
|
then drop
|
|
|
|
;
|
2021-01-05 17:13:10 +01:00
|
|
|
|
2021-01-05 21:28:30 +01:00
|
|
|
( === Heap Memory === )
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
need-defined (allocate)
|
|
|
|
|
|
|
|
: allocate ( size -- addr e )
|
|
|
|
(allocate) dup 0<> if success else ALLOCATE-ERROR then
|
|
|
|
;
|
2021-01-05 21:28:30 +01:00
|
|
|
|
|
|
|
\ allocate heap memory
|
|
|
|
: %allocate ( align size -- addr e )
|
2021-01-09 09:21:58 +01:00
|
|
|
over + allocate ?dup unless
|
|
|
|
swap 1- invert and success
|
|
|
|
then
|
2021-01-05 21:28:30 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
( === open/close === )
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
need-defined (open)
|
|
|
|
need-defined (close)
|
|
|
|
need-defined (write)
|
|
|
|
need-defined (read)
|
2021-01-05 17:13:10 +01:00
|
|
|
|
2021-01-09 11:35:30 +01:00
|
|
|
: open-file ( c-addr fam -- file e )
|
|
|
|
2dup (open) dup -1 = if
|
|
|
|
3drop 0 OPEN-FILE-ERROR exit
|
|
|
|
then
|
2021-01-05 22:07:08 +01:00
|
|
|
file% %allocate throw
|
2021-01-09 09:21:58 +01:00
|
|
|
tuck file>fd !
|
2021-01-05 19:01:58 +01:00
|
|
|
tuck file>fam !
|
|
|
|
tuck file>name !
|
2021-01-09 09:21:58 +01:00
|
|
|
['] (read) over file>read !
|
|
|
|
['] (write) over file>write !
|
2021-01-05 22:07:08 +01:00
|
|
|
dup file>fam @ W/O <> if
|
2021-01-09 09:21:58 +01:00
|
|
|
BUFSIZE allocate throw over file>rbuf !
|
|
|
|
dup file>rbuf @ over file>rbeg !
|
|
|
|
dup file>rbuf @ over file>rend !
|
2021-01-05 22:07:08 +01:00
|
|
|
then
|
|
|
|
dup file>fam @ R/O <> if
|
|
|
|
BUFSIZE allocate throw over file>wbuf !
|
2021-01-09 09:21:58 +01:00
|
|
|
dup file>wbuf @ over file>wbeg !
|
|
|
|
dup file>wbuf @ BUFSIZE + over file>wend !
|
2021-01-05 22:07:08 +01:00
|
|
|
then
|
2021-01-05 19:22:01 +01:00
|
|
|
success
|
2021-01-05 17:13:10 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
: close-file ( file -- e )
|
2021-01-09 09:21:58 +01:00
|
|
|
file>fd @ (close) 0= if success else CLOSE-FILE-ERROR then
|
2021-01-05 17:13:10 +01:00
|
|
|
;
|
|
|
|
|
2021-01-09 09:21:58 +01:00
|
|
|
( === File Include === )
|
|
|
|
|
|
|
|
: included ( c-addr -- )
|
|
|
|
R/O open-file throw
|
|
|
|
push-inputstream
|
|
|
|
['] interpret-loop catch drop
|
|
|
|
pop-inputstream close-file throw
|
|
|
|
;
|
|
|
|
|
|
|
|
: include ( "name" -- )
|
|
|
|
word throw included
|
|
|
|
;
|
|
|
|
|
|
|
|
( === Instructions === )
|
|
|
|
: DOCOL-INSN docol ;
|
|
|
|
: EXIT-INSN ['] e ;
|
|
|
|
: LIT-INSN ['] lit ;
|
|
|
|
: LITSTRING-INSN ['] litstring ;
|
|
|
|
: BRANCH-INSN ['] branch ;
|
|
|
|
: 0BRANCH-INSN ['] 0branch ;
|
|
|
|
|
|
|
|
( === Remove Unnecessary Words === )
|
|
|
|
|
|
|
|
\ compile: ( "name" -- )
|
|
|
|
\ runtime: ( nt1 -- nt2 )
|
|
|
|
: update-dictionary ( "name1" "name" ... -- )
|
|
|
|
compile 0
|
|
|
|
begin
|
|
|
|
word throw
|
|
|
|
dup s" end-update-dictionary" streq if
|
|
|
|
drop
|
|
|
|
compile &latest
|
|
|
|
compile !
|
|
|
|
exit
|
|
|
|
then
|
|
|
|
find ?dup if
|
|
|
|
[compile] literal
|
|
|
|
compile tuck
|
|
|
|
compile !
|
|
|
|
else
|
|
|
|
UNDEFINED-WORD-ERROR throw
|
|
|
|
then
|
|
|
|
again
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ rebuilt dictionary
|
|
|
|
:noname
|
|
|
|
update-dictionary
|
|
|
|
DOCOL-INSN EXIT-INSN LIT-INSN LITSTRING-INSN
|
|
|
|
BRANCH-INSN 0BRANCH-INSN
|
|
|
|
|
|
|
|
words id. name>string name>link
|
|
|
|
include included
|
|
|
|
next-arg shift-args arg argv argc
|
|
|
|
|
|
|
|
[if] [unless] [else] [then] defined?
|
|
|
|
open-file close-file write-file flush-file
|
|
|
|
read-file key-file read-line
|
2021-01-09 11:35:30 +01:00
|
|
|
R/W W/O R/O EOF
|
2021-01-09 09:21:58 +01:00
|
|
|
|
|
|
|
abort ABORTED-ERROR
|
|
|
|
QUIT not-reachable NOT-REACHABLE
|
|
|
|
not-implemented NOT-IMPLEMENTED
|
|
|
|
WRITE-FILE-ERROR READ-FILE-ERROR OPEN-FILE-ERROR
|
|
|
|
FLUSH-FILE-ERROR CLOSE-FILE-ERROR
|
|
|
|
ALLOCATE-ERROR UNEXPECTED-EOF-ERROR FILE-IO-ERROR
|
|
|
|
STRING-OVERFLOW-ERROR UNDEFINED-WORD-ERROR
|
|
|
|
exception
|
|
|
|
|
|
|
|
%allocate %allot char% cell% field struct end-struct
|
|
|
|
sp0 sp@ sp! dup ?dup drop swap over tuck pick nip rot -rot
|
|
|
|
2rot -2rot 2tuck 2over 2nip 2swap 2dup 2drop 3dup 3drop
|
|
|
|
rp0 rp@ rp! r> >r rdrop rpick
|
|
|
|
|
|
|
|
allocate allot memcpy strlen streq strcpy strcpy,
|
|
|
|
cell cell+ cell- cells align aligned +! -!
|
|
|
|
|
|
|
|
if else then unless begin until again while repeat
|
|
|
|
recurse case of rangeof endof endcase
|
|
|
|
do loop leave i j k
|
|
|
|
|
|
|
|
char [char] key
|
|
|
|
.s . .r u. u.r dec. hex. type
|
|
|
|
." s" bl '\n' cr space base decimal hex
|
|
|
|
catch throw success
|
|
|
|
: ; create :noname does> variable constant
|
|
|
|
' ['] compile [compile] literal
|
|
|
|
+ - * div mod not and or xor invert within max min
|
|
|
|
< > <= >= = <> 0< 0> 0<= 0>= 0= 0<> 1+ 1-
|
|
|
|
|
|
|
|
true false
|
|
|
|
|
|
|
|
( \
|
|
|
|
c@ c! c, @ ! ,
|
|
|
|
word find >cfa >dfa
|
|
|
|
bye emit execute exit here latest
|
|
|
|
end-update-dictionary
|
|
|
|
; execute
|
|
|
|
|
|
|
|
|
|
|
|
( === End of bootstrap === )
|
|
|
|
|
|
|
|
:noname
|
|
|
|
rdrop
|
|
|
|
argc @ 1 > if
|
|
|
|
next-arg dup argv @ !
|
|
|
|
included
|
|
|
|
else
|
|
|
|
." Ready." cr
|
|
|
|
s" /dev/tty" included
|
|
|
|
then
|
|
|
|
; execute
|