-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaasm.c
396 lines (348 loc) · 12 KB
/
aasm.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#ifndef AASM_MAIN
#define AASM_MAIN 0
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include "aasm_global.h"
#include "aasm_stdlib.h"
#include "aasm_quatlib.h"
int libFuncPtrs_AASM_MAIN();
int (*libFuncPtrs[libAmount])() = {
libFuncPtrs_AASM_MAIN
,libFuncPtrs_AASM_STDLIB
,libFuncPtrs_AASM_QUATLIB
};
char* argument;
int64_t dummyinteger;
//############################################### <Main essentials>
int init_main(GLOBAL* mainptrs) {
mainptrs->userInputLen = initialuserInputLen;
mainptrs->userInput = (char*) malloc((mainptrs->userInputLen + 10)*sizeof(char));
mainptrs->userInput[0] = '\0';
mainptrs->readhead = 0;
mainptrs->lookingMode = 'i';
mainptrs->running = true;
mainptrs->scriptLoops = 0;
mainptrs->inputMode = 'i';
mainptrs->bigEndian = 0;
mainptrs->flist = malloc(sizeof(file_t*));
mainptrs->flist[0] = malloc(sizeof(file_t));
mainptrs->fileNr = 0;
mainptrs->flist[mainptrs->fileNr]->fp = stdin;
mainptrs->flist[mainptrs->fileNr]->lineNr = 0;
mainptrs->flist[mainptrs->fileNr]->begin_time = time(&mainptrs->flist[mainptrs->fileNr]->begin_time);
// argument = (char*) malloc(sizeof(char[initialuserInputLen]));
// argument[0] = '\0';
mainptrs->debug = 's';
if(mainptrs->debug == 'v') printf("main initted\n");
return 0;
}
int instructhandler_main(GLOBAL* mainptrs){
char instructstringmain[][maxKeywordLen] = {"\\", "help", ">", "#", "\0end"};
enum instructsmain { slash, help, early, shebang };
time_t end_time;
file_t* thefile = mainptrs->flist[mainptrs->fileNr];
mainptrs->instructNr = strlook(mainptrs->userInput, instructstringmain, &mainptrs->readhead);
switch (mainptrs->instructNr){
case shebang:
mainptrs->lookingMode = 'd';
break;
case slash:
mainptrs->lookingMode = 'd';
mainptrs->running = false;
printf("Good day!\n");
break;
case help:
printf("To preform an operation, type an instruction mnemonic (e.g. `set`, `print`, `add`, `push`)\nand add the appropriate amount of arguments seperated by commas.\n\n");
printf("The general purpose registers are `gr1`, `gr2`, `gr3` and `gr4`.\n\n");
printf("To change notation from little endian (the default) to big endian, enter `set endian, 1`\n");
printf("To change maximum line length, set the register `inplen` to the desired value.\n\n");
printf("Enter `\\` to close the program.\n\n");
printf("More information can be found on https://www.github.com/StevenClifford/ArbAsm/wiki\n\n");
printf("(P.S. Did you know that the actual plural of \"comma\" is \"commata\"?)\n");
mainptrs->lookingMode = 'd';
break;
case early:
if(mainptrs->fileNr > 0){
end_time = time(&end_time);
inttonum(®s[ptme], (int64_t) end_time - thefile->begin_time);
EndScript(mainptrs);
} else printf("\aThis instruction only works in a script.\n");
break;
}
if(mainptrs->debug == 'v') printf("main instructed\n");
return 0;
}
int argumenthandler_main(GLOBAL* mainptrs){
int j = 0;
char entry = mainptrs->userInput[mainptrs->readhead];
while(mainptrs->readhead < mainptrs->userInputLen && ((entry >= 'a' && entry <= 'z') || (entry >= 'A' && entry <= 'Z') || (entry >= '0' && entry <= '9')) ){
entry = mainptrs->userInput[mainptrs->readhead];
argument[j] = entry;
mainptrs->readhead++;j++;
}
argument[j] = '\0';
mainptrs->lookingMode = 'e';
if(mainptrs->debug == 'v') printf("main argumented\n");
return 0;
}
int executehandler_main(GLOBAL* mainptrs){
/* enum instructsmain { slash, help, early };
file_t* thefile = mainptrs->flist[mainptrs->fileNr];
int dum = 0;
switch(mainptrs->instructNr){
}*/
if(mainptrs->debug == 'v') printf("main executed\n");
return 0;
}
int update_main(GLOBAL* mainptrs){
free(mainptrs->userInput);
mainptrs->userInput = (char*) malloc((mainptrs->userInputLen + maxKeywordLen)*sizeof(char));
mainptrs->userInput[0] = '\0';
// free(argument);
// argument = (char*) malloc(sizeof(char[initialuserInputLen]));
// argument[0] = '\0';
if(mainptrs->debug == 'v') printf("main updated\n");
return 0;
}
int free_main(GLOBAL* mainptrs){
free(mainptrs->userInput);
free(mainptrs->flist[0]);
free(mainptrs->flist);
// free(argument);
if(mainptrs->debug == 'v') printf("main freed\n");
return 0;
}
int libFuncPtrs_AASM_MAIN(){
initfuncs[AASM_MAIN] = &init_main;
instructhandlers[AASM_MAIN] = &instructhandler_main;
argumenthandlers[AASM_MAIN] = &argumenthandler_main;
executehandlers[AASM_MAIN] = &executehandler_main;
updatefuncs[AASM_MAIN] = &update_main;
freefuncs[AASM_MAIN] = &free_main;
return 0;
}
//############################################### </Main essentials>
//############################################### <Library declarations>
int initLibFuncPtrs(){
for(int libNr = 0; libNr < libAmount; libNr++){
libFuncPtrs[libNr]();
}
return 0;
}
//############################################### </Library declarations>
//############################################### <Main operation>
int handlecommandlineargs(int argc, char* argv[], GLOBAL* mainptrs){
for(int i = 1; i < argc; i++){
if(argv[i][0] == '-'){
switch(argv[i][1]){
case 'A':
case 'a':
if(i+1 < argc){
i++;
num_t dummy; initnum(&dummy, 0, 0, 0);
strtostrnum(&dummy, argv[i], 0);
pushtomst(&dummy);
} else printf("\aArgument missing. Enter `aasm -h` for help.\n");
break;
case 'E':
case 'e':
mainptrs->running = false;
break;
case 'C':
case 'c':
mainptrs->running = !mainptrs->running;
break;
case 'D':
case 'd':
mainptrs->debug = 'v';
printf("Debugging mode enabled.\n");
break;
case 'V':
case 'v':
printversion();
mainptrs->inputMode = 'w';
mainptrs->running = false;
break;
case 'B':
case 'b':
mainptrs->bigEndian = true;
inttonum(®s[endian], 1);
break;
case 'P':
if(i+1 < argc){
i++;
strtostrnum(®s[path], argv[i], 0);
} else printf("\aPath missing. Enter `aasm -h` for help.\n");
break;
case 'p':
if(i+1 < argc){
i++;
inpstrtonum(®s[decip], argv[i], 0, mainptrs->bigEndian);
decimalpoint = numtoint(®s[decip], true);
} else printf("\aNumber missing. Enter `aasm -h` for help.\n");
break;
case 'L':
case 'l':
if(i+1 < argc){
i++;
saveload(argv[i], 'l', mainptrs);
} else printf("\aStatefile missing. Enter `aasm -h` for help.\n");
break;
case 'I':
case 'i':
if(i+1 < argc){
i++;
sscanf(argv[i], "%s", mainptrs->userInput);
mainptrs->inputMode = 'w';
} else printf("\aStatement missing. Enter `aasm -h` for help.\n");
break;
case 'H':
case 'h':
printf("Usage: aasm <script> <options>\n");
printf("Options:\n");
printf(" -c Enter command line after the script has stopped running.\n");
printf(" -i <statement> Executes the designated statement. This statement can not contain spaces.\n");
printf(" -e Exit immediately after executing the statement that was passed as an argument of -i.\n");
printf(" -a <argument> Pushes the designated argument (as string) to the main stack from the standard library.\n");
printf(" -l <statefile> Loads the designated statefile before interpreting any statements.\n");
printf(" -b Sets the notation to big endian before interpreting any statements.\n");
printf(" -p <number> Sets the virtual decimal point to the number that was passed as an argument of -p.\n");
printf(" -P <path> Sets the path register to the path that was passed as an argument of -P\n");
printf(" -v Displays the name, version and libraries.\n");
printf(" -h Look ma! I'm on TV!\n");
mainptrs->inputMode = 'w';
mainptrs->running = false;
break;
}
} else {
if(RunScript(mainptrs, argv[i]) == 1) mainptrs->inputMode = 'w';
mainptrs->running = !mainptrs->running;
}
}
return 0;
}
int dothing(GLOBAL* mainptrs){
mainptrs->libNr = 0;
mainptrs->instructNr = -1;
mainptrs->readhead = 0;
mainptrs->argumentNr = 0;
mainptrs->lookingMode = 'i';
char entry = mainptrs->userInput[mainptrs->readhead];
// main parsing loop
while (mainptrs->lookingMode != 'd' && entry != '\0' && entry != '\n' && entry != '\r' && entry != ';' && entry != ':')
{
entry = mainptrs->userInput[mainptrs->readhead];
if(mainptrs->debug == 'v') printf("On position %d, entry: %c\n", mainptrs->readhead, entry);
if(entry != ' ' && entry != '\t' && entry != '\0' && entry != '\n' && entry != '\r' && entry != ';' && entry != ':'){
// functions
if(mainptrs->lookingMode == 'i'){
for(mainptrs->libNr = 0; mainptrs->libNr < libAmount && mainptrs->instructNr == -1; mainptrs->libNr++){
instructhandlers[mainptrs->libNr](mainptrs);
}
if(mainptrs->libNr == libAmount && mainptrs->instructNr == -1){
printf("\aInvalid instruction");
if(mainptrs->inputMode == 'f'){
printf(" in file %d:\n", mainptrs->fileNr);
printf("%d | %s", mainptrs->flist[mainptrs->fileNr]->lineNr, mainptrs->userInput);
} else printf(".\n");
mainptrs->lookingMode = 'd';
mainptrs->inputMode -= 0x20;
}
if(mainptrs->lookingMode == 'i'){
mainptrs->instructNr = -1;
}
mainptrs->libNr--;
// arguments
} else if(mainptrs->lookingMode == 'a'){
if(mainptrs->argumentNr == maxArgumentAmount) {
mainptrs->lookingMode = 'e';
} else if(entry == ',') {
mainptrs->argumentNr++;
} else
argumenthandlers[mainptrs->libNr](mainptrs);
}
}
mainptrs->readhead++;
if(mainptrs->readhead >= mainptrs->userInputLen){
printf("\aThe statement ");
if(mainptrs->inputMode == 'f'){
printf("on line %d in file %d ", mainptrs->flist[mainptrs->fileNr]->lineNr, mainptrs->fileNr);
}
printf("is too long (maximum is %u characters).\nIt is possible to change the maximum by modifying the `inplen` register.\n", mainptrs->userInputLen);
mainptrs->lookingMode = 'd';
mainptrs->inputMode = 'W';
}
}
if(mainptrs->lookingMode != 'd') executehandlers[mainptrs->libNr](mainptrs);
if(mainptrs->debug == 'v') printf("thing done\n");
return 0;
}
int getuserInput(GLOBAL* mainptrs){
file_t* thefile = mainptrs->flist[mainptrs->fileNr];
time_t end_time;
switch (mainptrs->inputMode){
case 'I':
mainptrs->inputMode = 'i';
case 'i':
printf("\\\\\\ ");
if(fgets(mainptrs->userInput, mainptrs->userInputLen, thefile->fp) == NULL){
mainptrs->running = false;
printf("\nGood day!\n");
}
mainptrs->userInput[mainptrs->userInputLen] = '\0';
break;
case 'F':
EndScript(mainptrs);
if(mainptrs->fileNr == 0) mainptrs->inputMode = 'i';
break;
case 'f':
thefile->lineNr++;
if(mfgets(mainptrs->userInput, mainptrs->userInputLen, thefile) == NULL){
thefile->lineNr = 0;
mainptrs->userInput[0] = '\0';
if(mainptrs->scriptLoops == 1){
thefile->pos = 0;
// fseek(thefile->fp, 0, SEEK_SET);
} else {
end_time = time(&end_time);
inttonum(®s[ptme], (int64_t) end_time - thefile->begin_time);
EndScript(mainptrs);
}
}
mainptrs->userInput[mainptrs->userInputLen] = '\0';
break;
case 'W':
printf("was\n");
fgets(mainptrs->userInput, mainptrs->userInputLen, thefile->fp);
mainptrs->userInput[0] = '\0';
case 'w':
mainptrs->inputMode = 'i';
break;
}
if(mainptrs->debug == 'v') printf("userInput gotten\n");
return 0;
}
//############################################### </Main operation>
int main(int argc, char* argv[]){
GLOBAL mainitems;
initLibFuncPtrs();
for(int i = 0; i < libAmount; i++)
initfuncs[i](&mainitems);
handlecommandlineargs(argc, argv, &mainitems);
if(mainitems.inputMode == 'i')
printf("Good to see you!\nEnter `help` for quick tips and `\\` to close the program.\n");
do {
getuserInput(&mainitems);
dothing(&mainitems);
for(int i = 0; i < libAmount; i++)
updatefuncs[i](&mainitems);
if(mainitems.debug == 'v') printf("end of main while loop\n");
} while(mainitems.running || mainitems.inputMode == 'f');
for(int i = 0; i < libAmount; i++)
freefuncs[i](&mainitems);
return 0;
}
#endif