-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTape.rb
82 lines (71 loc) · 1.3 KB
/
Tape.rb
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
$LOAD_PATH << './'
require 'Node.rb'
require 'Exception.rb'
class Tape
attr_accessor :current
attr_accessor :head
attr_reader :alphabet
attr_reader :position
def initialize(alph)
@head= Node.new
head.setElem(EMPTY)
@current = head
@alphabet = alph
@position = 0
end
def fillTape(init)
init.each_char do |e|
if alphabet.include? e.to_s
node = Node.new
node.setElem(e)
current.setNext(node)
@current = node
else
raise SymbolException.new("Symbol \'" + e + "\' not in alphabet")
end
end
@current = head.next
end
def putSymbol(s)
if alphabet.include? s.to_s
current.setElem(s)
else
raise SymbolException.new( "Symbol \'" + e + "\' not in alphabet")
end
end
def leftMove
if current.prev != nil
@current = current.prev
@position-=1
else
node = Node.new
node.setNext(current)
@current = node
@head = current
@position = 0
end
end
def rightMove
if current.next != nil
@current = current.next
else
node = Node.new
node.setPrev(current)
@current = node
end
@position+=1
end
def getUnderCur
return current.elem
end
def toString
cur = head.next
str_buf=[]
str_buf.push(position)
while cur != nil && cur.elem != nil
str_buf.push(cur.elem)
cur = cur.next
end
return str_buf
end
end