Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active July 30, 2021 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/e56379a3e2b3f719c92780c88f6516da to your computer and use it in GitHub Desktop.
Save chelseatroy/e56379a3e2b3f719c92780c88f6516da to your computer and use it in GitHub Desktop.
Interpreter Data Model
class Negative:
def __init__(self, value):
self.value = value
def __repr__(self):
return f"Negative({self.value})"
class Integer:
'''
Example: 42
'''
def __init__(self, value):
self.value = value
def __repr__(self):
return f"Integer(value={self.value})"
class Character:
'''
Example: 42
'''
def __init__(self, value):
self.value = value
def __repr__(self):
return f"Character({self.value})"
class Float:
'''
Example: 42.0
'''
def __init__(self, value):
self.value = float(value)
def __repr__(self):
return f"Float({self.value})"
class WabbitType:
def __init__(self, string_representation):
self.string_representation = string_representation
def __repr__(self):
return self.string_representation
class VariableAssignment:
def __init__(self, name, value, type=None):
self.name = name
self.value = value
self.type = type
def __repr__(self):
return f"VariableAssignment(name={self.name}, value={self.value})"
class VariableReAssignment:
def __init__(self, name, value):
self.name = name
self.value = value
def __repr__(self):
return f"VariableReAssignment(name={self.name}, value={self.value})"
class ConstantAssignment:
def __init__(self, name, value):
self.name = name
self.value = value
def __repr__(self):
return f"ConstantAssignment(name={self.name}, value={self.value})"
class VariableDeclaration:
def __init__(self, name, type):
self.name = name
self.type = type
def __repr__(self):
return f"VariableDeclaration(name={self.name}, value={self.type})"
class Name:
def __init__(self, name):
self.name = name
def __repr__(self):
return f"{self.name}"
class BinaryOperator:
'''
Example: left + right
'''
def __init__(self, operation, left, right):
self.operation = operation
self.left = left
self.right = right
def __repr__(self):
return f"BinaryOperator({self.operation}, {self.left}, {self.right})"
class Comparison:
'''
Example: left > right
'''
def __init__(self, op, left, right):
self.op = op
self.left = left
self.right = right
def __repr__(self):
return f"Comparison({self.op}, {self.left}, {self.right})"
class Conditional:
def __init__(self, if_condition, if_block, else_block=None):
self.if_condition = if_condition
self.if_block = if_block
self.else_block = else_block
def __repr__(self):
return f"Conditional(if_condition={self.if_condition}, if_block={self.if_block}, else_block={self.else_block})"
class WhileLoop:
def __init__(self, while_condition, while_block):
self.while_condition = while_condition
self.while_block = while_block
def __repr__(self):
return f"WhileLoop(while_condition={self.while_condition}, while_block={self.while_block})"
class Block:
def __init__(self, statements):
self.statements = statements
def __repr__(self):
printable = ',\n'.join([str(st) for st in self.statements])
return "Block(\n" + printable + "\n)"
class CompoundExpression:
def __init__(self, statements):
self.statements = statements
class Print:
def __init__(self, value):
self.value = value
def __repr__(self):
return f"Print({self.value})"
class Break:
pass
class Continue:
pass
class WabbitBoolean:
def __init__(self, value):
self.value = value
def __repr__(self):
return str(self.value).lower()
class Unit():
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment