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
|
#include "program.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
static FILE* f;
static uint32_t count;
static char next_char() {
char c;
if (fread(&c, 1, 1, f) != 1) {
return EOF;
} else {
count++;
return c;
}
}
static Symbol next_symbol() {
char c = next_char();
retest:
switch (c) {
case '<':
return MoveLeft;
case '>':
return MoveRight;
case '+':
return Increment;
case '-':
return Decrement;
case '[':
return StartLoop;
case ']':
return EndLoop;
case '.':
return PutChar;
case ',':
return GetChar;
case '*':
return Allocate;
case '!':
return Free;
case '(':
return EnterTape;
case ')':
return LeaveTape;
case '`':
return PutString;
case '~':
return GetString;
case '%':
return Clear;
case '\n':
case '\t':
case ' ':
while(c = next_char(), c == '\n' || c == '\t' || c == ' ');
goto retest;
case '/':
while(c = next_char(), c != '\n' && c != EOF);
goto retest;
case EOF:
return Eof;
default:
return Invalid;
}
}
void program_init(char* file_path, Program* program) {
f = fopen (file_path, "r");
if (f == NULL) {
printf("error: failed to open %s (%s)\n", file_path, strerror(errno));
exit(EXIT_FAILURE);
}
uint32_t capacity = 8;
count = 0;
program->data = malloc(capacity * sizeof(Symbol));
program->len = 0;
Symbol s;
while(true) {
s = next_symbol();
if (s == Invalid) {
printf("error: invalid symbol at character %d\n", count);
exit(EXIT_FAILURE);
}
if (program->len == capacity) {
capacity *= 2;
program->data = realloc(program->data, capacity * sizeof(Symbol));
}
program->data[program->len] = s;
program->len++;
if (s == Eof) break;
}
fclose(f);
}
void program_peek(Program* program, Symbol* symbol) {
*symbol = program->data[program->index];
}
void program_next(Program* program, Symbol* symbol) {
if (program->index >= program->len) {
*symbol = Eof;
return;
}
program_peek(program, symbol);
program->index++;
}
void program_last(Program* program, Symbol* symbol) {
if (program->index == 0) {
*symbol = Eof;
return;
}
program->index--;
program_peek(program, symbol);
}
void program_free(Program* program) {
free(program->data);
}
void tape_init(uint32_t len, Tape* tape) {
tape->len = len;
tape->data = malloc(len);
tape->index = 0;
memset(tape->data, 0, len);
}
void tape_left(Tape* tape) {
if (tape->index == 0) {
tape->index = tape->len - 1;
} else {
tape->index--;
}
}
void tape_right(Tape* tape) {
tape->index++;
tape->index %= tape->len;
}
uint8_t tape_get(Tape* tape) {
return tape->data[tape->index];
}
void tape_set(Tape* tape, uint8_t value) {
tape->data[tape->index] = value;
}
uint8_t* tape_ptr(Tape* tape) {
return tape->data;
}
void tape_free(Tape* tape) {
free(tape->data);
}
|