Ejemplo n.º 1
0
        public string Visit(StructExpression node)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("class " + node.Name + " {", currentIndent);
            codeWriter.NewLine();
            currentIndent++;

            if (node.Fields != null && node.Fields.Length > 0)
            {
                codeWriter.Write("constructor(", currentIndent);
                var fieldNames = node.Fields.Select(x => Visit(x).TrimWsRn()).ToArray();
                codeWriter.WriteLine(string.Join(", ", fieldNames) + ") {");

                currentIndent++;
                codeWriter.WriteLine(string.Join(Environment.NewLine, fieldNames.Select(x => $"this.{x} = {x};")), currentIndent);
                currentIndent--;

                codeWriter.WriteLine("}", currentIndent);
            }

            currentIndent--;
            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Ejemplo n.º 2
0
        public void TestListExpression()
        {
            Dictionary       test     = CreateDictionary("Test");
            NameSpace        n1       = CreateNameSpace(test, "N1");
            Structure        s1       = CreateStructure(n1, "S1");
            StructureElement el1      = CreateStructureElement(s1, "E1", "Boolean");
            Structure        s2       = CreateStructure(n1, "S2");
            StructureElement el2      = CreateStructureElement(s2, "E2", "S1");
            Function         function = CreateFunction(n1, "f", "S1");

            Collection collection = CreateCollection(n1, "Col", "S1", 10);
            Variable   v          = CreateVariable(n1, "V", "Col");

            Compiler.Compile_Synchronous(true, true);

            RuleCondition rc     = CreateRuleAndCondition(n1, "Rule1");
            Parser        parser = new Parser();

            {
                VariableUpdateStatement statement = parser.Statement(rc, "V <- [S1 { E1 => Tr", true, true) as VariableUpdateStatement;
                Assert.IsNotNull(statement);

                UnaryExpression unaryExpression = statement.Expression as UnaryExpression;
                Assert.IsNotNull(unaryExpression);
                ListExpression listExpression = unaryExpression.Term.LiteralValue as ListExpression;
                Assert.IsNotNull(listExpression);
                Assert.AreEqual(listExpression.ListElements.Count, 1);

                StructExpression structExpression = listExpression.ListElements[0] as StructExpression;
                Assert.IsNotNull(structExpression);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Visits a struct expression
        /// </summary>
        /// <param name="structExpression"></param>
        protected override void VisitStructExpression(StructExpression structExpression)
        {
            ModelElement backup    = User;
            Structure    structure = null;

            if (structExpression.Structure != null)
            {
                structure = structExpression.Structure.GetExpressionType() as Structure;
                VisitExpression(structExpression.Structure);
            }

            foreach (KeyValuePair <Designator, Expression> pair in structExpression.Associations)
            {
                ResetRemoveIndexes();
                if (pair.Key != null)
                {
                    User = structure;
                    VisitDesignator(pair.Key);
                    User = backup;
                }

                ResetRemoveIndexes();
                if (pair.Value != null)
                {
                    VisitExpression(pair.Value);
                }
            }
        }
Ejemplo n.º 4
0
        public static XzaarType CreateTypeFromStructExpression(StructExpression typeExpression)
        {
            var newType = new XzaarTypeBuilder(typeExpression.Name, null, Any, Any, null);

            foreach (var f in typeExpression.Fields)
            {
                if (f is FieldExpression fn)
                {
                    newType.AddField(fn.Type, fn.Name);
                }
            }
            return(newType);
        }
Ejemplo n.º 5
0
        public TypeDefinition Visit(StructExpression node)
        {
            var typeName  = node.Name;
            var newStruct = new TypeDefinition(typeName, true);
            var items     = node.Fields.Select(Visit).Cast <VariableReference>();

            foreach (var item in items)
            {
                newStruct.Fields.Add(item);
            }
            ctx.KnownTypes.Add(newStruct);
            ctx.Assembly.Types.Add(newStruct);
            return(newStruct);
        }
Ejemplo n.º 6
0
        public static bool TryGetType(string typeName, StructExpression typeExpression, out XzaarType newType)
        {
            var a = GetType(typeName);

            if (a == null || Equals(a, XzaarBaseTypes.Void) || Equals(a, XzaarBaseTypes.Any))
            {
                newType = XzaarBaseTypes.CreateTypeFromStructExpression(typeExpression);
                XzaarBaseTypes.AddTypeToCache(newType);
            }
            else
            {
                newType = a;
            }
            return(true);
        }
        public string Visit(StructExpression node)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("struct " + node.Name + " {", currentIndent);
            codeWriter.NewLine();
            currentIndent++;
            foreach (var field in node.Fields)
            {
                codeWriter.Write(Visit(field));
            }
            currentIndent--;
            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Constructor
        /// </summary>
        /// <param name="structureExpression"></param>
        /// <param name="context"></param>
        /// <param name="explain"></param>
        public StructureValue(StructExpression structureExpression, InterpretationContext context,
                              ExplanationPart explain)
            : base(structureExpression.GetExpressionType() as Structure, new Dictionary <string, INamable>())
        {
            Enclosing = Structure;

            try
            {
                HashSet <string> members = new HashSet <string>();
                foreach (KeyValuePair <Designator, Expression> pair in structureExpression.Associations)
                {
                    StructureElement structureElement = Structure.FindStructureElement(pair.Key.Image);
                    if (structureElement != null)
                    {
                        IValue val = pair.Value.GetExpressionValue(new InterpretationContext(context), explain);
                        if (val != null)
                        {
                            Field field = CreateField(structureElement, structureExpression.RootLog);
                            field.Value = val;
                            members.Add(field.Name);
                        }
                        else
                        {
                            structureExpression.AddError("Cannot evaluate value for " + pair.Value, RuleChecksEnum.ExecutionFailed);
                        }
                    }
                    else
                    {
                        structureExpression.AddError("Cannot find structure element " + pair.Key.Image, RuleChecksEnum.ExecutionFailed);
                    }
                }

                foreach (StructureElement element in Structure.Elements)
                {
                    if (!members.Contains(element.Name))
                    {
                        Field field = CreateField(element, structureExpression.RootLog);
                        field.Value = element.DefaultValue;
                    }
                }
            }
            finally
            {
                _depth -= 1;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Browses through the expression to find the value to edit
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        private Expression VisitExpression(Expression expression)
        {
            Expression retVal = expression;

            BinaryExpression binaryExpression = expression as BinaryExpression;

            if (binaryExpression != null)
            {
                binaryExpression.Left  = VisitExpression(binaryExpression.Left);
                binaryExpression.Right = VisitExpression(binaryExpression.Right);
            }

            UnaryExpression unaryExpression = expression as UnaryExpression;

            if (unaryExpression != null)
            {
                if (unaryExpression.Expression != null)
                {
                    unaryExpression.Expression = VisitExpression(unaryExpression.Expression);
                }
                else if (unaryExpression.Term != null)
                {
                    VisitTerm(unaryExpression.Term);
                }
            }

            StructExpression structExpression = expression as StructExpression;

            if (structExpression != null)
            {
                retVal = EditExpression(structExpression);
            }

            Call call = expression as Call;

            if (call != null)
            {
                foreach (Expression subExpression in call.AllParameters)
                {
                    VisitExpression(subExpression);
                }
            }

            return(retVal);
        }
    public void EvaluatesBasicExpressionWithConstantAndParameter()
    {
        using var inputParams  = new NativeArray <float>(new float[] { 2f }, Allocator.Persistent);
        using var operatorData = new NativeArray <OperatorDefinition>(new OperatorDefinition[]
        {
            new OperatorDefinition
            {
                operatorType = OperatorType.MULTIPLY,
                rhs          = 1,
                lhs          = 2
            },
            new OperatorDefinition
            {
                operatorType = OperatorType.CONSTANT_VALUE,
                nodeValue    = 1.5f
            },
            new OperatorDefinition
            {
                operatorType   = OperatorType.PARAMETER_VALUE,
                parameterIndex = 0
            },
        }, Allocator.Persistent);

        var expression = new StructExpression
        {
            operationDataSlice = new JaggedIndexing
            {
                index  = 0,
                length = 3
            }
        };

        var result = expression.EvaluateExpression(
            inputParams,
            new JaggedIndexing
        {
            index  = 0,
            length = 1
        },
            operatorData);

        Assert.AreEqual(3f, result);
    }
Ejemplo n.º 11
0
        /// <summary>
        ///     Splits the selected action to several sub-actions
        /// </summary>
        public virtual void SplitHandler(object sender, EventArgs args)
        {
            Statement statement = new Parser().Statement(Item, Item.ExpressionText);
            VariableUpdateStatement variableUpdateStatement = statement as VariableUpdateStatement;

            if (variableUpdateStatement != null)
            {
                Expression       expression       = variableUpdateStatement.Expression;
                StructExpression structExpression = expression as StructExpression;
                if (structExpression != null)
                {
                    Dictionary <Designator, Expression> associations = structExpression.Associations;
                    foreach (KeyValuePair <Designator, Expression> value in associations)
                    {
                        Action action = (Action)acceptor.getFactory().createAction();
                        action.ExpressionText = structExpression.Structure + "." + value.Key + " <- " +
                                                value.Value;
                        ActionTreeNode actionTreeNode = new ActionTreeNode(action, true);

                        BaseTreeNode parent = Parent as BaseTreeNode;
                        if (parent != null)
                        {
                            RuleCondition ruleCondition = Item.Enclosing as RuleCondition;
                            if (ruleCondition != null)
                            {
                                ruleCondition.appendActions(action);
                            }
                            else
                            {
                                SubStep subStep = Item.Enclosing as SubStep;
                                if (subStep != null)
                                {
                                    subStep.appendActions(action);
                                }
                            }
                            parent.Nodes.Add(actionTreeNode);
                        }
                    }
                }
            }
            Delete();
        }
        public float DynamicInvoke(params float[] input)
        {
            using var inputParams = new NativeArray <float>(input, Allocator.Temp);
            using var opDefs      = new NativeArray <OperatorDefinition>(definitionList.ToArray(), Allocator.Temp);

            var structExp = new StructExpression
            {
                operationDataSlice = new JaggedIndexing
                {
                    index  = 0,
                    length = (ushort)opDefs.Length
                }
            };

            return(structExp.EvaluateExpression(
                       inputParams,
                       new JaggedIndexing
            {
                index = 0,
                length = (ushort)inputParams.Length
            },
                       opDefs));
        }
Ejemplo n.º 13
0
        protected override void VisitStructExpression(StructExpression structExpression)
        {
            if (structExpression.Structure != null)
            {
                VisitExpression(structExpression.Structure);
            }

            ModelElement backup = BaseLocation;

            BaseLocation = structExpression.Structure.GetExpressionType();
            foreach (KeyValuePair <Designator, Expression> pair in structExpression.Associations)
            {
                if (pair.Key != null)
                {
                    VisitDesignator(pair.Key);
                }
                if (pair.Value != null)
                {
                    VisitExpression(pair.Value);
                }
            }
            BaseLocation = backup;
        }
Ejemplo n.º 14
0
        public void WriteDataIntoMemory(
            SystemLevelRuleNativeData dataArray,
            SymbolSeriesMatcherNativeDataWriter dataWriter)
        {
            ContextSuffix = forwardsMatchBuilder.BuildIntoManagedMemory(dataArray, dataWriter);
            ContextPrefix = backwardsMatchBuilder.BuildIntoManagedMemory(dataArray, dataWriter);

            foreach (var outcome in possibleOutcomes)
            {
                outcome.WriteIntoMemory(dataArray, dataWriter);
            }

            possibleOutcomeIndexing = new JaggedIndexing
            {
                index  = dataWriter.indexInRuleOutcomes,
                length = (ushort)possibleOutcomes.Length
            };
            for (int i = 0; i < possibleOutcomeIndexing.length; i++)
            {
                var possibleOutcome = possibleOutcomes[i];
                dataArray.ruleOutcomeMemorySpace[i + dataWriter.indexInRuleOutcomes] = possibleOutcome.AsBlittable();
            }
            dataWriter.indexInRuleOutcomes += possibleOutcomeIndexing.length;
            if (conditionalChecker != null)
            {
                var opSize = conditionalChecker.OperatorSpaceNeeded;
                conditionalCheckerBlittable = conditionalChecker.WriteIntoOpDataArray(
                    dataArray.dynamicOperatorMemory,
                    new JaggedIndexing
                {
                    index  = dataWriter.indexInOperatorMemory,
                    length = opSize
                });
                dataWriter.indexInOperatorMemory += opSize;
            }
        }
Ejemplo n.º 15
0
        static void SemanticsProcessor(StringBuilder stringBuilder, ShaderMetadata metadata)
        {
            if (metadata.Semantics == null)
            {
                return;
            }

            for (int i = 0; i < metadata.Semantics.Count; i++)
            {
                // Get
                StructExpression item = metadata.Semantics[i];

                // Write type
                stringBuilder.Append("struct");

                // Write name
                stringBuilder.Append(" ");
                stringBuilder.Append(item.ConstituentSymbols[1].ToString());

                // Begin
                stringBuilder.Append(Environment.NewLine);
                stringBuilder.Append("{");
                stringBuilder.Append(Environment.NewLine);

                // Get struct statement block
                StructStatementBlockExpression block = item.ConstituentSymbols.Find(s => s is StructStatementBlockExpression) as StructStatementBlockExpression;
                if (block == null)
                {
                    throw new ArgumentNullException("block");
                }

                // Write statement
                for (int j = 0; j < block.ConstituentSymbols.Count; j++)
                {
                    StructStatementExpression statement = block.ConstituentSymbols[j] as StructStatementExpression;

                    // Write tab
                    stringBuilder.Append("\t");

                    // Write type
                    stringBuilder.Append(statement.ConstituentSymbols[0].ToString());

                    // Write name
                    stringBuilder.Append(" ");
                    stringBuilder.Append(statement.ConstituentSymbols[1].ToString());

                    // Write double dot
                    stringBuilder.Append(" :");

                    // Write semantic
                    stringBuilder.Append(" ");
                    string semantic = statement.ConstituentSymbols[3].ToString();
                    int    index    = metadata.Semantics.IndexOf(item);
                    if (metadata is VertexShaderMetadata)
                    {
                        if (metadata.Semantics.IndexOf(item) == 1 && semantic.StartsWith("POSITION", StringComparison.InvariantCulture))
                        {
                            semantic = semantic.Replace("POSITION", "SV_POSITION");
                        }
                    }
                    else if (metadata is PixelShaderMetadata)
                    {
                        if (index == 0 && semantic.StartsWith("POSITION", StringComparison.InvariantCulture))
                        {
                            semantic = semantic.Replace("POSITION", "SV_POSITION");
                        }
                        else if (index == 1 && semantic.StartsWith("COLOR", StringComparison.InvariantCulture))
                        {
                            semantic = semantic.Replace("COLOR", "SV_TARGET");
                        }
                        else if (index == 1 && semantic.StartsWith("DEPTH", StringComparison.InvariantCulture))
                        {
                            semantic = semantic.Replace("DEPTH", "SV_DEPTH");
                        }
                    }
                    stringBuilder.Append(semantic);

                    // Finalize
                    stringBuilder.Append(";");
                    stringBuilder.Append(Environment.NewLine);
                }

                // End
                stringBuilder.Append("};");
                stringBuilder.Append(Environment.NewLine);
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        ///     Visits a struct expression
        /// </summary>
        /// <param name="structExpression"></param>
        protected override void VisitStructExpression(StructExpression structExpression)
        {
            ModelElement backup = User;
            Structure structure = null;
            if (structExpression.Structure != null)
            {
                structure = structExpression.Structure.GetExpressionType() as Structure;
                VisitExpression(structExpression.Structure);
            }

            foreach (KeyValuePair<Designator, Expression> pair in structExpression.Associations)
            {
                ResetRemoveIndexes();
                if (pair.Key != null)
                {
                    User = structure;
                    VisitDesignator(pair.Key);
                    User = backup;
                }

                ResetRemoveIndexes();
                if (pair.Value != null)
                {
                    VisitExpression(pair.Value);
                }
            }
        }
 public object Visit(StructExpression node)
 {
     return(null);
 }
        protected override void VisitStructExpression(StructExpression structExpression)
        {
            if (structExpression.Structure != null)
            {
                VisitExpression(structExpression.Structure);
            }

            ModelElement backup = BaseLocation;
            BaseLocation = structExpression.Structure.GetExpressionType();
            foreach (KeyValuePair<Designator, Expression> pair in structExpression.Associations)
            {
                if (pair.Key != null)
                {
                    VisitDesignator(pair.Key);
                }
                if (pair.Value != null)
                {
                    VisitExpression(pair.Value);
                }
            }
            BaseLocation = backup;
        }
 public object Visit(StructExpression node)
 {
     throw new System.NotImplementedException();
 }