1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include <colm/program.h>
#include <colm/struct.h>
#include "internal.h"
#include "bytecode.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
struct colm_tree *colm_get_global( Program *prg, long pos )
{
return colm_struct_get_field( prg->global, Tree*, pos );
}
void colm_struct_add( Program *prg, struct colm_struct *item )
{
if ( prg->heap.head == 0 ) {
prg->heap.head = prg->heap.tail = item;
item->prev = item->next = 0;
}
else {
item->prev = prg->heap.tail;
item->next = 0;
prg->heap.tail->next = item;
prg->heap.tail = item;
}
}
struct colm_struct *colm_struct_new_size( Program *prg, int size )
{
size_t memsize = sizeof(struct colm_struct) + ( sizeof(Tree*) * size );
struct colm_struct *item = (struct colm_struct*) malloc( memsize );
memset( item, 0, memsize );
colm_struct_add( prg, item );
return item;
}
struct colm_struct *colm_struct_new( Program *prg, int id )
{
struct colm_struct *s = colm_struct_new_size( prg, prg->rtd->selInfo[id].size );
s->id = id;
return s;
}
void colm_struct_delete( Program *prg, Tree **sp, struct colm_struct *el )
{
if ( el->id == STRUCT_INBUILT_ID ) {
colm_destructor_t destructor = ((struct colm_inbuilt*)el)->destructor;
if ( destructor != 0 )
(*destructor)( prg, sp, el );
}
if ( el->id >= 0 ) {
short *t = prg->rtd->selInfo[el->id].trees;
int i, len = prg->rtd->selInfo[el->id].treesLen;
for ( i = 0; i < len; i++ ) {
Tree *tree = colm_struct_get_field( el, Tree*, t[i] );
treeDownref( prg, sp, tree );
}
}
free( el );
}
void colm_parser_destroy( Program *prg, Tree **sp, struct colm_struct *s )
{
struct colm_parser *parser = (struct colm_parser*) s;
/* Free the PDA run. */
colm_pda_clear( prg, sp, parser->pdaRun );
free( parser->pdaRun );
/* Free the result. */
treeDownref( prg, sp, parser->result );
}
Parser *colm_parser_new( Program *prg, GenericInfo *gi )
{
PdaRun *pdaRun = malloc( sizeof(PdaRun) );
/* Start off the parsing process. */
colm_pda_init( prg, pdaRun, prg->rtd->pdaTables,
gi->parserId, 0, 0, 0 );
size_t memsize = sizeof(struct colm_parser);
struct colm_parser *parser = (struct colm_parser*) malloc( memsize );
memset( parser, 0, memsize );
colm_struct_add( prg, (struct colm_struct*) parser );
parser->id = STRUCT_INBUILT_ID;
parser->destructor = &colm_parser_destroy;
parser->pdaRun = pdaRun;
return parser;
}
void colm_map_destroy( Program *prg, Tree **sp, struct colm_struct *s )
{
struct colm_map *map = (struct colm_map*) s;
MapEl *el = map->head;
while ( el != 0 ) {
MapEl *next = el->next;
treeDownref( prg, sp, el->key );
mapElFree( prg, el );
el = next;
}
}
Map *colm_map_new( struct colm_program *prg )
{
size_t memsize = sizeof(struct colm_map);
struct colm_map *map = (struct colm_map*) malloc( memsize );
memset( map, 0, memsize );
colm_struct_add( prg, (struct colm_struct *)map );
map->id = STRUCT_INBUILT_ID;
return map;
}
Struct *colm_construct_generic( Program *prg, long genericId )
{
GenericInfo *genericInfo = &prg->rtd->genericInfo[genericId];
Struct *newGeneric = 0;
switch ( genericInfo->type ) {
case GEN_MAP_EL:
case GEN_LIST_EL:
break;
case GEN_MAP:
case GEN_VMAP:
{
Map *map = colm_map_new( prg );
map->genericInfo = genericInfo;
newGeneric = (Struct*) map;
break;
}
case GEN_LIST:
case GEN_VLIST:
{
List *list = colm_list_new( prg );
list->genericInfo = genericInfo;
newGeneric = (Struct*) list;
break;
}
case GEN_PARSER: {
Parser *parser = colm_parser_new( prg, genericInfo );
parser->input = colm_stream_new( prg );
newGeneric = (Struct*) parser;
break;
}
}
return newGeneric;
}
|