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.

218 lines
4.8 KiB

  1. #ifndef AST_H
  2. #define AST_H
  3. #include "sol.h"
  4. #include <stdio.h>
  5. typedef struct {
  6. size_t line;
  7. size_t col;
  8. } loc_t;
  9. struct tag_expr_node;
  10. typedef struct tag_expr_node expr_node;
  11. struct tag_stmt_node;
  12. typedef struct tag_stmt_node stmt_node;
  13. typedef enum {LIT_INT, LIT_FLOAT, LIT_STRING, LIT_NONE} lit_t;
  14. typedef struct {
  15. lit_t type;
  16. union {
  17. long ival;
  18. double fval;
  19. char *str;
  20. };
  21. } lit_node;
  22. typedef enum {OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_POW, OP_BAND, OP_BOR, OP_BXOR, OP_LAND, OP_LOR, OP_EQUAL, OP_NEQUAL, OP_LESS, OP_GREATER, OP_LESSEQ, OP_GREATEREQ, OP_LSHIFT, OP_RSHIFT} binop_t;
  23. typedef struct {
  24. binop_t type;
  25. expr_node *left;
  26. expr_node *right;
  27. } binop_node;
  28. typedef enum {OP_NEG, OP_BNOT, OP_LNOT, OP_LEN} unop_t;
  29. typedef struct {
  30. unop_t type;
  31. expr_node *expr;
  32. } unop_node;
  33. typedef struct {
  34. expr_node *expr;
  35. expr_node *index;
  36. } index_node;
  37. typedef struct {
  38. expr_node *expr;
  39. expr_node *index;
  40. expr_node *value;
  41. } setindex_node;
  42. typedef struct {
  43. char *ident;
  44. expr_node *value;
  45. } assign_node;
  46. typedef struct {
  47. char *ident;
  48. } ref_node;
  49. typedef struct tag_exprlist_node {
  50. expr_node *expr;
  51. struct tag_exprlist_node *next;
  52. } exprlist_node;
  53. typedef struct {
  54. expr_node *key;
  55. expr_node *value;
  56. } associtem_node;
  57. typedef struct tag_assoclist_node {
  58. associtem_node *item;
  59. struct tag_assoclist_node *next;
  60. } assoclist_node;
  61. typedef struct {
  62. exprlist_node *list;
  63. } listgen_node;
  64. typedef struct {
  65. assoclist_node *map;
  66. } mapgen_node;
  67. typedef struct {
  68. expr_node *expr;
  69. exprlist_node *args;
  70. } call_node;
  71. typedef struct tag_identlist_node {
  72. char *ident;
  73. struct tag_identlist_node *next;
  74. } identlist_node;
  75. typedef struct {
  76. char *name;
  77. identlist_node *args;
  78. stmt_node *body;
  79. } funcdecl_node;
  80. typedef enum {EX_LIT, EX_LISTGEN, EX_MAPGEN, EX_BINOP, EX_UNOP, EX_INDEX, EX_SETINDEX, EX_ASSIGN, EX_REF, EX_CALL, EX_FUNCDECL} expr_t;
  81. typedef struct tag_expr_node {
  82. expr_t type;
  83. loc_t loc;
  84. union {
  85. lit_node *lit;
  86. listgen_node *listgen;
  87. mapgen_node *mapgen;
  88. binop_node *binop;
  89. unop_node *unop;
  90. index_node *index;
  91. setindex_node *setindex;
  92. assign_node *assign;
  93. ref_node *ref;
  94. call_node *call;
  95. funcdecl_node *funcdecl;
  96. };
  97. } expr_node;
  98. typedef struct {
  99. expr_node *cond;
  100. stmt_node *iftrue;
  101. stmt_node *iffalse;
  102. } ifelse_node;
  103. typedef struct {
  104. expr_node *cond;
  105. stmt_node *loop;
  106. } loop_node;
  107. typedef struct {
  108. char *var;
  109. expr_node *iter;
  110. stmt_node *loop;
  111. } iter_node;
  112. typedef struct {
  113. expr_node *ret;
  114. } ret_node;
  115. typedef struct tag_stmtlist_node {
  116. stmt_node *stmt;
  117. struct tag_stmtlist_node *next;
  118. } stmtlist_node;
  119. typedef enum {ST_EXPR, ST_IFELSE, ST_LOOP, ST_ITER, ST_LIST, ST_RET, ST_CONT, ST_BREAK} stmt_t;
  120. typedef struct tag_stmt_node {
  121. stmt_t type;
  122. loc_t loc;
  123. union {
  124. expr_node *expr;
  125. ifelse_node *ifelse;
  126. loop_node *loop;
  127. iter_node *iter;
  128. stmtlist_node *stmtlist;
  129. ret_node *ret;
  130. };
  131. } stmt_node;
  132. #define AS_ST(arg) ((stmt_node *) (arg))
  133. #define AS_EX(arg) ((expr_node *) (arg))
  134. #define AS(arg, tp) ((tp *) (arg))
  135. #define NEW_ST() malloc(sizeof(stmt_node))
  136. #define NEW_EX() malloc(sizeof(expr_node))
  137. #define SET_LOC(node, l) do { (node)->loc.line = (l).first_line; (node)->loc.col = (l).first_column; } while(0)
  138. #define NEW(arg) malloc(sizeof(arg))
  139. #define MAKE_REF_BINOP(nd, tp, name, val) nd = NEW_EX(); \
  140. nd->type = EX_BINOP; \
  141. nd->binop = NEW(binop_node); \
  142. nd->binop->type = tp; \
  143. nd->binop->left = NEW_EX(); \
  144. nd->binop->left->type = EX_REF; \
  145. nd->binop->left->ref = NEW(ref_node); \
  146. nd->binop->left->ref->ident = name; \
  147. nd->binop->right = val
  148. #define MAKE_IDX_BINOP(nd, tp, obj, idx, val) nd = NEW_EX(); \
  149. nd->type = EX_BINOP; \
  150. nd->binop = NEW(binop_node); \
  151. nd->binop->type = tp; \
  152. nd->binop->left = NEW_EX(); \
  153. nd->binop->left->type = EX_INDEX; \
  154. nd->binop->left->index = NEW(index_node); \
  155. nd->binop->left->index->expr = obj; \
  156. nd->binop->left->index->index = idx; \
  157. nd->binop->right = val
  158. #define BOOL_TO_INT(cond) ((cond)?1:0)
  159. #define CALL_METHOD(state, obj, meth, args) ({\
  160. sol_object_t *res;\
  161. state->calling_type = obj->ops->tname;\
  162. state->calling_meth = #meth;\
  163. res = obj->ops->meth(state, args);\
  164. state->calling_type = "(none)";\
  165. state->calling_meth = "(none)";\
  166. res;\
  167. })
  168. sol_object_t *sol_new_func(sol_state_t *, identlist_node *, stmt_node *, char *);
  169. sol_object_t *sol_new_stmtnode(sol_state_t *, stmt_node *);
  170. sol_object_t *sol_new_exprnode(sol_state_t *, expr_node *);
  171. // runtime.c
  172. stmt_node *sol_compile(const char *);
  173. stmt_node *sol_compile_file(FILE *);
  174. expr_node *sol_comp_as_expr(stmt_node *);
  175. void sol_compile_free(stmt_node *);
  176. void st_free(stmt_node *);
  177. void ex_free(expr_node *);
  178. void st_print(sol_state_t *, stmt_node *);
  179. void ex_print(sol_state_t *, expr_node *);
  180. void ob_print(sol_object_t *);
  181. sol_object_t *sol_eval(sol_state_t *, expr_node *);
  182. void sol_exec(sol_state_t *, stmt_node *);
  183. #endif