about summary refs log tree commit diff stats
path: root/023jump.cc
blob: e9bd5011e7e51813e689891831c9ab78953bbc4e (plain) (blame)
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
//: Jump primitives

:(before "End Primitive Recipe Declarations")
JUMP,
:(before "End Primitive Recipe Numbers")
Recipe_number["jump"] = JUMP;
:(before "End Primitive Recipe Implementations")
case JUMP: {
  assert(ingredients.size() == 1);
  assert(ingredients.at(0).size() == 1);  // scalar
  instruction_counter += ingredients.at(0).at(0);
  trace("run") << "jumping to instruction " << instruction_counter+1;
  break;
}

:(scenario jump_can_skip_instructions)
recipe main [
  jump 1:offset
  1:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 0 is 1
-run: instruction main/1
-mem: storing 1 in location 1

:(scenario jump_backward)
recipe main [
  jump 1:offset  # 0 -+
  jump 1:offset  #    |   +-+ 1
                 #   \/  /\ |
  jump -2:offset #  2 +-->+ |
]                #         \/ 3
+run: instruction main/0
+run: instruction main/2
+run: instruction main/1

:(before "End Primitive Recipe Declarations")
JUMP_IF,
:(before "End Primitive Recipe Numbers")
Recipe_number["jump-if"] = JUMP_IF;
:(before "End Primitive Recipe Implementations")
case JUMP_IF: {
  assert(ingredients.size() == 2);
  assert(ingredients.at(0).size() == 1);  // scalar
  if (!ingredients.at(0).at(0)) {
    trace("run") << "jump-if fell through";
    break;
  }
  assert(ingredients.at(1).size() == 1);  // scalar
  instruction_counter += ingredients.at(1).at(0);
  trace("run") << "jumping to instruction " << instruction_counter+1;
  break;
}

:(scenario jump_if)
recipe main [
  jump-if 999:literal 1:offset
  1:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 1 is 1
+run: jumping to instruction 2
-run: instruction main/1
-mem: storing 1 in location 1

:(scenario jump_if_fallthrough)
recipe main [
  jump-if 0:literal 1:offset
  123:integer <- copy 1:literal
]
+run: instruction main/0
+run: jump-if fell through
+run: instruction main/1
+mem: storing 1 in location 123

:(before "End Primitive Recipe Declarations")
JUMP_UNLESS,
:(before "End Primitive Recipe Numbers")
Recipe_number["jump-unless"] = JUMP_UNLESS;
:(before "End Primitive Recipe Implementations")
case JUMP_UNLESS: {
  assert(ingredients.size() == 2);
  assert(ingredients.at(0).size() == 1);  // scalar
  if (ingredients.at(0).at(0)) {
    trace("run") << "jump-unless fell through";
    break;
  }
  assert(ingredients.at(1).size() == 1);  // scalar
  instruction_counter += ingredients.at(1).at(0);
  trace("run") << "jumping to instruction " << instruction_counter+1;
  break;
}

:(scenario jump_unless)
recipe main [
  jump-unless 0:literal 1:offset
  1:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 1 is 1
+run: jumping to instruction 2
-run: instruction main/1
-mem: storing 1 in location 1

:(scenario jump_unless_fallthrough)
recipe main [
  jump-unless 999:literal 1:offset
  123:integer <- copy 1:literal
]
+run: instruction main/0
+run: ingredient 0 is 999
+run: jump-unless fell through
+run: instruction main/1
+mem: storing 1 in location 123