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 6.7KB

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