meta.fs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. \ meta.fs
  2. \
  3. \ Copyright (c) 2009 Openmoko Inc.
  4. \
  5. \ Authors Christopher Hall <hsw@openmoko.com>
  6. \
  7. \ Redistribution and use in source and binary forms, with or without
  8. \ modification, are permitted provided that the following conditions are
  9. \ met:
  10. \
  11. \ 1. Redistributions of source code must retain the above copyright
  12. \ notice, this list of conditions and the following disclaimer.
  13. \
  14. \ 2. Redistributions in binary form must reproduce the above copyright
  15. \ notice, this list of conditions and the following disclaimer in
  16. \ the documentation and/or other materials provided with the
  17. \ distribution.
  18. \
  19. \ THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY
  20. \ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. \ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. \ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. \ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. \ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. \ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  26. \ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. \ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  28. \ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  29. \ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. forth definitions
  31. vocabulary meta-compiler immediate
  32. vocabulary meta-words immediate
  33. vocabulary meta-interpret immediate
  34. vocabulary meta-assemble immediate
  35. \ word for meta compiler
  36. only forth
  37. also meta-compiler definitions
  38. variable label-count
  39. variable meta-state
  40. variable suppress-once
  41. : tab 9 emit ;
  42. : getline
  43. refill 0= abort" premature EOF"
  44. ;
  45. variable cross-dict-flag
  46. : cross-dict-name ( -- )
  47. cross-dict-flag @
  48. 0 cross-dict-flag !
  49. case
  50. 0 of
  51. ." forth_dict"
  52. endof
  53. 1 of
  54. ." root_dict"
  55. endof
  56. endcase
  57. space ;
  58. : gen-label ( -- n )
  59. 1 label-count +! label-count @ ;
  60. : type-nodash ( c-addr u -- )
  61. 0 ?do
  62. dup c@ dup [char] - = if
  63. drop [char] _
  64. then
  65. emit
  66. char+
  67. loop drop ;
  68. : escaped-type ( c-addr u -- )
  69. 0 ?do
  70. dup c@ dup case
  71. [char] " of
  72. drop
  73. [char] \ emit [char] 0 emit
  74. [char] 4 emit [char] 2 emit
  75. endof
  76. [char] \ of
  77. drop
  78. [char] \ dup emit emit
  79. endof
  80. [char] ; of
  81. drop
  82. ." \073"
  83. endof
  84. emit
  85. endcase
  86. char+
  87. loop drop ;
  88. : hex. ( u -- ) base @ >r hex ." 0x" u. r> base ! ;
  89. : .long ( -- ) tab ." .long" tab ;
  90. : .byte ( -- ) tab ." .byte" tab ;
  91. : suppress true suppress-once ! ;
  92. : output-symbol-pre ( -- f )
  93. suppress-once @ 0= dup if
  94. .long
  95. then ;
  96. : output-symbol-post ( f -- )
  97. if cr then
  98. false suppress-once ! ;
  99. : output-symbol" ( string<quote> -- )
  100. postpone output-symbol-pre
  101. postpone ."
  102. postpone output-symbol-post
  103. ; immediate
  104. : .lstring ( -- \ "<string>" )
  105. tab ." LSTRING" tab 34 emit
  106. [char] " parse escaped-type 34 emit cr ;
  107. : _number ( s-addr -- u \ number )
  108. base @ >r \ R: base
  109. >r 0 dup r> count \ ud c-addr u
  110. over c@ \ ud c-addr u
  111. 0 >r \ R: 0 (positive)
  112. case
  113. [char] + of swap char+ swap 1- endof
  114. [char] - of swap char+ swap 1- r> drop 1 >r endof
  115. [char] % of swap char+ swap 1- 2 base ! endof
  116. [char] & of swap char+ swap 1- 8 base ! endof
  117. [char] # of swap char+ swap 1- 10 base ! endof
  118. [char] $ of swap char+ swap 1- 16 base ! endof
  119. endcase
  120. \ ud c-addr u R: base sign
  121. >number ( d c-addr u )
  122. ?dup if
  123. ." .error " 34 emit ." ***INVALID: " type 34 emit cr -1
  124. \ ." >>" type 2drop true abort" invalid number"
  125. then
  126. 2drop
  127. r> if negate then
  128. r> base !
  129. ;
  130. : _interpret ( -- )
  131. false meta-state ! ;
  132. : _compile ( -- )
  133. true meta-state ! ;
  134. : _literal ( u -- )
  135. .long ." paren_lit_paren, " . cr ;
  136. variable last-parsed-word-xt
  137. : quoted-parse-word ( flag -- )
  138. 0 last-parsed-word-xt !
  139. parse-word 2dup
  140. 34 emit
  141. escaped-type
  142. 34 emit
  143. space
  144. ['] meta-words >body
  145. search-wordlist if
  146. suppress dup last-parsed-word-xt ! execute
  147. else
  148. ." !!ERROR: not found in symbol.fi!!"
  149. then
  150. space
  151. if
  152. last-parsed-word-xt @ ?dup if
  153. ." flags_"
  154. suppress execute
  155. then
  156. else
  157. ." 0"
  158. then
  159. cr
  160. ;
  161. : set-flags-to-zero ( -- )
  162. last-parsed-word-xt @ ?dup if
  163. ." flags_"
  164. suppress execute
  165. ." = 0" cr
  166. then
  167. ;
  168. : meta-constant ( C: x "<spaces>name" -- ) ( -- x )
  169. >r get-order get-current
  170. only postpone forth also postpone meta-interpret
  171. definitions
  172. r> constant
  173. set-current set-order ;
  174. : meta-compile ( -- )
  175. ." ;;; Meta Compiler starting" cr
  176. begin
  177. \ cr ." >> "
  178. bl word dup count nip if
  179. \ dup count 34 emit type 34 emit bl emit
  180. meta-state @ if \ compiling
  181. only [compile] meta-words
  182. also [compile] meta-assemble
  183. find if
  184. execute
  185. else
  186. _number _literal
  187. then
  188. else \ interpreting
  189. only [compile] forth
  190. also [compile] meta-interpret
  191. find if
  192. execute
  193. else
  194. _number
  195. then
  196. then
  197. else
  198. drop
  199. refill 0= if
  200. ." ;;; Meta Compiler exiting" cr cr
  201. only [compile] forth
  202. exit
  203. then
  204. then
  205. again
  206. ;
  207. \ words that are more than just a simple print
  208. \ these override the meta-words versions
  209. \ used in interpret mode
  210. only forth
  211. also meta-interpret definitions
  212. meta-compiler
  213. \ the next definition will be in this dictionary
  214. : cross-root-definition ( -- ) 1 cross-dict-flag ! ;
  215. : :: ( -- \ word )
  216. parse-word 2drop ;
  217. : code ( -- \ string )
  218. cr
  219. tab ." CODE" tab cross-dict-name
  220. true quoted-parse-word
  221. \ rest of line is ignored
  222. begin
  223. getline
  224. tib #tib @ s" end-code" str= 0= while
  225. tib #tib @ type cr
  226. repeat
  227. getline
  228. tab ." END_CODE" cr
  229. set-flags-to-zero
  230. ;
  231. : ] ( -- ) _compile ;
  232. : : ( -- \ word )
  233. cr
  234. tab ." COLON" tab cross-dict-name
  235. true quoted-parse-word
  236. _compile
  237. ;
  238. : constant ( x -- \ word )
  239. cr
  240. tab ." CONSTANT" tab cross-dict-name
  241. dup constant
  242. latestxt >name cell+ dup cell+ swap @ 255 and
  243. 34 emit
  244. escaped-type
  245. 34 emit
  246. parse-word 2drop parse-word 2drop \ ignore :: <word>
  247. space
  248. latestxt >name cell+ dup cell+ swap @ 255 and
  249. ['] meta-words >body
  250. search-wordlist if
  251. suppress execute ." 0"
  252. else
  253. ." !!ERROR: not found in symbol.fi!!
  254. then
  255. cr .long . cr
  256. ;
  257. : forth ;
  258. : c33 ;
  259. : only ;
  260. : also ;
  261. : variable ( -- \ word )
  262. cr
  263. tab ." VARIABLE" tab cross-dict-name
  264. false quoted-parse-word
  265. .long 0 . cr
  266. ;
  267. : create ( -- \ word )
  268. cr
  269. tab ." CREATE" tab cross-dict-name
  270. false quoted-parse-word
  271. ;
  272. : <',> ( -- \ word)
  273. get-order
  274. only postpone meta-words
  275. bl word
  276. find if
  277. execute
  278. else
  279. ." .error ***unknown***" cr
  280. then
  281. set-order
  282. ;
  283. : allot ( u -- )
  284. 3 + 4 /
  285. tab ." .rept" tab . cr
  286. .long 0 . cr
  287. tab ." .endr" cr
  288. ;
  289. : , ( u -- )
  290. .long hex. cr
  291. ;
  292. : c, ( u -- )
  293. .byte hex. cr
  294. ;
  295. : immediate ( -- )
  296. last-parsed-word-xt @ ?dup if
  297. dup
  298. ." flags_" suppress execute ." = "
  299. ." flags_" suppress execute ." + FLAG_IMMEDIATE"
  300. cr
  301. then ;
  302. : compile-only ( -- )
  303. last-parsed-word-xt @ ?dup if
  304. dup
  305. ." flags_" suppress execute ." = "
  306. ." flags_" suppress execute ." + FLAG_COMPILE_ONLY"
  307. cr
  308. then ;
  309. \ should not be here **************************************************
  310. : literal ( u -- ) _literal ;
  311. : cells ( u -- u ) 4 * ;
  312. : cell+ ( u -- u ) 4 + ;
  313. : cell- ( u -- u ) 4 - ;
  314. \ word that are more than just a simple print
  315. \ these override the meta-words versions
  316. \ used in compile/assembly generation mode
  317. only forth
  318. also meta-assemble definitions
  319. meta-compiler
  320. : :: ( -- \ word )
  321. parse-word 2drop ;
  322. : .( ( -- \ string )
  323. [char] ) parse type ;
  324. : [char] ( -- c \ word)
  325. char _literal ;
  326. : [ctrl] ( -- c \ word)
  327. char 31 and _literal ;
  328. : literal ( u -- ) _literal ;
  329. : ; .long ." exit" cr
  330. tab ." END_COLON" cr
  331. set-flags-to-zero
  332. _interpret ;
  333. : [ ( -- )
  334. _interpret ;
  335. : do ( -- dest label )
  336. gen-label dup
  337. .long ." paren_do_paren, L" . cr
  338. ." L" gen-label dup . [char] : emit cr ;
  339. : ?do ( -- dest label )
  340. gen-label dup
  341. .long ." paren_question_do_paren, L" . cr
  342. ." L" gen-label dup . [char] : emit cr ;
  343. : loop ( dest label -- )
  344. .long ." paren_loop_paren, L" . cr
  345. ." L" . [char] : emit cr ;
  346. : +loop ( dest label -- )
  347. .long ." paren_plus_loop_paren, L" . cr
  348. ." L" . [char] : emit cr ;
  349. : begin ( -- label )
  350. ." L" gen-label dup . [char] : emit cr ;
  351. : again ( label -- )
  352. .long ." branch, L" . cr ;
  353. : while ( dest -- origin dest )
  354. .long ." question_branch, L" gen-label dup . cr swap ;
  355. : until ( dest -- )
  356. .long ." question_branch, L" . cr ;
  357. : repeat ( origin dest -- )
  358. .long ." branch, L" . cr
  359. ." L" . [char] : emit cr ;
  360. : if ( -- label )
  361. .long ." question_branch, L" gen-label dup . cr ;
  362. : then ( -- label )
  363. ." L" . [char] : emit cr ;
  364. : else ( label -- label )
  365. .long ." branch, L" gen-label dup . cr
  366. swap
  367. ." L" . [char] : emit cr ;
  368. : case ( -- 0 )
  369. 0 ;
  370. : of ( -- <if> )
  371. .long ." over, equals" cr
  372. .long ." question_branch, L" gen-label dup . cr
  373. .long ." drop" cr
  374. ;
  375. : endof ( <if> -- <else> )
  376. .long ." branch, L" gen-label dup . cr
  377. swap
  378. ." L" . [char] : emit cr
  379. ;
  380. : endcase ( 0 <if>*n -- )
  381. .long ." drop" cr
  382. begin
  383. ?dup
  384. while
  385. ." L" . [char] : emit cr
  386. repeat
  387. ;
  388. : ['] ( -- \ word)
  389. get-order
  390. only postpone meta-words
  391. bl word
  392. find if
  393. .long ." paren_lit_paren, "
  394. suppress
  395. execute
  396. cr
  397. else
  398. ." .error ***unknown***" cr
  399. then
  400. set-order
  401. ;
  402. : postpone ( -- \ word)
  403. get-order
  404. only [compile] meta-words
  405. bl word
  406. find case
  407. 1 of
  408. execute
  409. endof
  410. -1 of
  411. .long ." paren_lit_paren, "
  412. suppress
  413. execute
  414. ." , compile_comma" cr
  415. endof
  416. ." .error ****unknown***" cr
  417. endcase
  418. set-order
  419. ;
  420. : ( ( -- \ comment )
  421. [char] ) parse 2drop ;
  422. : \ ( -- \ comment )
  423. getline ;
  424. : ." ( -- \ "<string>" )
  425. .long ." paren_s_quote_paren" cr
  426. .lstring
  427. .long ." type" cr
  428. ;
  429. : lcd-." ( -- \ "<string>" )
  430. .long ." paren_s_quote_paren" cr
  431. .lstring
  432. .long ." lcd_type" cr
  433. ;
  434. : s" ( "string" -- )
  435. .long ." paren_s_quote_paren" cr
  436. .lstring
  437. ;
  438. : abort" ( -- \ "<string>" )
  439. .long ." question_branch, L" gen-label dup . cr
  440. .long ." paren_s_quote_paren" cr
  441. .lstring
  442. .long ." type, abort" cr
  443. ." L" . [char] : emit cr
  444. ;