1 /*
2     tcbasic - a small BASIC Interpreter written in C.
3     Copyright (C) 2015, 2016, 2017  Thomas Cort <linuxgeek@gmail.com>
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, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "expr_list.h"
25 #include "expr_item.h"
26 
27 #include "tokenizer.h"
28 
new_expr_list(struct expr_item * expr_item,struct expr_list * list)29 struct expr_list *new_expr_list(struct expr_item *expr_item, struct expr_list *list) {
30 
31 	struct expr_list *el;
32 
33 	el = (struct expr_list *) malloc(sizeof(struct expr_list));
34 	if (el == NULL) {
35 		perror("malloc");
36 		exit(EXIT_FAILURE);
37 	}
38 	memset(el, '\0', sizeof(struct expr_list));
39 
40 	el->expr_item = expr_item;
41 	el->list = list;
42 
43 	return el;
44 }
45 
parse_expr_list(struct tokenizer * t)46 struct expr_list *parse_expr_list(struct tokenizer *t) {
47 
48 	struct expr_item *i;
49 
50 	i = parse_expr_item(t);
51 	if (i == NULL) {
52 		return NULL;
53 	}
54 
55 	token_get(t);
56 	if (t->token.type == COMMA) {
57 		return new_expr_list(i, parse_expr_list(t));
58 	} else {
59 		token_unget(t);
60 		return new_expr_list(i, NULL);
61 	}
62 }
63 
eval_expr_list(struct expr_list * el)64 void eval_expr_list(struct expr_list *el) {
65 
66 	if (el == NULL) {
67 		return;
68 	}
69 
70 	eval_expr_item(el->expr_item);
71 	eval_expr_list(el->list);
72 }
73 
print_expr_list(struct expr_list * el)74 void print_expr_list(struct expr_list *el) {
75 
76 	if (el == NULL) {
77 		return;
78 	}
79 
80 	print_expr_item(el->expr_item);
81 	if (el->list != NULL) {
82 		printf(", ");
83 		print_expr_list(el->list);
84 	}
85 }
86 
free_expr_list(struct expr_list * el)87 void free_expr_list(struct expr_list *el) {
88 	if (el != NULL) {
89 		free_expr_item(el->expr_item);
90 		free_expr_list(el->list);
91 		free(el);
92 	}
93 }
94