Improve support for functions #1

This commit is contained in:
Bartłomiej Pluta
2019-07-04 02:09:24 +02:00
parent c8ff5ce38f
commit 6390ac20de
31 changed files with 514 additions and 328 deletions

View File

@@ -0,0 +1,31 @@
from smnp.error.runtime import RuntimeException
class Environment():
def __init__(self, scopes, functions, methods):
self.scopes = scopes
self.functions = functions
self.methods = methods
self.customFunctions = {}
self.callStack = [] #TODO remove
def findVariable(self, name, type=None, pos=None):
for scope in reversed(self.scopes):
if name in scope:
value = scope[name]
if type is not None:
if isinstance(value, type):
return value
else:
return value
raise RuntimeException(pos, f"Variable '{name}' is not declared" + (
"" if type is None else f" (expected type: {type})"))
def findVariableScope(self, name, type=None):
for scope in reversed(self.scopes):
if name in scope:
if type is not None:
if isinstance(scope[name], type):
return scope
else:
return scope