-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstasm.c
161 lines (132 loc) · 3.91 KB
/
stasm.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
#include "stasm.h"
void get_line(Lexer* lexer)
{
int size;
if (fgets(lexer->line, LINE_MAX, lexer->fs) == NULL) {
if (feof(lexer->fs))
lexer->c = EOF;
} else {
size = strlen(lexer->line);
for (int i = 0; i < size; i++){
if (lexer->line[i] < 91 && lexer->line[i] > 64)
lexer->line[i] += ' ';
}
lexer->line_cursor = 0;
lexer->c = lexer->line[0];
}
}
Lexer* init_lexer_from_fs(FILE *fs)
{
Lexer* res = calloc(1, sizeof(Lexer));
res->fs = fs;
get_line(res);
return res;
}
void lexer_advance(Lexer* lexer)
{
if (lexer->c != EOF && lexer->c != '\0') {
lexer->line_cursor++;
lexer->c = lexer->line[lexer->line_cursor];
} else {
lexer->c = EOF;
}
}
void lexer_skip_whitespace(Lexer* lexer)
{
while (lexer->c == ' ' || lexer->c == '\t')
lexer_advance(lexer);
}
void lexer_skip_token(Lexer* lexer)
{
while (lexer->c != ' ' && lexer->c != '\t' && lexer->c != 10 && lexer->c != EOF)
lexer_advance(lexer);
}
int lexer_get_token_num(Lexer* lexer)
{
int res = 0;
int i = lexer->line_cursor;
while (lexer->line[i] != '\n' && lexer->line[i] != '\0') {
while (lexer->line[i] != ' ' && lexer->line[i] != '\t' && lexer->line[i] != '\n' && lexer->line[i] != '\0')
i++;
while (lexer->line[i] == ' ' || lexer->line[i] == '\t')
i++;
res++;
}
return res;
}
int lexer_compare(Lexer* lexer, int start, char* comp)
{
if (strlen(comp) != (lexer->line_cursor-start))
return 0;
for (int i = start; i < lexer->line_cursor; i++){
if (lexer->line[i] != comp[i-start])
return 0;
}
return 1;
}
Element lexer_scan_number(Lexer* lexer)
{
int pos, temp;
Element res = 0;
lexer_skip_whitespace(lexer);
pos = lexer->line_cursor;
while (lexer->c > 47 && lexer->c < 58)
lexer_advance(lexer);
for(int i = pos; i < lexer->line_cursor; i++) {
temp = lexer->line[i] - '0';
for (int j = 0; j < lexer->line_cursor - i -1; j++)
temp = temp * 10;
res += temp;
}
return res;
}
int lexer_scan_line(Lexer* lexer, Vm* vm)
{
int res = 1;
int pos;
lexer_skip_whitespace(lexer);
if (lexer->c == EOF)
return -1;
if (lexer->c == '#'){
get_line(lexer);
return res;
}
pos = lexer->line_cursor;
lexer_skip_token(lexer);
if(lexer_compare(lexer, pos, "push"))
addInst(vm, INST_PUSH, lexer_scan_number(lexer));
else if (lexer_compare(lexer, pos, "drop"))
addInst(vm, INST_DROP, 0);
else if (lexer_compare(lexer, pos, "dup"))
addInst(vm, INST_DUP, 0);
else if (lexer_compare(lexer, pos, "swap"))
addInst(vm, INST_SWAP, 0);
else if (lexer_compare(lexer, pos, "over"))
addInst(vm, INST_OVER, 0);
else if (lexer_compare(lexer, pos, "not"))
addInst(vm, INST_NOT, 0);
else if (lexer_compare(lexer, pos, "eq"))
addInst(vm, INST_EQUAL, 0);
else if (lexer_compare(lexer, pos, "lt"))
addInst(vm, INST_LT, 0);
else if(lexer_compare(lexer, pos, "jump"))
addInst(vm, INST_JUMP, lexer_scan_number(lexer));
else if(lexer_compare(lexer, pos, "jumpif"))
addInst(vm, INST_JUMPIF, lexer_scan_number(lexer));
else if (lexer_compare(lexer, pos, "add"))
addInst(vm, INST_ADD, 0);
else if (lexer_compare(lexer, pos, "print"))
addInst(vm, INST_PRINT_INT, 0);
get_line(lexer);
return res;
}
void run_from_fs(FILE* fs)
{
int state = 1;
Lexer* lexer = init_lexer_from_fs(fs);
Vm* vm = initVm();
while (state == 1) {
state = lexer_scan_line(lexer, vm);
}
execute_program(vm);
}