-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_v2.py
44 lines (39 loc) · 1.7 KB
/
func_v2.py
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
from intbase import InterpreterBase
from env_v2 import EnvironmentManager
# FuncInfo is a class that represents information about a function
# Right now, the only thing this tracks is the line number of the first executable instruction
# of the function (i.e., the line after the function prototype: func foo)
class FuncInfo:
def __init__(self, start_ip, args, return_type, refs):
self.start_ip = start_ip # line number, zero-based
self.return_type = return_type
self.args = args # name, type
self.refs = refs
# FunctionManager keeps track of every function in the program, mapping the function name
# to a FuncInfo object (which has the starting line number/instruction pointer) of that function.
class FunctionManager:
def __init__(self, tokenized_program):
self.func_cache = {}
self._cache_function_line_numbers(tokenized_program)
def get_function_info(self, func_name):
if func_name not in self.func_cache:
return None
return self.func_cache[func_name]
def _cache_function_line_numbers(self, tokenized_program):
for line_num, line in enumerate(tokenized_program):
if line and line[0] == InterpreterBase.FUNC_DEF:
func_name = line[1]
return_type = line[-1]
args = []
refs = []
for a in line[2:-1]:
#assumes that we only get valid types
name, type = a.split(":")
if type[0] == 'r':
args.append((name, type[3:]))
refs.append(True)
else:
args.append((name, type))
refs.append(False)
func_info = FuncInfo(line_num + 1, args, return_type, refs) # function starts executing on line after funcdef
self.func_cache[func_name] = func_info