meta.fs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 c@ [char] \ =
  226. if ." ;;; " then
  227. tib #tib @ type cr
  228. repeat
  229. getline
  230. tab ." END_CODE" cr
  231. set-flags-to-zero
  232. ;
  233. : ] ( -- ) _compile ;
  234. : : ( -- \ word )
  235. cr
  236. tab ." COLON" tab cross-dict-name
  237. true quoted-parse-word
  238. _compile
  239. ;
  240. : constant ( x -- \ word )
  241. cr
  242. tab ." CONSTANT" tab cross-dict-name
  243. dup constant
  244. latestxt >name cell+ dup cell+ swap @ 255 and
  245. 34 emit
  246. escaped-type
  247. 34 emit
  248. parse-word 2drop parse-word 2drop \ ignore :: <word>
  249. space
  250. latestxt >name cell+ dup cell+ swap @ 255 and
  251. ['] meta-words >body
  252. search-wordlist if
  253. suppress execute ." 0"
  254. else
  255. ." !!ERROR: not found in symbol.fi!!
  256. then
  257. cr .long . cr
  258. ;
  259. : forth ;
  260. : c33 ;
  261. : only ;
  262. : also ;
  263. : variable ( -- \ word )
  264. cr
  265. tab ." VARIABLE" tab cross-dict-name
  266. false quoted-parse-word
  267. .long 0 . cr
  268. ;
  269. : create ( -- \ word )
  270. cr
  271. tab ." CREATE" tab cross-dict-name
  272. false quoted-parse-word
  273. ;
  274. : <',> ( -- \ word)
  275. get-order
  276. only postpone meta-words
  277. bl word
  278. find if
  279. execute
  280. else
  281. ." .error ***unknown***" cr
  282. then
  283. set-order
  284. ;
  285. : allot ( u -- )
  286. 3 + 4 /
  287. tab ." .rept" tab . cr
  288. .long 0 . cr
  289. tab ." .endr" cr
  290. ;
  291. : , ( u -- )
  292. .long hex. cr
  293. ;
  294. : c, ( u -- )
  295. .byte hex. cr
  296. ;
  297. : immediate ( -- )
  298. last-parsed-word-xt @ ?dup if
  299. dup
  300. ." flags_" suppress execute ." = "
  301. ." flags_" suppress execute ." + FLAG_IMMEDIATE"
  302. cr
  303. then ;
  304. : compile-only ( -- )
  305. last-parsed-word-xt @ ?dup if
  306. dup
  307. ." flags_" suppress execute ." = "
  308. ." flags_" suppress execute ." + FLAG_COMPILE_ONLY"
  309. cr
  310. then ;
  311. \ should not be here **************************************************
  312. : literal ( u -- ) _literal ;
  313. : cells ( u -- u ) 4 * ;
  314. : cell+ ( u -- u ) 4 + ;
  315. : cell- ( u -- u ) 4 - ;
  316. \ word that are more than just a simple print
  317. \ these override the meta-words versions
  318. \ used in compile/assembly generation mode
  319. only forth
  320. also meta-assemble definitions
  321. meta-compiler
  322. : :: ( -- \ word )
  323. parse-word 2drop ;
  324. : .( ( -- \ string )
  325. [char] ) parse type ;
  326. : [char] ( -- c \ word)
  327. char _literal ;
  328. : [ctrl] ( -- c \ word)
  329. char 31 and _literal ;
  330. : literal ( u -- ) _literal ;
  331. : ; .long ." exit" cr
  332. tab ." END_COLON" cr
  333. set-flags-to-zero
  334. _interpret ;
  335. : [ ( -- )
  336. _interpret ;
  337. : do ( -- dest label )
  338. gen-label dup
  339. .long ." paren_do_paren, L" . cr
  340. ." L" gen-label dup . [char] : emit cr ;
  341. : ?do ( -- dest label )
  342. gen-label dup
  343. .long ." paren_question_do_paren, L" . cr
  344. ." L" gen-label dup . [char] : emit cr ;
  345. : loop ( dest label -- )
  346. .long ." paren_loop_paren, L" . cr
  347. ." L" . [char] : emit cr ;
  348. : +loop ( dest label -- )
  349. .long ." paren_plus_loop_paren, L" . cr
  350. ." L" . [char] : emit cr ;
  351. : begin ( -- label )
  352. ." L" gen-label dup . [char] : emit cr ;
  353. : again ( label -- )
  354. .long ." branch, L" . cr ;
  355. : while ( dest -- origin dest )
  356. .long ." question_branch, L" gen-label dup . cr swap ;
  357. : until ( dest -- )
  358. .long ." question_branch, L" . cr ;
  359. : repeat ( origin dest -- )
  360. .long ." branch, L" . cr
  361. ." L" . [char] : emit cr ;
  362. : if ( -- label )
  363. .long ." question_branch, L" gen-label dup . cr ;
  364. : then ( -- label )
  365. ." L" . [char] : emit cr ;
  366. : else ( label -- label )
  367. .long ." branch, L" gen-label dup . cr
  368. swap
  369. ." L" . [char] : emit cr ;
  370. : case ( -- 0 )
  371. 0 ;
  372. : of ( -- <if> )
  373. .long ." over, equals" cr
  374. .long ." question_branch, L" gen-label dup . cr
  375. .long ." drop" cr
  376. ;
  377. : endof ( <if> -- <else> )
  378. .long ." branch, L" gen-label dup . cr
  379. swap
  380. ." L" . [char] : emit cr
  381. ;
  382. : endcase ( 0 <if>*n -- )
  383. .long ." drop" cr
  384. begin
  385. ?dup
  386. while
  387. ." L" . [char] : emit cr
  388. repeat
  389. ;
  390. : ['] ( -- \ word)
  391. get-order
  392. only postpone meta-words
  393. bl word
  394. find if
  395. .long ." paren_lit_paren, "
  396. suppress
  397. execute
  398. cr
  399. else
  400. ." .error ***unknown***" cr
  401. then
  402. set-order
  403. ;
  404. : postpone ( -- \ word)
  405. get-order
  406. only [compile] meta-words
  407. bl word
  408. find case
  409. 1 of
  410. execute
  411. endof
  412. -1 of
  413. .long ." paren_lit_paren, "
  414. suppress
  415. execute
  416. ." , compile_comma" cr
  417. endof
  418. ." .error ****unknown***" cr
  419. endcase
  420. set-order
  421. ;
  422. : ( ( -- \ comment )
  423. [char] ) parse 2drop ;
  424. : \ ( -- \ comment )
  425. getline ;
  426. : ." ( -- \ "<string>" )
  427. .long ." paren_s_quote_paren" cr
  428. .lstring
  429. .long ." type" cr
  430. ;
  431. : lcd-." ( -- \ "<string>" )
  432. .long ." paren_s_quote_paren" cr
  433. .lstring
  434. .long ." lcd_type" cr
  435. ;
  436. : s" ( "string" -- )
  437. .long ." paren_s_quote_paren" cr
  438. .lstring
  439. ;
  440. : abort" ( -- \ "<string>" )
  441. .long ." question_branch, L" gen-label dup . cr
  442. .long ." paren_s_quote_paren" cr
  443. .lstring
  444. .long ." type, abort" cr
  445. ." L" . [char] : emit cr
  446. ;