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.

324 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_POW:
  136. prlev(lev, "Op: **");
  137. break;
  138. case OP_BAND:
  139. prlev(lev, "Op: &");
  140. break;
  141. case OP_BOR:
  142. prlev(lev, "Op: |");
  143. break;
  144. case OP_BXOR:
  145. prlev(lev, "Op: ^");
  146. break;
  147. case OP_LAND:
  148. prlev(lev, "Op: &&");
  149. break;
  150. case OP_LOR:
  151. prlev(lev, "Op: ||");
  152. break;
  153. case OP_EQUAL:
  154. prlev(lev, "Op: ==");
  155. break;
  156. case OP_LESS:
  157. prlev(lev, "Op: <");
  158. break;
  159. case OP_GREATER:
  160. prlev(lev, "Op: >");
  161. break;
  162. case OP_LESSEQ:
  163. prlev(lev, "Op: <=");
  164. break;
  165. case OP_GREATEREQ:
  166. prlev(lev, "Op: >=");
  167. break;
  168. case OP_LSHIFT:
  169. prlev(lev, "Op: <<");
  170. break;
  171. case OP_RSHIFT:
  172. prlev(lev, "Op: >>");
  173. break;
  174. }
  175. prlev(lev, "Left:");
  176. prex(node->binop->left, lev+1);
  177. prlev(lev, "Right:");
  178. prex(node->binop->right, lev+1);
  179. break;
  180. case EX_UNOP:
  181. prlev(lev, "UnOp:");
  182. lev++;
  183. switch(node->unop->type) {
  184. case OP_NEG:
  185. prlev(lev, "Op: -");
  186. break;
  187. case OP_BNOT:
  188. prlev(lev, "Op: ~");
  189. break;
  190. case OP_LNOT:
  191. prlev(lev, "Op: !");
  192. break;
  193. case OP_LEN:
  194. prlev(lev, "Op: #");
  195. break;
  196. }
  197. prlev(lev, "Expr:");
  198. prex(node->unop->expr, lev+1);
  199. break;
  200. case EX_INDEX:
  201. prlev(lev, "Index:");
  202. lev++;
  203. prlev(lev, "Expr:");
  204. prex(node->index->expr, lev+1);
  205. prlev(lev, "Index:");
  206. prex(node->index->index, lev+1);
  207. break;
  208. case EX_SETINDEX:
  209. prlev(lev, "SetIndex:");
  210. lev++;
  211. prlev(lev, "Expr:");
  212. prex(node->setindex->expr, lev+1);
  213. prlev(lev, "Index:");
  214. prex(node->setindex->index, lev+1);
  215. prlev(lev, "Value:");
  216. prex(node->setindex->value, lev+1);
  217. break;
  218. case EX_ASSIGN:
  219. prlev(lev, "Assign:");
  220. lev++;
  221. prlev(lev, "Ident: %s", node->assign->ident);
  222. prlev(lev, "Value:");
  223. prex(node->assign->value, lev+1);
  224. break;
  225. case EX_REF:
  226. prlev(lev, "Ref: %s", node->ref->ident);
  227. break;
  228. case EX_CALL:
  229. prlev(lev, "Call:");
  230. lev++;
  231. prlev(lev, "Expr:");
  232. prex(node->call->expr, lev+1);
  233. prlev(lev, "Args:");
  234. cure = node->call->args;
  235. while(cure && cure->expr) {
  236. prex(cure->expr, lev+1);
  237. cure = cure->next;
  238. }
  239. break;
  240. case EX_FUNCDECL:
  241. prlev(lev, "FuncDecl:");
  242. lev++;
  243. prlev(lev, "Name: %s", node->funcdecl->name);
  244. prlev(lev, "Args:");
  245. curi = node->funcdecl->args;
  246. while(curi && curi->ident) {
  247. prlev(lev+1, curi->ident);
  248. curi = curi->next;
  249. }
  250. prlev(lev, "Body:");
  251. prst(node->funcdecl->body, lev+1);
  252. break;
  253. }
  254. }
  255. void st_print(stmt_node *stmt) {
  256. prst(stmt, 0);
  257. }
  258. void ex_print(expr_node *expr) {
  259. prex(expr, 0);
  260. }
  261. /*int main(int argc, char **argv) {
  262. stmt_node *program = NULL;
  263. if(argc>1) yydebug = 1;
  264. if(yyparse(&program)) {
  265. printf("Syntax error (somewhere)\n");
  266. printf("Partial tree:\n");
  267. prst(program, 0);
  268. return 1;
  269. }
  270. prst(program, 0);
  271. return 0;
  272. }*/