Exemplo n.º 1
0
        public static object SliceNotationVariable(Expr callee, Token token, Interpreter interpreter, object first, object second, object third)
        {
            //bounds checking
            if (!(first is double) || ((double)first != double.NegativeInfinity && (double)first < 0) || ((double)first != double.NegativeInfinity && (double)first >= ((string)interpreter.LookupVariable(callee)).Length))
            {
                throw new ErrorHandler.RuntimeError(token, "First index must be a number and between 0 and String.Length(string) -1 (inclusive)");
            }

            if (second != null)
            {
                if (!(second is double) || ((double)second != double.PositiveInfinity && (double)second < 0) || ((double)second != double.PositiveInfinity && (double)second >= ((string)interpreter.LookupVariable(callee)).Length))
                {
                    throw new ErrorHandler.RuntimeError(token, "Second index must be a number and between 0 and String.Length(string) -1 (inclusive)");
                }
            }

            if (second == null)
            {
                //access this character only
                return(((string)interpreter.LookupVariable(callee))[(int)(double)first]);
            }

            //default values for slice notation (begin and end are inclusive)
            int begin = (double)first == double.NegativeInfinity ? 0 : (int)(double)first;
            int end   = (double)second == double.PositiveInfinity ? ((string)interpreter.LookupVariable(callee)).Length - 1 : (int)(double)second;
            int step  = third == null ? 1 : (int)(double)third;

            //check for infinite loops
            if (step == 0)
            {
                throw new ErrorHandler.RuntimeError(token, "Can't have a string step of 0");
            }

            //build the new string
            string newString = "";

            for (int index = step > 0 ? begin : end; index >= begin && index <= end; index += step)
            {
                newString += ((string)interpreter.LookupVariable(callee))[index];
            }

            return(new StringVariableAssignableIndex(callee, newString, interpreter, begin, end, token, third));
        }
Exemplo n.º 2
0
        public static object VariableProperty(Variable expr, Interpreter interpreter, Token name, string lexeme)
        {
            string str = (string)interpreter.LookupVariable(expr);

            switch (lexeme)
            {
            case "Length": return(new Length(str));

            case "ToLower": return(new ToLower(str));

            case "ToUpper": return(new ToUpper(str));

            case "Replace": return(new Replace(str));

            case "Trim": return(new Trim(str));

            case "IndexOf": return(new IndexOf(str));

            case "LastIndexOf": return(new LastIndexOf(str));

            default:
                throw new ErrorHandler.RuntimeError(name, "Unknown property '" + lexeme + "'");
            }
        }