language

some fools attempt at an interpreted language
Log | Files | Refs | README

bytecode.py (4529B)


      1 from memonic import *
      2 from helper import *
      3 
      4 class PropertyAssignment():
      5 	def __init__(self, ovar, plabel, expression):
      6 		self.ovar = VariableGet(ovar)
      7 		self.label = plabel
      8 		self.expr = expression
      9 	
     10 	def action(self):
     11 		return([
     12 		        self.expr.action(),
     13 		        self.ovar.action(),
     14 		        OP_SETN,
     15 		        self.label.action()
     16 		       ])
     17 
     18 class PropertyGet():
     19 	def __init__(self, ovar, plabel):
     20 		self.ovar = VariableGet(ovar)
     21 		self.label = plabel
     22 	
     23 	def action(self):
     24 		return([
     25 		        self.ovar.action(),
     26 		        OP_GETN,
     27 		        self.label.action()
     28 		       ])
     29 
     30 class MethodCall():
     31 	def __init__(self, ovar, label, arguements):
     32 		self.ovar  = VariableGet(ovar)
     33 		self.label = label
     34 		self.args  = arguements
     35 	
     36 	def action(self):
     37 		return([
     38 		        self.args.action(),
     39 		        self.ovar.action(),
     40 		        OP_CALLM,
     41 		        self.label.action()
     42 		       ])
     43 
     44 class ClassDef():
     45 	def __init__(self, label, args):
     46 		self.label = label
     47 		self.args  = args
     48 	
     49 	def action(self):
     50 		tmp = self.args.action() if self.args != None else 0x0
     51 		return([
     52 		        OP_DECLASS,
     53 		        self.label.action(),
     54 		        tmp
     55 		       ])
     56 
     57 class NewClass():
     58 	def __init__(self, toset, label, args):
     59 		self.toset = toset
     60 		self.label = label
     61 		self.args  = args
     62 	
     63 	def action(self):
     64 		return([
     65 		        self.args.action(),
     66 		        OP_NEW,
     67 		        self.label.action(s=True),
     68 		        self.label.action(),
     69 		        OP_DEC,
     70 		        self.toset.action(s=True),
     71 		        0x06,
     72 		        self.toset.action(),
     73 		        OP_STV,
     74 		        self.toset.action(s=True),
     75 		        self.toset.action()
     76 		       ])
     77 
     78 class VariableNew():
     79 	def __init__(self, label, typed):
     80 		self.label = label
     81 		self.typed = typed
     82 	
     83 	def action(self):
     84 		return([
     85 		        OP_DEC,
     86 		        self.label.action(s=True),
     87 		        self.typed.action(),
     88 		        self.label.action()
     89 		       ])
     90 
     91 class VariableAssignment():
     92 	def __init__(self, label, expression):
     93 		self.label = label
     94 		self.expr  = expression
     95 	
     96 	def action(self):
     97 		if self.label.is_property:
     98 			return(PropertyAssignment(self.label.parent,
     99 			                          self.label,
    100 			                          self.expr).action())
    101 		else:
    102 			return([
    103 			        self.expr.action(),
    104 			        OP_STV,
    105 			        self.label.action(s=True),
    106 			        self.label.action()
    107 			       ])
    108 
    109 class VariableGet():
    110 	def __init__(self, label):
    111 		self.label = label
    112 	
    113 	def action(self):
    114 		if self.label.is_property:
    115 			return(PropertyGet(self.label.parent, self.label).action())
    116 		else:
    117 			return([
    118 			        OP_LOV,
    119 			        self.label.action(s=True),
    120 			        self.label.action()
    121 			       ])
    122 
    123 class FunctionDef():
    124 	def __init__(self, label, args, typed):
    125 		self.label = label
    126 		self.args  = args
    127 		self.typed = typed
    128 	
    129 	def action(self):
    130 		tmp = self.args.action() if self.args != None else 0x0
    131 		return([
    132 		        OP_DEFUN,
    133 		        self.label.action(),
    134 		        self.typed.action(),
    135 		        tmp
    136 		       ])
    137 
    138 class FunctionCall():
    139 	def __init__(self, label, arguements):
    140 		self.label = label
    141 		self.arguements = arguements
    142 	
    143 	def action(self):
    144 		if self.label.is_property:
    145 			return(MethodCall(self.label.parent,
    146 			                  self.label,
    147 			                  self.arguements).action())
    148 		else:
    149 			return([
    150 			        self.arguements.action(),
    151 			        OP_CALL,
    152 			        self.label.action(s=True),
    153 			        self.label.action()
    154 			       ])
    155 
    156 #TODO: Implement this
    157 class ForLoop():
    158 	def __init__(self, expression):
    159 		self.expr = expression
    160 	
    161 	def action(self):
    162 		return([0])
    163 
    164 class SerializeableType():
    165 	def __init__(self, value):
    166 		pass
    167 
    168 class StringConstant(SerializeableType):
    169 	def __init__(self, value):
    170 		self.value = []
    171 		for i in value:
    172 			self.value.append(ord(i))
    173 
    174 	def action(self):
    175 		return([
    176 		        OP_CTS,
    177 		        int_to_bytes(len(self.value) + 1),
    178 		        0x00,
    179 		        0x0A,
    180 		        self.value
    181 		       ])
    182 
    183 class IntegerConstant(SerializeableType):
    184 	def __init__(self, value):
    185 		t = int(value[0])
    186 		self.raw = t
    187 		self.value = int_to_bytes(t)
    188 	
    189 	def action(self):
    190 		return([
    191 		        OP_CTS,
    192 		        int_to_bytes(len(self.value) + 1),
    193 		        0x00,
    194 		        0x07,
    195 		        self.value
    196 		       ])
    197 
    198 class BinaryOp():
    199 	def __init__(self, expr1, op):
    200 		self.vals = [expr1]
    201 		self.op = op
    202 	
    203 	def action(self):
    204 		return([
    205 		        self.vals[1].action(),
    206 		        self.vals[0].action(),
    207 		        self.op.action()
    208 		       ])
    209 
    210 class Opcode():
    211 	def __init__(self, opcode):
    212 		self.opcode = opcode
    213 	
    214 	def action(self):
    215 		return(self.opcode)