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.

353 lines
8.5 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_BUFFER, 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. unsigned long *buf; ///< Buffer value for `LIT_BUFFER`; points to. (char *)(buf + 1) points to the first byte in the buffer. There are *buf bytes starting there. See also LENGTH_OF and BYTES_OF.
  33. };
  34. } lit_node;
  35. /** Returns the length (as an unsigned long) of the buffer in bytes, not including the length itself. */
  36. #define LENGTH_OF(buf) (*((unsigned long *) (buf)))
  37. /** Returns a (char *) pointing to the first byte in the buffer. */
  38. #define BYTES_OF(buf) ((char *) (((unsigned long *) (buf)) + 1))
  39. /** Binary operation type
  40. *
  41. * Defines the types of binary operators that may occur in a source program.
  42. */
  43. 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;
  44. /** Binary operation node
  45. *
  46. * Represents a binary operator in a source program.
  47. */
  48. typedef struct {
  49. binop_t type; ///< The type of binary operation
  50. expr_node *left; ///< The left hand side.
  51. expr_node *right; ///< The right hand side.
  52. } binop_node;
  53. /** Unary operation type
  54. *
  55. * Defines the types of unary operators that may occur in a source program.
  56. */
  57. typedef enum {OP_NEG=768, OP_BNOT, OP_LNOT, OP_LEN} unop_t;
  58. /** Unary opreation node
  59. *
  60. * Represents a unary operator in a source program.
  61. */
  62. typedef struct {
  63. unop_t type; ///< The type of unary operation.
  64. expr_node *expr; ///< The value to which it is applied.
  65. } unop_node;
  66. /** Index node
  67. *
  68. * Represents an index operation in a source program.
  69. */
  70. typedef struct {
  71. expr_node *expr; ///< Expression to index.
  72. expr_node *index; ///< Expression to index by.
  73. } index_node;
  74. /** Setindex node
  75. *
  76. * Represents a setindex operation in a source program.
  77. */
  78. typedef struct {
  79. expr_node *expr; ///< Expression to set the index of.
  80. expr_node *index; ///< Expression to index by.
  81. expr_node *value; ///< Value to set said index to.
  82. } setindex_node;
  83. typedef struct {
  84. char *ident;
  85. expr_node *value;
  86. } assign_node;
  87. typedef struct {
  88. char *ident;
  89. } ref_node;
  90. typedef struct tag_exprlist_node {
  91. expr_node *expr;
  92. struct tag_exprlist_node *next;
  93. } exprlist_node;
  94. typedef struct {
  95. expr_node *key;
  96. expr_node *value;
  97. } associtem_node;
  98. typedef struct tag_assoclist_node {
  99. associtem_node *item;
  100. struct tag_assoclist_node *next;
  101. } assoclist_node;
  102. typedef struct {
  103. exprlist_node *list;
  104. } listgen_node;
  105. typedef struct {
  106. assoclist_node *map;
  107. } mapgen_node;
  108. typedef struct {
  109. expr_node *expr;
  110. exprlist_node *args;
  111. char *method;
  112. } call_node;
  113. typedef struct tag_identlist_node {
  114. char *ident;
  115. struct tag_identlist_node *next;
  116. } identlist_node;
  117. typedef struct {
  118. identlist_node *args;
  119. exprlist_node *annos;
  120. identlist_node *clkeys;
  121. exprlist_node *clvalues;
  122. char *rest;
  123. } paramlist_node;
  124. typedef struct {
  125. char *name;
  126. paramlist_node *params;
  127. expr_node *anno;
  128. stmt_node *body;
  129. } funcdecl_node;
  130. typedef struct {
  131. expr_node *cond;
  132. stmt_node *iftrue;
  133. stmt_node *iffalse;
  134. } ifelse_node;
  135. typedef struct {
  136. expr_node *cond;
  137. stmt_node *loop;
  138. } loop_node;
  139. typedef struct {
  140. char *var;
  141. expr_node *iter;
  142. stmt_node *loop;
  143. } iter_node;
  144. 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;
  145. typedef struct tag_expr_node {
  146. expr_t type;
  147. loc_t loc;
  148. union {
  149. lit_node *lit;
  150. listgen_node *listgen;
  151. mapgen_node *mapgen;
  152. binop_node *binop;
  153. unop_node *unop;
  154. index_node *index;
  155. setindex_node *setindex;
  156. assign_node *assign;
  157. ref_node *ref;
  158. call_node *call;
  159. funcdecl_node *funcdecl;
  160. ifelse_node *ifelse;
  161. loop_node *loop;
  162. iter_node *iter;
  163. };
  164. } expr_node;
  165. typedef struct {
  166. expr_node *ret;
  167. } ret_node;
  168. typedef struct {
  169. expr_node *val;
  170. } cont_node;
  171. typedef struct {
  172. expr_node *val;
  173. } break_node;
  174. typedef struct tag_stmtlist_node {
  175. stmt_node *stmt;
  176. struct tag_stmtlist_node *next;
  177. } stmtlist_node;
  178. typedef enum {ST_EXPR, ST_LIST, ST_RET, ST_CONT, ST_BREAK} stmt_t;
  179. typedef struct tag_stmt_node {
  180. stmt_t type;
  181. loc_t loc;
  182. union {
  183. expr_node *expr;
  184. stmtlist_node *stmtlist;
  185. ret_node *ret;
  186. cont_node *cont;
  187. break_node *brk;
  188. };
  189. } stmt_node;
  190. typedef enum {
  191. BC_NULL,
  192. BC_ST_EXPR,
  193. BC_ST_LIST,
  194. BC_ST_RET,
  195. BC_ST_CONT,
  196. BC_ST_BREAK,
  197. BC_EX_LIT,
  198. BC_EX_LISTGEN,
  199. BC_EX_MAPGEN,
  200. BC_EX_BINOP,
  201. BC_EX_UNOP,
  202. BC_EX_INDEX,
  203. BC_EX_SETINDEX,
  204. BC_EX_ASSIGN,
  205. BC_EX_REF,
  206. BC_EX_CALL,
  207. BC_EX_FUNCDECL,
  208. BC_EX_IFELSE,
  209. BC_EX_LOOP,
  210. BC_EX_ITER,
  211. BC_LIT_INT,
  212. BC_LIT_FLOAT,
  213. BC_LIT_STRING,
  214. BC_LIT_BUFFER,
  215. BC_LIT_NONE,
  216. BC_INT,
  217. BC_FLOAT,
  218. BC_STRING,
  219. BC_BUFFER,
  220. BC_LIST_ST,
  221. BC_LIST_EX,
  222. BC_LIST_AS,
  223. BC_LIST_ID,
  224. BC_LIST_PM,
  225. BC_ENDLIST,
  226. } bytecode;
  227. #define AS_ST(arg) ((stmt_node *) (arg))
  228. #define AS_EX(arg) ((expr_node *) (arg))
  229. #define AS(arg, tp) ((tp *) (arg))
  230. #define NEW_ST() malloc(sizeof(stmt_node))
  231. #define NEW_EX() malloc(sizeof(expr_node))
  232. #define SET_LOC(node, l) do { (node)->loc.line = (l).first_line; (node)->loc.col = (l).first_column; } while(0)
  233. #define NEW(arg) malloc(sizeof(arg))
  234. #define MAKE_REF_BINOP(nd, tp, name, val) nd = NEW_EX(); \
  235. nd->type = EX_BINOP; \
  236. nd->binop = NEW(binop_node); \
  237. nd->binop->type = tp; \
  238. nd->binop->left = NEW_EX(); \
  239. nd->binop->left->type = EX_REF; \
  240. nd->binop->left->ref = NEW(ref_node); \
  241. nd->binop->left->ref->ident = strdup(name); \
  242. nd->binop->right = val
  243. #define MAKE_IDX_BINOP(nd, tp, obj, idx, val) nd = NEW_EX(); \
  244. nd->type = EX_BINOP; \
  245. nd->binop = NEW(binop_node); \
  246. nd->binop->type = tp; \
  247. nd->binop->left = NEW_EX(); \
  248. nd->binop->left->type = EX_INDEX; \
  249. nd->binop->left->index = NEW(index_node); \
  250. nd->binop->left->index->expr = ex_copy(obj); \
  251. nd->binop->left->index->index = ex_copy(idx); \
  252. nd->binop->right = val
  253. #define BOOL_TO_INT(cond) ((cond)?1:0)
  254. #define CALL_METHOD(state, obj, meth, args) ({\
  255. sol_object_t *res;\
  256. state->calling_type = obj->ops->tname;\
  257. state->calling_meth = #meth;\
  258. res = obj->ops->meth(state, args);\
  259. state->calling_type = "(none)";\
  260. state->calling_meth = "(none)";\
  261. res;\
  262. })
  263. sol_object_t *sol_new_func(sol_state_t *, identlist_node *, stmt_node *, char *, paramlist_node *, expr_node *);
  264. sol_object_t *sol_new_stmtnode(sol_state_t *, stmt_node *);
  265. sol_object_t *sol_new_exprnode(sol_state_t *, expr_node *);
  266. // runtime.c
  267. stmt_node *sol_compile(const char *);
  268. stmt_node *sol_compile_buffer(const char *, size_t);
  269. stmt_node *sol_compile_file(FILE *);
  270. expr_node *sol_comp_as_expr(stmt_node *);
  271. void sol_compile_free(stmt_node *);
  272. stmt_node *st_copy(stmt_node *);
  273. expr_node *ex_copy(expr_node *);
  274. stmtlist_node *stl_copy(stmtlist_node *);
  275. exprlist_node *exl_copy(exprlist_node *);
  276. assoclist_node *asl_copy(assoclist_node *);
  277. identlist_node *idl_copy(identlist_node *);
  278. paramlist_node *pl_copy(paramlist_node *);
  279. void st_free(stmt_node *);
  280. void ex_free(expr_node *);
  281. void stl_free(stmtlist_node *);
  282. void exl_free(exprlist_node *);
  283. void asl_free(assoclist_node *);
  284. void idl_free(identlist_node *);
  285. void pl_free(paramlist_node *);
  286. void st_print(sol_state_t *, stmt_node *);
  287. void ex_print(sol_state_t *, expr_node *);
  288. void ob_print(sol_object_t *);
  289. sol_object_t *sol_eval(sol_state_t *, expr_node *);
  290. void sol_exec(sol_state_t *, stmt_node *);
  291. // ser.c
  292. void sol_ser_stmt(FILE *, stmt_node *);
  293. void sol_ser_expr(FILE *, expr_node *);
  294. void sol_ser_stl(FILE *, stmtlist_node *);
  295. void sol_ser_exl(FILE *, exprlist_node *);
  296. void sol_ser_asl(FILE *, assoclist_node *);
  297. void sol_ser_idl(FILE *, identlist_node *);
  298. void sol_ser_pl(FILE *, paramlist_node *);
  299. void sol_ser_lit(FILE *, lit_node *);
  300. void sol_ser_int(FILE *, long);
  301. void sol_ser_float(FILE *, double);
  302. void sol_ser_str(FILE *, const char *);
  303. void sol_ser_buf(FILE *, unsigned long *);
  304. void *sol_deser(FILE *);
  305. void *sol_deser_checked(FILE *, bytecode);
  306. void *sol_deser_stmt(FILE *);
  307. void *sol_deser_expr(FILE *);
  308. void *sol_deser_lit(FILE *);
  309. #endif