Pegmatite
 All Classes Functions Variables Enumerations
interpreter.cc
1 #include "ast.hh"
2 #include <strings.h>
3 #include <stdio.h>
4 
5 using namespace AST;
6 
7 namespace Interpreter {
11 struct State
12 {
14  int16_t a[10] = {0};
16  int16_t g[10] = {0};
18  int16_t v = 0;
20  int16_t width = 0;
22  int16_t height = 0;
24  int16_t x = 0;
26  int16_t y = 0;
28  int16_t *grid = 0;
29 };
33 void runOneStep(int16_t *oldgrid,
34  int16_t *newgrid,
35  int16_t width,
36  int16_t height,
37  AST::StatementList *ast)
38 {
39  Interpreter::State state;
40  state.grid = oldgrid;
41  state.width = width;
42  state.height = height;
43  int i=0;
44  for (int x=0 ; x<width ; x++)
45  {
46  for (int y=0 ; y<height ; y++,i++)
47  {
48  state.v = oldgrid[i];
49  state.x = x;
50  state.y = y;
51  bzero(state.a, sizeof(state.a));
52  ast->interpret(state);
53  newgrid[i] = state.v;
54  }
55  }
56 }
57 
58 }
59 
61 {
62  return value;
63 }
65 {
66  assert(registerNumber >= 0 && registerNumber < 10);
67  return s.a[registerNumber];
68 }
70 {
71  assert(registerNumber >= 0 && registerNumber < 10);
72  s.a[registerNumber] = val;
73 }
75 {
76  return s.g[registerNumber];
77 }
79 {
80  s.g[registerNumber] = val;
81 }
83 {
84  return s.v;
85 }
86 void VRegister::assign(Interpreter::State &s, uint16_t val)
87 {
88  s.v = val;
89 }
90 
92 {
93  uint16_t v = value->interpret(s);
94  uint16_t o = target->interpret(s);
95  uint16_t result;
96  switch (op->op)
97  {
98  case Op::Add:
99  result = o+v;
100  break;
101  case Op::Assign:
102  result = v;
103  break;
104  case Op::Sub:
105  result = o-v;
106  break;
107  case Op::Mul:
108  result = o*v;
109  break;
110  case Op::Div:
111  result = o/v;
112  break;
113  case Op::Min:
114  result = std::min(o,v);
115  break;
116  case Op::Max:
117  result = std::max(o,v);
118  break;
119  }
120  target->assign(s, result);
121  return 0;
122 }
123 
125 {
126  uint16_t input = value->interpret(s);
127  for (auto &range : ranges.objects())
128  {
129  uint16_t end = range->end->value;
130  if (range->start.get())
131  {
132  uint16_t start = range->start->value;
133  if ((input >= start) && (input <= end))
134  {
135  return range->value->interpret(s);
136  }
137  }
138  else if (input == end)
139  {
140  return range->value->interpret(s);
141  }
142  }
143  return 0;
144 }
145 
147 {
148  // For each of the (valid) neighbours
149  for (int x = state.x - 1 ; x <= state.x + 1 ; x++)
150  {
151  if (x < 0 || x >= state.width) continue;
152  for (int y = state.y - 1 ; y <= state.y + 1 ; y++)
153  {
154  if (y < 0 || y >= state.height) continue;
155  if (x == state.x && y == state.y) continue;
156  // a0 contains the value for the currently visited neighbour
157  state.a[0] = state.grid[x*state.height + y];
158  // Run all of the statements
159  statements->interpret(state);
160  }
161  }
162  return 0;
163 }
164 
166 {
167  for (auto &s: statements.objects())
168  {
169  s->interpret(state);
170  }
171  return 0;
172 }
int16_t g[10]
The global registers.
Definition: interpreter.cc:16
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:91
pegmatite::ASTPtr< Statement > value
The value to assign to the register.
Definition: ast.hh:230
pegmatite::ASTPtr< Op > op
The operation to perform.
Definition: ast.hh:221
uint16_t value
The value for this literal.
Definition: ast.hh:112
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:146
virtual uint16_t interpret(Interpreter::State &) override
Returns the value in this register (when interpreting the AST).
Definition: interpreter.cc:64
int16_t x
The x coordinate of the current cell.
Definition: interpreter.cc:24
A list of statements that will be executed sequentially.
Definition: ast.hh:94
pegmatite::ASTPtr< Register > value
The register that is being compared against one or more ranges.
Definition: ast.hh:265
int16_t v
The current cell value.
Definition: interpreter.cc:18
int16_t y
The y coordinate of the current cell.
Definition: interpreter.cc:26
The current state for the interpreter.
Definition: interpreter.cc:11
int16_t * grid
The grid itself (non-owning pointer)
Definition: interpreter.cc:28
int16_t a[10]
The local registers.
Definition: interpreter.cc:14
pegmatite::ASTList< Range > ranges
The ranges in this range map.
Definition: ast.hh:269
int16_t height
The height of the grid.
Definition: interpreter.cc:22
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:60
int registerNumber
The number of the global register that this references.
Definition: ast.hh:182
pegmatite::ASTPtr< Register > target
The target register.
Definition: ast.hh:226
int16_t width
The width of the grid.
Definition: interpreter.cc:20
pegmatite::ASTList< Statement > statements
The ordered list of statements.
Definition: ast.hh:99
pegmatite::ASTPtr< StatementList > statements
The statements contained within this neighbours block.
Definition: ast.hh:282
Definition: ast.hh:26
virtual void assign(Interpreter::State &, uint16_t) override
Assigns a value to the register (when interpreting the AST).
Definition: interpreter.cc:69
int registerNumber
The number of the local register that this references.
Definition: ast.hh:164
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:124
virtual uint16_t interpret(Interpreter::State &) override
Returns the value in this register (when interpreting the AST).
Definition: interpreter.cc:74
uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:165
virtual void assign(Interpreter::State &, uint16_t) override
Assigns a value to the register (when interpreting the AST).
Definition: interpreter.cc:78
virtual uint16_t interpret(Interpreter::State &) override
Returns the value in this register (when interpreting the AST).
Definition: interpreter.cc:82
virtual void assign(Interpreter::State &, uint16_t) override
Assigns a value to the register (when interpreting the AST).
Definition: interpreter.cc:86