|
1 | 1 | #include "express/schema.h" |
2 | 2 | #include "express/type.h" |
| 3 | +#include "express/expr.h" |
3 | 4 |
|
4 | 5 | #include "express/dict.h" |
5 | 6 |
|
| 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 | + |
6 | 137 | /** Create and return an empty scope inside a parent scope. */ |
7 | 138 | Scope SCOPEcreate( char type ) { |
8 | 139 | Scope d = SCOPE_new(); |
@@ -141,3 +272,104 @@ Variable VARcreate( Expression name, Type type ) { |
141 | 272 | v->type = type; |
142 | 273 | return v; |
143 | 274 | } |
| 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