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.

ast.h 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef AST_H
  2. #define AST_H
  3. #include "sol.h"
  4. #include <stdio.h>
  5. /** Locator structure.
  6. *
  7. * Contains the location of a symbol; available on all `stmt_node`s and `expr_node`s as `loc`.
  8. */
  9. typedef struct {
  10. size_t line;
  11. size_t col;
  12. } loc_t;
  13. struct tag_expr_node;
  14. typedef struct tag_expr_node expr_node;
  15. struct tag_stmt_node;
  16. typedef struct tag_stmt_node stmt_node;
  17. /** Literal type
  18. *
  19. * Defines the types of literals that may appear in a source program.
  20. */
  21. typedef enum {LIT_INT=1024, LIT_FLOAT, LIT_STRING, LIT_NONE} lit_t;
  22. /** Literal node
  23. *
  24. * Represents a literal in a source program.
  25. */
  26. typedef struct {
  27. lit_t type; ///< The type of literal.
  28. union {
  29. long ival; ///< Integer value for `LIT_INT`.
  30. double fval; ///< Floating-point value for `LIT_FLOAT`.
  31. char *str; ///< String value for `LIT_STRING`.
  32. };
  33. } lit_node;
  34. /** Binary operation type
  35. *
  36. * Defines the types of binary operators that may occur in a source program.
  37. */
  38. typedef enum {OP_ADD=512, 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, OP_TBANG} binop_t;
  39. /** Binary operation node
  40. *
  41. * Represents a binary operator in a source program.
  42. */
  43. typedef struct {
  44. binop_t type; ///< The type of binary operation
  45. expr_node *left; ///< The left hand side.
  46. expr_node *right; ///< The right hand side.
  47. } binop_node;
  48. /** Unary operation type
  49. *
  50. * Defines the types of unary operators that may occur in a source program.
  51. */
  52. typedef enum {OP_NEG=768, OP_BNOT, OP_LNOT, OP_LEN} unop_t;
  53. /** Unary opreation node
  54. *
  55. * Represents a unary operator in a source program.
  56. */
  57. typedef struct {
  58. unop_t type; ///< The type of unary operation.
  59. expr_node *expr; ///< The value to which it is applied.
  60. } unop_node;
  61. /** Index node
  62. *
  63. * Represents an index operation in a source program.
  64. */
  65. typedef struct {
  66. expr_node *expr; ///< Expression to index.
  67. expr_node *index; ///< Expression to index by.
  68. } index_node;
  69. /** Setindex node
  70. *
  71. * Represents a setindex operation in a source program.
  72. */
  73. typedef struct {
  74. expr_node *expr; ///< Expression to set the index of.
  75. expr_node *index; ///< Expression to index by.
  76. expr_node *value; ///< Value to set said index to.
  77. } setindex_node;
  78. typedef struct {
  79. char *ident;
  80. expr_node *value;
  81. } assign_node;
  82. typedef struct {
  83. char *ident;
  84. } ref_node;
  85. typedef struct tag_exprlist_node {
  86. expr_node *expr;
  87. struct tag_exprlist_node *next;
  88. } exprlist_node;
  89. typedef struct {
  90. expr_node *key;
  91. expr_node *value;
  92. } associtem_node;
  93. typedef struct tag_assoclist_node {
  94. associtem_node *item;
  95. struct tag_assoclist_node *next;
  96. } assoclist_node;
  97. typedef struct {
  98. exprlist_node *list;
  99. } listgen_node;
  100. typedef struct {
  101. assoclist_node *map;
  102. } mapgen_node;
  103. typedef struct {
  104. expr_node *expr;
  105. exprlist_node *args;
  106. } call_node;
  107. typedef struct tag_identlist_node {
  108. char *ident;
  109. struct tag_identlist_node *next;
  110. } identlist_node;
  111. typedef struct {
  112. char *name;
  113. identlist_node *args;
  114. stmt_node *body;
  115. } funcdecl_node;
  116. typedef struct {
  117. expr_node *cond;
  118. stmt_node *iftrue;
  119. stmt_node *iffalse;
  120. } ifelse_node;
  121. typedef struct {
  122. expr_node *cond;
  123. stmt_node *loop;
  124. } loop_node;
  125. typedef struct {
  126. char *var;
  127. expr_node *iter;
  128. stmt_node *loop;
  129. } iter_node;
  130. typedef enum {EX_LIT=256, EX_LISTGEN, EX_MAPGEN, EX_BINOP, EX_UNOP, EX_INDEX, EX_SETINDEX, EX_ASSIGN, EX_REF, EX_CALL, EX_FUNCDECL, EX_IFELSE, EX_LOOP, EX_ITER} expr_t;
  131. typedef struct tag_expr_node {
  132. expr_t type;
  133. loc_t loc;
  134. union {
  135. lit_node *lit;
  136. listgen_node *listgen;
  137. mapgen_node *mapgen;
  138. binop_node *binop;
  139. unop_node *unop;
  140. index_node *index;
  141. setindex_node *setindex;
  142. assign_node *assign;
  143. ref_node *ref;
  144. call_node *call;
  145. funcdecl_node *funcdecl;
  146. ifelse_node *ifelse;
  147. loop_node *loop;
  148. iter_node *iter;
  149. };
  150. } expr_node;
  151. typedef struct {
  152. expr_node *ret;
  153. } ret_node;
  154. typedef struct {
  155. expr_node *val;
  156. } cont_node;
  157. typedef struct {
  158. expr_node *val;
  159. } break_node;
  160. typedef struct tag_stmtlist_node {
  161. stmt_node *stmt;
  162. struct tag_stmtlist_node *next;
  163. } stmtlist_node;
  164. typedef enum {ST_EXPR, ST_LIST, ST_RET, ST_CONT, ST_BREAK} stmt_t;
  165. typedef struct tag_stmt_node {
  166. stmt_t type;
  167. loc_t loc;
  168. union {
  169. expr_node *expr;
  170. stmtlist_node *stmtlist;
  171. ret_node *ret;
  172. cont_node *cont;
  173. break_node *brk;
  174. };
  175. } stmt_node;
  176. #define AS_ST(arg) ((stmt_node *) (arg))
  177. #define AS_EX(arg) ((expr_node *) (arg))
  178. #define AS(arg, tp) ((tp *) (arg))
  179. #define NEW_ST() malloc(sizeof(stmt_node))
  180. #define NEW_EX() malloc(sizeof(expr_node))
  181. #define SET_LOC(node, l) do { (node)->loc.line = (l).first_line; (node)->loc.col = (l).first_column; } while(0)
  182. #define NEW(arg) malloc(sizeof(arg))
  183. #define MAKE_REF_BINOP(nd, tp, name, val) nd = NEW_EX(); \
  184. nd->type = EX_BINOP; \
  185. nd->binop = NEW(binop_node); \
  186. nd->binop->type = tp; \
  187. nd->binop->left = NEW_EX(); \
  188. nd->binop->left->type = EX_REF; \
  189. nd->binop->left->ref = NEW(ref_node); \
  190. nd->binop->left->ref->ident = strdup(name); \
  191. nd->binop->right = val
  192. #define MAKE_IDX_BINOP(nd, tp, obj, idx, val) nd = NEW_EX(); \
  193. nd->type = EX_BINOP; \
  194. nd->binop = NEW(binop_node); \
  195. nd->binop->type = tp; \
  196. nd->binop->left = NEW_EX(); \
  197. nd->binop->left->type = EX_INDEX; \
  198. nd->binop->left->index = NEW(index_node); \
  199. nd->binop->left->index->expr = ex_copy(obj); \
  200. nd->binop->left->index->index = ex_copy(idx); \
  201. nd->binop->right = val
  202. #define BOOL_TO_INT(cond) ((cond)?1:0)
  203. #define CALL_METHOD(state, obj, meth, args) ({\
  204. sol_object_t *res;\
  205. state->calling_type = obj->ops->tname;\
  206. state->calling_meth = #meth;\
  207. res = obj->ops->meth(state, args);\
  208. state->calling_type = "(none)";\
  209. state->calling_meth = "(none)";\
  210. res;\
  211. })
  212. sol_object_t *sol_new_func(sol_state_t *, identlist_node *, stmt_node *, char *);
  213. sol_object_t *sol_new_stmtnode(sol_state_t *, stmt_node *);
  214. sol_object_t *sol_new_exprnode(sol_state_t *, expr_node *);
  215. // runtime.c
  216. stmt_node *sol_compile(const char *);
  217. stmt_node *sol_compile_file(FILE *);
  218. expr_node *sol_comp_as_expr(stmt_node *);
  219. void sol_compile_free(stmt_node *);
  220. stmt_node *st_copy(stmt_node *);
  221. expr_node *ex_copy(expr_node *);
  222. stmtlist_node *stl_copy(stmtlist_node *);
  223. exprlist_node *exl_copy(exprlist_node *);
  224. assoclist_node *asl_copy(assoclist_node *);
  225. identlist_node *idl_copy(identlist_node *);
  226. void st_free(stmt_node *);
  227. void ex_free(expr_node *);
  228. void stl_free(stmtlist_node *);
  229. void exl_free(exprlist_node *);
  230. void asl_free(assoclist_node *);
  231. void idl_free(identlist_node *);
  232. void st_print(sol_state_t *, stmt_node *);
  233. void ex_print(sol_state_t *, expr_node *);
  234. void ob_print(sol_object_t *);
  235. sol_object_t *sol_eval(sol_state_t *, expr_node *);
  236. void sol_exec(sol_state_t *, stmt_node *);
  237. #endif