7 /* Note: I will not be using a _t suffix for the
8 * "main" ast node types for now.
11 typedef union ast_node_u ast_node;
12 typedef union ast_expression_u ast_expression;
14 typedef struct ast_value_s ast_value;
15 typedef struct ast_function_s ast_function;
16 typedef struct ast_block_s ast_block;
17 typedef struct ast_binary_s ast_binary;
19 /* Node interface with common components
21 typedef void ast_node_delete(ast_node*);
25 /* I don't feel comfortable using keywords like 'delete' as names... */
26 ast_node_delete *destroy;
29 #define ast_delete(x) ( ( (ast_node*)(x) ) -> node.destroy )((ast_node*)(x))
31 /* Expression interface
33 * Any expression or block returns an ir_value, and needs
34 * to know the current function.
36 typedef qbool ast_expression_codegen(ast_expression*,
42 ast_expression_codegen *codegen;
43 } ast_expression_common;
47 * Types are also values, both have a type and a name.
48 * especially considering possible constructs like typedefs.
50 * is like creating a 'float foo', foo serving as the type's name.
54 ast_expression_common expression;
73 /* if vtype is qc_function, params contain parameters, and
74 * 'next' the return type.
76 MEM_VECTOR_MAKE(ast_value*, params);
78 ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int qctype);
79 void ast_value_delete(ast_value*);
81 void ast_value_set_name(ast_value*, const char *name);
83 qbool ast_value_codegen(ast_value*, ast_function*, ir_value**);
87 * A value-returning binary expression.
91 ast_expression_common expression;
97 ast_binary* ast_binary_new(lex_ctx_t ctx,
101 void ast_binary_delete(ast_binary*);
103 /* hmm, seperate functions? */
104 qbool ast_bin_add_codegen(ast_binary*, ir_function*, ir_value**);
107 /* maybe for this one */
108 qbool ast_bin_store_codegen(ast_binary*, ir_function*, ir_value**);
110 /* could decide what to use */
111 qbool ast_binary_codegen(ast_binary*, ir_function*, ir_value**);
118 ast_expression_common expression;
120 MEM_VECTOR_MAKE(ast_value*, locals);
121 MEM_VECTOR_MAKE(ast_expression*, exprs);
123 ast_block* ast_block_new(lex_ctx_t ctx);
124 void ast_block_delete(ast_block*);
126 MEM_VECTOR_PROTO(ast_block, ast_value*, locals);
127 MEM_VECTOR_PROTO(ast_block, ast_expression*, exprs);
129 qbool ast_block_codegen(ast_block*, ir_function*, ir_value**);
133 * Contains a list of blocks... at least in theory.
134 * Usually there's just the main block, other blocks are inside that.
136 * Technically, functions don't need to be an AST node, since we have
137 * neither functions inside functions, nor lambdas, and function
138 * pointers could just work with a name. However, this way could be
139 * more flexible, and adds no real complexity.
141 struct ast_function_s
143 ast_node_common node;
148 MEM_VECTOR_MAKE(ast_block*, blocks);
150 ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype);
151 void ast_function_delete(ast_function*);
153 MEM_VECTOR_PROTO(ast_function, ast_block*, blocks);
155 qbool ast_function_codegen(ast_function *self, ir_builder *builder);
159 union ast_expression_u
161 ast_expression_common expression;
171 ast_node_common node;
172 ast_expression expression;