public PointObject() { var keyX = new NameKey("x"); var localX = MemberDefinition.CreateAndBuild(keyX, new AnyType(), Access.ReadWrite); var keyY = new NameKey("y"); var localY = MemberDefinition.CreateAndBuild(keyY, new AnyType(), Access.ReadWrite); RootScope = Model.Instantiated.RootScope.CreateAndBuild( Scope.CreateAndBuild( new List <IsStatic>() { new IsStatic(MemberDefinition.CreateAndBuild(new NameKey("point"), new AnyType(), Access.ReadWrite), false) }), new[] { AssignOperation.CreateAndBuild( ObjectDefiniton.CreateAndBuild( Scope.CreateAndBuild( new List <IsStatic> { new IsStatic(localX, false), new IsStatic(localY, false) }), new IAssignOperation[] { AssignOperation.CreateAndBuild( ConstantNumber.CreateAndBuild(5), MemberReference.CreateAndBuild(localX)), AssignOperation.CreateAndBuild( ConstantNumber.CreateAndBuild(2), MemberReference.CreateAndBuild(localY)) }, InterfaceType.CreateAndBuild(new [] { localX, localY })), MemberReference.CreateAndBuild(MemberDefinition.CreateAndBuild(new NameKey("point"), new AnyType(), Access.ReadWrite))) }, EntryPointDefinition.CreateAndBuild(new EmptyType(), MemberDefinition.CreateAndBuild(new NameKey("input"), new NumberType(), Access.ReadWrite), Scope.CreateAndBuild(Array.Empty <IsStatic>()), Array.Empty <ICodeElement>(), Array.Empty <ICodeElement>())); }
protected override IDataFlowGraphExpressionNode Visit(ConstantNumber con) { var op = new InputConst(Guid.NewGuid(), new Value(con.Value)); _dataFlowGraph._inputs.Add(op); return(op); }
public IBuildIntention <IConstantNumber> GetBuildIntention(IConversionContext context) { var(toBuild, maker) = ConstantNumber.Create(); return(new BuildIntention <IConstantNumber>(toBuild, () => { maker.Build( Value.GetOrThrow()); })); }
static BaseExpression HandleExpression(BaseExpression right) { return(right switch { Variable v => new Increment(v.Name), ConstantNumber n => new ConstantNumber(new Add(n, new ConstantNumber((Number)1)).StaticEvaluate().Number), ConstantString s => new ConstantString(s.Value + " "), Bracketed b => HandleExpression(b.Parameter), _ => throw new InvalidOperationException(right.GetType().Name) });
protected override BaseExpression Visit(ConstantNumber con) { if (_replacements.TryGetValue(new Value(con.Value), out var name)) { return(new Variable(new VariableName(name))); } else { return(con); } }
protected override BaseExpression Visit(Bracketed brk) { var inner = Visit(brk.Parameter); return(inner switch { Variable v => v, ConstantNumber n => n, ConstantString s => s, Bracketed b => b, _ => base.Visit(new Bracketed(inner)), });
public virtual TResult Visit(BaseExpression expression) { return(expression switch { Phi a => Visit(a), Increment a => Visit(a), Decrement a => Visit(a), ErrorExpression a => Visit(a), Bracketed a => Visit(a), Abs a => Visit(a), Sqrt a => Visit(a), Sine a => Visit(a), Cosine a => Visit(a), Tangent a => Visit(a), ArcSine a => Visit(a), ArcCos a => Visit(a), ArcTan a => Visit(a), PostIncrement a => Visit(a), PreIncrement a => Visit(a), PostDecrement a => Visit(a), PreDecrement a => Visit(a), Add a => Visit(a), Subtract a => Visit(a), Multiply a => Visit(a), Divide a => Visit(a), Modulo a => Visit(a), Negate a => Visit(a), Exponent a => Visit(a), Or a => Visit(a), And a => Visit(a), Not a => Visit(a), Factorial a => Visit(a), Variable a => Visit(a), ConstantNumber a => Visit(a), ConstantString a => Visit(a), EqualTo a => Visit(a), NotEqualTo a => Visit(a), GreaterThan a => Visit(a), GreaterThanEqualTo a => Visit(a), LessThan a => Visit(a), LessThanEqualTo a => Visit(a), _ => VisitUnknown(expression) });
[NotNull] private BaseStatement Replace([NotNull] If @if) { if (@if.FalseBranch.Statements.Count != 0) { return(@if); } if (@if.TrueBranch.Statements.Count != 1) { return(@if); } if (!(@if.TrueBranch.Statements.Single() is Goto @goto)) { return(@if); } // Replace: // `if A then goto B end <fallthrough to next line>` // With: // `goto B + (Next_Line * (A != 0))` var condition = @if.Condition.IsBoolean ? Invert(@if.Condition) : new Bracketed(new EqualTo(new Bracketed(@if.Condition), new ConstantNumber(0))); BaseExpression diff = new Bracketed(new Subtract(new ConstantNumber(_lineNumber + 1), @goto.Destination)); if (diff.IsConstant) { diff = new ConstantNumber(diff.StaticEvaluate().Number); } var dest2 = new Add(@goto.Destination, new Multiply(diff, new Bracketed(condition))); return(new Goto(dest2)); }
protected override BaseExpression Visit(ConstantNumber con) { if (con.Value.ToString().Length <= 3) { return(base.Visit(con)); } var replacements = new[] { SmallestExponents(con.Value), BestFraction(con.Value) }; var shortestLength = int.MaxValue; BaseExpression shortest = null; foreach (var replacement in replacements) { if (replacement == null) { continue; } var l = replacement.ToString().Length; if (l < shortestLength) { shortestLength = l; shortest = replacement; } } if (shortest != null && shortestLength < con.ToString().Length) { return(base.Visit(shortest)); } return(base.Visit(con)); }
protected override bool Visit(ConstantNumber con) => StaticFalse(con);
public Factorial() { var ifBlockScope = Scope.CreateAndBuild(new List <Scope.IsStatic> { }); var elseBlock = Scope.CreateAndBuild(new List <Scope.IsStatic> { }); var inputKey = new NameKey("input"); var input = MemberDefinition.CreateAndBuild(inputKey, new NumberType(), false); var facKey = new NameKey("fac"); var fac = MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild(new NumberType(), new NumberType()), false); var methodScope = Scope.CreateAndBuild(new List <Scope.IsStatic> { new Scope.IsStatic(input, false) }); Module = ModuleDefinition.CreateAndBuild( Scope.CreateAndBuild( new List <Scope.IsStatic> { new Scope.IsStatic(MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild(new NumberType(), new NumberType()), false), false) }), new ICodeElement[] { AssignOperation.CreateAndBuild( MethodDefinition.CreateAndBuild( new NumberType(), new NumberType(), input, methodScope, new ICodeElement[] { ElseOperation.CreateAndBuild( IfOperation.CreateAndBuild( LessThanOperation.CreateAndBuild( MemberReference.CreateAndBuild(input), ConstantNumber.CreateAndBuild(2)), BlockDefinition.CreateAndBuild( ifBlockScope, new ICodeElement[] { ReturnOperation.CreateAndBuild( ConstantNumber.CreateAndBuild(1)) }, new ICodeElement[0])), BlockDefinition.CreateAndBuild( elseBlock, new ICodeElement[] { ReturnOperation.CreateAndBuild( MultiplyOperation.CreateAndBuild( NextCallOperation.CreateAndBuild( SubtractOperation.CreateAndBuild( MemberReference.CreateAndBuild(input), ConstantNumber.CreateAndBuild(1)), MemberReference.CreateAndBuild(fac)), MemberReference.CreateAndBuild(input))) }, new ICodeElement[0])) }, new ICodeElement[0], false), MemberReference.CreateAndBuild(fac) ) }, new NameKey("factorial") ); }
protected override Type Visit(ConstantNumber con) => Type.Number;
protected override BaseExpression Visit(ConstantNumber con) { _constantCount.AddOrUpdate(new Value(con.Value), 1, (_, a) => a + 1); return(base.Visit(con)); }
[NotNull] protected abstract TResult Visit([NotNull] ConstantNumber con);
protected override BaseExpression Visit(ConstantNumber con) { using (AppendLine(con.Value.ToString())) return(base.Visit(con)); }
[NotNull] protected virtual BaseExpression Visit([NotNull] ConstantNumber con) { return(con); }
public Factorial() { var ifBlockScope = Scope.CreateAndBuild(new List <IsStatic> { }); var elseBlock = Scope.CreateAndBuild(new List <IsStatic> { }); var inputKey = new NameKey("input"); var input = MemberDefinition.CreateAndBuild(inputKey, new NumberType(), Access.ReadWrite); var facKey = new NameKey("fac"); var fac = MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild(new NumberType(), new NumberType()), Access.ReadWrite); var methodScope = Scope.CreateAndBuild(new List <IsStatic> { new IsStatic(input, false) }); RootScope = Model.Instantiated.RootScope.CreateAndBuild( Scope.CreateAndBuild( new List <IsStatic> { new IsStatic(MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild( new NumberType(), new NumberType()), Access.ReadWrite), false) }), new [] { AssignOperation.CreateAndBuild( MethodDefinition.CreateAndBuild( new NumberType(), input, methodScope, new ICodeElement[] { ElseOperation.CreateAndBuild( IfOperation.CreateAndBuild( LessThanOperation.CreateAndBuild( MemberReference.CreateAndBuild(input), ConstantNumber.CreateAndBuild(2)), BlockDefinition.CreateAndBuild( ifBlockScope, new ICodeElement[] { ReturnOperation.CreateAndBuild( ConstantNumber.CreateAndBuild(1)) }, Array.Empty <ICodeElement>())), BlockDefinition.CreateAndBuild( elseBlock, new ICodeElement[] { ReturnOperation.CreateAndBuild( MultiplyOperation.CreateAndBuild( NextCallOperation.CreateAndBuild( SubtractOperation.CreateAndBuild( MemberReference.CreateAndBuild(input), ConstantNumber.CreateAndBuild(1)), MemberReference.CreateAndBuild(fac)), MemberReference.CreateAndBuild(input))) }, Array.Empty <ICodeElement>())) }, Array.Empty <ICodeElement>()), MemberReference.CreateAndBuild(fac) ) }, EntryPointDefinition.CreateAndBuild(new EmptyType(), MemberDefinition.CreateAndBuild(new NameKey("input"), new NumberType(), Access.ReadWrite), Scope.CreateAndBuild(Array.Empty <IsStatic>()), Array.Empty <ICodeElement>(), Array.Empty <ICodeElement>()) ); }
protected override bool Visit(ConstantNumber con) => true;
protected override IEnumerable <BaseStatement> Visit(ConstantNumber con) => new[] { new Assignment(MkTmp(), con) };