-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
73 lines (57 loc) · 2.06 KB
/
state.py
File metadata and controls
73 lines (57 loc) · 2.06 KB
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
# Module to define the current state of the parser/interpreter
from collections import deque
class Statement:
def __init__(self, body_,):
self.body = body_
class Variable:
def __init__(self, type_, name_, value_: dict):
self.type = type_
self.name = name_
self.value = value_
self.address = self.allocate_mem()
def __eq__(self, value: "Variable"):
return self.name == value.name
def __hash__(self):
return hash(self.address)
def __str__(self):
return f"({self.type}, {self.name}, {self.value}, {self.address})"
def allocate_mem(self):
pass
class Literal:
def __init__(self, type_, value_, negative_):
self.type = type_
self.value = value_
self.negative = negative_
class Function:
def __init__(self, type_, parameters_, body_):
self.type = type_
self.parameters = parameters_
self.body = body_
class State:
def __init__(self, memsize_: int):
# Stores the name and return type of current function being parsed
self.current_function = None
self.local_variables = {}
# Memory
self.memory_size = memsize_
self.memory = [0 for i in range(memsize_)]
# Keeps track of order in which function calls were made
self.call_stack = deque()
# Stores the initial program instructions in the main function
self.main_call = []
self.functions = {}
self.global_variables = {}
def variable_lookup(self, name: str) -> dict:
""" Returns the type and value of variable
Args:
name (str): Name of variable to lookup
Returns:
dict: {'type': <type>, 'value': <value>}
None: If variable has not been declared or not in scope
"""
# TODO: Handle local variables in different stack layers
if name in self.local_variables:
pass
elif name in self.global_variables:
return self.global_variables[name]
state = State(memsize_=1_000_000)