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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#![warn(missing_docs)]
#![crate_name = "rustws"]

//! 
//! This crate can be used to easily compile and interpret WhiteSpace programs
//! 


mod wsiter;
mod symbol;
mod instruction;
mod error;

/// The errors that can be thrown during runtime
pub use error::Error;

#[allow(unused_imports)]
use symbol::Symbol;
use instruction::{Instruction, Label};
use wsiter::WsIter;
use std::collections::HashMap;


struct Program {
	instructions: Vec<Instruction>,
	labels: std::collections::HashMap<Label, Label>
}

fn compile(code: String, print_instructions: bool) -> Program {
	let mut iter = WsIter::new(code.chars());
	let mut instructions = Vec::<Instruction>::new();
	let mut labels = std::collections::HashMap::new();
	let mut position : Label = 0;
	while let Some(instr) = Instruction::next(&mut iter) {
		if print_instructions { println!("{:?}", instr); }
		match instr {
			Instruction::LABEL(l) => {labels.insert(l, position);},
			i => {
				instructions.push(i); 
				position += 1;
			}
		}
	}

	Program { instructions, labels }
}

/// Current state of the program if no error is thrown
pub enum Status {
	/// No action is needed from outside, next instruction can be called
	Step, 
	/// Print the string 
	Print(String), 
	/// Convert integer to a character and print it
	PrintChar(i32), 
	/// Read an integer from i/o
	ReadInt, 
	/// Read a character fom i/o
	ReadChar, 
	/// The program should quit without any errors. Don't call the step function anymore
	Exit
}
/// The interpreter used for running whitespace
/// 
pub struct Interpreter {
	program: Program,
	pc: Label,
	stack: Vec<i32>,
	flowstack: Vec<Label>,
	heap: HashMap<instruction::Number, instruction::Number>,
}

type RuntimeStatus = Result<Status, Error>;

impl Interpreter {
	/// Create an Interpreter from a source code passed as the argument
	/// 
	/// The print_instructions parameter prints the instructions. This should be set to True when debugging the code
	/// 
	/// # Example
	/// ```
	/// # use rustws::Interpreter;
	/// let source = String::from("   \t\t\n");
	/// let interpreter = Interpreter::new(source, false);
	/// ```
	pub fn new(code: String, print_instructions: bool) -> Interpreter {
		Interpreter {
			program: compile(code, print_instructions),
			pc: 0,
			stack: Vec::new(),
			flowstack: Vec::new(),
			heap: HashMap::new()
		}
	}

	/// Initialize an interpreter from a file
	/// 
	/// The print_instructions parameter prints the instructions. This should be set to True when debugging the code
	/// 
	/// # Example
	/// ```
	/// # use rustws::Interpreter;
	/// let interpreter = Interpreter::from_file("main.ws", false);
	/// ```
	pub fn from_file(file: &str, print_instructions: bool) -> Option<Interpreter> {
		use std::fs;

		if let Ok(code) = fs::read_to_string(file) {
			Some(Interpreter::new(code, print_instructions))
		} else {
			None
		}
	}

	/// Write a value on top of the stack
	/// Should be used when the Status is set to ReadChar/ReadInt
	pub fn write(&mut self, num: i32) { 
		self.stack.push(num) 
	}

	/// Step to the next instructions
	/// 
	/// See Status and Error for the return value
	pub fn step(&mut self) -> RuntimeStatus {
		if let Some(&instr) = self.program.instructions.get(self.pc) {
			self.step_instr(instr)
		} else {
			Err(Error::PC_OUT_OF_BOUNDS)
		}
	}

	/// Current Program Counter
	pub fn pc(&self) -> usize {
		self.pc
	}
}

impl Interpreter {
	fn step_instr(&mut self, instr: Instruction) -> RuntimeStatus {
		match instr {
			Instruction::PUSH(num) => self.instr_push(num),
			Instruction::DUP       => self.instr_dup(),
			Instruction::SWAP      => self.instr_swap(),
			Instruction::DROP      => self.instr_drop(),

			Instruction::ADD       => self.instr_arithmetic(|l, r| Ok(l + r)),
			Instruction::SUB       => self.instr_arithmetic(|l, r| Ok(l - r)),
			Instruction::MUL       => self.instr_arithmetic(|l, r| Ok(l % r)),
			Instruction::DIV       => self.instr_arithmetic(|l, r| if r != 0 {Ok(l / r)} else {Err(Error::DIVISION_BY_ZERO)}),
			Instruction::MOD       => self.instr_arithmetic(|l, r| Ok(l % r)),

			Instruction::SHEAP     => self.instr_sheap(),
			Instruction::RHEAP     => self.instr_rheap(),

			Instruction::CALL(l)   => self.instr_call(l),
			Instruction::JUMP(l)   => self.instr_jump(l),
			Instruction::EJUMP(l)  => self.instr_ifjump(l, |v| v == 0),
			Instruction::NJUMP(l)  => self.instr_ifjump(l, |v| v < 0),
			Instruction::RET       => self.instr_ret(),
			Instruction::EXIT      => Ok(Status::Exit),

			Instruction::PRINTC    => self.instr_printc(),
			Instruction::PRINTN    => self.instr_print(),

			Instruction::READC     => {self.pc += 1; Ok(Status::ReadChar)},
			Instruction::READN     => {self.pc += 1; Ok(Status::ReadInt)},
			_ => Err(Error::UNIMPLEMENTED_INSTRUCTION)
		}
	}


	fn instr_push(&mut self, num: instruction::Number) -> RuntimeStatus {
		self.stack.push(num);
		self.pc += 1;
		Ok(Status::Step)
	}

	fn instr_dup(&mut self) -> RuntimeStatus {
		if let Some(&last) = self.stack.last() {
			self.stack.push(last);
			self.pc += 1;
			Ok(Status::Step)
		} else {
			Err(Error::EMPTY_STACK)
		}
	}

	fn instr_swap(&mut self) -> RuntimeStatus {
		let f = self.stack.pop();
		let s = self.stack.pop();

		match (f, s) {
			(Some(first), Some(second)) => {
				self.stack.push(first); self.stack.push(second);
				self.pc += 1;
				Ok(Status::Step)
			}
			_ => Err(Error::EMPTY_STACK)
		}
	}

	fn instr_drop(&mut self) -> RuntimeStatus {
		if let Some(_) = self.stack.pop() {
			self.pc += 1;
			Ok(Status::Step)
		} else {
			Err(Error::EMPTY_STACK)
		}
	}

	fn instr_arithmetic(&mut self, fun: fn(i32, i32) -> Result<i32, Error>) -> RuntimeStatus {
		let right = self.stack.pop();
		let left = self.stack.pop();
		match (left, right) {
			(Some(l), Some(r)) => {
				match fun(l, r) {
					Ok(res) => {
						self.stack.push(res); 
						self.pc += 1;
						Ok(Status::Step)},
					Err(err) => Err(err),
				}
			},
			_ => Err(Error::EMPTY_STACK)
		}
	}


	fn instr_sheap(&mut self) -> RuntimeStatus {
		let value = self.stack.pop();
		let addr = self.stack.pop();
		match (addr, value) {
			(Some(a), Some(v)) => {
				self.heap.insert(a, v);
				self.pc += 1;
				Ok(Status::Step)
			},
			_ => Err(Error::EMPTY_STACK)
		}
	}

	fn instr_rheap(&mut self) -> RuntimeStatus {
		if let Some(addr) = self.stack.pop() {
			if let Some(&val) = self.heap.get(&addr) {
				self.stack.push(val);
				self.pc += 1;
				Ok(Status::Step)
			} else {
				Err(Error::UNDEFINED_HEAP_ADDRESS)
			}
		} else {
			Err(Error::EMPTY_STACK)
		}
	}

	fn to_label(&mut self, label: Label) -> RuntimeStatus {
		if let Some(&l) = self.program.labels.get(&label) {
			self.pc = l;			
			Ok(Status::Step)
		} else {
			Err(Error::UNDEFINED_LABEL)
		}
	}

	fn instr_call(&mut self, label: Label) -> RuntimeStatus {
		self.flowstack.push(self.pc);
		self.to_label(label)
	}

	fn instr_jump(&mut self, label: Label) -> RuntimeStatus {
		self.to_label(label)
	}
	
	fn instr_ifjump(&mut self, label: Label, fun: fn(i32) -> bool) -> RuntimeStatus {
		if let Some(&last) = self.stack.last() {
			if let Some(&l) = self.program.labels.get(&label) {
				if fun(last) {
					self.pc = l;
				} else {
					self.pc += 1;
				}
				Ok(Status::Step)
			} else {
				Err(Error::UNDEFINED_LABEL)
			}
		} else {
			Err(Error::EMPTY_STACK)
		}
	}

	fn instr_ret(&mut self) -> RuntimeStatus {
		if let Some(&last) = self.flowstack.last() {
			self.pc = last;
			Ok(Status::Step)
		} else {
			Err(Error::EMPTY_FLOW_STACK)
		}
	}

	fn instr_printc(&mut self) -> RuntimeStatus {
		if let Some(last) = self.stack.pop() {
			self.pc += 1;
			Ok(Status::PrintChar(last))
		} else {
			Err(Error::EMPTY_STACK)
		}

	}

	fn instr_print(&mut self) -> RuntimeStatus {
		if let Some(last) = self.stack.pop() {
			self.pc += 1;
			Ok(Status::Print(last.to_string()))
		} else {
			Err(Error::EMPTY_STACK)
		}
	}

}