forked from embedded2015/rubi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.c
158 lines (138 loc) · 4.26 KB
/
engine.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
#include "rubi.h"
#include "parser.h"
#include "asm.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
struct {
uint32_t addr[0xff];
int count;
} mem;
static unsigned int w;
static void set_xor128() { w = 1234 + (getpid() ^ 0xFFBA9285); }
void init()
{
tok.pos = ntvCount = 0;
tok.size = 0xfff;
set_xor128();
tok.tok = calloc(sizeof(Token), tok.size);
brks.addr = calloc(sizeof(uint32_t), 1);
rets.addr = calloc(sizeof(uint32_t), 1);
}
static void freeAddr()
{
if (mem.count > 0) {
for (--mem.count; mem.count >= 0; --mem.count)
free((void *)mem.addr[mem.count]);
mem.count = 0;
}
}
void dispose()
{
free(brks.addr);
free(rets.addr);
free(tok.tok);
freeAddr();
}
int32_t lex(char *code)
{
int32_t codeSize = strlen(code), line = 1;
int32_t is_crlf = 0;
for (int32_t i = 0; i < codeSize; i++) {
if (tok.size <= i)
tok.tok = realloc(tok.tok, (tok.size += 512 * sizeof(Token)));
if (isdigit(code[i])) { // number?
for (; isdigit(code[i]); i++)
strncat(tok.tok[tok.pos].val, &(code[i]), 1);
tok.tok[tok.pos].nline = line;
i--;
tok.pos++;
} else if (isalpha(code[i])) { // ident?
char *str = tok.tok[tok.pos].val;
for (; isalpha(code[i]) || isdigit(code[i]) || code[i] == '_'; i++)
*str++ = code[i];
tok.tok[tok.pos].nline = line;
i--;
tok.pos++;
} else if (code[i] == ' ' || code[i] == '\t') { // space or tab?
} else if (code[i] == '#') { // comment?
for (i++; code[i] != '\n'; i++) { } line++;
} else if (code[i] == '"') { // string?
strcpy(tok.tok[tok.pos].val, "\"");
tok.tok[tok.pos++].nline = line;
for (i++; code[i] != '"' && code[i] != '\0'; i++)
strncat(tok.tok[tok.pos].val, &(code[i]), 1);
tok.tok[tok.pos].nline = line;
if (code[i] == '\0')
error("%d: expected expression '\"'",
tok.tok[tok.pos].nline);
tok.pos++;
} else if (code[i] == '\n' ||
(is_crlf = (code[i] == '\r' && code[i + 1] == '\n'))) {
i += is_crlf;
strcpy(tok.tok[tok.pos].val, ";");
tok.tok[tok.pos].nline = line++;
tok.pos++;
} else {
strncat(tok.tok[tok.pos].val, &(code[i]), 1);
if (code[i + 1] == '=' || (code[i] == '+' && code[i + 1] == '+') ||
(code[i] == '-' && code[i + 1] == '-'))
strncat(tok.tok[tok.pos].val, &(code[++i]), 1);
tok.tok[tok.pos].nline = line;
tok.pos++;
}
}
tok.tok[tok.pos].nline = line;
tok.size = tok.pos - 1;
return 0;
}
static void put_i32(int32_t n) { printf("%d", n); }
static void put_str(int32_t *n) { printf("%s", (char *) n); }
static void put_ln() { printf("\n"); }
static void ssleep(uint32_t t) { usleep(t * CLOCKS_PER_SEC / 1000); }
static void add_mem(int32_t addr) { mem.addr[mem.count++] = addr; }
static int xor128()
{
static uint32_t x = 123456789, y = 362436069, z = 521288629;
uint32_t t;
t = x ^ (x << 11);
x = y; y = z; z = w;
w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
return ((int32_t) w < 0) ? -(int32_t) w : (int32_t) w;
}
static void *funcTable[] = {
put_i32, /* 0 */ put_str, /* 4 */ put_ln, /* 8 */ malloc, /* 12 */
xor128, /* 16 */ printf, /* 20 */ add_mem, /* 24 */ ssleep, /* 28 */
fopen, /* 32 */ fprintf, /* 36 */ fclose, /* 40 */ fgets, /* 44 */
free, /* 48 */ freeAddr /* 52 */
};
static int execute(char *source)
{
init();
lex(source);
parser();
((int (*)(int *, void **)) ntvCode)(0, funcTable);
dispose();
return 0;
}
int main(int argc, char **argv)
{
char *src;
if (argc < 2) error("no given filename");
FILE *fp = fopen(argv[1], "rb");
size_t ssz = 0;
if (!fp) {
perror("file not found");
exit(0);
}
fseek(fp, 0, SEEK_END);
ssz = ftell(fp);
fseek(fp, 0, SEEK_SET);
src = calloc(sizeof(char), ssz + 2);
fread(src, sizeof(char), ssz, fp);
fclose(fp);
return execute(src);
}