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.

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