Arduino-FVM
Byte Token Threaded Forth Virtual Machine (FVM) for Arduino
Interpret.ino
Go to the documentation of this file.
1 
46 #include "FVM.h"
47 
48 // : array ( n -- ) create cells allot
49 // does> ( n -- a-addr ) swap cells plus ;
50 FVM_COLON(0, ARRAY, "(array)")
51  FVM_OP(DOES),
52  FVM_OP(SWAP),
53  FVM_OP(CELLS),
54  FVM_OP(PLUS),
55  FVM_OP(EXIT)
56 };
57 
58 // : 2constant ( x1 x2 -- ) create swap , ,
59 // does> ( -- x1 x2 ) dup @ swap cell + @ ;
60 FVM_COLON(1, TWO_CONSTANT, "(2constant)")
61  FVM_OP(DOES),
62  FVM_OP(DUP),
63  FVM_OP(FETCH),
64  FVM_OP(SWAP),
65  FVM_OP(CELL),
66  FVM_OP(PLUS),
67  FVM_OP(FETCH),
68  FVM_OP(EXIT)
69 };
70 
71 // variable x
72 // 42 x !
73 int x = 42;
74 FVM_VARIABLE(2, X, x);
75 
76 // -42 constant y
77 FVM_CONSTANT(3, Y, "y", -42);
78 
79 // 4 array z
80 int z[] = { 1, 2, 4, 8 };
81 FVM_CREATE(4, Z, ARRAY, z);
82 
83 // 1 2 2constant c2
84 int c2[] = { 1, 2 };
85 FVM_CREATE(5, C2, TWO_CONSTANT, c2);
86 
87 // create pad 32 allot
88 const int PAD_MAX = 32;
89 char pad[PAD_MAX];
90 FVM_VARIABLE(6, PAD, pad);
91 
92 // external void numbers ( -- 1 2 3 a-addr +n )
93 void numbers(FVM::task_t &task, void* env)
94 {
95  task.push(1);
96  task.push(2);
97  task.push(3);
98  task.push((FVM::cell_t) env);
99  task.push(task.depth());
100 }
101 FVM_FUNCTION(7, NUMBERS, numbers, pad);
102 
103 // 1 constant OUTPUT
104 FVM_CONSTANT(8, OUTPUT_MODE, "OUTPUT", 1);
105 
106 // 13 constant LED
107 FVM_CONSTANT(9, LED_PIN, "LED", 13);
108 
109 // : blink ( ms pin n -- ) 0 do dup digitaltoggle over delay loop 2drop ;
110 FVM_COLON(10, BLINK, "blink")
111  FVM_OP(ZERO),
112  FVM_OP(DO), 7,
113  FVM_OP(DUP),
114  FVM_OP(DIGITALTOGGLE),
115  FVM_OP(OVER),
116  FVM_OP(DELAY),
117  FVM_OP(LOOP), -5,
118  FVM_OP(TWO_DROP),
119  FVM_OP(EXIT)
120 };
121 
122 const FVM::code_P FVM::fntab[] PROGMEM = {
123  (code_P) ARRAY_CODE,
124  (code_P) TWO_CONSTANT_CODE,
125  (code_P) &X_VAR,
126  (code_P) &Y_CONST,
127  (code_P) &Z_VAR,
128  (code_P) &C2_VAR,
129  (code_P) &PAD_VAR,
130  (code_P) &NUMBERS_FUNC,
131  (code_P) &OUTPUT_MODE_CONST,
132  (code_P) &LED_PIN_CONST,
133  (code_P) BLINK_CODE,
134 };
135 
136 const str_P FVM::fnstr[] PROGMEM = {
137  (str_P) ARRAY_PSTR,
138  (str_P) TWO_CONSTANT_PSTR,
139  (str_P) X_PSTR,
140  (str_P) Y_PSTR,
141  (str_P) Z_PSTR,
142  (str_P) C2_PSTR,
143  (str_P) PAD_PSTR,
144  (str_P) NUMBERS_PSTR,
145  (str_P) OUTPUT_MODE_PSTR,
146  (str_P) LED_PIN_PSTR,
147  (str_P) BLINK_PSTR,
148  0
149 };
150 
151 const int DATA_MAX = 1024;
152 uint8_t data[1024];
153 
154 FVM fvm(data, DATA_MAX);
155 FVM::Task<32,16> task(Serial);
156 
157 void setup()
158 {
159  Serial.begin(57600);
160  while (!Serial);
161  Serial.println(F("FVM/Interpret: started [Newline]"));
162 }
163 
164 void loop()
165 {
166  fvm.interpret(task);
167 }
char pad[PAD_MAX]
Definition: Interpret.ino:89
const int DATA_MAX
Definition: Interpret.ino:151
uint8_t data[1024]
Definition: Interpret.ino:152
FVM_FUNCTION(7, NUMBERS, numbers, pad)
int c2[]
Definition: Interpret.ino:84
static const code_P fntab[]
Definition: FVM.h:628
void numbers(FVM::task_t &task, void *env)
Definition: Interpret.ino:93
int depth()
Definition: FVM.h:303
Definition: FVM.h:339
FVM fvm(data, DATA_MAX)
FVM_CREATE(4, Z, ARRAY, z)
const char * str_P
Definition: FVM.h:32
const code_t * code_P
Definition: FVM.h:249
int x
Definition: Interpret.ino:73
static const str_P fnstr[]
Definition: FVM.h:629
const int PAD_MAX
Definition: Interpret.ino:88
void setup()
Definition: Interpret.ino:157
void loop()
Definition: Interpret.ino:164
int interpret(task_t &task)
Definition: FVM.cpp:1848
FVM_OP(DOES)
int z[]
Definition: Interpret.ino:80
void push(cell_t value)
Definition: FVM.h:285
FVM_VARIABLE(2, X, x)
#define FVM_COLON(id, var, name)
Definition: FVM.h:728
int32_t cell_t
Definition: FVM.h:235
Definition: FVM.h:34
FVM::Task< 32, 16 > task(Serial)
FVM_CONSTANT(3, Y,"y",-42)