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 8.6KB

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