protected Expression Visit(UnaryExpression expression) { // First, dispatch to resolve type of node at deeper level Visit((Node)expression); var unaryType = expression.TypeInference.TargetType; var inputType = expression.Expression.TypeInference.TargetType; if (unaryType == null || inputType == null) return expression; if (unaryType == ScalarType.Bool && inputType != ScalarType.Bool && expression.Operator == UnaryOperator.LogicalNot) expression.Expression = new MethodInvocationExpression(new TypeReferenceExpression(ScalarType.Bool), expression.Expression) { TypeInference = { TargetType = ScalarType.Bool } }; return expression; }
public virtual void Visit(UnaryExpression unaryExpression) { if (unaryExpression.Operator.IsPostFix()) { VisitDynamic(unaryExpression.Expression); Write(unaryExpression.Operator.ConvertToString()); } else { Write(unaryExpression.Operator.ConvertToString()); VisitDynamic(unaryExpression.Expression); } }
private static UnaryExpression Clone(UnaryExpression expression) { return new UnaryExpression(expression.Operator, Clone(expression.Expression)); }
/// <summary> /// Creates a ForStatement with the same behavior /// </summary> /// <param name="forEachStatement">the ForEachStatement</param> /// <returns>the ForStatement</returns> private static ForStatement ExpandForEachStatement(ForEachStatement forEachStatement) { if (forEachStatement != null) { var collec = forEachStatement.Collection.TypeInference.Declaration as Variable; LiteralExpression dimLit = null; if (collec.Type is ArrayType) { if ((collec.Type as ArrayType).Dimensions.Count == 1) { dimLit = (collec.Type as ArrayType).Dimensions[0] as LiteralExpression; } } if (dimLit != null) { var initializer = new Variable(ScalarType.Int, forEachStatement.Variable.Name.Text + "Iter", new LiteralExpression(0)); var vre = new VariableReferenceExpression(initializer.Name); var condition = new BinaryExpression(BinaryOperator.Less, vre, dimLit); var next = new UnaryExpression(UnaryOperator.PreIncrement, vre); ForStatement forStatement = new ForStatement(new DeclarationStatement(initializer), condition, next); var body = new BlockStatement(); var variable = forEachStatement.Variable; variable.InitialValue = new IndexerExpression(forEachStatement.Collection, new VariableReferenceExpression(initializer)); body.Statements.Add(new DeclarationStatement(variable)); if (forEachStatement.Body is BlockStatement) body.Statements.AddRange((forEachStatement.Body as BlockStatement).Statements); else body.Statements.Add(forEachStatement.Body); forStatement.Body = body; return forStatement; } // TODO: multidimension-array? // TODO: unroll? // TODO: multiple foreach? } return null; }
public override void Visit(UnaryExpression expression) { var prevStreamUsage = currentStreamUsage; currentStreamUsage = StreamUsage.Read; base.Visit(expression); currentStreamUsage = prevStreamUsage; }
protected void Visit(UnaryExpression expression) { var prevStreamUsage = currentStreamUsage; currentStreamUsage = StreamUsage.Read; Visit((Node)expression); currentStreamUsage = prevStreamUsage; }
/// <inheritdoc/> public override void Visit(UnaryExpression unaryExpression) { base.Visit(unaryExpression); if (values.Count == 0) { return; } var value = values.Pop(); switch (unaryExpression.Operator) { case UnaryOperator.Plus: values.Push(value); break; case UnaryOperator.Minus: values.Push(-value); break; case UnaryOperator.PreIncrement: case UnaryOperator.PostIncrement: // TODO Pre/Post increment/decrement are not correctly handled value++; values.Push(value); break; case UnaryOperator.PreDecrement: case UnaryOperator.PostDecrement: value--; values.Push(value); break; case UnaryOperator.LogicalNot: values.Push(value == 0.0 ? 1.0 : 0.0); break; default: result.Error("Unary operator [{0}] is not supported", unaryExpression.Span, unaryExpression); values.Push(0); break; } }
protected virtual void Visit(UnaryExpression unaryExpression) { Visit((Node)unaryExpression); // TODO check for unaryExpression.TypeInference = (TypeInference)unaryExpression.Expression.TypeInference.Clone(); // If this is a logical not, transform the value to a bool (bool2 bool3 bool4 / matrix<bool,1,1> matrix<bool,1,2> ..etc. var subType = unaryExpression.Expression.TypeInference.TargetType; if (subType != null && unaryExpression.Operator == UnaryOperator.LogicalNot) unaryExpression.TypeInference.TargetType = TypeBase.CreateWithBaseType(subType, ScalarType.Bool); }
/// <summary> /// Creates assignement statements with its default value /// </summary> /// <param name="streamStruct">the stream structure</param> /// <param name="streamName">the name of the stream</param> /// <param name="inputStruct">the input structure</param> /// <param name="initialValue">the initial value</param> /// <param name="scopeStack">???</param> /// <returns>A collection of statements</returns> private static IEnumerable<Statement> AssignStreamFromInput(StructType streamStruct, string streamName, StructType inputStruct, Expression initialValue, bool basicTransformation) { foreach (var currentField in inputStruct.Fields) { // Ignore fields that don't exist in Streams. // It could happen if HSConstantMain references a stream (gets added to HS_OUTPUT), // and in HSMain CreateStreamFromInput() is called (this stream doesn't exist in DS_STREAMS). if (streamStruct.Fields.All(x => x.Name != currentField.Name)) continue; // If we have a scope stack (advanced analysis), then convert expression by appending // field to each reference to a variable of inputStruct type // i.e. "output = input1 * 3 + input2 * 5" will become "output.A = input1.A * 3 + input2.A * 5" // Otherwise consider it is as a simple a variable reference and directly append field. if (basicTransformation) { yield return new ExpressionStatement( new AssignmentExpression( AssignmentOperator.Default, new MemberReferenceExpression(new VariableReferenceExpression(streamName), currentField.Name), new MemberReferenceExpression(initialValue, currentField.Name))); } else { //yield return AssignStreamFieldFromInput(streamName, inputStruct, initialValue, scopeStack, currentField); foreach (var field in streamStruct.Fields.Where(x => x.Name == currentField.Name)) // TODO: where might be useless { if (field.Type is ArrayType) { //create a for loop var iteratorName = field.Name.Text + "_Iter"; var iterator = new Variable(ScalarType.Int, iteratorName, new LiteralExpression(0)); var start = new DeclarationStatement(iterator); var condition = new BinaryExpression(BinaryOperator.Less, new VariableReferenceExpression(iterator), (field.Type as ArrayType).Dimensions[0]); var next = new UnaryExpression(UnaryOperator.PreIncrement, new VariableReferenceExpression(iterator)); var forLoop = new ForStatement(start, condition, next); var fieldAssigner = new StreamFieldVisitor(field, new VariableReferenceExpression(iterator)); var clonedExpression = fieldAssigner.Run(ParadoxAssignmentCloner.Run(initialValue)); forLoop.Body = new ExpressionStatement( new AssignmentExpression( AssignmentOperator.Default, new IndexerExpression(new MemberReferenceExpression(new VariableReferenceExpression(streamName), currentField.Name), new VariableReferenceExpression(iterator)), clonedExpression)); yield return forLoop; } else { var fieldAssigner = new StreamFieldVisitor(field); //var clonedExpression = fieldAssigner.Run(initialValue.DeepClone()); var clonedExpression = fieldAssigner.Run(ParadoxAssignmentCloner.Run(initialValue)); yield return new ExpressionStatement( new AssignmentExpression( AssignmentOperator.Default, new MemberReferenceExpression(new VariableReferenceExpression(streamName), currentField.Name), clonedExpression)); } } } } }
/// <summary> /// Inserts the break variable in the flow of the loop /// </summary> /// <param name="breakFlag">the break variable</param> protected void TransformBreaks(Variable breakFlag) { var breakTest = new UnaryExpression(UnaryOperator.LogicalNot, new VariableReferenceExpression(breakFlag)); scopeList.Reverse(); foreach (var breakScope in scopeList) { for (int i = 0; i < breakScope.Count - 1; ++i) { var currentScope = breakScope[i]; var nextScope = breakScope[i+1]; if (currentScope is StatementList) { var typedScope = currentScope as StatementList; var index = typedScope.Statements.IndexOf(nextScope); if (index == -1) { parserResult.Error("unable to find the next scope when replacing break/continue", nextScope.Span); break; } var testBlock = new IfStatement(); testBlock.Condition = breakTest; var thenBlock = new StatementList(); for (int j = index + 1; j < typedScope.Statements.Count; ++j) thenBlock.Add(typedScope.Statements[j]); testBlock.Then = thenBlock; typedScope.Statements.RemoveRange(index + 1, typedScope.Statements.Count - index - 1); if (typedScope.Statements.Count > 0 && i != breakScope.Count - 2) // do not add the statements behind the break/continue typedScope.Statements.Add(testBlock); } } var last = breakScope.LastOrDefault() as ExpressionStatement; if (last != null) last.Expression = new AssignmentExpression(AssignmentOperator.Default, new VariableReferenceExpression(breakFlag), new LiteralExpression(true)); } }