Initial Revision
[ohcount] / ext / ohcount_native / polyglot.c
1 /*
2  *  polyglot.c
3  *  Ohcount
4  *
5  *  Created by Jason Allen on 6/23/06.
6  *  Copyright 2006 Ohloh. All rights reserved.
7  *
8  */
9 #include "common.h"
10
11
12 /*
13  * polyglot_state_count
14  *
15  * Returns the number of states a polyglot contains
16  *
17  */
18 int polyglot_state_count(Polyglot *polyglot) {
19         int count = 0;
20         for ( ; polyglot->states[count] != NULL; count++) {}
21         return count;
22 }
23
24 /*
25  * polyglot_compile_states
26  *
27  * creates an associated CompiledState for every State
28  * contained in a polyglot. CompiledStates are used by
29  * the parser to avoid doing expensive lookups and avoid
30  * redundant processing.
31  *
32  */
33 void polyglot_compile_states(Polyglot *polyglot) {
34         if (polyglot->compiled_states == NULL) {
35                 // compile them!
36                 int state_count = polyglot_state_count(polyglot);
37                 polyglot->compiled_states = malloc(sizeof(CompiledState) * state_count);
38                 polyglot->compiled_state_count = state_count;
39                 int i = 0;
40                 for (; i < state_count; i++) {
41                         compiled_state_initialize(&polyglot->compiled_states[i], polyglot->states[i], polyglot->transitions);
42                 }
43         }
44 }