/// <summary> /// Returns <c>true</c> if the expression is a unary operation. /// </summary> public static bool IsUnOp(this Expression e, UnOp.Kind op) { UnOp uop = e as UnOp; if (uop == null) return false; return uop.Operation == op; }
public Expression TransformUnOp(UnOp x) { Expression dx0 = der(x.Operand); switch (x.Operation) { case UnOp.Kind.Abs: return(Expression.Conditional( Expression.LessThan(x, SpecialConstant.ScalarZero), -dx0, dx0)); case UnOp.Kind.BitwiseNot: case UnOp.Kind.BoolNot: throw new NotImplementedException(); case UnOp.Kind.Exp: return(dx0 * x); case UnOp.Kind.ExtendSign: throw new NotImplementedException(); case UnOp.Kind.Identity: return(x); case UnOp.Kind.Log: return(dx0 / x); case UnOp.Kind.Neg: return(-dx0); default: throw new NotImplementedException(); } }
/// <summary> /// Returns <c>true</c> if the expression is a unary operation. /// </summary> public static bool IsUnOp(this Expression e, UnOp.Kind op) { UnOp uop = e as UnOp; if (uop == null) { return(false); } return(uop.Operation == op); }
public Expression TransformUnOp(UnOp expr) { if (expr.IsConst()) { object result = expr.Eval(new DefaultEvaluator()); return(LiteralReference.CreateConstant(result)); } else { return(expr); } }
private string GenerateExpr(Expression e) { return(e switch { BinOp binop => GenerateBinExpr(binop), UnOp unop => GenerateUnExpr(unop), ConstExpression constExpression => GenerateConstExpr(constExpression), VarExpression varExpression => GenerateVarExpr(varExpression), CallExpression callExpression => GenerateCallExpression(callExpression), ConditionalExpression conditionalExpression => GenerateConditionalExpression(conditionalExpression), _ => throw new CompilerException(e.GetType().ToString(), e.Row, e.Column) });
public override void CollectDataFromChildren() { var numberNode = FindFirstChildNodeByType <NumberNode>(); var stringLiteralNode = FindFirstChildNodeByType <StringLiteralNode>(); var idNod = FindFirstChildNodeByType <IdNode>(); VariablesNames = new List <string>(); _unOp = FindFirstChildNodeByType <UnOpNode>().UnOp; _childOperator = FindFirstChildNodeByType <OperatorNode>(); if (numberNode != null) { switch (numberNode.NumberType) { case NumberType.Double: Value = numberNode.NumberDouble; break; case NumberType.Int: Value = numberNode.NumberInt; break; } ; IsCompressed = true; } if (stringLiteralNode != null) { Value = stringLiteralNode.StringLiteral; IsCompressed = true; } if (idNod != null) { _variableName = idNod.Id.ToString(); VariablesNames.Add(_variableName.ToString()); ConstOnly = false; IsCompressed = true; AddVariableBorder(_variableName, new VariableBorder()); } if (_childOperator != null) { ConstOnly = _childOperator.ConstOnly; IsCompressed = ConstOnly; GetAllValuesNamesFromNode(_childOperator); } }
private string GenerateUnExpr(UnOp e) { string code = ""; var expr = GenerateExpr(e.Expression); if (e.Op == TokenKind.MINUS) { code = expr + $"\npop eax\nneg eax\npush eax\n"; } else { throw new CompilerException($"Sorry, but {e.Op.ToString()} not implemented yet"); } return(code); }
/// <summary> /// Creates a matching for the specified kind of unary operations. /// </summary> /// <param name="kind">kind of unary operation to match</param> public Matching MUnOp(UnOp.Kind kind) { UnOp cmp = new UnOp() { Operation = kind }; Matching newm = new Matching(); newm._func = e => e.NodeEquals(cmp) && this.Match(e.Children.ElementAt(0)); newm._gen = () => newm._expr == null ? new UnOp() { Operation = kind, Operand = _gen() } : newm._expr; return(newm); }
public override void CollectDataFromChildren() { switch ((ChildNodes.First <ISqlNode>() as SqlKeyNode).Text.ToUpper()) { case "+": UnOp = UnOp.Plus; break; case "-": UnOp = UnOp.Minus; break; case "~": UnOp = UnOp.Tilde; break; case "NOT": UnOp = UnOp.Not; break; } }
public int TransformUnOp(UnOp expr) { expr.Children[0].Accept(this); ExtractCILIndex(expr); if (expr.Operation == UnOp.Kind.Identity) { return(0); } XILInstr xi; switch (expr.Operation) { case UnOp.Kind.Abs: xi = ISet.Abs(); break; case UnOp.Kind.BitwiseNot: case UnOp.Kind.BoolNot: xi = ISet.Not(); break; case UnOp.Kind.ExtendSign: xi = ISet.ExtendSign(); break; case UnOp.Kind.Neg: xi = ISet.Neg(); break; case UnOp.Kind.Cos: xi = ISet.Cos(); break; case UnOp.Kind.Sin: xi = ISet.Sin(); break; case UnOp.Kind.Sqrt: xi = ISet.Sqrt(); break; case UnOp.Kind.Exp: case UnOp.Kind.Log: default: throw new NotImplementedException(); } Emit(xi, expr, 1, expr.ResultType); return(0); }
/// <summary> /// Transforms a unary expression. The default implementation clones it. /// </summary> /// <param name="expr">unary expression</param> /// <returns>transformation result</returns> public virtual Expression TransformUnOp(UnOp expr) { return expr.CloneThis(expr.Children.Select(e => e.Transform(this)).ToArray()); }
/// <summary> /// Creates a new unary operation /// </summary> /// <param name="token">The token where the operation was found</param> /// <param name="operation">The operation</param> public UnaryOperation(ParseToken token, UnOp operation) : base(token) { Operation = operation; }
public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder) { object[] vargs = args.Select(arg => arg.Sample).ToArray(); Expression[] eargs = args.Select(arg => arg.Expr).ToArray(); object sample = null; try { sample = callee.Invoke(vargs); } catch (Exception) { } Expression result = new UnOp() { Operation = Kind }; Array.Copy(eargs, result.Children, 1); if (sample != null) result.ResultType = TypeDescriptor.GetTypeOf(sample); else { Type rtype; callee.IsFunction(out rtype); result.ResultType = (TypeDescriptor)rtype; } stack.Push(result, sample); return true; }
/// <summary> /// Creates the attribute. /// </summary> /// <param name="kind">kind of unary operation</param> public MapToUnOp(UnOp.Kind kind) { Kind = kind; }
/// <summary> /// Constructs a generator for unary expressions. /// </summary> /// <param name="kind">kind of unary expression to construct</param> /// <param name="g">operand generator</param> public static ExpressionGenerator UnOp(UnOp.Kind kind, ExpressionGenerator g) { return () => new UnOp() { Operation = kind, Operand = g() }; }
public NodeUnOp(UnOp type, AstNode val) : base("Unary Operation", NodeType.Operation) { Type = type; AddChild (val); }
public NotateFunc GetNotation(UnOp.Kind op) { switch (op) { case UnOp.Kind.Abs: return DefaultNotators.Prefix("abs "); case UnOp.Kind.BitwiseNot: return DefaultNotators.Prefix("not "); case UnOp.Kind.BoolNot: return DefaultNotators.Prefix("not "); case UnOp.Kind.Exp: return DefaultNotators.Function("EXP"); case UnOp.Kind.ExtendSign: return DefaultNotators.Function("XTS"); case UnOp.Kind.Identity: return DefaultNotators.Prefix(""); case UnOp.Kind.Log: return DefaultNotators.Function("LOG"); case UnOp.Kind.Neg: return DefaultNotators.Prefix("-"); case UnOp.Kind.Sin: return DefaultNotators.Function("SIN"); case UnOp.Kind.Cos: return DefaultNotators.Function("COS"); case UnOp.Kind.Sqrt: return DefaultNotators.Function("SQRT"); default: throw new NotImplementedException(); } }
public EOperatorAssociativity GetOperatorAssociativity(UnOp.Kind op) { return EOperatorAssociativity.UseParenthesis; }
public int GetOperatorOrder(UnOp.Kind op) { switch (op) { case UnOp.Kind.Abs: return 0; case UnOp.Kind.BitwiseNot: return 0; case UnOp.Kind.BoolNot: return 0; case UnOp.Kind.Exp: return 0; case UnOp.Kind.ExtendSign: return -1; case UnOp.Kind.Identity: return -1; case UnOp.Kind.Log: return -1; case UnOp.Kind.Neg: return 2; case UnOp.Kind.Sin: return -1; case UnOp.Kind.Cos: return -1; case UnOp.Kind.Sqrt: return -1; default: throw new NotImplementedException(); } }
public virtual Result Visit(UnOp unOp) { return(default(Result)); }
/// <summary> /// Creates a matching for the specified kind of unary operations. /// </summary> /// <param name="kind">kind of unary operation to match</param> public Matching MUnOp(UnOp.Kind kind) { UnOp cmp = new UnOp() { Operation = kind }; Matching newm = new Matching(); newm._func = e => e.NodeEquals(cmp) && this.Match(e.Children.ElementAt(0)); newm._gen = () => newm._expr == null ? new UnOp() { Operation = kind, Operand = _gen() } : newm._expr; return newm; }
// Simplified version public NotateFunc GetNotation(UnOp.Kind op) { switch (op) { case UnOp.Kind.BitwiseNot: return DefaultNotators.Prefix("~"); case UnOp.Kind.BoolNot: return DefaultNotators.Prefix("!"); case UnOp.Kind.Exp: return DefaultNotators.Function("exp"); case UnOp.Kind.ExtendSign: return DefaultNotators.Function("XTS"); case UnOp.Kind.Identity: return DefaultNotators.Prefix(""); case UnOp.Kind.Log: return DefaultNotators.Function("log"); case UnOp.Kind.Neg: return DefaultNotators.Prefix("-"); case UnOp.Kind.Sin: return DefaultNotators.Function("sin"); case UnOp.Kind.Cos: return DefaultNotators.Function("cos"); default: throw new NotImplementedException(); } }
/// <summary> /// Transforms a unary expression. The default implementation clones it. /// </summary> /// <param name="expr">unary expression</param> /// <returns>transformation result</returns> public virtual Expression TransformUnOp(UnOp expr) { return(expr.CloneThis(expr.Children.Select(e => e.Transform(this)).ToArray())); }
public bool TransformUnOp(UnOp expr) { return(expr.Operand.Accept(this)); }
public void VisitUnOp(UnOp n) { // Todo ? }
public NodeUnOp(UnOp type, AstNode val) : base("Unary Operation", NodeType.Operation) { Type = type; AddChild(val); }