Exemple #1
0
        private ILinkable Visit(DeclareAST declareAST)
        {
            string    id           = Visit(declareAST.Id);
            AST       modelTypeAST = declareAST.ModelType;
            ILinkable linkable     = Visit(modelTypeAST) as ILinkable;

            if (linkable != null)
            {
                switch (linkable)
                {
                case Model model:
                    model.Name = id;
                    break;

                case Route route:
                    route.Name = id;
                    break;
                }
                TypeSymbol  typeSymbol  = currentScope.Lookup(linkable.GetTypeName()) as TypeSymbol;
                ModelSymbol modelSymbol = new ModelSymbol(id, typeSymbol, linkable);
                if (!currentScope.Insert(modelSymbol, false))
                {
                    throw logger.Error(new SemanticException(declareAST.Id.ReferenceToken, $"Duplicated ID {id}"));
                }
                return(linkable);
            }
            else
            {
                throw logger.Error(new SemanticException(modelTypeAST.FindToken(), $"Invalid type"));
            }
        }
Exemple #2
0
        private InPort Find(JInPort jInPort, Model sourceModel)
        {
            if (jInPort.Id is null)
            {
                throw logger.Error(new ModelException(sourceModel, "Property `id` not found."));
            }
            if (jInPort.InPortName is null)
            {
                throw logger.Error(new ModelException(sourceModel, "Property `in-port` not found."));
            }
            ModelSymbol modelSymbol = symbolTable.Lookup(jInPort.Id) as ModelSymbol;

            if (modelSymbol is null)
            {
                throw logger.Error(new LigralException($"Reference {jInPort.Id} not exist"));
            }
            Model  destinationModel = modelSymbol.GetValue() as Model;
            InPort inPort           = destinationModel.Expose(jInPort.InPortName) as InPort;

            if (inPort is null)
            {
                throw logger.Error(new LigralException($"{jInPort.InPortName} is not in port."));
            }
            return(inPort);
        }
Exemple #3
0
        private ILinkable Visit(SelectAST selectAST)
        {
            ILinkable linkable = Visit(selectAST.ModelObject) as ILinkable;

            if (linkable != null)
            {
                string portId = Visit(selectAST.Port.PortId);
                try
                {
                    Port port = linkable.Expose(portId);
                    switch (port)
                    {
                    case InPort inPort:
                        return(inPort);

                    case OutPort outPort:
                        string signalName = selectAST.Port.PortName == null? null : Visit(selectAST.Port.PortName);
                        if (signalName != null)
                        {
                            Node node = ModelManager.Create("Node") as Node;
                            outPort.SignalName = signalName;
                            node.Name          = outPort.SignalName;
                            outPort.Bind(node.Expose(0));
                            TypeSymbol  typeSymbol  = currentScope.Lookup("Node") as TypeSymbol;
                            ModelSymbol modelSymbol = new ModelSymbol(signalName, typeSymbol, node);
                            if (!currentScope.Insert(modelSymbol, false))
                            {
                                throw logger.Error(new SemanticException(selectAST.Port.PortName.ReferenceToken, $"Duplicated ID {signalName}"));
                            }
                        }
                        Group group = new Group();
                        group.AddInputModel(linkable);
                        group.AddOutputModel(outPort);
                        return(group);

                    default:
                        throw logger.Error(new SemanticException(selectAST.Port.FindToken(), "Ambiguous port"));
                    }
                }
                catch (LigralException)
                {
                    throw logger.Error(new SemanticException(selectAST.Port.FindToken()));
                }
            }
            else
            {
                throw logger.Error(new SemanticException(selectAST.ModelObject.FindToken(), "Non model object"));
            }
        }
Exemple #4
0
        private object Visit(IdAST idAST)
        {
            Symbol symbol = currentScope.Lookup(idAST.Id);

            if (symbol == null)
            {
                TypeSymbol typeSymbol = currentScope.Lookup("Node") as TypeSymbol;
                Node       node       = typeSymbol.GetValue() as Node;
                node.Name = idAST.Id;
                ModelSymbol modelSymbol = new ModelSymbol(idAST.Id, typeSymbol, node);
                currentScope.Insert(modelSymbol);
                return(node);
                // throw logger.Error(new SemanticException(idAST.FindToken(), $"Undefined variable {idAST.Id}"));
            }
            else
            {
                return(symbol.GetValue());
            }
        }
Exemple #5
0
        private void Construct(JModel jModel)
        {
            ModelSymbol modelSymbol = symbolTable.Lookup(jModel.Id) as ModelSymbol;

            if (modelSymbol is null)
            {
                throw logger.Error(new NotFoundException($"Reference {jModel.Id}"));
            }
            Model model = modelSymbol.GetValue() as Model;

            if (jModel.OutPorts is null)
            {
                throw logger.Error(new ModelException(model, "Property `out-ports` not found."));
            }
            foreach (JOutPort jOutPort in jModel.OutPorts)
            {
                Connect(jOutPort, model);
            }
        }
Exemple #6
0
        private void Register(JModel jModel)
        {
            if (jModel.Type is null)
            {
                throw logger.Error(new NotFoundException("Property `type`"));
            }
            Model model = ModelManager.Create(jModel.Type);

            if (jModel.Id is null)
            {
                throw logger.Error(new ModelException(model, "Property `id` not found."));
            }
            model.Name = jModel.Id;
            TypeSymbol  typeSymbol  = symbolTable.Lookup("MODEL") as TypeSymbol;
            ModelSymbol modelSymbol = new ModelSymbol(jModel.Id, typeSymbol, model);

            symbolTable.Insert(modelSymbol);
            if (jModel.Parameters is null)
            {
                throw logger.Error(new ModelException(model, "Property `parameters` not found."));
            }
            Config(jModel.Parameters, model);
        }