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 "tokenizer.h"
25 
26 #include "mulop.h"
27 
new_mulop(int type)28 struct mulop *new_mulop(int type) {
29 
30 	struct mulop *op;
31 
32 	op = (struct mulop *) malloc(sizeof(struct mulop));
33 	if (op == NULL) {
34 		perror("malloc");
35 		exit(EXIT_FAILURE);
36 	}
37 	memset(op, '\0', sizeof(struct mulop));
38 
39 	op->type = type;
40 
41 	return op;
42 }
43 
parse_mulop(struct tokenizer * t)44 struct mulop *parse_mulop(struct tokenizer *t) {
45 
46 	token_get(t);
47 	switch (t->token.type) {
48 		case TIMES:
49 			return new_mulop(TIMES);
50 		case DIVIDE:
51 			return new_mulop(DIVIDE);
52 		case MOD:
53 			return new_mulop(MOD);
54 		default:
55 			token_unget(t);
56 			return NULL;
57 	}
58 }
59 
print_mulop(struct mulop * op)60 void print_mulop(struct mulop *op) {
61 
62 	if (op == NULL) {
63 		return;
64 	}
65 
66 	switch (op->type) {
67 		case TIMES:
68 			printf("*");
69 			break;
70 		case DIVIDE:
71 			printf("/");
72 			break;
73 		case MOD:
74 			printf("MOD");
75 			break;
76 		default:
77 			printf("?");
78 			break;
79 	}
80 }
81 
free_mulop(struct mulop * op)82 void free_mulop(struct mulop *op) {
83 	if (op != NULL) {
84 		free(op);
85 	}
86 }
87