Ejemplo n.º 1
0
    public static string PrintExpr(AstExpr astRoot)
    {
        switch (astRoot.m_exprk)
        {
        case EXPRK.Binary:
        {
            AstBinaryExpr expr = (AstBinaryExpr)astRoot;
            return("( " + expr.m_tokenkOp.ToString() + " " + PrintExpr(expr.m_leftExpr) + " " + PrintExpr(expr.m_rightExpr) + " )");
        } break;

        case EXPRK.Group:
        {
            AstGroupExpr group = (AstGroupExpr)astRoot;
            return("( " + PrintExpr(group.m_expr) + " )");
        } break;

        case EXPRK.Literal:
        {
            AstLiteralExpr expr = (AstLiteralExpr)astRoot;
            return(expr.m_value.ToString());
        } break;

        case EXPRK.Unary:
        {
            AstUnaryExpr expr = (AstUnaryExpr)astRoot;
            return("( " + expr.m_tokenkOp.ToString() + " " + PrintExpr(expr.m_expr) + " )");
        } break;

        default:
        {
            Debug.Fail("Print not implemented for Ast Node type: " + astRoot.m_astnodek);
            return("<error>");
        } break;
        }
    }
Ejemplo n.º 2
0
    protected object EvaluateUnaryExpr(AstUnaryExpr expr)
    {
        if (HadErrorOrReturn())
        {
            return(null);
        }

        object value = EvaluateExpr(expr.m_expr);

        switch (expr.m_tokenkOp)
        {
        case TOKENK.Bang:
        {
            return(!IsTruthy(value));
        } break;

        case TOKENK.Minus:
        {
            double?numVal = value as double?;

            if (numVal.HasValue)
            {
                return(-numVal.Value);
            }

            m_runtimeError = true;
            Lox.Error(expr.m_startLine, "Unary - operator can only be applied to numbers");
        } break;

        default:
        {
            m_runtimeError = true;
            Lox.InternalError(expr.m_startLine, "Unary expression has unexpected operator: " + expr.m_tokenkOp);
        } break;
        }

        Debug.Assert(m_runtimeError);
        return(null);
    }
Ejemplo n.º 3
0
 public virtual ReturnType VisitUnaryExpr(AstUnaryExpr expr, DataType data             = default) => default;
Ejemplo n.º 4
0
 protected void ResolveUnaryExpr(AstUnaryExpr expr)
 {
     ResolveExpr(expr.m_expr);
 }
Ejemplo n.º 5
0
        public override string VisitUnaryExpr(AstUnaryExpr bin, int data = 0)
        {
            var sub = bin.SubExpr.Accept(this, 0);

            return(bin.Operator + sub);
        }