1 /* Declarations for the tree builder.
2  */
3 
4 /*
5 
6     Copyright (C) 1991-2003 The National Gallery
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 
22  */
23 
24 /*
25 
26     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
27 
28  */
29 
30 /* The kinds of nodes we can have in a parse tree.
31  */
32 typedef enum {
33 	NODE_NONE,		/* Empty */
34 	NODE_APPLY,		/* A function application */
35 	NODE_BINOP,		/* A binary operator */
36 	NODE_UOP,		/* A unary operator */
37 	NODE_LEAF,		/* A leaf .. a symbol of some sort */
38 	NODE_CLASS,		/* A class */
39 	NODE_TAG,		/* A tag .. rhs of '.' */
40 	NODE_CONST,		/* A constant */
41 	NODE_GENERATOR,		/* A list generator */
42 	NODE_COMPOSE,		/* Function composition */
43 	NODE_SUPER,		/* Superclass constructor */
44 	NODE_PATTERN_CLASS,	/* A class pattern match */
45 	NODE_LISTCONST		/* A list expression "[1, 2, 3]" */
46 } NodeTypes;
47 
48 /* Binary operators.
49 
50 	Order important! Keep changes in step with operator_table[] in
51 	action.c
52 
53  */
54 typedef enum {
55 	BI_NONE = 0,		/* Nothing */
56 	BI_ADD,			/* Addition and subtraction */
57 	BI_SUB,
58 	BI_REM,			/* Remainder after division */
59 	BI_POW,			/* Raise to power */
60 	BI_SELECT,		/* Select a channel/subscript */
61 	BI_LSHIFT,		/* Shift left */
62 	BI_RSHIFT,		/* Shift right */
63 	BI_DIV,			/* Divide by a constant */
64 	BI_JOIN,		/* Join of two objects */
65 	BI_DOT,			/* Projection operator */
66 	BI_COMMA,		/* Form complex number */
67 	BI_MUL,			/* Mult by a constant */
68 	BI_LAND,		/* Logical and */
69 	BI_LOR,			/* Logical or */
70 	BI_BAND,		/* Bitwise and */
71 	BI_BOR,			/* Bitwise or */
72 	BI_EOR,			/* Either - or */
73 	BI_EQ,			/* Equality */
74 	BI_NOTEQ,
75 	BI_PEQ,			/* Pointer equality */
76 	BI_PNOTEQ,
77 	BI_LESS,		/* Relational ops */
78 	BI_LESSEQ,
79 	BI_MORE,
80 	BI_MOREEQ,
81 	BI_IF,			/* if-then-else */
82 	BI_CONS			/* List cons ... has to be last, see below */
83 } BinOp;
84 
85 /* Unary operators.
86 
87 	Order important! Keep changes in step with operator_table[] in
88 	action.c
89 
90  */
91 typedef enum {
92 	UN_NONE = BI_CONS + 1,	/* Nothing */
93 	UN_CSCHAR,		/* Convert to signed char */
94 	UN_CUCHAR,		/* Convert to unsigned char */
95 	UN_CSSHORT,		/* Convert to signed short */
96 	UN_CUSHORT,		/* Convert to unsigned short */
97 	UN_CSINT,		/* Convert to signed int */
98 	UN_CUINT,		/* Convert to unsigned int */
99 	UN_CFLOAT,		/* Convert to signed float */
100 	UN_CDOUBLE,		/* Convert to signed double */
101 	UN_CCOMPLEX,		/* Convert to complex */
102 	UN_CDCOMPLEX,		/* Convert to double complex */
103 	UN_MINUS,		/* Unary minus */
104 	UN_NEG,			/* Logical negation, "!" */
105 	UN_COMPLEMENT,		/* 1s complement, "~" */
106 	UN_PLUS,		/* Unary plus */
107 	UN_LAST			/* Sanity check with this */
108 } UnOp;
109 
110 /* The sorts of constants we can have in expressions.
111  */
112 typedef enum {
113 	PARSE_CONST_NONE,
114 	PARSE_CONST_STR,
115 	PARSE_CONST_BOOL,
116 	PARSE_CONST_NUM,
117 	PARSE_CONST_COMPLEX,		/* Eg. 12j == (0, 12) */
118 	PARSE_CONST_CHAR,
119 	PARSE_CONST_ELIST		/* Empty list [] */
120 } ParseConstTypes;
121 
122 /* Constants in expressions.
123  */
124 struct _ParseConst {
125 	ParseConstTypes type;
126 	union {
127 		double num;
128 		char *str;
129 		gboolean bool;
130 		int ch;
131 	} val;
132 };
133 
134 /* A parse tree node.
135  */
136 struct _ParseNode {
137 	/* Compiled in here.
138 	 */
139 	Compile *compile;
140 
141 	NodeTypes type;
142 
143 	/* Bundle for node types with up to two arguments.
144 	 */
145 	BinOp biop;
146 	UnOp uop;
147 	ParseNode *arg1;
148 	ParseNode *arg2;
149 
150 	/* Just for generators, eg. [a, b .. c]
151 	 */
152 	ParseNode *arg3;
153 
154 	/* A symbol reference.
155 	 */
156 	Symbol *leaf;
157 
158 	/* A class.
159 	 */
160 	Compile *klass;
161 
162 	/* Expression list ... super constructor plus args, or list constant.
163 	 */
164 	GSList *elist;
165 
166 	/* A tag.
167 	 */
168 	char *tag;
169 
170 	/* A constant.
171 	 */
172 	ParseConst con;
173 };
174 
175 void tree_const_destroy( ParseConst *pc );
176 
177 ParseNode *tree_binop_new( Compile *compile,
178 	BinOp op, ParseNode *l, ParseNode *r );
179 ParseNode *tree_generator_new( Compile *compile,
180 	ParseNode *s, ParseNode *i, ParseNode *f );
181 ParseNode *tree_lconst_new( Compile *compile, ParseNode *s );
182 ParseNode *tree_lconst_extend( Compile *compile, ParseNode *s, ParseNode *n );
183 ParseNode *tree_super_new( Compile *compile );
184 ParseNode *tree_super_extend( Compile *compile, ParseNode *base, ParseNode *n );
185 ParseNode *tree_ifelse_new( Compile *compile,
186 	ParseNode *c, ParseNode *t, ParseNode *e );
187 ParseNode *tree_appl_new( Compile *compile, ParseNode *l, ParseNode *r );
188 ParseNode *tree_tag_new( Compile *compile, const char *r );
189 ParseNode *tree_unop_new( Compile *compile, UnOp op, ParseNode *a );
190 ParseNode *tree_leaf_new( Compile *compile, const char *name );
191 ParseNode *tree_leafsym_new( Compile *compile, Symbol *sym );
192 ParseNode *tree_const_new( Compile *compile, ParseConst n );
193 ParseNode *tree_class_new( Compile *compile );
194 ParseNode *tree_compose_new( Compile *compile, ParseNode *f, ParseNode *g );
195 ParseNode *tree_pattern_new( Compile *compile );
196 ParseNode *tree_pattern_class_new( Compile *compile,
197 	const char *class_name, ParseNode *l );
198 
199 void *tree_node_destroy( ParseNode *n );
200 
201 typedef ParseNode *(*tree_map_fn)( Compile *, ParseNode *, void *, void * );
202 ParseNode *tree_map( Compile *compile,
203 	tree_map_fn fn, ParseNode *node, void *a, void *b );
204 
205 /* Copy a tree into a new context.
206  */
207 ParseNode *tree_copy( Compile *compile, ParseNode *node );
208