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.

328 lines
5.8 KiB

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