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.

343 lines
7.9 KiB

  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. char *method;
  107. } call_node;
  108. typedef struct tag_identlist_node {
  109. char *ident;
  110. struct tag_identlist_node *next;
  111. } identlist_node;
  112. typedef struct {
  113. identlist_node *args;
  114. exprlist_node *annos;
  115. identlist_node *clkeys;
  116. exprlist_node *clvalues;
  117. char *rest;
  118. } paramlist_node;
  119. typedef struct {
  120. char *name;
  121. paramlist_node *params;
  122. expr_node *anno;
  123. stmt_node *body;
  124. } funcdecl_node;
  125. typedef struct {
  126. expr_node *cond;
  127. stmt_node *iftrue;
  128. stmt_node *iffalse;
  129. } ifelse_node;
  130. typedef struct {
  131. expr_node *cond;
  132. stmt_node *loop;
  133. } loop_node;
  134. typedef struct {
  135. char *var;
  136. expr_node *iter;
  137. stmt_node *loop;
  138. } iter_node;
  139. 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;
  140. typedef struct tag_expr_node {
  141. expr_t type;
  142. loc_t loc;
  143. union {
  144. lit_node *lit;
  145. listgen_node *listgen;
  146. mapgen_node *mapgen;
  147. binop_node *binop;
  148. unop_node *unop;
  149. index_node *index;
  150. setindex_node *setindex;
  151. assign_node *assign;
  152. ref_node *ref;
  153. call_node *call;
  154. funcdecl_node *funcdecl;
  155. ifelse_node *ifelse;
  156. loop_node *loop;
  157. iter_node *iter;
  158. };
  159. } expr_node;
  160. typedef struct {
  161. expr_node *ret;
  162. } ret_node;
  163. typedef struct {
  164. expr_node *val;
  165. } cont_node;
  166. typedef struct {
  167. expr_node *val;
  168. } break_node;
  169. typedef struct tag_stmtlist_node {
  170. stmt_node *stmt;
  171. struct tag_stmtlist_node *next;
  172. } stmtlist_node;
  173. typedef enum {ST_EXPR, ST_LIST, ST_RET, ST_CONT, ST_BREAK} stmt_t;
  174. typedef struct tag_stmt_node {
  175. stmt_t type;
  176. loc_t loc;
  177. union {
  178. expr_node *expr;
  179. stmtlist_node *stmtlist;
  180. ret_node *ret;
  181. cont_node *cont;
  182. break_node *brk;
  183. };
  184. } stmt_node;
  185. typedef enum {
  186. BC_NULL,
  187. BC_ST_EXPR,
  188. BC_ST_LIST,
  189. BC_ST_RET,
  190. BC_ST_CONT,
  191. BC_ST_BREAK,
  192. BC_EX_LIT,
  193. BC_EX_LISTGEN,
  194. BC_EX_MAPGEN,
  195. BC_EX_BINOP,
  196. BC_EX_UNOP,
  197. BC_EX_INDEX,
  198. BC_EX_SETINDEX,
  199. BC_EX_ASSIGN,
  200. BC_EX_REF,
  201. BC_EX_CALL,
  202. BC_EX_FUNCDECL,
  203. BC_EX_IFELSE,
  204. BC_EX_LOOP,
  205. BC_EX_ITER,
  206. BC_LIT_INT,
  207. BC_LIT_FLOAT,
  208. BC_LIT_STRING,
  209. BC_LIT_NONE,
  210. BC_INT,
  211. BC_FLOAT,
  212. BC_STRING,
  213. BC_LIST_ST,
  214. BC_LIST_EX,
  215. BC_LIST_AS,
  216. BC_LIST_ID,
  217. BC_LIST_PM,
  218. BC_ENDLIST,
  219. } bytecode;
  220. #define AS_ST(arg) ((stmt_node *) (arg))
  221. #define AS_EX(arg) ((expr_node *) (arg))
  222. #define AS(arg, tp) ((tp *) (arg))
  223. #define NEW_ST() malloc(sizeof(stmt_node))
  224. #define NEW_EX() malloc(sizeof(expr_node))
  225. #define SET_LOC(node, l) do { (node)->loc.line = (l).first_line; (node)->loc.col = (l).first_column; } while(0)
  226. #define NEW(arg) malloc(sizeof(arg))
  227. #define MAKE_REF_BINOP(nd, tp, name, val) nd = NEW_EX(); \
  228. nd->type = EX_BINOP; \
  229. nd->binop = NEW(binop_node); \
  230. nd->binop->type = tp; \
  231. nd->binop->left = NEW_EX(); \
  232. nd->binop->left->type = EX_REF; \
  233. nd->binop->left->ref = NEW(ref_node); \
  234. nd->binop->left->ref->ident = strdup(name); \
  235. nd->binop->right = val
  236. #define MAKE_IDX_BINOP(nd, tp, obj, idx, val) nd = NEW_EX(); \
  237. nd->type = EX_BINOP; \
  238. nd->binop = NEW(binop_node); \
  239. nd->binop->type = tp; \
  240. nd->binop->left = NEW_EX(); \
  241. nd->binop->left->type = EX_INDEX; \
  242. nd->binop->left->index = NEW(index_node); \
  243. nd->binop->left->index->expr = ex_copy(obj); \
  244. nd->binop->left->index->index = ex_copy(idx); \
  245. nd->binop->right = val
  246. #define BOOL_TO_INT(cond) ((cond)?1:0)
  247. #define CALL_METHOD(state, obj, meth, args) ({\
  248. sol_object_t *res;\
  249. state->calling_type = obj->ops->tname;\
  250. state->calling_meth = #meth;\
  251. res = obj->ops->meth(state, args);\
  252. state->calling_type = "(none)";\
  253. state->calling_meth = "(none)";\
  254. res;\
  255. })
  256. sol_object_t *sol_new_func(sol_state_t *, identlist_node *, stmt_node *, char *, paramlist_node *, expr_node *);
  257. sol_object_t *sol_new_stmtnode(sol_state_t *, stmt_node *);
  258. sol_object_t *sol_new_exprnode(sol_state_t *, expr_node *);
  259. // runtime.c
  260. stmt_node *sol_compile(const char *);
  261. stmt_node *sol_compile_file(FILE *);
  262. expr_node *sol_comp_as_expr(stmt_node *);
  263. void sol_compile_free(stmt_node *);
  264. stmt_node *st_copy(stmt_node *);
  265. expr_node *ex_copy(expr_node *);
  266. stmtlist_node *stl_copy(stmtlist_node *);
  267. exprlist_node *exl_copy(exprlist_node *);
  268. assoclist_node *asl_copy(assoclist_node *);
  269. identlist_node *idl_copy(identlist_node *);
  270. paramlist_node *pl_copy(paramlist_node *);
  271. void st_free(stmt_node *);
  272. void ex_free(expr_node *);
  273. void stl_free(stmtlist_node *);
  274. void exl_free(exprlist_node *);
  275. void asl_free(assoclist_node *);
  276. void idl_free(identlist_node *);
  277. void pl_free(paramlist_node *);
  278. void st_print(sol_state_t *, stmt_node *);
  279. void ex_print(sol_state_t *, expr_node *);
  280. void ob_print(sol_object_t *);
  281. sol_object_t *sol_eval(sol_state_t *, expr_node *);
  282. void sol_exec(sol_state_t *, stmt_node *);
  283. // ser.c
  284. void sol_ser_stmt(FILE *, stmt_node *);
  285. void sol_ser_expr(FILE *, expr_node *);
  286. void sol_ser_stl(FILE *, stmtlist_node *);
  287. void sol_ser_exl(FILE *, exprlist_node *);
  288. void sol_ser_asl(FILE *, assoclist_node *);
  289. void sol_ser_idl(FILE *, identlist_node *);
  290. void sol_ser_pl(FILE *, paramlist_node *);
  291. void sol_ser_lit(FILE *, lit_node *);
  292. void sol_ser_int(FILE *, long);
  293. void sol_ser_float(FILE *, double);
  294. void sol_ser_str(FILE *, const char *);
  295. void *sol_deser(FILE *);
  296. void *sol_deser_checked(FILE *, bytecode);
  297. void *sol_deser_stmt(FILE *);
  298. void *sol_deser_expr(FILE *);
  299. void *sol_deser_lit(FILE *);
  300. #endif