Beispiel #1
0
        public static ForthPrimativeResult Execute(ForthPrimativeParameters parameters)
        {
            /*
             * ROTATE ( ni ... n1 i -- n(i-1) ... n1 ni )
             * Rotates the top i things on the stack. Using a negative rotational value rotates backwards. Examples:
             *  "a"  "b"  "c"  "d"  4  rotate
             * would leave
             *  "b"  "c"  "d"  "a"
             * on the stack.
             *  "a"  "b"  "c"  "d"  -4  rotate
             * would leave
             *  "d" "a" "b" "c" on the stack.
             */
            if (parameters.Stack.Count == 0)
            {
                return(new ForthPrimativeResult(ForthErrorResult.STACK_UNDERFLOW, "ROTATE requires at least one parameter"));
            }

            var si = parameters.Stack.Pop();

            if (si.Type != DatumType.Integer)
            {
                return(new ForthPrimativeResult(ForthErrorResult.TYPE_MISMATCH, "ROTATE requires the top parameter on the stack to be an integer"));
            }

            var i  = si.UnwrapInt();
            var ai = Math.Abs(i);

            if (parameters.Stack.Count < ai)
            {
                return(new ForthPrimativeResult(ForthErrorResult.STACK_UNDERFLOW, $"ROTATE would have rotated {ai} items on the stack, but only {parameters.Stack.Count} were present."));
            }

            var data = new ForthDatum[ai];

            for (int n = 0; n < ai; n++)
            {
                data[n] = parameters.Stack.Pop();
            }

            if (i > 0)
            {
                for (int n = ai - 2; n >= 0; n--)
                {
                    parameters.Stack.Push(data[n]);
                }
                parameters.Stack.Push(data[ai - 1]);
            }
            else
            {
                parameters.Stack.Push(data[0]);
                for (int n = ai - 1; n > 0; n--)
                {
                    parameters.Stack.Push(data[n]);
                }
            }

            return(ForthPrimativeResult.SUCCESS);
        }
Beispiel #2
0
        public static ForthPrimativeResult Execute(ForthPrimativeParameters parameters)
        {
            /*
             * INT ( x -- i )
             * Converts variable, float, or dbref x to integer i.
             */
            if (parameters.Stack.Count < 1)
            {
                return(new ForthPrimativeResult(ForthErrorResult.STACK_UNDERFLOW, "INT requires one parameter"));
            }

            ForthDatum datum;

            var reference = parameters.Stack.Pop();

            if (reference.Type == DatumType.Unknown)
            {
                // Resolve variable
                var variableName = reference.Value.ToString().ToLowerInvariant();
                var variable     = At.ResolveVariableByName(parameters.Variables, parameters.Player, parameters.Location, parameters.Trigger, parameters.Command, variableName);

                if (default(ForthVariable).Equals(variable) && !parameters.Variables.ContainsKey(variableName))
                {
                    return(new ForthPrimativeResult(ForthErrorResult.VARIABLE_NOT_FOUND, $"No variable named {variableName} was found"));
                }

                if (default(ForthVariable).Equals(variable))
                {
                    return(new ForthPrimativeResult(ForthErrorResult.UNKNOWN_TYPE, $"Unable to determine data type for: {variable.Value}"));
                }

                datum = new ForthDatum(variable);
            }
            else
            {
                datum = reference;
            }

            parameters.Stack.Push(datum.ToInteger());
            return(ForthPrimativeResult.SUCCESS);
        }