Class: Mapleseed::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mapleseed.rb

Overview

loads and interprets Whirl code

Instance Method Summary collapse

Constructor Details

#initializeBase

initialize the interpreter



8
9
10
# File 'lib/mapleseed.rb', line 8

def initialize
	@interpreter = Interpreter.new
end

Instance Method Details

#load_file(file_path) ⇒ Object

load code from a file and run it



18
19
20
21
# File 'lib/mapleseed.rb', line 18

def load_file(file_path)
	code = File.open(file_path) { |f| f.read }
	run(code)
end

#replObject

interactive read-evaluate-print loop



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
# File 'lib/mapleseed.rb', line 24

def repl
	puts "'exit' or Ctrl-X leaves the REPL"
	puts "'op' gives information about the operation ring position and value"
	puts "'math' gives information about the math ring position and value"
	puts "'mem' gives information about the current cell of memory"
	puts "'reset' clears the memory and resets the rings"
	while true
		puts ""
		print "mapleseed> "
		input = gets.chomp
		case input
		when "exit", 24.chr
			puts "Exiting"
			break
		when "op"
			puts "Operations Ring:" 
			puts "Position: #{@interpreter.op_ring.position} Value: #{@interpreter.op_ring.value}"
			dir = ""
			@interpreter.op_ring.direction == 1 ? dir = "Clockwise" : dir = "Counter-clockwise"
			puts "Direction: #{dir} Current operation: #{@interpreter.op_ring.commands[@interpreter.op_ring.position].to_s}"
		when "math"
			puts "Math Ring:" 
			puts "Position: #{@interpreter.math_ring.position} Value: #{@interpreter.math_ring.value}"
			dir = ""
			@interpreter.math_ring.direction == 1 ? dir = "Clockwise" : dir = "Counter-clockwise"
			puts "Direction: #{dir} Current operation: #{@interpreter.math_ring.commands[@interpreter.math_ring.position].to_s}"
		when "mem"
			puts "Cell: #{@interpreter.memory_position} Value: #{@interpreter.memory.get(@interpreter.memory_position)}"
		when "reset"
			@interpreter.initialize_environment
		else
			@interpreter.run(input)
		end
	end
end

#run(code) ⇒ Object

run the given code



13
14
15
# File 'lib/mapleseed.rb', line 13

def run(code)
	@interpreter.run(code)
end