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.

199 lines
4.2 KiB

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