summaryrefslogtreecommitdiff
path: root/src/struct.c
blob: 5ee58ed3085a40e076038decb6e6590fe79f960a (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright 2016-2018 Adrian Thurston <thurston@colm.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include <colm/program.h>
#include <colm/struct.h>

#include "internal.h"
#include "bytecode.h"

struct colm_tree *colm_get_global( program_t *prg, long pos )
{
	return colm_struct_get_field( prg->global, tree_t*, pos );
}

void colm_struct_add( program_t *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_t *prg, int size )
{
	size_t memsize = sizeof(struct colm_struct) + ( sizeof(tree_t*) * 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_t *prg, int id )
{
	struct colm_struct *s = colm_struct_new_size( prg, prg->rtd->sel_info[id - prg->rtd->num_lang_els].size );
	s->id = id;
	return s;
}

struct struct_el_info *colm_sel_info( program_t *prg, int id )
{
	return &prg->rtd->sel_info[id - prg->rtd->num_lang_els];
}

void colm_struct_delete( program_t *prg, tree_t **sp, struct colm_struct *el )
{
	if ( el->id == prg->rtd->struct_inbuilt_id || el->id == prg->rtd->struct_stream_id ) {
		colm_destructor_t destructor = ((struct colm_inbuilt*)el)->destructor;
		if ( destructor != 0 )
			(*destructor)( prg, sp, el );
	}
	else {
		int tree_i;
		struct struct_el_info *sel = colm_sel_info( prg, el->id );
		for ( tree_i = 0; tree_i < sel->trees_len; tree_i++ ) {
			tree_t *tree = colm_struct_get_field( el, tree_t*, sel->trees[tree_i] );
			colm_tree_downref( prg, sp, tree );
		}
	}
	free( el );
}

void colm_parser_destroy( program_t *prg, tree_t **sp, struct colm_struct *s )
{
	struct colm_parser *parser = (struct colm_parser*) s;

	/* Free the PDA run. */
	colm_pda_clear( prg, sp, parser->pda_run );
	free( parser->pda_run );

	/* Free the result. */
	colm_tree_downref( prg, sp, parser->result );
}

parser_t *colm_parser_new( program_t *prg, struct generic_info *gi, int stop_id, int reducer )
{
	struct pda_run *pda_run = malloc( sizeof(struct pda_run) );

	/* Start off the parsing process. */
	colm_pda_init( prg, pda_run, prg->rtd->pda_tables, 
			gi->parser_id, stop_id, 0, 0, reducer );
	
	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 = prg->rtd->struct_inbuilt_id;
	parser->destructor = &colm_parser_destroy;
	parser->pda_run = pda_run;

	return parser;
}

void colm_map_destroy( program_t *prg, tree_t **sp, struct colm_struct *s )
{
	struct colm_map *map = (struct colm_map*) s;

	map_el_t *el = map->head;
	while ( el != 0 ) {
		map_el_t *next = el->next;
		colm_tree_downref( prg, sp, el->key );
		//mapElFree( prg, el );
		el = next;
	}
}

map_t *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 = prg->rtd->struct_inbuilt_id;
	return map;
}

struct_t *colm_construct_generic( program_t *prg, long generic_id, int stop_id )
{
	struct generic_info *generic_info = &prg->rtd->generic_info[generic_id];
	struct_t *new_generic = 0;
	switch ( generic_info->type ) {
		case GEN_MAP: {
			map_t *map = colm_map_new( prg );
			map->generic_info = generic_info;
			new_generic = (struct_t*) map;
			break;
		}
		case GEN_LIST: {
			list_t *list = colm_list_new( prg );
			list->generic_info = generic_info;
			new_generic = (struct_t*) list;
			break;
		}
		case GEN_PARSER: {
			parser_t *parser = colm_parser_new( prg, generic_info, stop_id, 0 );
			parser->input = colm_input_new( prg );
			new_generic = (struct_t*) parser;
			break;
		}
	}

	return new_generic;
}

struct_t *colm_construct_reducer( program_t *prg, long generic_id, int reducer_id )
{
	struct generic_info *generic_info = &prg->rtd->generic_info[generic_id];
	struct_t *new_generic = 0;

	parser_t *parser = colm_parser_new( prg, generic_info, 0, reducer_id );
	parser->input = colm_input_new( prg );
	new_generic = (struct_t*) parser;

	return new_generic;
}