-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhrm.c
452 lines (412 loc) · 7 KB
/
hrm.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#define debug
#ifdef debug
#define dprint printf
#else
#define dprint(...) ;
#endif
#define bufend (buf + buflen)
#define LAW_SIZE 80
#define BUF_SIZE 128
#define LABEL_NUM 64
#define CARPET_SIZE 64
#define KW_NEED_NEWLINE 4
const char *kwtbl[] = {
"[",
"]",
"inbox",
"outbox",
//ここから改行なし
"copyfrom",
"copyto",
"add",
"sub",
"bump+",
"bump-",
"jumpifzero",
"jumpifneg",
"jump",
//ここから独自のキーワード
":", //ラベル
"get", //即値
//ここから中間コードに対応なし
"run" //実行
};
const int KWTBL_SIZE = sizeof(kwtbl)/sizeof(char *);
enum {
i_open,
i_close,
i_inbox,
i_outbox,
i_copyfrom,
i_copyto,
i_add,
i_sub,
i_bump_p,
i_bump_m,
i_jumpifzero,
i_jumpifneg,
i_jump,
//ここから独自のキーワード
i_label, //ラベル
i_get, //即値
i_run,
//特殊キーワード
i_num,
i_pass,
i_char,
};
typedef struct
{
int name;
int* p;
} label;
enum {
type_empty,
type_num,
type_char
};
typedef struct
{
char type;
int num;
} tile;
char raw[LAW_SIZE];
int buf[BUF_SIZE];
int buflen = 0;
label lblls[LABEL_NUM];
int labelpt = 0;
tile acm = {type_empty, 0};
tile carpet[CARPET_SIZE];
void init()
{
for(int i=0;i<CARPET_SIZE;i++){
carpet[i].type = type_empty;
carpet[i].num = 0;
}
}
void buf_clear()
{
buflen = 0;
}
void trans()
{
char *rawp = raw; //文字列走査位置
int iword = 0; //中間コード置き場
bool is_find;
while(*rawp){//文字列の終端まで
is_find = false;
while(isspace(*rawp)) rawp++;
if(!*rawp) break;
//数字変換
if(isdigit(*rawp)||
*rawp=='-'||*rawp=='+'){
iword = atoi(rawp);
buf[buflen++] = i_num;
buf[buflen++] = iword;
while(isdigit(*rawp)||
*rawp=='-'||*rawp=='+')
rawp++;
continue;
}
//文字解決
if(*rawp=='\''){
char temp = *(++rawp);
if(temp == '\\'){
switch(*(++rawp)){
case 'n':
temp = '\n';
break;
case '\\':
temp = '\\';
break;
case 'b':
temp = '\b';
break;
default:
printf("char error\n");
return;
}
}
if(*(++rawp) != '\''){ //終端確認
printf("char error\n");
return;
}
buf[buflen++] = i_char;
buf[buflen++] = temp;
rawp++;
continue;
}
//テーブル走査
for(iword=0;iword < KWTBL_SIZE;iword++)
{
int k = 0;
while(kwtbl[iword][k]!=0 &&
kwtbl[iword][k]==rawp[k])
k++;
if(kwtbl[iword][k]==0 &&
!isalpha(rawp[k])){
buf[buflen++] = iword;
rawp += k;
is_find = true;
break;
}
dprint("i:%d\n",iword);
}
if(!is_find){
printf("error find unkown word\n");
return;//----------error---------->
}
//end---------
}
printf("ok\n");
}
//LABELS***************************
void setlabel(int name, int* p)
{
dprint("set %d\n",name);
lblls[labelpt].name = name;
lblls[labelpt].p = p;
labelpt++;
}
int* labelsearch(int name)
{
//dprint("search num%d\n",name);
for(int i = 0;i < labelpt;i++)
{
if(lblls[i].name == name)
return lblls[i].p;
}
printf("label error\nnumber:%d\n",labelpt);
return NULL;
}
/* old
void input()
{
fgets(buf, BUF_SIZE, stdin);
//bufend = buf + BUF_SIZE;
}
*/
void setup()
{
int* bufp = buf;
int lbname;
for(;bufp < bufend;bufp++)
{
if(*bufp == i_label)
{
if(*(bufp+1) != i_num){
printf("no name label\n");
return;//---------error------->
}
bufp += 2;
lbname = *bufp;
*(bufp-2)= *(bufp-1)= *bufp=i_pass;
//^i_label ^i_num ^label name
setlabel(lbname, bufp);
}
}
}
//reader*******************************
int* bufp;
void stop(){
bufp=bufend;
}
int refsolver()
{ // " i_num NUM " or " [ i_num NUM ] "
bufp++;
if(*bufp==i_num){
return *(++bufp);
}else if(*bufp==i_open){
bufp += 2;
if(0<=*bufp || *bufp<CARPET_SIZE){
if(carpet[*bufp].type==type_num){
return carpet[*bufp++].num;
}else{
printf("type error");
}
}else{
printf("out of index");
}
}
printf("\nref-error stop\n");
stop();
return 0;
}
void reader()
{
int refn;
int command;
for(bufp=buf;bufp<bufend;bufp++)
{
command = *bufp;
switch(command)
{
case i_copyto:
if(acm.type==type_empty){
printf("no value");
}
carpet[refsolver()] = acm;
break;
case i_copyfrom:
refn = refsolver();
if(carpet[refn].type==type_empty){
printf("no value");
bufp=buf;
}else{
acm=carpet[refn];
}
break;
case i_bump_p:
case i_bump_m:
refn = refsolver();
if(carpet[refn].type==type_empty){
printf("no value");
bufp=buf;
}else{
if(command==i_bump_p){
carpet[refn].num++;
}else{
carpet[refn].num--;
}
acm=carpet[refn];
}
break;
case i_outbox:
switch (acm.type) {
case type_empty:
printf("error nothing to outbox\n");
return;
case type_char:
putchar(acm.num);
break;
case type_num:
printf(" %d",acm.num);
break;
}
acm.type = type_empty;
//printf("!outbox\n");
break;
case i_get:
switch(*(++bufp)){
case i_num:
acm.type = type_num;
acm.num = *(++bufp);
break;
case i_char:
acm.type = type_char;
acm.num = *(++bufp);
break;
default:
printf("get error\n");
return;
}
break;
//jump----------------
case i_jumpifneg:
//原作の仕様通り、型チェックはしない
//空のときのみ例外
bufp += 2;
if(acm.type == type_empty){
printf("if error no tile\n");
return;
}
if(acm.num < 0){
int* p = labelsearch(*bufp);
if(p == NULL)
return;
bufp = p;
}
break;
case i_jumpifzero:
//原作の仕様通り、型チェックはしない
//空のときのみ例外
bufp += 2;
if(acm.type == type_empty){
printf("if error no tile\n");
return;
}
if(acm.num==0){
int* p = labelsearch(*bufp);
if(p == NULL)
return;
bufp = p;
}
break;
case i_label:
printf("?\n");
case i_jump:
bufp += 2;
int* p = labelsearch(*bufp);
if(p == NULL)
return;
bufp = p;
break;
//jump end --------------
}
//dprint("iok\n");
}
}
//-------------------------------
void run()
{
setup();
reader();
}
int main()
{
init();
bool run_start = false;
dprint("table size: %d\n",KWTBL_SIZE);
for(;;)
{
putchar('>');
if(fgets(raw, 256, stdin) == NULL){
putchar('\n');
return 0;
}
trans();
run_start = false;
dprint("-----------\n");
for(int i=0;i<buflen;i++){
if(buf[i] == i_run){
buflen = i;
run_start = true;
break;
}
#ifdef debug
if(buf[i] == i_num){
//printf("i_num\n");
printf("%d\n",buf[++i]);
continue;
}
if(buf[i] == i_char){
printf("'");
printf("%c",buf[++i]);
printf("'\n");
continue;
}
if(buf[i] == i_pass){
//printf("i_pass\n");
continue;
}
if(0<buf[i] && buf[i]<KWTBL_SIZE){
if(buf[i]<KW_NEED_NEWLINE){
printf("%s\n",kwtbl[buf[i]]);
}else{
printf("%s ",kwtbl[buf[i]]);
}
}
#endif
}
dprint("-----------\n");
if(run_start){
printf("start!\n");
run();
}
}
return 0;
}