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.

335 lines
6.7 KiB

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