protected override ICode VisitUnary(ExprUnary e)
        {
            var op   = e.Op;
            var expr = (Expr)this.Visit(e.Expr);

            if (op == UnaryOp.Not)
            {
                if (expr.ExprType == Expr.NodeType.Binary)
                {
                    var eBin = (ExprBinary)expr;
                    if (eBin.Op == BinaryOp.Equal)
                    {
                        return(e.Ctx.ExprGen.NotEqual(eBin.Left, eBin.Right));
                    }
                    if (eBin.Op == BinaryOp.NotEqual)
                    {
                        return(e.Ctx.ExprGen.Equal(eBin.Left, eBin.Right));
                    }
                }
            }

            if (expr != e.Expr)
            {
                return(new ExprUnary(e.Ctx, e.Op, e.Type, expr));
            }
            else
            {
                return(e);
            }
        }
Exemplo n.º 2
0
        protected override ICode VisitUnary(ExprUnary e)
        {
            if (e.Expr.Type.IsInt64() || e.Expr.Type.IsUInt64())
            {
                var      ctx    = e.Ctx;
                var      signed = e.Expr.Type.IsInt64();
                Delegate d;
                switch (e.Op)
                {
                case UnaryOp.Negate:
                    var zero    = ctx.Literal(0L, ctx.Int64);
                    var subCall = new ExprBinary(ctx, BinaryOp.Sub, ctx.Int64, zero, e.Expr);
                    return(subCall);

                case UnaryOp.BitwiseNot:
                    d = signed ? (Delegate)(Func <Int64, Int64>)_Int64.BitwiseNot : (Func <UInt64, UInt64>)_UInt64.BitwiseNot;
                    break;

                default:
                    throw new NotImplementedException("Cannot handle: " + e.Op);
                }
                var m    = ctx.Module.Import(d.Method);
                var expr = (Expr)this.Visit(e.Expr);
                var call = new ExprCall(ctx, m, null, expr);
                return(call);
            }
            return(base.VisitUnary(e));
        }
Exemplo n.º 3
0
 public string Visit(ExprUnary node)
 {
     if (node.AnchorToken.Lexeme.Equals("!"))
     {
         var lbl = GenerateLabel();
         return
             (Line(Indent() + "ldc.i4.0") +
              VisitChildren(node) +
              Line(Indent() + "ldc.i4 42") +
              Line(Indent() + "beq '" + lbl + "'") +
              Line(Indent() + "pop") +
              Line(Indent() + "ldc.i4 42") +
              Line(Indent() + "'" + lbl + "':"));
     }
     if (node.AnchorToken.Lexeme.Equals("-"))
     {
         return
             (Line(Indent() + "ldc.i4.0") +
              VisitChildren(node) +
              Line(Indent() + "sub"));
     }
     if (node.AnchorToken.Lexeme.Equals("+"))
     {
         return(VisitChildren(node));
     }
     return(VisitChildren(node));
 }
Exemplo n.º 4
0
            public Stmt GetImpl(Ctx ctx)
            {
                var a       = ctx.MethodParameter(0, "a");
                var b       = ctx.MethodParameter(1, "b");
                var neg     = ctx.Local(ctx.Boolean, "neg");
                var aNegate = new ExprUnary(ctx, UnaryOp.Negate, ctx.Int64, a.Expr).Named("aNegate");
                var bNegate = new ExprUnary(ctx, UnaryOp.Negate, ctx.Int64, b.Expr).Named("bNegate");
                var divMod  = new ExprCall(ctx, (Func <UInt64, UInt64, object>)_Int64UInt64.UInt64DivRem, null, a.Expr, b.Expr).Named("divMod");
                var r       = ctx.Local(ctx.Int64, "r");
                var rNegate = new ExprUnary(ctx, UnaryOp.Negate, ctx.Int64, r.Expr).Named("rNegate");
                // TODO: Throw ArithmeticException if a == Int64.MinValue and b == -1
                // TODO: Handle a or b being Int64.MinValue
                var js   = @"
neg = false;
if (a[0] >>> 31) {
    a = aNegate;
    neg = true;
}
if (b[0] >>> 31) b = bNegate;
r = divMod[1];
return neg ? rNegate : r;
";
                var stmt = new StmtJsExplicit(ctx, js, a, b, neg, r, aNegate, bNegate, divMod, rNegate);

                return(stmt);
            }
Exemplo n.º 5
0
            protected override void BooleanVisitNot(ExprUnary e)
            {
                base.BooleanVisitNot(e);
                var value = this.stack.Pop();

                this.stack.Push(!value);
            }
Exemplo n.º 6
0
 protected override ICode VisitUnary(ExprUnary e)
 {
     this.js.Append("(");
     this.js.Append(unaryOps[e.Op]);
     this.Visit(e.Expr);
     this.js.Append(")");
     return(e);
 }
Exemplo n.º 7
0
        protected virtual ICode VisitUnary(ExprUnary e)
        {
            this.ThrowOnNoOverride();
            var expr = this.Visit(e.Expr);

            if (expr != e.Expr)
            {
                return(new ExprUnary(e.Ctx, e.Op, e.Type, (Expr)expr));
            }
            else
            {
                return(e);
            }
        }
Exemplo n.º 8
0
        public Node ExprUnary()
        {
            //< ‹op-unary› >* ‹expr-primary›
            var top  = new ExprUnary();
            var temp = top; //reference _magic_

            if (firstOfExprUnary.Contains(CurrentToken))
            {
                while (firstOfExprUnary.Contains(CurrentToken))
                {
                    switch (CurrentToken)
                    {
                    case TokenCategory.PLUS:
                        temp.AnchorToken = Expect(TokenCategory.PLUS);
                        break;

                    case TokenCategory.MINUS:
                        temp.AnchorToken = Expect(TokenCategory.MINUS);
                        break;

                    case TokenCategory.NOT:
                        temp.AnchorToken = Expect(TokenCategory.NOT);
                        break;

                    default:
                        throw new SyntaxError(firstOfExprUnary, tokenStream.Current);
                    }
                    if (!firstOfExprUnary.Contains(CurrentToken))
                    {
                        temp.Add(ExprPrimary());
                    }
                    else
                    {
                        var newNode = new ExprUnary(); //ToBeReplacedPrimary or next Unary
                        temp.Add(newNode);
                        temp = newNode;
                    }
                }
            }
            else
            {
                return(ExprPrimary()); //replace the invisible current node for the real Primary
            }
            return(top);               //Always return the head
        }
 public void Visit(ExprUnary node)
 {
     VisitChildren(node);
 }
Exemplo n.º 10
0
 protected virtual void BooleanVisitNot(ExprUnary e)
 {
     this.Visit(e.Expr);
 }