Last active
August 7, 2016 20:07
-
-
Save thomasjslone/d565d997d3473fef697ca23e3695da9d to your computer and use it in GitHub Desktop.
Virtual Abstraction Processor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# this was an attempt of making the vap program more efficient and compact | |
# this processor works almost the same but loops and branches are all one line operations, however there is still clock control and labels because technically | |
# this is running afc 1.2, an updated version of afc that works the same way. | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# | |
class Processor | |
def initialize | |
@homedir = Dir.getwd.to_s | |
@clock = 0 #keeps track of a task loads paticular instruction | |
@jump_labes = [] | |
@feedback = [] #holds feedback and exceptions | |
@exceptions = false #indicates exceptions to display | |
@return_value = nil #returns a value from task load | |
@data = [] #holds memory for the processor | |
@address = [] #allocates variable names to the labels, to not interfere with common strings and values, use format: 0x00x00 | |
end | |
def shell_in(inp) #this lets the shell app use this class | |
return exe(inp).to_s | |
end | |
def exe(inst) # this is the main evaluate function, it contains all the heavy code | |
@feedback = [] | |
@exceptions = false | |
@return_value = nil | |
@instructions = [] | |
@jumps = [] | |
@loop = false | |
@loops = [] | |
@clock = 0 | |
if inst.include?("\n") | |
insts = inst.split("\n").join(":.:").to_s | |
@instructions = insts.split(":.:").to_a | |
else | |
if inst.include?(":.:") | |
@instructions = inst.split(":.:").to_a | |
else | |
@instructions = [inst.to_s] | |
end | |
end | |
@run = true | |
while @run | |
if @instructions.length.to_i > @clock.to_i | |
inst = @instructions[@clock.to_i].to_s | |
else | |
@run = false | |
break | |
end | |
if inst.to_s[0] == "<" and inst.to_s[-1] == ";" | |
res = "" | |
begin | |
res = eval(inst.to_s[1..-2].to_s) | |
rescue | |
res = "Error: Code Crashed" | |
end | |
@return_value = res | |
elsif inst.to_s.split(":")[0].to_s == "print" | |
v = inst.split(":")[1].to_s | |
if v.to_s[0] == "<" and v.to_s[-1] == ";" | |
begin | |
print eval(v.to_s[1..-2].to_s).to_s | |
rescue | |
@exceptions = true | |
@feedback << "Error: Could not print result of ruby code because it crashed: " + v.to_s | |
end | |
elsif v.to_s[0] == "{" and v.to_s[-1] == "}" | |
res = self.exe(v.to_s[1..-2].to_s) | |
if res.to_s[0..11] != "EXCEPTIONS: " | |
print res.to_s | |
else | |
@exceptions = true | |
@feedback << "Error: Could not print result of afc code because of exception: " + res.to_s | |
end | |
else | |
print v.to_s | |
end | |
elsif inst.to_s[0] == "$" and inst.to_s.include?("=") and inst.to_s[-1] == "!" ################## $ = ! | |
addr = inst.to_s[1..-1].split("=")[0].to_s | |
if addr.to_s[0] == "<" and addr.to_s[-1] == ";" | |
begin | |
addr = [eval(addr.to_s[1..-2].to_s).to_s] | |
rescue | |
@exception = true | |
@feedback << "Error: memory assignment name error" | |
end | |
elsif addr.to_s[0] == "{" and addr.to_s[-1] == "}" | |
addr = [self.exe(addr.to_s[1..-2]).to_s] | |
elsif addr.include?(",") | |
addr = addr.split(",").to_a | |
else | |
addr = [addr.to_s] | |
end | |
val = inst.split("=")[1..-1].join("=").to_s | |
val = val[0..-2].to_s | |
if val.to_s[0] == "<" and val.to_s[-1] == ";" | |
begin | |
val = eval(val[1..-2].to_s) | |
rescue | |
val = "NULL_ERROR" | |
end | |
elsif val.to_s[0] == "{" and val.to_s[-1] == "}" | |
val = self.exe(val.to_s[1..-2].to_s) | |
else | |
if rmem(val).to_s != "[NULL_ERROR]" | |
val = rmem(val) | |
else | |
val = val | |
end | |
end | |
if addr.length > 1 | |
addr.each do |ad| | |
wmem(ad.to_s,val) | |
end | |
else | |
wmem(addr[0].to_s,val) | |
end | |
elsif inst.to_s[0] == "$" ################## $ ################## | |
addr = inst.to_s[1..-1].to_s | |
if addr.to_s[0] == "<" and addr.to_s[-1] == ";" | |
addr = addr[1..-2].to_s | |
begin | |
addr = eval(addr.to_s).to_s | |
val = rmem(addr.to_s) | |
if val.to_s != "[NULL_ERROR]" | |
@return_value = rmem(addr.to_s) | |
else | |
@exceptions = true | |
@feedback << "Error: Address does not exist: " + addr.to_s | |
end | |
rescue | |
@exceptions = true | |
@feedback << "Error: rmem address ruby code processing crashed: " + addr.to_s | |
end | |
elsif addr.to_s[0] == "{" and addr.to_s == "}" | |
if self.exe(addr[1..-2].to_s).to_s[0..11] != "EXCEPTIONS: " | |
addr = self.exe(addr[1..-2].to_s).to_s | |
if rmem(addr).to_s != "[NULL_ERROR]" | |
@return_value = rmem(addr) | |
else | |
@exceptions = true | |
@feedback << "Error: Address does not exist: " + addr.to_s | |
end | |
else | |
@exceptions = true | |
@feedback << "Error: rmem address afc processing returned an exception. : " + self.exe(addr[1..-2].to_s).to_s | |
end | |
elsif rmem(addr.to_s) != "[NULL_ERROR]" | |
@return_value = rmem(addr.to_s) | |
else | |
@exceptions = true | |
@feedback << "Error: Address does not exist: " + addr.to_s | |
end | |
elsif inst.to_s[0..1].to_s == "!$" ################## !$ ################## | |
addr = inst.to_s[2..-1] | |
if addr.to_s[0] == "<" and addr.to_s[-1] == ";" | |
begin | |
addr = eval(addr.to_s[1..-2].to_s).to_s | |
if dmem(addr.to_s).to_s != "[NULL_ERROR]" | |
else | |
@exceptions = true | |
@feedback << "Error: Delete Memory Address Non-existent: " + addr.to_s | |
end | |
rescue | |
@exceptions = true | |
@feedback << "Error: Delete Address Ruby Expression Failed: " + addr[1..-2].to_s | |
end | |
elsif addr.to_s[0] == "{" and addr.to_s[-1] == "}" | |
addr = self.exe(addr.to_s[1..-2].to_s).to_s | |
if dmem(addr.to_s).to_s != "[NULL_ERROR]" | |
else | |
@exceptions = true | |
@feedback << "Error: Delete Memory Address Non-existent: " + addr.to_s | |
end | |
elsif addr.to_s.include?(",") | |
addr = addr.to_s.split(",") | |
addr.each do |ad| | |
if dmem(ad).to_s != "NULL_ERROR" | |
else | |
@exceptions = true | |
@feedback << "Error: Delete Memory Addres Non-existent: " + ad.to_s | |
end | |
end | |
else | |
if dmem(addr.to_s).to_s != "[NULL_ERROR]" | |
else | |
@exceptions = true | |
@feedback << "Error: Delete Memory Addren Non-existent: " + addr.to_s | |
end | |
end | |
elsif inst.to_s.split(":")[0].to_s == "glob" ########### GLOB ################## | |
term = inst.to_s.split(":")[1] | |
if term.to_s == "time" | |
@return_value = Time.now.to_s.split(" ")[1].to_s.split(":").join("").to_s | |
elsif term.to_s == "date" | |
@return_value = Time.now.to_s.split(" ")[0].to_s.split("-").join("").to_s | |
elsif term.to_s == "pi" | |
elsif term.to_s == "c" | |
elsif term.to_s == "e" | |
elsif term.to_s == "homedir" | |
else | |
end | |
elsif inst.to_s[0..2] == "add" ################# ADD ################### | |
items = inst.to_s.split(":")[1..-1].join(":").to_s | |
if items.include?(",") | |
items = items.to_s.split(",") | |
else | |
items = [items] | |
end | |
ritems = [] | |
items.each do |item| | |
if item.to_s[0] == "<" and item.to_s[-1] == ";" | |
ritems << eval(item.to_s[1..-2].to_s).to_s | |
elsif item.to_s[0] == "{" and item.to_s[-1] == "}" | |
ritems << self.exe(item.to_s[1..-2].to_s).to_s | |
elsif rmem(item.to_s).to_s != "[NULL_ERROR]" | |
ritems << rmem(item.to_s).to_s | |
else | |
ritems << item.to_i | |
end | |
end | |
integers = [] | |
ritems.each {|i| integers << i.to_i } | |
base = integers[0].to_i | |
integers.delete_at(0) | |
integers.each { |i| base += i.to_i } | |
@return_value = base.to_i | |
elsif inst.to_s.split(":")[0].to_s == "sub" ################ SUB ################### | |
items = inst.to_s.split(":")[1..-1].join(":").to_s | |
if items.include?(",") | |
items = items.to_s.split(",") | |
else | |
items = [items] | |
end | |
ritems = [] | |
items.each do |item| | |
if item.to_s[0] == "<" and item.to_s[-1] == ";" | |
ritems << eval(item.to_s[1..-2].to_s).to_s | |
elsif item.to_s[0] == "{" and item.to_s[-1] == "}" | |
ritems << self.exe(item.to_s[1..-2].to_s).to_s | |
elsif rmem(item.to_s).to_s != "[NULL_ERROR]" | |
ritems << rmem(item.to_s).to_s | |
else | |
ritems << item.to_i | |
end | |
end | |
integers = [] | |
ritems.each {|i| integers << i.to_i } | |
base = integers[0].to_i | |
integers.delete_at(0) | |
integers.each { |i| base -= i.to_i } | |
@return_value = base.to_i | |
elsif inst.to_s.split(":")[0].to_s == "mul" ################# MUL #################### | |
items = inst.to_s.split(":")[1..-1].join(":").to_s | |
if items.include?(",") | |
items = items.to_s.split(",") | |
else | |
items = [items] | |
end | |
ritems = [] | |
items.each do |item| | |
if item.to_s[0] == "<" and item.to_s[-1] == ";" | |
ritems << eval(item.to_s[1..-2].to_s).to_s | |
elsif item.to_s[0] == "{" and item.to_s[-1] == "}" | |
ritems << self.exe(item.to_s[1..-2].to_s).to_s | |
elsif rmem(item.to_s).to_s != "[NULL_ERROR]" | |
ritems << rmem(item.to_s).to_s | |
else | |
ritems << item.to_i | |
end | |
end | |
integers = [] | |
ritems.each {|i| integers << i.to_i } | |
base = integers[0].to_i | |
integers.delete_at(0) | |
integers.each { |i| base *= i.to_i } | |
@return_value = base.to_i | |
elsif inst.to_s.split(":")[0].to_s == "div" ################## DIV ###################### | |
items = inst.to_s.split(":")[1..-1].join(":").to_s | |
if items.include?(",") | |
items = items.to_s.split(",") | |
else | |
items = [items] | |
end | |
ritems = [] | |
items.each do |item| | |
if item.to_s[0] == "<" and item.to_s[-1] == ";" | |
ritems << eval(item.to_s[1..-2].to_s).to_s | |
elsif item.to_s[0] == "{" and item.to_s[-1] == "}" | |
ritems << self.exe(item.to_s[1..-2].to_s).to_s | |
elsif rmem(item.to_s).to_s != "[NULL_ERROR]" | |
ritems << rmem(item.to_s).to_s | |
else | |
ritems << item.to_i | |
end | |
end | |
integers = [] | |
ritems.each {|i| integers << i.to_i } | |
base = integers[0].to_i | |
integers.delete_at(0) | |
integers.each { |i| base /= i.to_i } | |
@return_value = base.to_i | |
elsif inst.to_s.split(":")[0].to_s == "sqrt" ################# SQRT ###################### | |
elsif inst.to_s.split(":")[0].to_s == "exp" ################# EXP ####################### | |
elsif inst.to_s.split(":")[0].to_s == "fac" ################### FAC ###################### | |
elsif inst.to_s[0..4].to_s == "prand" ################# PRAND #################### | |
range = false | |
val = 1 | |
if inst.include?(":") | |
params = inst.split(":")[1..-1].join(":").to_s | |
if params.length == 1 | |
val = params[0].to_s | |
elsif params.length == 2 | |
val = params[0].to_s + ":" + params[1].to_s | |
end | |
else | |
val = "1000000000000" | |
end | |
if range | |
@return_value = rand(val.split(":")[0].to_i,val.split(":")[1].to_i).to_i | |
else | |
@return_value = rand(val.to_i).to_i | |
end | |
elsif inst.to_s.split(":")[0].to_s == "even" ################### EVEN #################### | |
objs = inst.split(":")[1..-1].join(":").to_s | |
robjs = [] | |
objs.each do |obj| | |
if obj.to_s[0] == "<" and obj.to_s[-1] == ";" | |
elsif obj.to_s[1] == "{" and obj.to_s[-1] == "}" | |
end | |
end | |
elsif inst.to_s.split(":")[0].to_s == "odd" ################## ODD ################### | |
elsif inst.to_s.split(":")[0].to_s == "prime" ################# PRIME ################# | |
elsif inst.to_s.split(":")[0].to_s == "zero" #################### ZERO ############## | |
elsif inst.to_s.split(":")[0].to_s == "positive" ############### POS ################# | |
elsif inst.to_s.split(":")[0].to_s == "nagative" ############# NEG ########### | |
elsif inst.to_s.split(":")[0].to_s == "decimal" ################ DEC ################ | |
elsif inst.to_s.split(":")[0].to_s == "whole" ################## WHOLE ################# | |
elsif inst[0].to_s == "@" and inst.to_s.include?("=") and inst.to_s[-1] == "!" ################### @ = ################## | |
addr = inst[1..-1].to_s.split("=")[0].to_s | |
code = inst.to_s.split("=")[1..-1].join("=").to_s[0..-2].to_s | |
if addr.to_s[0] == "<" and addr.to_s[-1] == ";" | |
begin | |
addr = eval(addr.to_s[1..-2].to_s).to_s | |
rescue | |
@exceptions = true | |
@feedback << "Error: Function Assignment Address Ruby Expression Failed: " + addr.to_s | |
end | |
elsif addr.to_s[0] == "{" and addr.to_s[-1] == "}" | |
addr = self.exe(addr.to_s[1..-2]).to_s | |
end | |
wmem(addr,code.to_s) | |
elsif inst.to_s[0].to_s == "@" ################### @ ################## | |
addr = inst.to_s[1..-1] | |
if addr.to_s[0] == "<" and addr.to_s[-1] == ";" | |
begin | |
addr = eval(addr.to_s[1..-2].to_s).to_s | |
rescue | |
@exceptions = true | |
@feedback << "Error: Function Call Address Ruby Expression Failed: " + addr.to_s | |
end | |
elsif addr.to_s[0] == "{" and addr.to_s[-1] == "}" | |
addr = self.exe(addr.to_s[1..-2]).to_s | |
end | |
if rmem(addr.to_s).to_s != "[NULL_ERROR]" | |
code = rmem(addr.to_s).to_s | |
@return_value = self.exe(code.to_s) | |
else | |
@exceptions = true | |
@feedback << "Error: Function Call Address Lookup Failed: " + addr.to_s | |
end | |
elsif inst.to_s[0..1] == "if" ################### IF ################## | |
# if:ex:ex:code:els;if:ex:ex:code:else:code | |
if inst.include?(":else:") | |
else_code = inst.split(":else:")[-1].to_s | |
rem_code = inst[2..-1].split(":else:")[0..-2].join(":else:").to_s | |
else | |
else_code = "[NULL]" | |
rem_code = inst[2..-1].to_s | |
end | |
if rem_code.include?(":els;if:") | |
sets = rem_code.split(":els;if:") | |
else | |
sets = [rem_code.to_s] | |
end | |
sets.each do |set| | |
if set.split(":").length == 2 | |
elsif set.split(":").length == 3 | |
end | |
end | |
elsif inst.to_s.split(":")[0].to_s == "while" ################### WHILE #################### | |
elsif inst.to_s.split(":")[0].to_s == "loop" ################## LOOP ################## | |
code = inst.to_s.split(":")[1..-1].join(":").to_s | |
begin | |
run = true | |
while run | |
self.exe(code) | |
end | |
rescue | |
@exceptions = true | |
@feedback << "Error: Exception Encountered in loop." | |
end | |
elsif inst.to_s.split(":")[0].to_s == "iterate" ################## ITERATE ################## | |
elsif inst.to_s.split(":")[0].to_s == "break" ################## BREAK ################## | |
break | |
elsif inst.to_s.split(":")[0].to_s == "next" ################## NEXT ################## | |
@next_flag = true | |
break | |
elsif inst.to_s.split(":")[0].to_s == "label" ################## LABEL ################## | |
name = inst.to_s.split(":")[1..-1].join(":").to_s | |
if name.to_s[0] == "<" and name.to_s[-1] == ";" | |
begin | |
name = eval(name.to_s[1..-2]).to_s | |
@jump_labes << name.to_s | |
@jump_indexes << @clock.to_i | |
rescue | |
@exceptions = true | |
@feedback << "Error: Label Name Assignment Ruby Expression Failed: " + name.to_s | |
end | |
elsif name.to_s[0] == "{" and name.to_s[-1] == "}" | |
name = self.exe(name.to_s[1..-2].to_s).to_s | |
@jump_labes << name.to_s | |
@jump_indexes << @clock.to_i | |
else | |
if rmem(name.to_s).to_s != "[NULL_ERROR]" | |
name = rmem(name.to_s).to_s | |
@jump_labes << name.to_s | |
@jump_indexes << @clock.to_i | |
else | |
@exceptions = true | |
@feedback << "Error: Label Name Assignment Unknown Format: " + name.to_S | |
end | |
end | |
elsif inst.to_s.split(":")[0].to_s == "jump" ################## JUMP ################## | |
name = inst.to_s.split(":")[1..-1].join(":").to_s | |
if name.to_s[0] == "<" and name.to_s[-1] == ";" | |
begin | |
name = eval(name.to_s[1..-2].to_s).to_s | |
rescue | |
@exceptions = true | |
@feedback << "Error: Label Jump Name Ruby Expression Failed: " + name.to_s | |
end | |
elsif name.to_s[0] == "{" and name.to_s[-1] == "}" | |
name = self.exe(name.to_s[1..-2].to_s).to_s | |
else | |
if rmem(name.to_s).to_s != "false" | |
name = rmem(name.to_s) | |
else | |
@exceptions = true | |
@feedback << "Error: Label Jump Name Invalid Format: " + name.to_s | |
end | |
end | |
@clock = @jump_indexes[@jump_labels.index(name.to_s).to_i].to_i | |
elsif inst.to_s[0..5] == "sleep:" ################## SLEEP ################## | |
sleep inst.to_s.split(":")[-1].to_f | |
elsif inst.to_s.split(":")[0].to_s == "clock" ################## CLOCK ################## | |
index = inst.split(":")[1..-1].join(":").to_s | |
if index.to_s[0] == "<" and index.to_s[-1] == ";" | |
begin | |
index = eval(index.to_s[1..-2].to_s).to_s | |
rescue | |
@exceptions = true | |
@feedback << "Error: Clock Index Ruby Expression Failed: " + index.to_s | |
end | |
elsif index.to_s[0] == "{" and index.to_s[-1] == "}" | |
index = self.exe(index.to_s[1..-2].to_s).to_i | |
else | |
if rmem(index.to_s).to_s != "[NULL_ERROR]" | |
index = rmem(index.to_s).to_i | |
else | |
@exceptions = true | |
@feedback << "Error: Clock Index Format Invalid: " + index.to_s | |
end | |
end | |
if index.is_a? Integer and index >= 0 | |
@clock = index.to_i | |
else | |
@exceptions = true | |
@feedback << "Error: Clock Index Invalid Value: " + index.to_s | |
end | |
elsif inst.to_s.split(":")[0].to_s == "print" ################## PRINT ################## | |
str = inst.to_s.split(":")[1..-1].join(":").to_s | |
if str.to_s[0] == "<" and str.to_s[-1] == ";" | |
begin | |
str = eval(str.to_s[1..-2].to_s).to_s | |
rescue | |
@exceptions = true | |
@feedback << "Error: Print value of Ruby Expression failed: " + inst.to_s.split(":")[1..-1].join(":").to_s | |
end | |
elsif str.to_s[0] == "{" and str.to_s[-1] == "}" | |
str = self.exe(str.to_s[1..-2].to_s).to_s | |
else | |
if str.include?(",") | |
str = str.split(",") | |
str.each do |addr| | |
if rmem(addr.to_s).to_s != "[NULL_ERROR]" | |
print rmem(addr.to_s).to_s | |
else | |
@exceptions = true | |
@feedback << "Error: Print Scren from address that was missing: " + addr.to_s | |
end | |
end | |
else | |
if rmem(str.to_s) | |
print rmem(str.to_s).to_s | |
else | |
@exceptions = true | |
@feedback << "Error: Print Screen from adddress that was missing: " + str.to_s | |
end | |
end | |
end | |
elsif inst.to_s.split(":")[0].to_s == "gets" ################## GETS ################## | |
begin | |
@return_value = gets.chomp.to_s | |
rescue | |
@exceptions = true | |
@feedback << "Error: STINGETS RUBY ACCESS FAILED, NO SUPPORT IN THIS ENVIRONMNT" | |
end | |
elsif inst.to_s.split(":")[0].to_s == "file" ################## FILE ################## | |
elsif inst.to_s.split(":")[0].to_s == "thread" ################## THREAD ################## | |
elsif inst.to_s.split(":")[0].to_s == "return:" ################## RETURN ################## | |
if rmem(inst.split(":")[-1].to_s).to_s != "[NULL_ERROR]" | |
@return_value = rmem(inst.split(":")[-1].to_s) | |
else | |
@return_value = inst.split(":")[-1].to_s | |
end | |
else | |
@exceptions = true | |
@feedback << "Error: Bad instruction: " + inst.to_s | |
end | |
@clock += 1 | |
end | |
if @return_value == nil | |
if @feedback.length > 0 | |
if @exceptions | |
@feedback = "EXCEPTIONS: " + @feedback.to_s | |
end | |
return @feedback | |
end | |
else | |
return @return_value | |
end | |
end | |
def memory_size | |
gt = 0 | |
@data.each { |d| gt += d.bytes.length.to_i } | |
@address.each { |a| gt += a.bytes.length.to_i } | |
return [gt.to_i,@address.length.to_i] | |
end | |
def wmem(addr,val) | |
if @address.include?(addr) | |
@data[@address.index(addr.to_s).to_i] = val | |
else | |
@data << val | |
@address << addr.to_s | |
end | |
end | |
def rmem(addr) | |
if @address.include?(addr) | |
return @data[@address.index(addr.to_s).to_i] | |
else | |
return "[NULL_ERROR]" | |
end | |
end | |
def dmem(addr) | |
if @address.include?(addr) | |
@data.delete_at(@address.index(addr.to_s).to_i) | |
@address.delete_at(@address.index(addr.to_s).to_i) | |
return true | |
else | |
return "[NULL_ERROR]" | |
end | |
end | |
def initialize_memory | |
@data = [] | |
@address = [] | |
end | |
end | |
$p = Processor.new() | |
class WinShell | |
def initialize | |
@run = false | |
end | |
def start | |
@run = true | |
while @run | |
print "<:" | |
inp = gets.chomp.to_s | |
print $p.shell_in(inp.to_s).to_s + "\n" | |
end | |
@run = false | |
end | |
def stop | |
@run = false | |
end | |
end | |
$s = WinShell.new() | |
puts "Shell: " + self.to_s + " Parser: " + $p.to_s | |
$s.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment