-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
64 lines (57 loc) · 1.04 KB
/
parser.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
#include <stdlib.h>
#include <string.h>
#include "bf.h"
#include "vm.h"
#include "lexer.h"
byte ch2instr(char ch)
{
Command instr = UNKNOWN;
switch (ch) {
case '<':
instr = LEFT;
break;
case '>':
instr = RIGHT;
break;
case '+':
instr = INC;
break;
case '-':
instr = SUB;
break;
case ',':
instr = IN;
break;
case '.':
instr = OUT;
break;
case '[':
instr = LOOP_COND;
break;
case ']':
instr = LOOP_END;
break;
default:
instr = UNKNOWN;
break;
}
return (byte) instr;
}
unsigned int code_size(byte *code)
{
unsigned int i = 0;
while (code[i])
i++;
return i;
}
byte *parser() {
byte *code = (byte *) malloc(sizeof(byte) * DEFAULT_MEMSIZE);
char ch = '\0';
int i = 0;
memset(code, 0, sizeof(byte) * DEFAULT_MEMSIZE);
while ((ch = get_instr_ch()) != EOF) {
code[i] = ch2instr(ch);
i++;
}
return code;
}