Commit 4874ff86 authored by Chris Jewell's avatar Chris Jewell
Browse files

Cleaned symbol_table.py

parent 0f693003
Pipeline #274 passed with stage
in 4 minutes and 57 seconds
......@@ -17,17 +17,18 @@
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# GEM Transformer
"""GEM symbol resolution algorithms"""
from collections import namedtuple
from gem.gemlang.ast.ast_expression import * # pylint: disable=unused-wildcard-import
import gem.gemlang.semantics.symbol
from gem.gemlang.ast.ast_expression import * # pylint: disable=unused-wildcard-import, wildcard-import
from gem.gemlang.semantics.scoped_ast_walker import ScopedWalker
from gem.gemlang.semantics.symbol_table import SymbolTable
import gem.gemlang.semantics.symbol
class SymbolResolver(ScopedWalker):
"""GEM Symbol Resolver class."""
# pylint: disable=missing-function-docstring,no-self-use,unused-argument,invalid-name
......@@ -86,7 +87,8 @@ class SymbolResolver(ScopedWalker):
kwsym = fsym.resolve(argname)
except NameError:
raise NameError(
f"Unexpected keyword argument '{argname}' at line {call.meta.line} column {call.meta.line}")
f"Unexpected keyword argument '{argname}' at line \
{call.meta.line} column {call.meta.line}")
arg.children[0].symbol = kwsym
def onExit_AssignExpr(self, assign):
......@@ -109,7 +111,8 @@ class SymbolResolver(ScopedWalker):
lhs.symbol.type = sym.type
except:
raise ValueError(
f"Assignment should be of a random variable at line {assign.meta.line} column {assign.meta.column}")
f"Assignment should be of a random variable at line {assign.meta.line} column \
{assign.meta.column}")
def onExit_IdRef(self, ast_node):
......@@ -151,7 +154,8 @@ def get_random_vars(symtab):
free_rvs = []
observed_rvs = []
symbols = [sym for sym in symtab.globals.get_by_type(gem.gemlang.semantics.symbol.VariableSymbol) if sym.type == SymbolTable.RANDOM_VARIABLE]
symbols = [sym for sym in symtab.globals.get_by_type(gem.gemlang.semantics.symbol.VariableSymbol) if
sym.type == SymbolTable.RANDOM_VARIABLE]
for symbol in symbols:
ast = symbol.ast_def
assert ast is not None, f"symbol {symbol} is not annotated with an ASTNode"
......
......@@ -17,6 +17,8 @@
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
"""GEM Symbol Table implementation"""
from gem.gemlang.semantics.symbol import BuiltinTypeSymbol, VariableSymbol, SYMBOL_ATTR, _make_method_symbol
from gem.gemlang.semantics.scope import GlobalScope
......@@ -39,6 +41,7 @@ class SymbolTable:
@property
def globals(self):
"""Returns global scope"""
return self.__globals
def __init_type_system(self):
......@@ -63,8 +66,9 @@ class SymbolTable:
['x'], enclosing_scope=global_scope))
def declare_extern(self, var_dict: dict) -> None:
for k, v in var_dict.items():
sym = VariableSymbol(name=k)
"""Declares external data"""
for key in var_dict.keys():
sym = VariableSymbol(name=key)
self.globals.declare(sym)
sym.attrib |= SymbolTable.ATTRIB.EXTERN
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment