summaryrefslogtreecommitdiff
path: root/src/struct.c
blob: 643dd6c1ccc3a4ad98ece9643f18f03debfd9616 (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
#include <colm/program.h>
#include <colm/struct.h>

#include "internal.h"

#include <stdlib.h>
#include <string.h>
#include <assert.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. */
	clearPdaRun( 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;
}

Stream *colm_stream_new_struct( Program *prg )
{
	size_t memsize = sizeof(struct colm_stream);
	struct colm_stream *stream = (struct colm_stream*) malloc( memsize );
	memset( stream, 0, memsize );
	colm_struct_add( prg, (struct colm_struct *)stream );
	stream->id = STRUCT_INBUILT_ID;
	return stream;
}

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;
}