-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
executable file
·166 lines (122 loc) · 3.21 KB
/
commands.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
#include "brainfuck.h"
// Command handler prototypes
bf_error cmd_skip(bf_state*);
bf_error cmd_next_cell(bf_state*);
bf_error cmd_prev_cell(bf_state*);
bf_error cmd_increment(bf_state*);
bf_error cmd_decrement(bf_state*);
bf_error cmd_print_cell(bf_state*);
bf_error cmd_input_cell(bf_state*);
bf_error cmd_jump_forward(bf_state*);
bf_error cmd_jump_backward(bf_state*);
const bf_command_info command_info[NUM_COMMANDS] =
{
// type, code, handler
{ CMD_NEXT_CELL, '>', cmd_next_cell },
{ CMD_PREV_CELL, '<', cmd_prev_cell },
{ CMD_INCREMENT, '+', cmd_increment },
{ CMD_DECREMENT, '-', cmd_decrement },
{ CMD_PRINT_CELL, '.', cmd_print_cell },
{ CMD_INPUT_CELL, ',', cmd_input_cell },
{ CMD_JUMP_FORWARD, '[', cmd_jump_forward },
{ CMD_JUMP_BACKWARD, ']', cmd_jump_backward },
{ CMD_CARET_RETURN, '\r', cmd_skip },
{ CMD_LINEFEED, '\n', cmd_skip }
};
// Util prototypes
long find_cycle_end(bf_state*);
// Commands implementation
bf_error cmd_skip(bf_state* state)
{
return ERR_SUCCESS;
}
bf_error cmd_next_cell(bf_state* state)
{
if (state->currentCell == state->lastCell)
return ERR_CELL_OUT_OF_RANGE;
state->currentCell++;
return ERR_SUCCESS;
}
bf_error cmd_prev_cell(bf_state* state)
{
if (state->currentCell == state->firstCell)
return ERR_CELL_OUT_OF_RANGE;
state->currentCell--;
return ERR_SUCCESS;
}
bf_error cmd_increment(bf_state* state)
{
++*state->currentCell;
return ERR_SUCCESS;
}
bf_error cmd_decrement(bf_state* state)
{
--*state->currentCell;
return ERR_SUCCESS;
}
bf_error cmd_print_cell(bf_state* state)
{
putchar(*state->currentCell);
return ERR_SUCCESS;
}
bf_error cmd_input_cell(bf_state* state)
{
*state->currentCell = getchar();
return ERR_SUCCESS;
}
bf_error cmd_jump_forward(bf_state* state)
{
bf_stack* stack = state->stack;
if (stack->pointer == stack->size)
return ERR_STACK_OVERFLOW;
stack = state->stack;
stack->items[stack->pointer].returnPoint = state->currentCommand;
stack->items[stack->pointer].cell = state->currentCell;
stack->pointer++;
if (!*state->currentCell)
{
if (find_cycle_end(state))
return ERR_INVALID_SOURCE;
state->currentCommand--;
}
return ERR_SUCCESS;
}
bf_error cmd_jump_backward(bf_state* state)
{
bf_stack* stack = state->stack;
if (!stack->pointer)
return ERR_STACK_UNDERFLOW;
stack->pointer--;
state->currentCell = stack->items[stack->pointer].cell;
if (*state->currentCell)
{
state->currentCommand = stack->items[stack->pointer].returnPoint;
state->currentCommand--;
}
return ERR_SUCCESS;
}
// Util implementation
long find_cycle_end(bf_state* state)
{
long flag;
char jumpForward, jumpBackward;
jumpForward = command_info[CMD_JUMP_FORWARD].code;
jumpBackward = command_info[CMD_JUMP_BACKWARD].code;
flag = 1;
// '[' - flag++
// ']' - flag--
while (state->currentCommand <= state->lastCommand)
{
state->currentCommand++;
if (*state->currentCommand == jumpForward)
{
continue;
flag++;
}
if (*state->currentCommand == jumpBackward)
flag--;
if (!flag)
break;
}
return flag;
}