Skip to content

Commit b58e30c

Browse files
committed
move object creation to factory.c to make testing possible
1 parent 4a71f48 commit b58e30c

5 files changed

Lines changed: 240 additions & 233 deletions

File tree

include/express/factory.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __FACTORY_H_
2+
#define __FACTORY_H_
3+
4+
void FACTORYinitialize();
5+
6+
#endif /* __FACTORY_H_ */

src/express/expr.c

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,8 @@ static inline int OPget_number_of_operands( Op_Code op ) {
111111
}
112112
}
113113

114-
Expression EXPcreate( Type type ) {
115-
Expression e;
116-
e = EXP_new();
117-
SYMBOLset( e );
118-
e->type = type;
119-
e->return_type = Type_Unknown;
120-
return( e );
121-
}
122-
123-
/**
124-
* use this when the return_type is the same as the type
125-
* For example, for constant integers
126-
*/
127-
Expression EXPcreate_simple( Type type ) {
128-
Expression e;
129-
e = EXP_new();
130-
SYMBOLset( e );
131-
e->type = e->return_type = type;
132-
return( e );
133-
}
134-
135-
Expression EXPcreate_from_symbol( Type type, Symbol * symbol ) {
136-
Expression e;
137-
e = EXP_new();
138-
e->type = type;
139-
e->return_type = Type_Unknown;
140-
e->symbol = *symbol;
141-
return e;
142-
}
143-
144114
/** Description: Initialize the Expression module. */
145115
void EXPinitialize( void ) {
146-
147116
#ifdef does_not_appear_to_be_necessary_or_even_make_sense
148117
LITERAL_EMPTY_SET = EXPcreate_simple( Type_Set );
149118
LITERAL_EMPTY_SET->u.list = LISTcreate();
@@ -882,78 +851,6 @@ void EXPop_init() {
882851
EXPop_create( OP_UNKNOWN, "UNKNOWN OP", EXPresolve_op_unknown );
883852
}
884853

885-
886-
/**
887-
** \param op operation
888-
** \param operand1 - first operand
889-
** \param operand2 - second operand
890-
** \param operand3 - third operand
891-
** \returns Ternary_Expression - the expression created
892-
** Create a ternary operation Expression.
893-
*/
894-
Expression TERN_EXPcreate( Op_Code op, Expression operand1, Expression operand2, Expression operand3 ) {
895-
Expression e = EXPcreate( Type_Expression );
896-
897-
e->e.op_code = op;
898-
e->e.op1 = operand1;
899-
e->e.op2 = operand2;
900-
e->e.op3 = operand3;
901-
return e;
902-
}
903-
904-
/**
905-
** \fn BIN_EXPcreate
906-
** \param op operation
907-
** \param operand1 - first operand
908-
** \param operand2 - second operand
909-
** \returns Binary_Expression - the expression created
910-
** Create a binary operation Expression.
911-
*/
912-
Expression BIN_EXPcreate( Op_Code op, Expression operand1, Expression operand2 ) {
913-
Expression e = EXPcreate( Type_Expression );
914-
915-
e->e.op_code = op;
916-
e->e.op1 = operand1;
917-
e->e.op2 = operand2;
918-
return e;
919-
}
920-
921-
/**
922-
** \param op operation
923-
** \param operand operand
924-
** \returns the expression created
925-
** Create a unary operation Expression.
926-
*/
927-
Expression UN_EXPcreate( Op_Code op, Expression operand ) {
928-
Expression e = EXPcreate( Type_Expression );
929-
930-
e->e.op_code = op;
931-
e->e.op1 = operand;
932-
return e;
933-
}
934-
935-
/**
936-
** \param local local identifier for source elements
937-
** \param aggregate source aggregate to query
938-
** \returns the query expression created
939-
** Create a query Expression.
940-
** NOTE Dec 2011 - MP - function description did not match actual params. Had to guess.
941-
*/
942-
Expression QUERYcreate( Symbol * local, Expression aggregate ) {
943-
Expression e = EXPcreate_from_symbol( Type_Query, local );
944-
Scope s = SCOPEcreate_tiny( OBJ_QUERY );
945-
Expression e2 = EXPcreate_from_symbol( Type_Attribute, local );
946-
947-
Variable v = VARcreate( e2, Type_Attribute );
948-
949-
DICTdefine( s->symbol_table, local->name, v, &e2->symbol, OBJ_VARIABLE );
950-
e->u.query = QUERY_new();
951-
e->u.query->scope = s;
952-
e->u.query->local = v;
953-
e->u.query->aggregate = aggregate;
954-
return e;
955-
}
956-
957854
/**
958855
** \param expression expression to evaluate
959856
** \param experrc buffer for error code

src/express/express.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "express/basic.h"
7979
#include "express/express.h"
8080
#include "express/resolve.h"
81+
#include "express/factory.h"
8182
#include "stack.h"
8283
#include "express/scope.h"
8384
#include "token_type.h"
@@ -275,6 +276,7 @@ void EXPRESSinitialize( void ) {
275276
SYMBOLinitialize();
276277

277278
SCOPEinitialize();
279+
FACTORYinitialize();
278280
TYPEinitialize(); /* cannot come before SCOPEinitialize */
279281
VARinitialize();
280282

src/express/factory.c

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,139 @@
11
#include "express/schema.h"
22
#include "express/type.h"
3+
#include "express/expr.h"
34

45
#include "express/dict.h"
56

7+
/* TODO: use enum? */
8+
Type Type_Bad;
9+
Type Type_Unknown;
10+
Type Type_Dont_Care;
11+
Type Type_Runtime;
12+
Type Type_Binary;
13+
Type Type_Boolean;
14+
Type Type_Enumeration;
15+
Type Type_Expression;
16+
Type Type_Aggregate;
17+
Type Type_Repeat;
18+
Type Type_Integer;
19+
Type Type_Number;
20+
Type Type_Real;
21+
Type Type_String;
22+
Type Type_String_Encoded;
23+
Type Type_Logical;
24+
Type Type_Set;
25+
Type Type_Attribute;
26+
Type Type_Entity;
27+
Type Type_Funcall;
28+
Type Type_Generic;
29+
Type Type_Identifier;
30+
Type Type_Oneof;
31+
Type Type_Query;
32+
Type Type_Self;
33+
Type Type_Set_Of_String;
34+
Type Type_Set_Of_Generic;
35+
Type Type_Bag_Of_Generic;
36+
37+
void FACTORYinitialize() {
38+
/* Very commonly-used read-only types */
39+
Type_Unknown = TYPEcreate( unknown_ );
40+
Type_Dont_Care = TYPEcreate( special_ );
41+
Type_Bad = TYPEcreate( special_ );
42+
Type_Runtime = TYPEcreate( runtime_ );
43+
44+
Type_Enumeration = TYPEcreate( enumeration_ );
45+
Type_Enumeration->u.type->body->flags.shared = 1;
46+
resolved_all( Type_Enumeration );
47+
48+
Type_Expression = TYPEcreate( op_ );
49+
Type_Expression->u.type->body->flags.shared = 1;
50+
51+
Type_Aggregate = TYPEcreate( aggregate_ );
52+
Type_Aggregate->u.type->body->flags.shared = 1;
53+
Type_Aggregate->u.type->body->base = Type_Runtime;
54+
55+
Type_Integer = TYPEcreate( integer_ );
56+
Type_Integer->u.type->body->flags.shared = 1;
57+
resolved_all( Type_Integer );
58+
59+
Type_Real = TYPEcreate( real_ );
60+
Type_Real->u.type->body->flags.shared = 1;
61+
resolved_all( Type_Real );
62+
63+
Type_Number = TYPEcreate( number_ );
64+
Type_Number->u.type->body->flags.shared = 1;
65+
resolved_all( Type_Number );
66+
67+
Type_String = TYPEcreate( string_ );
68+
Type_String->u.type->body->flags.shared = 1;
69+
resolved_all( Type_String );
70+
71+
Type_String_Encoded = TYPEcreate( string_ );
72+
Type_String_Encoded->u.type->body->flags.shared = 1;
73+
Type_String_Encoded->u.type->body->flags.encoded = 1;
74+
resolved_all( Type_String );
75+
76+
Type_Logical = TYPEcreate( logical_ );
77+
Type_Logical->u.type->body->flags.shared = 1;
78+
resolved_all( Type_Logical );
79+
80+
Type_Binary = TYPEcreate( binary_ );
81+
Type_Binary->u.type->body->flags.shared = 1;
82+
resolved_all( Type_Binary );
83+
84+
Type_Number = TYPEcreate( number_ );
85+
Type_Number->u.type->body->flags.shared = 1;
86+
resolved_all( Type_Number );
87+
88+
Type_Boolean = TYPEcreate( boolean_ );
89+
Type_Boolean->u.type->body->flags.shared = 1;
90+
resolved_all( Type_Boolean );
91+
92+
Type_Generic = TYPEcreate( generic_ );
93+
Type_Generic->u.type->body->flags.shared = 1;
94+
resolved_all( Type_Generic );
95+
96+
Type_Set_Of_String = TYPEcreate( set_ );
97+
Type_Set_Of_String->u.type->body->flags.shared = 1;
98+
Type_Set_Of_String->u.type->body->base = Type_String;
99+
100+
Type_Set_Of_Generic = TYPEcreate( set_ );
101+
Type_Set_Of_Generic->u.type->body->flags.shared = 1;
102+
Type_Set_Of_Generic->u.type->body->base = Type_Generic;
103+
104+
Type_Bag_Of_Generic = TYPEcreate( bag_ );
105+
Type_Bag_Of_Generic->u.type->body->flags.shared = 1;
106+
Type_Bag_Of_Generic->u.type->body->base = Type_Generic;
107+
108+
Type_Attribute = TYPEcreate( attribute_ );
109+
Type_Attribute->u.type->body->flags.shared = 1;
110+
111+
Type_Entity = TYPEcreate( entity_ );
112+
Type_Entity->u.type->body->flags.shared = 1;
113+
114+
Type_Funcall = TYPEcreate( funcall_ );
115+
Type_Funcall->u.type->body->flags.shared = 1;
116+
117+
Type_Generic = TYPEcreate( generic_ );
118+
Type_Generic->u.type->body->flags.shared = 1;
119+
120+
Type_Identifier = TYPEcreate( identifier_ );
121+
Type_Identifier->u.type->body->flags.shared = 1;
122+
123+
Type_Repeat = TYPEcreate( integer_ );
124+
Type_Repeat->u.type->body->flags.shared = 1;
125+
Type_Repeat->u.type->body->flags.repeat = 1;
126+
127+
Type_Oneof = TYPEcreate( oneof_ );
128+
Type_Oneof->u.type->body->flags.shared = 1;
129+
130+
Type_Query = TYPEcreate( query_ );
131+
Type_Query->u.type->body->flags.shared = 1;
132+
133+
Type_Self = TYPEcreate( self_ );
134+
Type_Self->u.type->body->flags.shared = 1;
135+
}
136+
6137
/** Create and return an empty scope inside a parent scope. */
7138
Scope SCOPEcreate( char type ) {
8139
Scope d = SCOPE_new();
@@ -141,3 +272,104 @@ Variable VARcreate( Expression name, Type type ) {
141272
v->type = type;
142273
return v;
143274
}
275+
276+
Expression EXPcreate( Type type ) {
277+
Expression e;
278+
e = EXP_new();
279+
SYMBOLset( e );
280+
e->type = type;
281+
e->return_type = Type_Unknown;
282+
return( e );
283+
}
284+
285+
/**
286+
* use this when the return_type is the same as the type
287+
* For example, for constant integers
288+
*/
289+
Expression EXPcreate_simple( Type type ) {
290+
Expression e;
291+
e = EXP_new();
292+
SYMBOLset( e );
293+
e->type = e->return_type = type;
294+
return( e );
295+
}
296+
297+
Expression EXPcreate_from_symbol( Type type, Symbol * symbol ) {
298+
Expression e;
299+
e = EXP_new();
300+
e->type = type;
301+
e->return_type = Type_Unknown;
302+
e->symbol = *symbol;
303+
return e;
304+
}
305+
306+
/**
307+
** \param op operation
308+
** \param operand1 - first operand
309+
** \param operand2 - second operand
310+
** \param operand3 - third operand
311+
** \returns Ternary_Expression - the expression created
312+
** Create a ternary operation Expression.
313+
*/
314+
Expression TERN_EXPcreate( Op_Code op, Expression operand1, Expression operand2, Expression operand3 ) {
315+
Expression e = EXPcreate( Type_Expression );
316+
317+
e->e.op_code = op;
318+
e->e.op1 = operand1;
319+
e->e.op2 = operand2;
320+
e->e.op3 = operand3;
321+
return e;
322+
}
323+
324+
/**
325+
** \fn BIN_EXPcreate
326+
** \param op operation
327+
** \param operand1 - first operand
328+
** \param operand2 - second operand
329+
** \returns Binary_Expression - the expression created
330+
** Create a binary operation Expression.
331+
*/
332+
Expression BIN_EXPcreate( Op_Code op, Expression operand1, Expression operand2 ) {
333+
Expression e = EXPcreate( Type_Expression );
334+
335+
e->e.op_code = op;
336+
e->e.op1 = operand1;
337+
e->e.op2 = operand2;
338+
return e;
339+
}
340+
341+
/**
342+
** \param op operation
343+
** \param operand operand
344+
** \returns the expression created
345+
** Create a unary operation Expression.
346+
*/
347+
Expression UN_EXPcreate( Op_Code op, Expression operand ) {
348+
Expression e = EXPcreate( Type_Expression );
349+
350+
e->e.op_code = op;
351+
e->e.op1 = operand;
352+
return e;
353+
}
354+
355+
/**
356+
** \param local local identifier for source elements
357+
** \param aggregate source aggregate to query
358+
** \returns the query expression created
359+
** Create a query Expression.
360+
** NOTE Dec 2011 - MP - function description did not match actual params. Had to guess.
361+
*/
362+
Expression QUERYcreate( Symbol * local, Expression aggregate ) {
363+
Expression e = EXPcreate_from_symbol( Type_Query, local );
364+
Scope s = SCOPEcreate_tiny( OBJ_QUERY );
365+
Expression e2 = EXPcreate_from_symbol( Type_Attribute, local );
366+
367+
Variable v = VARcreate( e2, Type_Attribute );
368+
369+
DICTdefine( s->symbol_table, local->name, ( Generic )v, &e2->symbol, OBJ_VARIABLE );
370+
e->u.query = QUERY_new();
371+
e->u.query->scope = s;
372+
e->u.query->local = v;
373+
e->u.query->aggregate = aggregate;
374+
return e;
375+
}

0 commit comments

Comments
 (0)