c_ast.g 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. const AST_TYPE 0 # 0 for operand, 1 for operator
  15. const AST_NAME 4 # char*
  16. const AST_LEFT 8 # AST*
  17. const AST_RIGHT 12 # AST*
  18. const AST_TYPE_IDX 16 # int
  19. const AST_ORIG_TYPE_IDX 20 # int
  20. const SIZEOF_AST 24
  21. fun ast_init 0 {
  22. $ptr
  23. @ptr SIZEOF_AST malloc = ;
  24. ptr AST_TYPE take_addr 0 = ;
  25. ptr AST_NAME take_addr 0 = ;
  26. ptr AST_LEFT take_addr 0 = ;
  27. ptr AST_RIGHT take_addr 0 = ;
  28. ptr AST_TYPE_IDX take_addr 0xffffffff = ;
  29. ptr AST_ORIG_TYPE_IDX take_addr 0xffffffff = ;
  30. ptr ret ;
  31. }
  32. fun ast_destroy 1 {
  33. $ptr
  34. @ptr 0 param = ;
  35. ptr AST_NAME take free ;
  36. if ptr AST_LEFT take {
  37. ptr AST_LEFT take ast_destroy ;
  38. }
  39. if ptr AST_RIGHT take {
  40. ptr AST_RIGHT take ast_destroy ;
  41. }
  42. ptr free ;
  43. }
  44. fun ast_is_operator 1 {
  45. $str
  46. @str 0 param = ;
  47. str "++" strcmp 0 ==
  48. str "--" strcmp 0 == ||
  49. str "." strcmp 0 == ||
  50. str "->" strcmp 0 == ||
  51. str "defined" strcmp 0 == ||
  52. str "+" strcmp 0 == ||
  53. str "-" strcmp 0 == ||
  54. str "!" strcmp 0 == ||
  55. str "~" strcmp 0 == ||
  56. str "*" strcmp 0 == ||
  57. str "&" strcmp 0 == ||
  58. str "sizeof" strcmp 0 == ||
  59. str "/" strcmp 0 == ||
  60. str "%" strcmp 0 == ||
  61. str "<<" strcmp 0 == ||
  62. str ">>" strcmp 0 == ||
  63. str "<" strcmp 0 == ||
  64. str "<=" strcmp 0 == ||
  65. str ">" strcmp 0 == ||
  66. str ">=" strcmp 0 == ||
  67. str "==" strcmp 0 == ||
  68. str "!=" strcmp 0 == ||
  69. str "^" strcmp 0 == ||
  70. str "|" strcmp 0 == ||
  71. str "&&" strcmp 0 == ||
  72. str "||" strcmp 0 == ||
  73. str "=" strcmp 0 == ||
  74. str "+=" strcmp 0 == ||
  75. str "-=" strcmp 0 == ||
  76. str "*=" strcmp 0 == ||
  77. str "/=" strcmp 0 == ||
  78. str "%=" strcmp 0 == ||
  79. str "<<=" strcmp 0 == ||
  80. str ">>=" strcmp 0 == ||
  81. str "&=" strcmp 0 == ||
  82. str "^=" strcmp 0 == ||
  83. str "|=" strcmp 0 == ||
  84. str "," strcmp 0 == ||
  85. ret ;
  86. }
  87. # See http://en.cppreference.com/w/c/language/operator_precedence
  88. fun ast_get_priority 1 {
  89. $str
  90. @str 0 param = ;
  91. if str "++_POST" strcmp 0 == { 1 ret ; }
  92. if str "--_POST" strcmp 0 == { 1 ret ; }
  93. if str "(" strcmp 0 == { 1 ret ; }
  94. if str "[" strcmp 0 == { 1 ret ; }
  95. if str "." strcmp 0 == { 1 ret ; }
  96. if str "->" strcmp 0 == { 1 ret ; }
  97. if str "defined_PRE" strcmp 0 == { 2 ret ; }
  98. if str "++_PRE" strcmp 0 == { 2 ret ; }
  99. if str "--_PRE" strcmp 0 == { 2 ret ; }
  100. if str "+_PRE" strcmp 0 == { 2 ret ; }
  101. if str "-_PRE" strcmp 0 == { 2 ret ; }
  102. if str "!_PRE" strcmp 0 == { 2 ret ; }
  103. if str "~_PRE" strcmp 0 == { 2 ret ; }
  104. if str "*_PRE" strcmp 0 == { 2 ret ; }
  105. if str "&_PRE" strcmp 0 == { 2 ret ; }
  106. if str "sizeof" strcmp 0 == { 2 ret ; }
  107. if str "*" strcmp 0 == { 3 ret ; }
  108. if str "/" strcmp 0 == { 3 ret ; }
  109. if str "%" strcmp 0 == { 3 ret ; }
  110. if str "+" strcmp 0 == { 4 ret ; }
  111. if str "-" strcmp 0 == { 4 ret ; }
  112. if str "<<" strcmp 0 == { 5 ret ; }
  113. if str ">>" strcmp 0 == { 5 ret ; }
  114. if str "<" strcmp 0 == { 6 ret ; }
  115. if str "<=" strcmp 0 == { 6 ret ; }
  116. if str ">" strcmp 0 == { 6 ret ; }
  117. if str ">=" strcmp 0 == { 6 ret ; }
  118. if str "==" strcmp 0 == { 7 ret ; }
  119. if str "!=" strcmp 0 == { 7 ret ; }
  120. if str "&" strcmp 0 == { 8 ret ; }
  121. if str "^" strcmp 0 == { 9 ret ; }
  122. if str "|" strcmp 0 == { 10 ret ; }
  123. if str "&&" strcmp 0 == { 11 ret ; }
  124. if str "||" strcmp 0 == { 12 ret ; }
  125. if str "=" strcmp 0 == { 14 ret ; }
  126. if str "+=" strcmp 0 == { 14 ret ; }
  127. if str "-=" strcmp 0 == { 14 ret ; }
  128. if str "*=" strcmp 0 == { 14 ret ; }
  129. if str "/=" strcmp 0 == { 14 ret ; }
  130. if str "%=" strcmp 0 == { 14 ret ; }
  131. if str "<<=" strcmp 0 == { 14 ret ; }
  132. if str ">>=" strcmp 0 == { 14 ret ; }
  133. if str "&=" strcmp 0 == { 14 ret ; }
  134. if str "^=" strcmp 0 == { 14 ret ; }
  135. if str "|=" strcmp 0 == { 14 ret ; }
  136. if str "," strcmp 0 == { 15 ret ; }
  137. if str " " strcmp 0 == { 100 ret ; }
  138. 0 "Not an operator" assert_msg ;
  139. }
  140. # See http://en.cppreference.com/w/c/language/operator_precedence
  141. # 0 is right-to-left
  142. # 1 is left-to-right
  143. fun ast_get_ass_direction 1 {
  144. $str
  145. @str 0 param = ;
  146. if str "++_POST" strcmp 0 == { 1 ret ; }
  147. if str "--_POST" strcmp 0 == { 1 ret ; }
  148. if str "(" strcmp 0 == { 1 ret ; }
  149. if str "[" strcmp 0 == { 1 ret ; }
  150. if str "." strcmp 0 == { 1 ret ; }
  151. if str "->" strcmp 0 == { 1 ret ; }
  152. if str "defined_PRE" strcmp 0 == { 0 ret ; }
  153. if str "++_PRE" strcmp 0 == { 0 ret ; }
  154. if str "--_PRE" strcmp 0 == { 0 ret ; }
  155. if str "+_PRE" strcmp 0 == { 0 ret ; }
  156. if str "-_PRE" strcmp 0 == { 0 ret ; }
  157. if str "!_PRE" strcmp 0 == { 0 ret ; }
  158. if str "~_PRE" strcmp 0 == { 0 ret ; }
  159. if str "*_PRE" strcmp 0 == { 0 ret ; }
  160. if str "&_PRE" strcmp 0 == { 0 ret ; }
  161. if str "sizeof" strcmp 0 == { 0 ret ; }
  162. if str "*" strcmp 0 == { 1 ret ; }
  163. if str "/" strcmp 0 == { 1 ret ; }
  164. if str "%" strcmp 0 == { 1 ret ; }
  165. if str "+" strcmp 0 == { 1 ret ; }
  166. if str "-" strcmp 0 == { 1 ret ; }
  167. if str "<<" strcmp 0 == { 1 ret ; }
  168. if str ">>" strcmp 0 == { 1 ret ; }
  169. if str "<" strcmp 0 == { 1 ret ; }
  170. if str "<=" strcmp 0 == { 1 ret ; }
  171. if str ">" strcmp 0 == { 1 ret ; }
  172. if str ">=" strcmp 0 == { 1 ret ; }
  173. if str "==" strcmp 0 == { 1 ret ; }
  174. if str "!=" strcmp 0 == { 1 ret ; }
  175. if str "&" strcmp 0 == { 1 ret ; }
  176. if str "^" strcmp 0 == { 1 ret ; }
  177. if str "|" strcmp 0 == { 1 ret ; }
  178. if str "&&" strcmp 0 == { 1 ret ; }
  179. if str "||" strcmp 0 == { 1 ret ; }
  180. if str "=" strcmp 0 == { 0 ret ; }
  181. if str "+=" strcmp 0 == { 0 ret ; }
  182. if str "-=" strcmp 0 == { 0 ret ; }
  183. if str "*=" strcmp 0 == { 0 ret ; }
  184. if str "/=" strcmp 0 == { 0 ret ; }
  185. if str "%=" strcmp 0 == { 0 ret ; }
  186. if str "<<=" strcmp 0 == { 0 ret ; }
  187. if str ">>=" strcmp 0 == { 0 ret ; }
  188. if str "&=" strcmp 0 == { 0 ret ; }
  189. if str "^=" strcmp 0 == { 0 ret ; }
  190. if str "|=" strcmp 0 == { 0 ret ; }
  191. if str "," strcmp 0 == { 1 ret ; }
  192. if str " " strcmp 0 == { 0 ret ; }
  193. 0 "Not an operator" assert_msg ;
  194. }
  195. fun ast_rewind_stack 2 {
  196. $operator_stack
  197. $operand_stack
  198. @operator_stack 1 param = ;
  199. @operand_stack 0 param = ;
  200. operand_stack vector_size operator_stack vector_size == "Stacks do not have the same size" assert_msg ;
  201. $cont
  202. @cont 1 = ;
  203. while operator_stack vector_size 2 >= cont && {
  204. $last_pri
  205. $nlast_pri
  206. $dir
  207. @last_pri operator_stack operator_stack vector_size 1 - vector_at ast_get_priority = ;
  208. @nlast_pri operator_stack operator_stack vector_size 2 - vector_at ast_get_priority = ;
  209. @dir operator_stack operator_stack vector_size 1 - vector_at ast_get_ass_direction = ;
  210. if last_pri nlast_pri > last_pri nlast_pri == dir && || {
  211. $ast
  212. @ast ast_init = ;
  213. $tmp
  214. @tmp operator_stack vector_pop_back = ;
  215. ast AST_TYPE take_addr 1 = ;
  216. ast AST_RIGHT take_addr operand_stack vector_pop_back = ;
  217. ast AST_LEFT take_addr operand_stack vector_pop_back = ;
  218. ast AST_NAME take_addr operator_stack vector_pop_back = ;
  219. operand_stack ast vector_push_back ;
  220. operator_stack tmp vector_push_back ;
  221. } else {
  222. @cont 0 = ;
  223. }
  224. operand_stack vector_size operator_stack vector_size == "Stacks do not have the same size" assert_msg ;
  225. }
  226. }
  227. ifun ast_parse 3
  228. fun ast_parse2 3 {
  229. $intoks
  230. $iptr
  231. $end_toks
  232. @intoks 2 param = ;
  233. @iptr 1 param = ;
  234. @end_toks 0 param = ;
  235. $cont
  236. @cont 1 = ;
  237. $expect_operator
  238. @expect_operator 0 = ;
  239. $operator_stack
  240. @operator_stack 4 vector_init = ;
  241. $operand_stack
  242. @operand_stack 4 vector_init = ;
  243. # "Beginning parse\n" 1 platform_log ;
  244. while cont {
  245. $tok
  246. iptr iptr ** 1 + = ;
  247. $stop
  248. @stop 0 = ;
  249. if iptr ** intoks vector_size >= {
  250. @stop 1 = ;
  251. } else {
  252. @tok intoks iptr ** vector_at = ;
  253. }
  254. $i
  255. @i 0 = ;
  256. while i end_toks vector_size < stop ! && {
  257. if end_toks i vector_at tok strcmp 0 == {
  258. @stop 1 = ;
  259. }
  260. @i i 1 + = ;
  261. }
  262. if stop {
  263. @cont 0 = ;
  264. } else {
  265. if " " tok strcmp 0 != {
  266. $is_operator
  267. @is_operator tok ast_is_operator = ;
  268. # "Found: " 1 platform_log ;
  269. # tok 1 platform_log ;
  270. # if is_operator {
  271. # " (operator)" 1 platform_log ;
  272. # } else {
  273. # " (operand)" 1 platform_log ;
  274. # }
  275. # "\n" 1 platform_log ;
  276. if expect_operator {
  277. if is_operator {
  278. # Operator as we expect, push it in the operator stack.
  279. # If the operator is postfix, mangle it and push
  280. # a placeholder operand in the operand stack
  281. $is_postfix
  282. @is_postfix 0 = ;
  283. if tok "++" strcmp 0 == {
  284. @tok "++_POST" = ;
  285. @is_postfix 1 = ;
  286. }
  287. if tok "--" strcmp 0 == {
  288. @tok "--_POST" = ;
  289. @is_postfix 1 = ;
  290. }
  291. operator_stack tok strdup vector_push_back ;
  292. operator_stack operand_stack ast_rewind_stack ;
  293. if is_postfix {
  294. operand_stack ast_init vector_push_back ;
  295. } else {
  296. @expect_operator 0 = ;
  297. }
  298. } else {
  299. $ast
  300. # Here we treat the argument-separating comma as the
  301. # comma operator; this is not correct, in theory, because
  302. # it makes a((b,c),d) the same thing as a(b,c,d). However
  303. # we hope that no sane program relies on that.
  304. if tok "(" strcmp 0 == {
  305. $tok2
  306. iptr iptr ** 1 + = ;
  307. iptr ** intoks vector_size < "ast_parse2: token expected" assert_msg ;
  308. @tok2 intoks iptr ** vector_at = ;
  309. if tok2 ")" strcmp 0 == {
  310. # No arguments, push a placeholder
  311. @ast ast_init = ;
  312. } else {
  313. # Roll back token and parse arguments
  314. iptr iptr ** 1 - = ;
  315. @ast intoks iptr ")" ast_parse = ;
  316. }
  317. operator_stack tok strdup vector_push_back ;
  318. operator_stack operand_stack ast_rewind_stack ;
  319. operand_stack ast vector_push_back ;
  320. } else {
  321. if tok "[" strcmp 0 == {
  322. @ast intoks iptr "]" ast_parse = ;
  323. operator_stack tok strdup vector_push_back ;
  324. operator_stack operand_stack ast_rewind_stack ;
  325. operand_stack ast vector_push_back ;
  326. } else {
  327. # Operand instead of operator: error!
  328. 0 "Operand instead of operator" assert_msg ;
  329. }
  330. }
  331. }
  332. } else {
  333. if is_operator {
  334. # Operator instead of operand, it must be a prefix.
  335. # Mangle it, push it in the operator stack
  336. # and push a placeholder operand in the operand stack
  337. $found
  338. @found 0 = ;
  339. if tok "defined" strcmp 0 == {
  340. @tok "defined_PRE" = ;
  341. @found 1 = ;
  342. }
  343. if tok "!" strcmp 0 == {
  344. @tok "!_PRE" = ;
  345. @found 1 = ;
  346. }
  347. if tok "~" strcmp 0 == {
  348. @tok "~_PRE" = ;
  349. @found 1 = ;
  350. }
  351. if tok "++" strcmp 0 == {
  352. @tok "++_PRE" = ;
  353. @found 1 = ;
  354. }
  355. if tok "--" strcmp 0 == {
  356. @tok "--_PRE" = ;
  357. @found 1 = ;
  358. }
  359. if tok "+" strcmp 0 == {
  360. @tok "+_PRE" = ;
  361. @found 1 = ;
  362. }
  363. if tok "-" strcmp 0 == {
  364. @tok "-_PRE" = ;
  365. @found 1 = ;
  366. }
  367. if tok "*" strcmp 0 == {
  368. @tok "*_PRE" = ;
  369. @found 1 = ;
  370. }
  371. if tok "&" strcmp 0 == {
  372. @tok "&_PRE" = ;
  373. @found 1 = ;
  374. }
  375. found "Expect prefix operator" assert_msg ;
  376. operator_stack tok strdup vector_push_back ;
  377. operand_stack ast_init vector_push_back ;
  378. } else {
  379. $ast
  380. if tok "(" strcmp 0 == {
  381. @ast intoks iptr ")" ast_parse = ;
  382. } else {
  383. # Operand as we expect, push it in the operand stack
  384. @ast ast_init = ;
  385. ast AST_TYPE take_addr 0 = ;
  386. ast AST_NAME take_addr tok strdup = ;
  387. }
  388. operand_stack ast vector_push_back ;
  389. @expect_operator 1 = ;
  390. }
  391. }
  392. # Partially rewind the stack so that priority is decreasing
  393. if expect_operator ! {
  394. operator_stack operand_stack ast_rewind_stack ;
  395. }
  396. }
  397. }
  398. }
  399. expect_operator "Expect operand" assert_msg ;
  400. # Ad a final placeholder operator with the weakest possible priority,
  401. # in order to force the whole stack rewind
  402. $tmp
  403. @tmp " " strdup = ;
  404. operator_stack tmp vector_push_back ;
  405. operator_stack operand_stack ast_rewind_stack ;
  406. operand_stack vector_size 1 == "Internal error" assert_msg ;
  407. operator_stack vector_size 1 == "Internal error" assert_msg ;
  408. operator_stack vector_pop_back tmp == "Internal error" assert_msg ;
  409. tmp free ;
  410. $res
  411. @res operand_stack vector_pop_back = ;
  412. operand_stack vector_destroy ;
  413. operator_stack vector_destroy ;
  414. # "Ending parse\n" 1 platform_log ;
  415. res ret ;
  416. }
  417. fun ast_parse 3 {
  418. $intoks
  419. $iptr
  420. $end_tok
  421. @intoks 2 param = ;
  422. @iptr 1 param = ;
  423. @end_tok 0 param = ;
  424. $end_toks
  425. @end_toks 4 vector_init = ;
  426. end_toks end_tok vector_push_back ;
  427. $res
  428. @res intoks iptr end_toks ast_parse2 = ;
  429. end_toks vector_destroy ;
  430. res ret ;
  431. }
  432. fun ast_parse3 4 {
  433. $intoks
  434. $iptr
  435. $end_tok1
  436. $end_tok2
  437. @intoks 3 param = ;
  438. @iptr 2 param = ;
  439. @end_tok1 1 param = ;
  440. @end_tok2 0 param = ;
  441. $end_toks
  442. @end_toks 4 vector_init = ;
  443. end_toks end_tok1 vector_push_back ;
  444. end_toks end_tok2 vector_push_back ;
  445. $res
  446. @res intoks iptr end_toks ast_parse2 = ;
  447. end_toks vector_destroy ;
  448. res ret ;
  449. }
  450. fun ast_parse4 5 {
  451. $intoks
  452. $iptr
  453. $end_tok1
  454. $end_tok2
  455. $end_tok3
  456. @intoks 4 param = ;
  457. @iptr 3 param = ;
  458. @end_tok1 2 param = ;
  459. @end_tok2 1 param = ;
  460. @end_tok3 0 param = ;
  461. $end_toks
  462. @end_toks 4 vector_init = ;
  463. end_toks end_tok1 vector_push_back ;
  464. end_toks end_tok2 vector_push_back ;
  465. end_toks end_tok3 vector_push_back ;
  466. $res
  467. @res intoks iptr end_toks ast_parse2 = ;
  468. end_toks vector_destroy ;
  469. res ret ;
  470. }
  471. fun ast_dump_int 2 {
  472. $ast
  473. $depth
  474. @ast 1 param = ;
  475. @depth 0 param = ;
  476. $i
  477. @i 0 = ;
  478. while i depth < {
  479. " " 1 platform_log ;
  480. @i i 1 + = ;
  481. }
  482. if ast AST_TYPE take 0 == {
  483. if ast AST_NAME take 0 == {
  484. "Placeholder operand" 1 platform_log ;
  485. } else {
  486. "Operand of type #" 1 platform_log ;
  487. ast AST_TYPE_IDX take itoa 1 platform_log ;
  488. ": " 1 platform_log ;
  489. ast AST_NAME take 1 platform_log ;
  490. }
  491. "\n" 1 platform_log ;
  492. } else {
  493. ast AST_TYPE take 1 == "ast_dump_int: error 1" assert_msg ;
  494. "Operator of type #" 1 platform_log ;
  495. ast AST_TYPE_IDX take itoa 1 platform_log ;
  496. ": " 1 platform_log ;
  497. ast AST_NAME take 1 platform_log ;
  498. "\n" 1 platform_log ;
  499. ast AST_LEFT take depth 1 + ast_dump_int ;
  500. ast AST_RIGHT take depth 1 + ast_dump_int ;
  501. }
  502. }
  503. fun ast_dump 1 {
  504. 0 param 0 ast_dump_int ;
  505. }