-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpret.rb
190 lines (188 loc) · 6.16 KB
/
interpret.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
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
if __FILE__ == $0
puts "Oops, did you mean to run HiveStart.rb instead?"
exit
end
def HIVE_interpret(cmd, isLocal, interactive, lVarStore, hVarStore)
if cmd.start_with?("var")
lVarStore[cmd.split(' ')[1]] = cmd.split(' ')[2]
elsif cmd.start_with?("hivevar")
hVarStore[cmd.split(' ')[1]] = cmd.split(' ')[2]
elsif cmd.start_with?("is")
if(cmd.split(' ')[1].start_with?("var_") and cmd.split(' ')[2].start_with?("var_"))
lVarStore[cmd.split(' ')[1]] = lVarStore[cmd.split(' ')[2]]
elsif(cmd.split(' ')[1].start_with?("hvar_") and cmd.split(' ')[2].start_with?("var_"))
hVarStore[cmd.split(' ')[1]] = lVarStore[cmd.split(' ')[2]]
elsif(cmd.split(' ')[1].start_with?("var_") and cmd.split(' ')[2].start_with?("hvar_"))
lVarStore[cmd.split(' ')[1]] = hVarStore[cmd.split(' ')[2]]
else
hVarStore[cmd.split(' ')[1]] = hVarStore[cmd.split(' ')[2]]
end
elsif cmd.start_with?("%")
#Do nothing, this is a comment
else
cmd2 = cmd.split(' ')
indexi = 0
while indexi < cmd2.length
if cmd2[indexi].start_with?("var_")
cmd2[indexi] = lVarStore[cmd2[indexi]]
elsif cmd2[indexi].start_with?("hvar_")
cmd2[indexi] = hVarStore[cmd2[indexi]]
end
indexi = indexi + 1
end
cmd = " "
indexi = 0
while indexi < cmd2.count
#HIVE_print(indexi.to_s)
#cmd = cmd + cmd2[indexi] + " "
cmd = "#{ cmd } #{ cmd2[indexi] }"
indexi = indexi + 1
end
end
cmd = cmd.strip
if cmd.start_with?("print")
if isLocal
cmd.slice! "print"
HIVE_print(cmd)
end
elsif cmd.start_with?("var")
#varStore[cmd.split(' ')[1]] = cmd.split(' ')[2]
elsif cmd.split(' ')[0] == "sys"
if cmd.split(' ')[1] == "sudo"
HIVE_print("Use of sudo is not allowed")
elsif cmd.split(' ')[1] == "su"
HIVE_print("Logging in as root is not allowed")
else
cmd['sys '] = ''
require 'rbconfig'
include RbConfig
case CONFIG['host_os']
when /mswin|windows/i
# Windows
HIVE_print("The SYS command is not yet supported on your platform.")
when /linux|arch/i
# Linux
system(cmd)
when /sunos|solaris/i
# Solaris
HIVE_print("The SYS command is not yet supported on your platform.")
when /darwin/i
#MAC OS X
HIVE_print("The SYS command is not yet supported on your platform.")
else
# whatever
HIVE_print("The SYS command is not yet supported on your platform.")
end
end
elsif cmd.split(' ')[0] == "sysc"
if cmd.split(' ')[1] == "shutdown"
require 'rbconfig'
include RbConfig
case CONFIG['host_os']
when /mswin|windows/i
# Windows
system("shutdown -s -f -t 10")
when /linux|arch/i
# Linux
system("sudo halt")
when /sunos|solaris/i
# Solaris
HIVE_print("The SYSc command is not yet supported on your platform.")
when /darwin/i
#MAC OS X
HIVE_print("The SYSc command is not yet supported on your platform.")
else
# whatever
HIVE_print("The SYSc command is not yet supported on your platform.")
end
elsif cmd.split(' ')[1] == "install"
require 'rbconfig'
include RbConfig
case CONFIG['host_os']
when /mswin|windows/i
# Windows
HIVE_print("The SYSc command is not yet supported on your platform.")
when /linux|arch/i
# Linux
HIVE_print(system("sudo apt-get install " + cmd.split(' ')[2]))
when /sunos|solaris/i
# Solaris
HIVE_print("The SYSc command is not yet supported on your platform.")
when /darwin/i
#MAC OS X
HIVE_print("The SYSc command is not yet supported on your platform.")
else
# whatever
HIVE_print("The SYSc command is not yet supported on your platform.")
end
end
elsif cmd.start_with?("#")
#This is a directive
dcmd = cmd.split(' ')[0]
dcmd2 = cmd.split(' ')[1]
dcmd.slice!("#")
if dcmd == "HIVE"
#For now, just print version
dcmd.slice!('HIVE:')
if dcmd2.split('.')[3].to_i > lVarStore["var_hiveenv_version"].split('.')[3].to_i
HIVE_print("Insufficient version. You have version #{ lVarStore["var_hiveenv_version"] }, this program requires #{ dcmd2 }")
require './hivelib/warning'
declare_warning("DIRECTIVE:VERSION:INSUFFICIENTVERSION", isLocal)
#terminate the program
else
HIVE_print("This program requires HIVE v" + cmd.split(' ')[1].to_s)
HIVE_print("You have HIVE v" + lVarStore["var_hiveenv_version"].to_s)
end
else
HIVE_print("ERROR: bad directive")
end
elsif cmd.split(' ')[0].split('>')[0] == "math"
if cmd.split(' ')[0].split('>')[1] == "add"
require "./hivelib/math.rb"
result = add(cmd.split(' ')[1].to_i, cmd.split(' ')[2].to_i)
if isLocal
HIVE_print(result)
else
return result
end
elsif cmd.split(' ')[0].split('>')[1] == "sub"
require "./hivelib/math.rb"
result = subtract(cmd.split(' ')[1].to_i, cmd.split(' ')[2].to_i)
if isLocal
HIVE_print(result)
else
return result
end
elsif cmd.split(' ')[0].split('>')[1] == "mul"
require "./hivelib/math.rb"
result = multiply(cmd.split(' ')[1].to_i, cmd.split(' ')[2].to_i)
if isLocal
HIVE_print(result)
else
return result
end
elsif cmd.split(' ')[0].split('>')[1] == "div"
require "./hivelib/math.rb"
result = divide(cmd.split(' ')[1].to_i, cmd.split(' ')[2].to_i)
if isLocal
HIVE_print(result)
else
return result
end
end
elsif cmd.start_with?("pause")
HIVE_print("Pausing")
if cmd.split(' ')[1].to_i == nil
sleep(1)
else
sleep(cmd.split(' ')[1].to_i)
end
return nil
else
return "PARSEERROR"
end
end
def HIVE_print(line)
a = line.to_s.strip
puts "[HIVEPARSE]: " + a
end