Class: Scratch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScratch

Returns a new instance of Scratch.



27
28
29
30
# File 'lib/lem.rb', line 27

def initialize
	@dictionary = {}
	@stack = []
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



25
26
27
# File 'lib/lem.rb', line 25

def stack
  @stack
end

Instance Method Details

#add_words(dict) ⇒ Object



32
33
34
35
36
# File 'lib/lem.rb', line 32

def add_words(dict)
	dict.each do |k,v|
		@dictionary[k] = v
	end
end

#run(text) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lem.rb', line 38

def run(text)
	@lexer = ScratchLexer.new(text)
	@lexer.each do |word|
		word.upcase!
		if word.is_num?
			@stack.push word.to_f
		elsif @dictionary[word]
			@dictionary[word].call self
		else
			raise "Unknown word"
		end
	end
end