855977b84ad8a64d3be34ad6e388169b6954e659
[mw/milkymist.git] / software / demo / parser_helper.c
1 /*
2  * Milkymist VJ SoC (Software)
3  * Copyright (C) 2007, 2008, 2009 Sebastien Bourdeauducq
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdio.h>
19 #include <malloc.h>
20
21 #include "scanner.h"
22 #include "ast.h"
23 #include "parser_helper.h"
24
25 struct preset *generate_ast(char *preset_code)
26 {
27         struct scanner *s;
28         int tok;
29         char *identifier;
30         void *p;
31         struct preset *ast;
32         
33         s = new_scanner(preset_code);
34         ast = NULL;
35         p = ParseAlloc(malloc);
36         tok = scan(s);
37         while(tok != TOK_EOF) {
38                 identifier = malloc(IDENTIFIER_SIZE);
39                 get_token(s, identifier, IDENTIFIER_SIZE);
40                 Parse(p, tok, identifier, &ast);
41                 if(tok == TOK_ERROR) {
42                         printf("Scan error\n");
43                         break;
44                 }
45                 tok = scan(s);
46         }
47         Parse(p, TOK_EOF, NULL, &ast);
48         ParseFree(p, free);
49         delete_scanner(s);
50
51         if(ast == NULL) {
52                 printf("Parse error\n");
53                 return NULL;
54         }
55
56         return ast;
57 }
58
59 static void free_ast_nodes(struct ast_node *node)
60 {
61         if(node == NULL) return;
62         if(node->label[0] != 0) {
63                 free_ast_nodes(node->contents.branches.a);
64                 free_ast_nodes(node->contents.branches.b);
65                 free_ast_nodes(node->contents.branches.c);
66         }
67         free(node);
68 }
69
70 static void free_equations(struct preset_equation *equations)
71 {
72         struct preset_equation *next;
73
74         while(equations != NULL) {
75                 free_ast_nodes(equations->topnode);
76                 next = equations->next;
77                 free(equations);
78                 equations = next;
79         }
80 }
81
82 static void free_lines(struct preset_line *line)
83 {
84         struct preset_line *next;
85
86         while(line != NULL) {
87                 if(line->iseq)
88                         free_equations(line->contents.equations);
89                 next = line->next;
90                 free(line);
91                 line = next;
92         }
93 }
94
95 void free_ast(struct preset *p)
96 {
97         free_lines(p->lines);
98         free(p);
99 }
100