-
Notifications
You must be signed in to change notification settings - Fork 1
/
aasm_global.h
200 lines (167 loc) · 5.06 KB
/
aasm_global.h
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
#ifndef GLOBAL_DEF
#define GLOBAL_DEF
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#define maxKeywordLen 10
#define maxArgumentAmount 4
#define libAmount 3
#define initialuserInputLen 256
void printversion(){
printf("\n");
// printf(" Arbitrary Assembly Vga \n");
printf(" Arbitrary Assembly pVga.sa \n");
printf(" x-------x \n");
printf(" | A r b | \n");
printf(" | A s m | \n");
// printf(" | V g a | \n");
printf(" |pVga.sa| \n");
printf(" x-------x \n");
printf(" github.com/StevenClifford/ArbAsm\n\n");
printf(" Libraries:\n");
printf(" stdlib\n");
printf(" quatlib\n");
printf("\n");
}
typedef struct FileInfo {
FILE* fp;
char* mfp;
int64_t len;
int64_t pos;
int lineNr;
time_t begin_time;
int64_t* labelposs;
char (*labels)[maxKeywordLen];
} file_t;
typedef struct global {
char* userInput;
int userInputLen;
int readhead;
char lookingMode;
// 'i' is for instruction mnemonics
// 'a' is for arguments
// 'd' is for done
// 'e' is for execute
int libNr;
int instructNr;
int argumentNr;
bool running;
int scriptLoops;
char inputMode;
// 'i' is from stdin
// 'f' is from file
// 'w' is there already was a command
// if they are capitalized there was an error
int bigEndian;
int fileNr;
file_t** flist;
char debug;
// 's' is silent
// 'v' is verbose
} GLOBAL;
typedef int (*fun)(GLOBAL*);
fun initfuncs[libAmount];
fun instructhandlers[libAmount];
fun argumenthandlers[libAmount];
fun executehandlers[libAmount];
fun updatefuncs[libAmount];
fun freefuncs[libAmount];
int strlook(char string[], char source[][maxKeywordLen], int* readhead){
int item = 0;
int j;
bool isthis;
while (source[item][0] != '\0'){
isthis = true;
j = 0;
while (source[item][j] != '\0'){
if(string[j + *readhead] != source[item][j]) isthis = false;
j++;
}
if(isthis){
*readhead += --j;
return item;
}
item++;
}
return -1;
}
char* mfgets(char* string, int size, file_t* file){
int64_t i = 0;
if(file->pos == file->len) return NULL;
do {
string[i] = file->mfp[file->pos + i];
i++;
} while(file->mfp[file->pos + i - 1] != '\n' && i <= size-1 && file->pos + i <= file->len);
string[i] = '\0';
file->pos += i;
// printf("-------------------------------- %s\n", string);
return string;
}
int RunScript(GLOBAL* mainptrs, char* name){
if(mainptrs->debug == 'v') printf("RUNSCRIPT: '%s'\n", name);
mainptrs->fileNr++;
mainptrs->flist = realloc(mainptrs->flist, (mainptrs->fileNr + 1)*sizeof(file_t*));
mainptrs->flist[mainptrs->fileNr] = malloc(sizeof(file_t));
mainptrs->flist[mainptrs->fileNr]->fp = fopen(name, "r");
file_t* thefile = mainptrs->flist[mainptrs->fileNr];
if(thefile->fp == NULL){
printf("\aCould not open script '%s'.\n", name);
free(mainptrs->flist[mainptrs->fileNr]);
mainptrs->flist = realloc(mainptrs->flist, (mainptrs->fileNr)*sizeof(file_t*));
mainptrs->fileNr--;
return 1;
}
mainptrs->inputMode = 'f';
thefile->pos = 0;
thefile->lineNr = 0;
thefile->begin_time = time(&thefile->begin_time);
fseek(thefile->fp, 0, SEEK_END);
thefile->len = ftell(thefile->fp);
fseek(thefile->fp, 0, SEEK_SET);
thefile->labelposs = NULL;
thefile->labels = malloc(1);
thefile->mfp = (char*) malloc(sizeof(char)*(thefile->len + maxKeywordLen));
fread(thefile->mfp, 1, thefile->len, thefile->fp);
int labelam = 0;
for(int i = 0; i < thefile->len;i++){
if(thefile->mfp[i] == '\n' && thefile->mfp[i+1] == ':'){
i += 2;
thefile->labels = (char(*)[maxKeywordLen]) realloc(thefile->labels, sizeof(char[labelam+1][maxKeywordLen]));
thefile->labelposs = (int64_t*) realloc(thefile->labelposs, sizeof(int64_t[labelam+1]));
int j = 0;
for(j = 0;
j < maxKeywordLen && ((thefile->mfp[i+j] >= 'a' && thefile->mfp[i+j] <= 'z') || (thefile->mfp[i+j] >= 'A' && thefile->mfp[i+j] <= 'Z') || (thefile->mfp[i+j] >= '0' && thefile->mfp[i+j] <= '9'));
j++)
{
thefile->labels[labelam][j] = thefile->mfp[i+j];
}
thefile->labels[labelam][j] = '\0';
thefile->labelposs[labelam] = i + j;
// printf("%s, %ld\n", thefile->labels[labelam], thefile->labelposs[labelam]);
labelam++;
}
}
thefile->labels = (char(*)[maxKeywordLen]) realloc(thefile->labels, sizeof(char[labelam+1][maxKeywordLen]));
thefile->labels[labelam][0] = '\0';
fseek(thefile->fp, 0, SEEK_SET);
if(mainptrs->debug == 'v') printf("File %d opened, length: %ld.\n", mainptrs->fileNr, thefile->len);
return 0;
}
void EndScript(GLOBAL* mainptrs){
file_t* thefile = mainptrs->flist[mainptrs->fileNr];
free(thefile->labels);
free(thefile->labelposs);
thefile->labelposs = NULL;
thefile->lineNr = 0;
mainptrs->userInput[0] = '\0';
fclose(thefile->fp);
free(thefile->mfp);
free(mainptrs->flist[mainptrs->fileNr]);
mainptrs->flist[mainptrs->fileNr] = NULL;
mainptrs->flist = realloc(mainptrs->flist, (mainptrs->fileNr)*sizeof(file_t*));
mainptrs->fileNr--;
if(mainptrs->fileNr == 0) mainptrs->inputMode = 'i';
}
#endif