The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

astprint.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "ast.h"
  2. #include "parser.tab.h"
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. void prlev(sol_state_t *state, int lev, const char *fmt, ...) {
  6. va_list vl;
  7. int i;
  8. for(i = 0; i < lev; i++) {
  9. sol_putchar(state, '|');
  10. sol_putchar(state, ' ');
  11. }
  12. va_start(vl, fmt);
  13. sol_vprintf(state, fmt, vl);
  14. va_end(vl);
  15. sol_putchar(state, '\n');
  16. }
  17. void prex( sol_state_t *, expr_node *, int);
  18. void prst(sol_state_t *state, stmt_node *node, int lev) {
  19. if(!node) {
  20. prlev(state, lev, "<NULL>");
  21. return;
  22. }
  23. switch(node->type) {
  24. case ST_EXPR:
  25. prlev(state, lev, "Stmt<Expr>:");
  26. prex(state, node->expr, lev + 1);
  27. break;
  28. case ST_LIST:
  29. prlev(state, lev, "Stmt<List>:");
  30. stmtlist_node *cur = node->stmtlist;
  31. while(cur && cur->stmt) {
  32. prst(state, cur->stmt, lev + 1);
  33. cur = cur->next;
  34. }
  35. break;
  36. case ST_RET:
  37. prlev(state, lev, "Stmt<Ret>:");
  38. prex(state, node->ret->ret, lev + 1);
  39. break;
  40. case ST_CONT:
  41. prlev(state, lev, "Stmt<Continue>:");
  42. prex(state, node->cont->val, lev + 1);
  43. break;
  44. case ST_BREAK:
  45. prlev(state, lev, "Stmt<Break>:");
  46. prex(state, node->brk->val, lev + 1);
  47. break;
  48. }
  49. }
  50. void prex(sol_state_t *state, expr_node *node, int lev) {
  51. assoclist_node *cura;
  52. exprlist_node *cure;
  53. identlist_node *curi;
  54. if(!node) {
  55. prlev(state, lev, "<NULL>");
  56. return;
  57. }
  58. switch(node->type) {
  59. case EX_LIT:
  60. prlev(state, lev, "Literal:");
  61. lev++;
  62. switch(node->lit->type) {
  63. case LIT_INT:
  64. prlev(state, lev, "Int: %ld", node->lit->ival);
  65. break;
  66. case LIT_FLOAT:
  67. prlev(state, lev, "Float: %f", node->lit->fval);
  68. break;
  69. case LIT_STRING:
  70. prlev(state, lev, "String: %s", node->lit->str);
  71. break;
  72. case LIT_BUFFER:
  73. prlev(state, lev, "Buffer of %lu bytes:", LENGTH_OF(node->lit->buf));
  74. prlev(state, lev + 1, "%s", BYTES_OF(node->lit->buf));
  75. case LIT_NONE:
  76. prlev(state, lev, "None");
  77. break;
  78. }
  79. break;
  80. case EX_LISTGEN:
  81. prlev(state, lev, "ListGen:");
  82. cure = node->listgen->list;
  83. while(cure && cure->expr) {
  84. prex(state, cure->expr, lev + 1);
  85. cure = cure->next;
  86. }
  87. break;
  88. case EX_MAPGEN:
  89. prlev(state, lev, "MapGen:");
  90. lev++;
  91. cura = node->mapgen->map;
  92. while(cura && cura->item) {
  93. prlev(state, lev, "<Key>:");
  94. prex(state, cura->item->key, lev + 1);
  95. prlev(state, lev, "<Value>:");
  96. prex(state, cura->item->value, lev + 1);
  97. cura = cura->next;
  98. }
  99. break;
  100. case EX_BINOP:
  101. prlev(state, lev, "BinOp:");
  102. lev++;
  103. switch(node->binop->type) {
  104. case OP_ADD:
  105. prlev(state, lev, "Op: +");
  106. break;
  107. case OP_SUB:
  108. prlev(state, lev, "Op: -");
  109. break;
  110. case OP_MUL:
  111. prlev(state, lev, "Op: *");
  112. break;
  113. case OP_DIV:
  114. prlev(state, lev, "Op: /");
  115. break;
  116. case OP_MOD:
  117. prlev(state, lev, "Op: %");
  118. break;
  119. case OP_POW:
  120. prlev(state, lev, "Op: **");
  121. break;
  122. case OP_TBANG:
  123. prlev(state, lev, "Op: !!!");
  124. break;
  125. case OP_BAND:
  126. prlev(state, lev, "Op: &");
  127. break;
  128. case OP_BOR:
  129. prlev(state, lev, "Op: |");
  130. break;
  131. case OP_BXOR:
  132. prlev(state, lev, "Op: ^");
  133. break;
  134. case OP_LAND:
  135. prlev(state, lev, "Op: &&");
  136. break;
  137. case OP_LOR:
  138. prlev(state, lev, "Op: ||");
  139. break;
  140. case OP_EQUAL:
  141. prlev(state, lev, "Op: ==");
  142. break;
  143. case OP_NEQUAL:
  144. prlev(state, lev, "Op: !=");
  145. break;
  146. case OP_LESS:
  147. prlev(state, lev, "Op: <");
  148. break;
  149. case OP_GREATER:
  150. prlev(state, lev, "Op: >");
  151. break;
  152. case OP_LESSEQ:
  153. prlev(state, lev, "Op: <=");
  154. break;
  155. case OP_GREATEREQ:
  156. prlev(state, lev, "Op: >=");
  157. break;
  158. case OP_LSHIFT:
  159. prlev(state, lev, "Op: <<");
  160. break;
  161. case OP_RSHIFT:
  162. prlev(state, lev, "Op: >>");
  163. break;
  164. }
  165. prlev(state, lev, "Left:");
  166. prex(state, node->binop->left, lev + 1);
  167. prlev(state, lev, "Right:");
  168. prex(state, node->binop->right, lev + 1);
  169. break;
  170. case EX_UNOP:
  171. prlev(state, lev, "UnOp:");
  172. lev++;
  173. switch(node->unop->type) {
  174. case OP_NEG:
  175. prlev(state, lev, "Op: -");
  176. break;
  177. case OP_BNOT:
  178. prlev(state, lev, "Op: ~");
  179. break;
  180. case OP_LNOT:
  181. prlev(state, lev, "Op: !");
  182. break;
  183. case OP_LEN:
  184. prlev(state, lev, "Op: #");
  185. break;
  186. }
  187. prlev(state, lev, "Expr:");
  188. prex(state, node->unop->expr, lev + 1);
  189. break;
  190. case EX_INDEX:
  191. prlev(state, lev, "Index:");
  192. lev++;
  193. prlev(state, lev, "Expr:");
  194. prex(state, node->index->expr, lev + 1);
  195. prlev(state, lev, "Index:");
  196. prex(state, node->index->index, lev + 1);
  197. break;
  198. case EX_SETINDEX:
  199. prlev(state, lev, "SetIndex:");
  200. lev++;
  201. prlev(state, lev, "Expr:");
  202. prex(state, node->setindex->expr, lev + 1);
  203. prlev(state, lev, "Index:");
  204. prex(state, node->setindex->index, lev + 1);
  205. prlev(state, lev, "Value:");
  206. prex(state, node->setindex->value, lev + 1);
  207. break;
  208. case EX_ASSIGN:
  209. prlev(state, lev, "Assign:");
  210. lev++;
  211. prlev(state, lev, "Ident: %s", node->assign->ident);
  212. prlev(state, lev, "Value:");
  213. prex(state, node->assign->value, lev + 1);
  214. break;
  215. case EX_REF:
  216. prlev(state, lev, "Ref: %s", node->ref->ident);
  217. break;
  218. case EX_CALL:
  219. prlev(state, lev, "Call:");
  220. lev++;
  221. prlev(state, lev, "Expr:");
  222. prex(state, node->call->expr, lev + 1);
  223. prlev(state, lev, "Method: %s", node->call->method);
  224. prlev(state, lev, "Args:");
  225. cure = node->call->args;
  226. while(cure && cure->expr) {
  227. prex(state, cure->expr, lev + 1);
  228. cure = cure->next;
  229. }
  230. break;
  231. case EX_FUNCDECL:
  232. prlev(state, lev, "FuncDecl:");
  233. lev++;
  234. prlev(state, lev, "Name: %s", node->funcdecl->name);
  235. prlev(state, lev, "Params:");
  236. if(!node->funcdecl->params) {
  237. prlev(state, lev + 1, "<NULL>");
  238. } else {
  239. prlev(state, lev + 1, "Args:");
  240. curi = node->funcdecl->params->args;
  241. while(curi && curi->ident) {
  242. prlev(state, lev + 2, curi->ident);
  243. curi = curi->next;
  244. }
  245. prlev(state, lev + 1, "ClKeys:");
  246. curi = node->funcdecl->params->clkeys;
  247. while(curi && curi->ident) {
  248. prlev(state, lev + 2, curi->ident);
  249. curi = curi->next;
  250. }
  251. prlev(state, lev + 1, "ClValues:");
  252. cure = node->funcdecl->params->clvalues;
  253. while(cure && cure->expr) {
  254. prex(state, cure->expr, lev + 2);
  255. cure = cure->next;
  256. }
  257. prlev(state, lev + 1, "Rest: %s", node->funcdecl->params->rest);
  258. }
  259. prlev(state, lev, "Body:");
  260. prst(state, node->funcdecl->body, lev + 1);
  261. break;
  262. case EX_IFELSE:
  263. prlev(state, lev, "Expr<IfElse>:");
  264. lev++;
  265. prlev(state, lev, "Cond:");
  266. prex(state, node->ifelse->cond, lev + 1);
  267. prlev(state, lev, "IfTrue:");
  268. prst(state, node->ifelse->iftrue, lev + 1);
  269. prlev(state, lev, "IfFalse:");
  270. prst(state, node->ifelse->iffalse, lev + 1);
  271. break;
  272. case EX_LOOP:
  273. prlev(state, lev, "Expr<Loop>:");
  274. lev++;
  275. prlev(state, lev, "Cond:");
  276. prex(state, node->loop->cond, lev + 1);
  277. prlev(state, lev, "Loop:");
  278. prst(state, node->loop->loop, lev + 1);
  279. break;
  280. case EX_ITER:
  281. prlev(state, lev, "Expr<Iter>:");
  282. lev++;
  283. prlev(state, lev, "Var: %s", node->iter->var);
  284. prlev(state, lev, "Iter:");
  285. prex(state, node->iter->iter, lev + 1);
  286. prlev(state, lev, "Loop:");
  287. prst(state, node->iter->loop, lev + 1);
  288. break;
  289. }
  290. }
  291. void st_print(sol_state_t *state, stmt_node *stmt) {
  292. prst(state, stmt, 0);
  293. }
  294. void ex_print(sol_state_t *state, expr_node *expr) {
  295. prex(state, expr, 0);
  296. }
  297. /*int main(int argc, char **argv) {
  298. stmt_node *program = NULL;
  299. if(argc>1) yydebug = 1;
  300. if(yyparse(&program)) {
  301. printf("Syntax error (somewhere)\n");
  302. printf("Partial tree:\n");
  303. prst(state, program, 0);
  304. return 1;
  305. }
  306. prst(state, program, 0);
  307. return 0;
  308. }*/