Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class label(object):
- def __init__(self, queue, name, id):
- self.name = name
- self.id = id
- queue += [ self, ]
- def getString(self, queue, labels):
- return ""
- def getPart(self):
- print("ERROR: LABEL cannot be part of a statement")
- return ""
- class node(object):
- def __init__(self, queue, partStatement):
- self.partStatement = partStatement
- queue += [ self, ]
- def getString(self, queue, labels):
- if self.partStatement:
- print("ERROR: " + str(type(self)) + " must be part of a statement")
- return
- return self.string
- def getPart(self):
- if not self.partStatement:
- print("ERROR: " + str(type(self)) + " cannot be part of a statement")
- return
- return self.string
- class c_call(node):
- def __init__(self, queue, params, id):
- func = params[1]
- for p in params[2:]: func += p.capitalize()
- self.string = params[0].lower() + "." + func
- queue += [ self, ]
- def getString(self, queue, labels):
- return self.string + "()"
- def getPart(self):
- return self.string
- class c_drop(node):
- def __init__(self, queue, params, id):
- f_call = queue.pop()
- if type(f_call) != c_call:
- print("ERROR: DROP not after CALL")
- return
- par = []
- for i in range(int(params[0])):
- par += [ queue.pop().getPart(), ]
- par = ", ".join(par)
- self.string = f_call.string + "(" + par + ")"
- queue += [ self, ]
- def getString(self, queue, labels):
- return self.string
- def getPart(self):
- return self.string
- class c_jne(node):
- def __init__(self, queue, params, id):
- self.id = id
- self.string = queue.pop().getPart()
- self.label = params[0]
- queue += [ self, ]
- def getString(self, queue, labels):
- if not self.label in labels:
- print("ERROR: invalid label")
- return ""
- if self.id < labels[self.label].id:
- ret = "if (" + self.string + "):\n"
- lq = None
- q = queue.pop()
- if q == None: return ret + " pass"
- while q != None:
- if type(q) == label:
- if q.name == self.label: break
- for l in q.getString(queue, labels).split("\n"):
- if l == "": continue
- ret += " " + l + "\n"
- lq = q
- q = queue.pop()
- if type(lq) == c_jmp:
- if self.id >= labels[lq.label].id:
- # The only thing that is allowed to jump back, are whiles
- ret = ret.split("\n")
- ret[0] = ret[0].replace("if ", "while ")
- ret = "\n".join(ret)
- else:
- # The only thing that is allowed to jump forward, are else
- ret += "else:\n"
- q = queue.pop()
- if q == None: return ret + " pass"
- while q != None:
- if type(q) == label:
- if q.name == lq.label: break
- for l in q.getString(queue, labels).split("\n"):
- if l == "": continue
- ret += " " + l + "\n"
- q = queue.pop()
- return ret.strip()
- return "if not (" + self.string + "): goto " + self.label
- def getPart(self):
- print("ERROR: JNE cannot be part of a statement")
- return ""
- class c_push(node):
- def __init__(self, queue, params, id):
- if params[0] == "variable":
- self.string = "var" + params[1]
- elif params[0] == "returnvalue":
- # We silently assume this always happens at the right moment
- return
- elif str(int(params[0])) != params[0]:
- print("ERROR: PUSH " + params[0] + " not supported")
- return
- else:
- self.string = params[0]
- node.__init__(self, queue, True)
- class c_pop(node):
- def __init__(self, queue, params, id):
- if params[0] == "variable":
- self.string = "var" + params[1] + " = " + queue.pop().getPart()
- else:
- print("ERROR: POP " + params[0] + " not support")
- return
- node.__init__(self, queue, False)
- class c_and(node):
- def __init__(self, queue, params, id):
- self.string = queue.pop().getPart() + " and " + queue.pop().getPart()
- node.__init__(self, queue, True)
- class c_jmp(node):
- def __init__(self, queue, params, id):
- self.string = ""
- self.id = id
- self.label = params[0]
- node.__init__(self, queue, False)
- class c_return(node):
- def __init__(self, queue, params, id):
- self.string = "return"
- node.__init__(self, queue, False)
- f = open("script.emc", "r")
- lines = f.readlines()
- labels = {}
- queue = []
- i = 0
- for line in lines:
- i += 1
- line = line.strip().lower()
- if line == "": continue;
- if line.find(':') != -1:
- pos = line.find(':')
- labels[line[:pos]] = label(queue, line[:pos], i)
- continue
- command = line.split(' ')[0]
- params = line.split(' ')[1:]
- globals()["c_" + command](queue, params, i)
- queue += [ None, ]
- queue.reverse()
- q = queue.pop()
- while q != None:
- ret = q.getString(queue, labels)
- if ret != "": print(ret)
- q = queue.pop()
Advertisement
Add Comment
Please, Sign In to add comment