-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.rb
135 lines (107 loc) · 2.52 KB
/
state.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
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
module LookupTable
attr_reader :lt
def initialize
@lt = {}
end
def store(name, value)
@lt.store(name.downcase.to_sym, value)
end
def get(name)
@lt.fetch(name.downcase.to_sym, "undefined")
end
def delete(name)
@lt.delete(name.downcase.to_sym)
end
def map(&block)
@lt.map &block
end
def keys
@lt.keys
end
def length
@lt.length
end
def to_s
@lt.to_s
end
end
class VariableLookupTable
include LookupTable
def get(name)
case name.downcase.to_sym
when :date
Time.new.strftime("%Y-%m-%d")
when :time
Time.new.strftime("%H:%M:%S")
when :random
rand(0..32767).to_s
when :username
`whoami`.chomp
else
@lt.fetch(name.downcase.to_sym, "undefined")
end
end
end
class LabelLookupTable
include LookupTable
end
class FileLookupTable
include LookupTable
def store(name, value)
@lt.store(name.delete('"').to_sym, value)
end
def get(name)
@lt.fetch(name.to_sym, "undefined")
end
def delete(name)
@lt.delete(name.to_sym)
end
def append(name, content)
orig_content = ""
orig_content = self.get(name) if self.file_exists?(name)
self.store(name, orig_content + content + "\n")
end
def file_exists?(name)
get(name) != "undefined"
end
def delete(name)
delete(name)
end
def copy(source, dest)
src = get(source) # Explicitly choosing not to error on non-existent source
store(dest, src.dup)
end
def create(name)
store(name, "")
end
def read(name)
content = get(name)
content != "undefined" ? content : "\n"
end
def overwrite(name, content)
store(name, content)
end
def write(name, content)
overwrite(name, content)
end
end
class SubroutineLookupTable
include LookupTable
def store(name, value)
name = "MAIN" if name.empty? or name.nil?
@lt.store(name.downcase.delete('"').to_sym, value)
end
def get(name)
@lt.fetch(name.downcase.delete('"').to_sym, nil)
end
def default
raise "No programs loaded!" unless @lt.any?
@lt.first[1]
end
def entry_point(global_entry)
ep = get(global_entry) unless global_entry.nil?
ep = get("MAIN") if ep.nil?
ep = default() if ep.nil?
ep
end
end