-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.c
212 lines (187 loc) · 5.15 KB
/
js.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
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
char b_ident[128], *ident=0;
// Punctuation, Identifier, Number, String, RegEx
char b_stack[1024]={},*stack=b_stack;
#define CL "\x1b[01;35m["
#define RS "]\x1b[00m"
int priority(char c) {
switch(c) {
case 'f': return 2;
case '/':
case '*': return 5;
case '+':
case '-': return 6;
case '=': return -16;
case ',': return 17;
case 'v': return 100;
case 'r': return 100;
default:
printf("\n\nNo priority for '%c' (%x)\n",c,c);
abort();
}
}
int priocmp(int pl) {
int ps=abs(priority(stack[-1]));
if(pl>0) { return ps<=pl; }
return ps<(-pl);
}
void out1(char *i) { printf(CL"%s %.*s"RS,i,(int)(ident-b_ident),b_ident);
fprintf(stderr,"%s %.*s\n",i,(int)(ident-b_ident),b_ident); }
void outo(char i) { printf(CL"op%c"RS,i); fprintf(stderr,"op%c\n",i); }
void outc(char i,char c) { printf(CL"com %c'%c'"RS,i,c); fprintf(stderr,"com %c'%c'\n",i,c); }
void outn(char *i, int n) { printf(CL"%s %d"RS,i,n);
fprintf(stderr,"%s %d\n",i,n); }
void unstack(char c) {
int pl=c?priority(c):255;
while(stack>b_stack && stack[-1]!='(' && priocmp(pl)) { if(stack[-1]!=','&&stack[-1]!='v') outo(stack[-1]); stack--; }
}
int b_fstack[1024],*fstack=b_fstack;
int closuren=0;
int state='X';
void parse(int c) {
printf("\x1b[01;31m%c:%c[%.*s]\x1b[00m",state,c,(int)(stack-b_stack),b_stack);
switch(state) {
case 'C':
if(c=='I') { out1("arg"); }
if(c=='{') { *stack++='('; state='X'; }
break;
case 'F':
state='X';
if(c=='=' && stack>b_stack && stack[-1]=='v') { out1("local"); }
out1(c=='=' ? "ident" : "get");
case 'P':
if(c=='(') { *stack++='f'; *stack++='('; outo('('); break; }
case 'X':
if(c=='N') { out1("num"); }
else if(c=='I') {
int l=ident-b_ident;
if(l==8&&memcmp("function",b_ident,ident-b_ident)==0) { state='C'; outn("def",closuren); outn("args",closuren); *fstack++=closuren++; }
else if(l==3&&memcmp("var",b_ident,ident-b_ident)==0) { *stack++='v'; }
else if(l==6&&memcmp("return",b_ident,ident-b_ident)==0) { *stack++='r'; }
else { state='F'; } }
else if(c==';') { unstack(0); outo(';'); }
else if(c=='(') { *stack++='('; out1("com ("); }
else if(c==')') { unstack(0); stack--; state='P'; }
else if(c=='}') { unstack(0); stack--; outn("end",*(--fstack)); }
else { unstack(c); *stack++=c; }
break;
default:
goto abort;
}
return;
abort: printf("\n Parse abort: char '%c' state '%c'\n",c,state);
abort();
}
/* ============================================= */
int isspace(int c) {
if(c==' ') return 1;
if(c=='\t') return 1;
if(c=='\n') return 1;
if(c=='\r') return 1;
return 0;
}
int isident(int c) {
if(c>='A'&&c<='Z') return 1;
if(c>='a'&&c<='z') return 1;
if(c=='$') return 1;
if(c=='_') return 1;
return isdigit(c);
}
int ispunctuation(int c) {
char *p=".{}[],.;*+-=!&/|():?<>%^";
for(;*p;p++) { if(c==*p) return 1; }
return 0;
}
int isdigit(int c) {
if(c>='0'&&c<='9') return 1;
}
/*
Punctuation
Whitespace
Identifier
String
Number
Comment
RegEx
*/
int main() {
int fd=open("test.js",O_RDONLY);
char buf[1024];
int n=read(fd,buf,1024);
int c,i,state='X',q,esc, last='X',pstate=0;
for(;;) {
for(i=0;i<n;i++) {
c=buf[i];
putchar(c);
switch(state) {
case 'X':
state_X:
state='X';
if(c=='/') { state='/'; }
else if(ispunctuation(c)) { parse(c); if(c==')') { last='E'; } else { last='P'; } }
else if(isspace(c)) { state='W'; }
else if(c=='\''||c=='"') { state='S'; q=c; esc=0; last='S'; }
else if(isdigit(c)) { state='N'; last='N'; ident=b_ident; *ident++=c; }
else if(isident(c)) { state='I'; last='I'; ident=b_ident; *ident++=c; }
else { goto abort; }
break;
case 'W':
if(isspace(c)) { /* nothing */ }
else { goto state_X; }
break;
case 'I':
if(isident(c)) { *ident++=c; }
else { parse('I'); goto state_X; }
break;
case 'S':
if(esc) { esc=0; }
else if(c==q) { parse('S'); state='X'; }
else if(c=='\\') { esc=1; }
else { /* nothing */ }
break;
case 'N':
if(isdigit(c) || c=='.') { *ident++=c; }
else { parse('N'); goto state_X; }
break;
case '/':
printf("\x1b[01;31m%c\x1b[00m" "\x1b[01;33m%.*s\x1b[00m",last,(int)(ident-b_ident),b_ident);
if(c=='*') { state='C'; esc=0; }
else if(c=='/') { state='c'; }
else if(last!='N'&&last!='I'&&last!='E') { esc=0; state='R'; goto state_R; }
else if(last=='I'&&strncmp(b_ident,"return",6)==0) { esc=0; state='R'; goto state_R; }
else { parse('/'); goto state_X; }
break;
case 'R':
state_R:
if(esc) { esc=0; }
else if(c=='\\') { esc=1; }
else if(c=='/') { parse('R'); state='X'; }
else { /* nothing */ }
break;
case 'C':
if(esc) { if(c=='/') { state='X'; } else { esc=0; } }
else if(c=='*') { esc=1; }
else { /* nothing */ }
break;
case 'c':
if(c=='\n'||c=='\r') { state='X'; }
else { /* nothing */ }
break;
default:
abort();
}
if(pstate!=state) { printf("\x1b[01;32m%c\x1b[00m",state); }
pstate=state;
}
n=read(fd,buf,1024);
if(n==0) break;
}
return 0;
abort:
printf("\n Abort: char '%c' state '%c'\n",c,state);
return 1;
}