-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh_exec.c
223 lines (200 loc) · 5.42 KB
/
sh_exec.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "util.h"
#include "kernel.h"
#include "malloc.h"
#include "sh_exec.h"
int iregisters[10];
char cregisters[10];
int cmp1, cmp2;
void exec_sleep(char*);
void exec_seti(char*);
void exec_setc(char*);
void exec_printi(char*);
void exec_printc(char*);
void exec_addi(char*);
void exec_multi(char*);
void exec_null(char*);
void exec_randi(char*);
void exec_inc(char*);
void exec_dec(char*);
void exec_setcmp1(char*);
void exec_setcmp2(char*);
void exec_status(char*);
struct StringListNode * exec_findLine(int);
char userQuit = 0;
char *execCommandList[] = { "sleep", "seti", "setc", "printi", "printc", "addi", "multi", "randi", "inc", "dec", "setcmp1", "setcmp2", "status" , ""};
void (*execFunctionList[])(char*) = {exec_sleep, exec_seti, exec_setc, exec_printi, exec_printc, exec_addi, exec_multi, exec_randi, exec_inc, exec_dec, exec_setcmp1, exec_setcmp2, exec_status, exec_null};
void sh_exec(char* unused_params){
terminalMode = INTERPRETER;
char line[width+1];
line[width] = 0;
int index = 0;
for(index = 0; index < 10; index++){
iregisters[index] = 0;
cregisters[index] = 0;
}
//iterate through the buffer
struct StringListNode *execLine = fileBuffer->firstLine;
while(execLine){
if(userQuit){
ttprintln("ESC interrupt");
break;
}
if((execLine->str)[0] == 0 || (execLine->str)[0] == '#'){
execLine = execLine->next;
continue;
}
int len = strLen(execLine->str);
int j = 0;
while(j < len && (execLine->str)[j] != ' ' && (execLine->str)[j] != ';') j++;
char *function = (char*) malloc(j+1);
char *params = (char*) malloc(len-j);
memCopy(execLine->str, function, j);
function[j] = 0;
int k = j;
while(k < len && (execLine->str)[k] != ';') k++;
memCopy(execLine->str + j + 1, params, k-j-1);
if(strEquals(function, "jeq")){
if(cmp1 == cmp2) execLine = exec_findLine(strToInt(params) - 1);
else execLine = execLine->next;
continue;
}
if(strEquals(function, "jne")){
if(cmp1 != cmp2) execLine = exec_findLine(strToInt(params) - 1);
else execLine = execLine->next;
continue;
}
if(strEquals(function, "jge")){
if(cmp1 >= cmp2) execLine = exec_findLine(strToInt(params) - 1);
else execLine = execLine->next;
continue;
}
if(strEquals(function, "jle")){
if(cmp1 <= cmp2) execLine = exec_findLine(strToInt(params) - 1);
else execLine = execLine->next;
continue;
}
if(strEquals(function, "jlt")){
if(cmp1 < cmp2) execLine = exec_findLine(strToInt(params) - 1);
else execLine = execLine->next;
continue;
}
if(strEquals(function, "jgt")){
if(cmp1 > cmp2) execLine = exec_findLine(strToInt(params) - 1);
else execLine = execLine->next;
continue;
}
j = 0;
while(!strEquals(execCommandList[j],function) && !strEquals(execCommandList[j],"")) j++;
if(!strEquals(execCommandList[j],"")) (*execFunctionList[j])(params);
else {
ttprint("err: no such function: `");
ttprint(function);
ttprint("` of length:");
ttprintIntln(strLen(function));
break;
}
execLine = execLine->next;
}
terminalMode = TERMINAL;
return;
}
void interpreter_keyPressed(unsigned char code, char c){
//this doesn't work, key presses aren't registered while a function is running
if(code == 0x01){ // escape
userQuit = 1;
}
}
struct StringListNode* exec_findLine(int lineNum){
struct StringListNode *iter = fileBuffer->firstLine;
while(lineNum-- > 1 && iter) iter = iter->next;
return iter;
}
// `sleep milliseconds`
void exec_sleep(char* params){
sleep(strToInt(params));
}
// `seti registerIndex value`
void exec_seti(char* params){
int index = params[0] - '0';
iregisters[index] = strToInt( (params + 2) );
}
// `setc registerIndex value`
void exec_setc(char* params){
int index = params[0] - '0';
cregisters[index] = params[2];
}
// `printi registerIndex`
void exec_printi(char* params){
//printf("%d\n", iregisters[params[0] - '0']);
ttprintIntln(iregisters[params[0] - '0']);
}
// `printc registerIndex`
void exec_printc(char* params){
//printf("%c\n", cregisters[params[0] - '0']);
ttprintChar(cregisters[params[0] - '0']);
ttprintChar('\n');
}
// `addi operand operand destination
void exec_addi(char* params){
char a = params[0] - '0';
char b = params[2] - '0';
char c = params[4] - '0';
iregisters[c] = iregisters[a] + iregisters[b];
}
// `multi operand operand destination
void exec_multi(char* params){
char a = params[0] - '0';
char b = params[2] - '0';
char c = params[4] - '0';
iregisters[c] = iregisters[a] * iregisters[b];
}
// `randi limit destination
void exec_randi(char* params){
int i;
int limit = 10;
char index = 0;
for(i = 0; i < strLen(params); i++){
if(params[i] == ' '){
params[i] = 0;
limit = strToInt(params);
params[i] = ' ';
index = params[i+1] - '0';
break;
}
}
iregisters[index] = (int)rand(limit);
}
void exec_inc(char* params){
iregisters[params[0] - '0'] += 1;
}
void exec_dec(char* params){
iregisters[params[0] - '0'] -= 1;
}
void exec_setcmp1(char* params){
cmp1 = iregisters[params[0] - '0'];
}
void exec_setcmp2(char* params){
cmp2 = iregisters[params[0] - '0'];
}
void exec_status(char* params){
int i;
ttprint("ireg:");
for(i = 0; i < 10; i++){
ttprintInt(iregisters[i]);
ttprintChar(',');
}
ttprintln("");
ttprint("creg:");
for(i = 0; i < 10; i++){
ttprintChar(cregisters[i]);
ttprintChar(',');
}
ttprintln("");
ttprint("cmp:");
ttprintInt(cmp1);
ttprintChar(',');
ttprintInt(cmp2);
ttprintln("");
}
void exec_null(char * params){
}