public SemanticAnalyzer() { if (scope == null) { scope = new ScopeSymbolTable(0, null); } }
public ScopeSymbolTable(int level, ScopeSymbolTable enclosingScope) { symbols = new Dictionary <string, ISymbol>(); this.scopeLevel = level; this.globalScope = enclosingScope; init(); }
public static void UsingPlugin(string pluginName, ScopeSymbolTable currentScope, string moduleName) { if (Plugins.ContainsKey(pluginName)) { IPlugin plugin = Plugins[pluginName]; if (ModelManager.ExtendedModelTypePool.ContainsKey(moduleName)) { throw logger.Error(new PluginException(plugin, $"Cannot using {moduleName} since the same plugin has already exists.")); } else { ModelManager.ExtendedModelTypePool.Add(moduleName, plugin.ModelTypePool); } ScopeSymbolTable pluginScope = new ScopeSymbolTable(plugin.ReferenceName, currentScope.scopeLevel + 1, currentScope); TypeSymbol scopeType = currentScope.Lookup("SCOPE") as TypeSymbol; TypeSymbol modelType = currentScope.Lookup("MODEL") as TypeSymbol; foreach (string modelName in plugin.ModelTypePool.Keys) { ScopedModelType scopedModelType = new ScopedModelType() { ModelName = modelName, ScopeName = moduleName }; pluginScope.Insert(new TypeSymbol(modelName, modelType, scopedModelType)); } ScopeSymbol scopeSymbol = new ScopeSymbol(moduleName, scopeType, pluginScope); if (!currentScope.Insert(scopeSymbol, false)) { throw logger.Error(new PluginException(plugin, $"Cannot using {moduleName} since the same symbol has already exists.")); } } else { throw logger.Error(new NotFoundException($"Plugin {pluginName}")); } }
public Route Construct() { Route route = new Route(); ScopeSymbolTable routeScope = new ScopeSymbolTable(Name, scopeLevel, enclosingScope); route.SetUp(Name, Type, routeScope, parameters, inPortNameList, outPortNameList, statementsAST, routeFileName); route.Name = Name + id.ToString(); id++; return(route); }
private void Interpret() { Interpreter interpreter = Interpreter.GetInstance(); string lastFileName = interpreter.CurrentFileName; interpreter.CurrentFileName = routeFileName; ScopeSymbolTable scope = interpreter.SetScope(RouteScope); foreach (var routeInPort in inPortNameList) { Input model = (Input)ModelManager.Create("<Input>"); model.Name = routeInPort.Name; if (routeInPort.Nullable) { model.SetDefaultSource(routeInPort.Default); } inputModels.Add(model); TypeSymbol modelType = RouteScope.Lookup(Type) as TypeSymbol; ModelSymbol modelSymbol = new ModelSymbol(routeInPort.Name, modelType, model); RouteScope.Insert(modelSymbol); } foreach (string outPortName in outPortNameList) { Model model = ModelManager.Create("<Output>"); model.Name = outPortName; outputModels.Add(model); TypeSymbol modelType = RouteScope.Lookup(Type) as TypeSymbol; ModelSymbol modelSymbol = new ModelSymbol(outPortName, modelType, model); RouteScope.Insert(modelSymbol); } int modelsCount = ModelManager.ModelPool.Count; interpreter.Interpret(statementsAST); var innerModels = ModelManager.ModelPool.Skip(modelsCount); if (inputModels.Find(model => ((Model)model).IsConnected()) is Model connectedModel) { throw logger.Error(new ModelException(connectedModel, "Input cannot be connected inside the route.")); } if (outputModels.Union(innerModels).ToList().Find(model => !((Model)model).IsConnected()) is Model unconnectedModel) { throw logger.Error(new ModelException(unconnectedModel, "models inside the route apart from inputs cannot be unconnected")); } interpreter.SetScope(scope); interpreter.CurrentFileName = lastFileName; }
public void SetUp( string type, string baseType, ScopeSymbolTable scope, List <RouteParam> parameters, List <RouteInPort> inPortNameList, List <string> outPortNameList, StatementsAST statementsAST, string routeFileName) { Type = type; Base = baseType; RouteScope = scope; this.inPortNameList = inPortNameList; this.outPortNameList = outPortNameList; this.parameters = parameters; this.statementsAST = statementsAST; this.routeFileName = routeFileName; }
public void visit(ProcDecl procDecl) { if (scope.scopeLevel == Dictionary.MAX_NEST_LEVEL) { throw new Exception(Dictionary.max_nest_level_exc); } ProcedureSymbol procSym = new ProcedureSymbol(procDecl.name); scope.define(procSym); scope = new ScopeSymbolTable(scope.scopeLevel + 1, scope); foreach (Param param in procDecl.parameters) { VarSymbol paramSymbol = createParamSymbol(param); scope.define(paramSymbol); procSym.parameters.Add(paramSymbol); } foreach (IAST child in procDecl.children) { child.accept(this); } scope = scope.globalScope; }
public static void ImportPlugin(string pluginName, ScopeSymbolTable currentScope, IEnumerable <string> items = null) { if (Plugins.ContainsKey(pluginName)) { IPlugin plugin = Plugins[pluginName]; if (ModelManager.ExtendedModelTypePool.ContainsKey(pluginName)) { throw logger.Error(new PluginException(plugin, $"Cannot import {pluginName} since the same plugin has already exists.")); } else { ModelManager.ExtendedModelTypePool.Add(pluginName, plugin.ModelTypePool); } items = items ?? plugin.ModelTypePool.Keys; TypeSymbol modelType = currentScope.Lookup("MODEL") as TypeSymbol; foreach (string modelName in items) { if (!plugin.ModelTypePool.ContainsKey(modelName)) { throw logger.Error(new PluginException(plugin, $"No model named {modelName}")); } ScopedModelType scopedModelType = new ScopedModelType() { ModelName = modelName, ScopeName = pluginName }; if (!currentScope.Insert(new TypeSymbol(modelName, modelType, scopedModelType), false)) { throw logger.Error(new PluginException(plugin, $"Cannot import model {modelName} since the same symbol has already existed.")); } } } else { throw logger.Error(new NotFoundException($"Plugin {pluginName}")); } }
public void SetUp(int scopeLevel, ScopeSymbolTable enclosingScope) { this.scopeLevel = scopeLevel; this.enclosingScope = enclosingScope; }