-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruct.c
More file actions
83 lines (74 loc) · 1.63 KB
/
instruct.c
File metadata and controls
83 lines (74 loc) · 1.63 KB
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
#include "instruct.h"
#include <stdio.h>
struct instruct instruct_move_right(int n) {
return (struct instruct){
.type = INST_MOVE_RIGHT,
.data.move_right_amount = n,
};
}
struct instruct instruct_move_left(int n) {
return (struct instruct){
.type = INST_MOVE_LEFT,
.data.move_left_amount = n,
};
}
struct instruct instruct_inc(int n) {
return (struct instruct){
.type = INST_INC,
.data.inc_amount = n,
};
}
struct instruct instruct_dec(int n) {
return (struct instruct){
.type = INST_DEC,
.data.dec_amount = n,
};
}
struct instruct instruct_write() {
return (struct instruct){
.type = INST_WRITE,
};
}
struct instruct instruct_loop_start() {
return (struct instruct){
.type = INST_LOOP_START,
};
}
struct instruct instruct_loop_end() {
return (struct instruct){
.type = INST_LOOP_END,
};
}
struct instruct instruct_read() {
return (struct instruct){
.type = INST_READ,
};
}
void instruct_sprint(struct instruct inst, char *dest) {
switch (inst.type) {
case INST_MOVE_RIGHT:
sprintf(dest, "move_right=%d", inst.data.move_right_amount);
break;
case INST_MOVE_LEFT:
sprintf(dest, "move_left=%d", inst.data.move_left_amount);
break;
case INST_INC:
sprintf(dest, "inc=%d", inst.data.inc_amount);
break;
case INST_DEC:
sprintf(dest, "dec=%d", inst.data.dec_amount);
break;
case INST_LOOP_START:
sprintf(dest, "loop_start");
break;
case INST_LOOP_END:
sprintf(dest, "loop_end");
break;
case INST_WRITE:
sprintf(dest, "write");
break;
case INST_READ:
sprintf(dest, "read");
break;
}
}