public object VisitUnary(UnaryExpr expr) { return null; }
/// <summary> /// Evaluate /// </summary> /// <returns></returns> public object VisitUnary(UnaryExpr expr) { // Logical not? if (expr.Op == Operator.LogicalNot) return ComLib.Lang.Runtime.EvalHelper.HandleLogicalNot(expr, this); var valobj = (LObject)expr.Ctx.Memory.Get<object>(expr.Name); // Double ? if (valobj.Type == LTypes.Number) return ComLib.Lang.Runtime.EvalHelper.IncrementNumber(expr, (LNumber)valobj, this); // String ? if (valobj.Type == LTypes.String) return ComLib.Lang.Runtime.EvalHelper.IncrementString(expr, (LString)valobj, this); throw new LangException("Syntax Error", "Unexpected operation", expr.Ref.ScriptName, expr.Ref.Line, expr.Ref.CharPos); }
/// <summary> /// Creates a unary expression with symbol scope, context, script refernce set. /// </summary> /// <param name="name"></param> /// <param name="token"></param> /// <returns></returns> public static Expr Unary(string name, Expr incExpr, double incValue, Operator op, TokenData token) { var exp = new UnaryExpr(); exp.Name = name; exp.Op = op; exp.Increment = incValue; exp.Expression = incExpr; SetupContext(exp, token); return exp; }
public static LString IncrementString(UnaryExpr expr, LString sourceVal, IAstVisitor visitor) { // Check 1: Can only do += on strings. if (expr.Op != Operator.PlusEqual) throw new LangException("Syntax Error", "string operation with " + expr.Op.ToString() + " not supported", expr.Ref.ScriptName, expr.Ref.Line, expr.Ref.CharPos); //expr.DataType = typeof(string); var val = expr.Expression.Evaluate(visitor) as LObject; // Check 2: Check for null if (val == LObjects.Null) return sourceVal; // Check 3: Limit size if string Ctx.Limits.CheckStringLength(expr, sourceVal.Value, val.GetValue().ToString()); // Finally do the appending. var appended = sourceVal.Value + val.GetValue().ToString(); sourceVal.Value = appended; expr.Ctx.Memory.SetValue(expr.Name, sourceVal); return sourceVal; }
public static LNumber IncrementNumber(UnaryExpr expr, LNumber val, IAstVisitor visitor) { var inc = expr.Increment == 0 ? 1 : expr.Increment; if (expr.Expression != null) { var incval = expr.Expression.Evaluate(visitor); // TODO: Check if null and throw langexception? inc = ((LNumber)incval).Value; } // 1. Calculate the unary value val = ComLib.Lang.Runtime.EvalHelper.CalcUnary(val, expr.Op, inc); // 2. Set the value back into scope expr.Ctx.Memory.SetValue(expr.Name, val); return val; }
public static object HandleLogicalNot(UnaryExpr expr, IAstVisitor visitor) { var result = expr.Expression.Evaluate(visitor) as LObject; // Check 1: This is actually an assert and should not happen. if (result == null) throw ExceptionHelper.BuildRunTimeException(expr, "Null value encountered"); var retVal = false; // Only handle bool for logical not !true !false if (result.Type == LTypes.Bool) retVal = !((LBool)result).Value; else if (result == LObjects.Null) retVal = true; return new LBool(retVal); }