-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
164 lines (140 loc) · 3.74 KB
/
main.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "rip.h"
//#define show_instructions
/*
Prime:
21W[D1W[1sSDD4RDD4r5rqms]P1EI[DO9io]i1]
Still to implement fully:
does prime.rip work?
n, g, F<bla>[code], <bla>
Ideas:
decrease mallocs?
*/
char *prog;
void **jump_table;
void error(char *e) {
printf("\n%s\n",e);
exit(1);
}
// Clutch
char *build_jt_h(char *p, char *b) {
char c;
char *t;
while(c=*(t=p++)) {
if(c=='I' || c=='W') {
while(isspace(*p)) p++;
if(*p!='[') error("I or W not followed by code block. ");
p=build_jt_h(p+1,t);
if(!p) error("Brackets don't match. Wrong '['. ");
*(t-prog+jump_table) = p-1;
} else if(c==']') {
*(t-prog+jump_table) = b;
return p;
} else if(c=='[') {
error("Code block without I or W. ");
} else if(isdigit(c)) {
//printf(":%c\n",c);
mpz_t *constant = malloc(sizeof(*constant));
mpz_init_set_ui(*constant, c-'0');
*(t-prog+jump_table) = constant;
}
}
return NULL;
}
// jump on '[', ']'
void build_jump_table() {
jump_table = malloc(strlen(prog)*sizeof(*jump_table));
if(build_jt_h(prog,0)) error("Brackets don't match. Wrong ']'. ");
}
// DOES NOT FREE MPZ_T NUMBERS!!!
void delete_jt() {
free(jump_table);
}
void interpret() {
char *p = prog;
while(*p) {
#ifdef show_instructions
printf(":%c:\t",*p);
dump();
printf("\n");
#endif
switch(*p) {
case 'P': op_pop(); break;
case 'S': op_swap(); break;
case 'D': op_dupl(); break;
case 'i': op_inc(); break;
case 'd': op_dec(); break;
case 'r': op_r_lower(); break;
case 'R': op_r_upper(); break;
case 'a': op_add(); break;
case 's': op_sub(); break;
case 'm': op_mul(); break;
case 'q': op_div(); break;
case 'G': op_greater(); break;
case 'L': op_less(); break;
case 'E': op_eq(); break;
case 'I':
case 'W':
if(!mpz_sgn(glob_t->data)) {
p = jump_table[p-prog];
}
op_pop();
break;
case ']':
if(*(char*)jump_table[p-prog]=='W') {
p = jump_table[p-prog];
continue;
}
break;
case 'o': op_charout(); break;
case 'O': op_numout(); break;
case 'g': op_charin(); break;
case '$': dump(); break;
default:
if(isspace(*p)) {
while(isspace(*p)) {
p++;
}
continue;
}
if(isdigit(*p)) {
list_node *temp = malloc(sizeof(*temp));
mpz_init_set(temp->data,*(mpz_t*)jump_table[p-prog]);
temp->down = glob_t;
glob_t = temp;
break;
}
//error("Command not recognized. ");
}
p++;
}
}
void load_prog(char *filename) {
FILE *iFile = fopen(filename, "rb");
if(!iFile) error("Error opening file. ");
long iFileSize;
fseek(iFile, 0, SEEK_END);
iFileSize = ftell(iFile);
rewind(iFile);
prog = malloc(iFileSize+1);
fread(prog, 1, iFileSize, iFile);
fclose(iFile);
prog[iFileSize] = '\0';
}
int main(int argc, char **argv) {
if(argc != 2) {
printf("No filename given.\n");
return 0;
}
load_prog(argv[1]);
//prog = "21W[D1W[1sSDD4RDD4r5rqms]1EI[DO9io]i1]";
build_jump_table();
list_init();
interpret();
delete_list();
delete_jt();
exit(0);
}