-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathreadme.c
More file actions
executable file
·153 lines (124 loc) · 3.58 KB
/
Copy pathreadme.c
File metadata and controls
executable file
·153 lines (124 loc) · 3.58 KB
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
// MINIScript
// ---
// - Building
// [osx]
// $ gcc -m32 miniscript.c mslib.c readme.c && ./a.out < sample.js
// [arm thumb size]
// $ ~/gcc-arm-none-eabi-4_9-2015q2/bin/arm-none-eabi-gcc -c -mthumb -Os miniscript.c
// $ ~/gcc-arm-none-eabi-4_9-2015q2/bin/arm-none-eabi-size -A miniscript.o
// - Changes
// [0.1] 2016.03.11
// + code parser
// + allocation: var
// + operators: =,+,-
// + statement: ;
// + native function
// [0.2] 2016.03.16
// + comment: //
// + if-else: nested ok
// + operators: *,/,==,!=,(,)
// + scope: {}
// + scoped var
// [0.3] 2016.03.23
// + while: nested ok
// + function: prams ok
// [0.4] 2016.04.04
// + refactored
// + object: new,.
// [0.5] 2016.0?.??
// + stack,scope... overflow check
// + refactored
// + immediate value: +,-
// + operators: %,<,>,<=,>=
// + statement: break
// ---
// + multiple var
// + operators: !
// + try-catch, throw
// + tuned for speed
#include <stdio.h>
#include <string.h>
#include "miniscript.config.h"
#include "miniscript.h"
#define SIZE_STACK 0x40
#define SIZE_SCOPE 0x40
#define SIZE_POOL 0x04
#define SIZE_POOLA 0x01
typedef struct{
Stack base;
Var vars[SIZE_STACK];
} MyStack;
typedef struct{
Scope base;
VarMap nvars[SIZE_SCOPE];
} MyScope;
typedef struct{
Pool base;
ObjectForPool objs[SIZE_POOL];
} MyPool;
typedef struct{
ArrayPool base;
ArrayForPool arrs[SIZE_POOLA];
} MyArrayPool;
Var Stack_OVERFLOW;
void gf_print(Thread* p){
Var v = *Stack_pop(p->s);
Var__( Stack_pop(p->s));
Var__( Stack_pop(p->s));
Var* pv = (v.vt == VT_Refer) ? v.ref : &v;
if(pv->vt == VT_Number){
printf("%d\n", pv->num);
}else
if(pv->vt == VT_CodeString){
const char* b = pv->code;
const char* p = b + 1;
while(*p != *b) printf("%c", *(p++));
printf("\n");
}else{
printf("VT: %d\n", pv->vt);
}
Var__(&v);
Stack_push(p->s);//void
}
#define SIZE_SRC 0x1000
int main(){
char buf[SIZE_SRC];
size_t n = 0;
ssize_t r;
while( 0 < (r = read(0, buf+n, SIZE_SRC-n)) ) n += r;
buf[n] = '\0';
{
MyPool pool;
Pool_global( Pool_(&pool.base, SIZE_POOL) );
MyArrayPool poola;
ArrayPool_global( ArrayPool_(&poola.base, SIZE_POOLA) );
MyStack stack;
Stack_(&stack.base, SIZE_STACK);
MyScope scope;
Scope_(&scope.base, SIZE_SCOPE);
Thread thread;
Thread_(&thread, buf, &stack.base, &scope.base);
Var* pv;
pv = Scope_add(thread.o, "print", 5, Stack_push(thread.s));{
pv->vt = VT_Function;
pv->func = gf_print;
}
pv = Scope_add(thread.o, "Array", 5, Stack_push(thread.s));{
pv->vt = VT_Function;
pv->func = mslib_Array;
}
Stack_ground(thread.s);
Error* e = Thread_run(&thread);
if(e){
char a[0x10];
strncpy(a, e->code, e->len)[e->len] = '\0';
printf("[ERROR!] %s('%s')\n", e->reason, a);
}
Thread__(&thread);
Scope__(&scope.base);
Stack__(&stack.base);
ArrayPool__(&poola.base);
Pool__(&pool.base);
}
return 0;
}