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.

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