Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            String str = "";

            if (args.Length >= 1)
            {
                str = File.ReadAllText(args[0]);
            }
            else
            {
                StringBuilder b = new StringBuilder();
                int           j = 0;
                while ((j = Console.Read()) != -1)
                {
                    b.Append((char)j);
                }

                str = b.ToString();
            }

            TSRPStack stack = new TSRPStack();

            Parser p = new Parser();

            p.AddVariable("Console", new StackConsole());
            p.AddVariable("System", new StackSystem());
            p.Parse(str);
        }
Ejemplo n.º 2
0
 public override bool Execute(string command, TSRPStack stack)
 {
     if (command == "WriteLine")
     {
         if (stack.Top is StackString)
         {
             Console.WriteLine(((StackString)stack.Pop()).Value);
         }
         return(true);
     }
     else
     if (command == "Write")
     {
         if (stack.Top is StackString)
         {
             Console.Write(((StackString)stack.Pop()).Value);
         }
         return(true);
     }
     else
     if (command == "Read")
     {
         var l = Console.ReadLine();
         stack.Push(l);
         return(true);
     }
     else
     if (command == "ReadChar")
     {
         string str = Console.ReadKey().KeyChar + "";
         stack.Push(str);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 3
0
 public override bool Execute(string command, TSRPStack stack)
 {
     if (command == "string")
     {
         stack.Pop();
         stack.Push(value);
         return(true);
     }
     return(base.Execute(command, stack));
 }
Ejemplo n.º 4
0
        private bool Xor(TSRPStack stack, bool flip = false)
        {
            if (stack.Top is StackBool && stack.FromTop(1) is StackBool)
            {
                StackBool a = (StackBool)stack.Pop();
                StackBool b = (StackBool)stack.Pop();
                var       c = Xor(a, b) ^ flip;
                stack.Push(new StackBool(c));
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        public override bool Execute(string command, TSRPStack stack)
        {
            if (command == "open_file")
            {
                if (stack.Top is StackString)
                {
                    StackString s = (StackString)stack.Pop();

                    stack.Push(new StackFile(s.Value));
                }
            }
            else if (command == "call_windows")
            {
                if (stack.Top is StackString)
                {
                    StackString s = (StackString)stack.Pop();

                    System.Diagnostics.Process          process   = new System.Diagnostics.Process();
                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    startInfo.FileName    = "cmd.exe";
                    startInfo.Arguments   = "/C " + s.Value;
                    process.StartInfo     = startInfo;
                    process.Start();
                    return(true);
                }
            }
            else if (command == "call_program")
            {
                if (stack.Top is StackString && stack.FromTop(1) is StackString)
                {
                    StackString args         = (StackString)stack.Pop();
                    StackString program_name = (StackString)stack.Pop();


                    System.Diagnostics.Process          process   = new System.Diagnostics.Process();
                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    startInfo.FileName  = program_name.Value;
                    startInfo.Arguments = args.Value;
                    process.StartInfo   = startInfo;
                    process.Start();
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 6
0
 public abstract bool Execute(string command, TSRPStack stack);
Ejemplo n.º 7
0
        public override bool Execute(string command, TSRPStack stack)
        {
            if (command == "function")
            {
                stack.Pop();
                stack.Push(new StackFunction(value));
                return(true);
            }
            else
            if (types.Contains(command))
            {
                stack.Pop();
                StackNumber a = StackNumber.Convert(command, value);
                stack.Push(a);
                return(true);
            }
            else if (command == "+")
            {
                if (stack.FromTop(1) is StackNumber)
                {
                    stack.Pop();
                    var a = (StackNumber)stack.Pop();
                    stack.Push(new StackString(a.ToString() + value));
                }
                else
                if (stack.FromTop(1) is StackString)
                {
                    stack.Pop();
                    var a = (StackString)stack.Pop();
                    stack.Push(new StackString(a.value + value));
                }
                else
                {
                    return(false);
                }
                return(true);
            }
            else if (command == "array")
            {
                stack.Pop();
                stack.Push(new StackArray(this));
                return(true);
            }
            else if (command == "chararray" || command == "carray")
            {
                stack.Pop();
                stack.Push(new StackArray(this, true));
                return(true);
            }
            else if (command == "string")
            {
                return(true);
            }
            else if (command == "==")
            {
                if (stack.FromTop(1) is StackString)
                {
                    stack.Pop();
                    var a = (StackString)stack.Pop();
                    stack.Push(a.value == value);
                    return(true);
                }
            }
            else if (command == "!=")
            {
                if (stack.FromTop(1) is StackString)
                {
                    stack.Pop();
                    var a = (StackString)stack.Pop();
                    stack.Push(a.value != value);
                    return(true);
                }
            }
            else if (command == "contains")
            {
                if (stack.FromTop(1) is StackString)
                {
                    stack.Pop();
                    var a = (StackString)stack.Pop();
                    stack.Push(a.value.Contains(value));
                    return(true);
                }
            }
            else if (command == "split")
            {
                if (stack.FromTop(1) is StackString)
                {
                    stack.Pop();
                    var a = (StackString)stack.Pop();
                    var b = new TSRPStack();

                    var c = a.value.Split(new string[] { value }, StringSplitOptions.None);
                    foreach (string d in c)
                    {
                        b.Push(d);
                    }
                    stack.Push(new StackArray(b));

                    return(true);
                }
            }
            else if (command == "trim")
            {
                stack.Pop();
                stack.Push(value.Trim().Replace("\n", "").Replace("\r", "").Replace("\t", ""));
                return(true);
            }
            else if (command == "len" || command == "length")
            {
                stack.Pop();
                stack.Push(value.Length);
                return(true);
            }


            return(false);
        }
Ejemplo n.º 8
0
        public override bool Execute(string command, TSRPStack stack)
        {
            if (closed)
            {
                return(false);
            }
            if (command == "CheckLine")
            {
                stack.Push(reader.Peek() >= 0);
                return(true);
            }
            else
            if (command == "Check")
            {
                stack.Push(file.Position < file.Length);
                return(true);
            }
            else
            if (command == "Close")
            {
                Dispose();
                closed = true;
                return(true);
            }
            else
            if (command == "Write")
            {
                if (stack.Top is StackString)
                {
                    StackString s = (StackString)stack.Pop();
                    writer.Write(s.Value);
                    return(true);
                }
            }
            else
            if (command == "ReadByte")
            {
                stack.Push((byte)file.ReadByte());
                return(true);
            }
            else
            if (command == "Read")
            {
                stack.Push("" + (char)reader.Read());
                return(true);
            }
            else
            if (command == "WriteLine")
            {
                StackString s = (StackString)stack.Pop();
                writer.WriteLine(s.Value);
                return(true);
            }
            else
            if (command == "ReadLine")
            {
                stack.Push(reader.ReadLine());
                return(true);
            }
            else
            if (command == "ReadToEnd")
            {
                stack.Push(reader.ReadToEnd());
                return(true);
            }
            else
            if (command == "WriteByte")
            {
                if (stack.Top is StackNumber)
                {
                    StackNumber s = (StackNumber)stack.Pop();
                    file.WriteByte(s.ToByte());
                    return(true);
                }
            }
            else
            if (command == "ReadBytes")
            {
                if (stack.Top is StackNumber)
                {
                    int    x   = ((StackNumber)stack.Pop()).ToInt();
                    byte[] buf = new byte[x];
                    file.Read(buf, 0, x);
                    TSRPStack s = new TSRPStack();
                    foreach (byte b in buf)
                    {
                        s.Push(b);
                    }
                    stack.Push(new StackArray(s));
                    return(true);
                }
            }
            else
            if (command == "WriteBytes")
            {
                if (stack.Top is StackArray)
                {
                    StackElement[] els = ((StackArray)stack.Pop()).Elements;
                    foreach (StackNumber n in els)
                    {
                        file.WriteByte(n.ToByte());
                    }
                    return(true);
                }
            }
            else
            if (command == "ReadWord")
            {
                StringBuilder b = new StringBuilder();
                while (reader.Peek() >= 0 && ((char)reader.Peek() != ' ') && ((char)reader.Peek() != '\n') && ((char)reader.Peek() != '\t') && ((char)reader.Peek() != '\r'))
                {
                    b.Append((char)reader.Read());
                }


                if (reader.Peek() >= 0)
                {
                    b.Append((char)reader.Read());
                }

                stack.Push(b.ToString());
                return(true);
            }
            return(false);
        }
Ejemplo n.º 9
0
        public override bool Execute(string command, TSRPStack stack)
        {
            if (command == "!" || command == "not")
            {
                stack.Pop();
                stack.Push(!value);
                return(true);
            }
            else if (command == "string")
            {
                stack.Pop();
                stack.Push(value.ToString());
                return(true);
            }
            else if (command == "==")
            {
                return(Equals(stack));
            }
            else if (command == "!=")
            {
                return(Equals(stack, true));
            }
            else if (command == "&&" || command == "and")
            {
                return(And(stack));
            }
            else if (command == "nand")
            {
                return(And(stack, true));
            }
            else if (command == "or" || command == "||")
            {
                return(Or(stack));
            }
            else if (command == "xor" || command == "^")
            {
                return(Xor(stack));
            }
            else if (command == "nor")
            {
                return(Or(stack, true));
            }
            else if (command == "int")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((int)a);
                return(true);
            }
            else if (command == "double")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((double)a);
                return(true);
            }
            else if (command == "float")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((float)a);
                return(true);
            }
            else if (command == "uint")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((uint)a);
                return(true);
            }
            else if (command == "long")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((long)a);
                return(true);
            }
            else if (command == "ulong")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((ulong)a);
                return(true);
            }
            else if (command == "short")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((short)a);
                return(true);
            }
            else if (command == "ushort")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((ushort)a);
                return(true);
            }
            else if (command == "byte")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((byte)a);
                return(true);
            }
            else if (command == "sbyte")
            {
                stack.Pop();
                byte a = 1;
                if (!value)
                {
                    a = 0;
                }
                stack.Push((sbyte)a);
                return(true);
            }


            return(false);
        }
Ejemplo n.º 10
0
        public override bool Execute(string command, TSRPStack stack)
        {
            if (command.EndsWith("*") && (stack.Top == this || command.EndsWith("**")))
            {
                int k = 1;
                if (command.EndsWith("**"))
                {
                    k = 2;
                }
                bool j = Execute(command.Substring(0, command.Length - k), stack);

                int counter = 0;

                while (stack.Top != this)
                {
                    counter++;
                    elements.Push(stack.Pop());
                }

                stack.Pop();

                for (int i = 0; i < counter; i++)
                {
                    stack.Push(elements.Pop());
                }
            }
            else
            if (command == "size")
            {
                stack.Push(Count);
                return(true);
            }
            else
            if (command == "at")
            {
                if (stack.Top is StackNumber)
                {
                    int n = ((StackNumber)stack.Pop()).ToInt();
                    if (n < Count)
                    {
                        stack.Push(((StackElement)elements.FromTop(Count - n - 1).Clone()));
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }

                return(false);
            }
            else
            if (command == "push" || command == "array_push")
            {
                if (stack.Count != 0)
                {
                    elements.Push(stack.Pop());
                }
                return(true);
            }
            else
            if (command == "array_pop")
            {
                if (elements.Count > 0)
                {
                    stack.Push(elements.Pop());
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            if (command == "set")
            {
                if (stack.Top is StackNumber)
                {
                    int          n  = ((StackNumber)stack.Pop()).ToInt();
                    StackElement el = stack.Pop();
                    var          x  = Elements;
                    if (n < Count)
                    {
                        x[n] = el;
                        elements.Elements = x;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(false);
            }
            else if (command == "chars2str")
            {
                var           x = Elements;
                StringBuilder b = new StringBuilder();
                for (int i = 0; i < x.Length; i++)
                {
                    StackNumber y = null;
                    if (x[i] is StackNumber)
                    {
                        y = (StackNumber)x[i];
                    }
                    else
                    {
                        continue;
                    }
                    b.Append((char)y.ToUnsignedShort());
                }
                stack.Push(b.ToString());
                return(true);
            }
            else if (command == "concatstr")
            {
                var           x = Elements;
                StringBuilder b = new StringBuilder();
                for (int i = 0; i < x.Length; i++)
                {
                    StackString y = null;
                    if (x[i] is StackString)
                    {
                        y = (StackString)x[i];
                    }
                    else
                    {
                        continue;
                    }
                    b.Append(y.Value);
                }
                stack.Push(b.ToString());
                return(true);
            }
            else if (command == "array_clear" || command == "empty")
            {
                elements.Clear();
                return(true);
            }
            else if (command == "release")
            {
                stack.Pop();
                var els = elements.Elements;
                //Array.Reverse(els);
                foreach (StackElement el in els)
                {
                    stack.Push(el);
                }
                elements.Clear();

                return(true);
            }
            else if (command == "clone")
            {
                stack.Push(new StackArray(this));
                return(true);
            }
            else if (command == "reverse")
            {
                var x = elements.Elements;

                elements.Clear();
                for (var i = x.Length - 1; i >= 0; i--)
                {
                    elements.Push(x[i]);
                }

                return(true);
            }


            return(false);
        }
Ejemplo n.º 11
0
 public StackArray(TSRPStack s)
 {
     elements = s;
 }
Ejemplo n.º 12
0
 public StackArray()
 {
     elements = new TSRPStack();
 }
Ejemplo n.º 13
0
        public void Parse(string p, bool pre = false, Dictionary <string, StackElement> local_variables = null, List <String> held_variables = null)
        {
            int    brace_count     = 0;
            string function_making = "";
            Dictionary <string, StackElement> changes = null;

            List <string> local_held_variables = null;

            bool hold = false;

            if (held_variables != null && local_variables != null)
            {
                changes = new Dictionary <string, StackElement>();
            }

            if (!pre)
            {
                p = PreprocessString(p);
            }

            var arr = p.Split(' ');


            foreach (var s1 in arr)
            {
                if (s1.Length == 0)
                {
                    continue;
                }
                var s = s1;
                if (s == "[%d%%%d]")
                {
                    s = "%";
                }

                int    a;
                double b;


                if (s == "{")
                {
                    if (brace_count > 0)
                    {
                        function_making += " { ";
                    }

                    brace_count++;
                }
                else
                if (s == "}")
                {
                    brace_count--;

                    if (brace_count == 0)
                    {
                        Stack.Push(new StackFunction(function_making));
                        function_making = "";
                    }
                    else
                    {
                        function_making += " } ";
                    }
                }
                else
                if (brace_count > 0)
                {
                    function_making += s + " ";
                }
                else
                if (s == "[]")
                {
                    Stack.Push(new StackArray());
                }
                else
                if (s == "enter" || s == "Enter")
                {
                    if (Stack.Top is StackArray)
                    {
                        stacks.Push(((StackArray)Stack.Top).GetTSRPStack());
                    }
                }
                else
                if (s == "hold")
                {
                    hold = true;
                }
                else
                if (s == "set_hold" && Stack.Top is StackBool)
                {
                    StackBool v = (StackBool)Stack.Top;
                    hold = v.Value;
                }
                else
                if (s == "leave" || s == "Leave" || s == "exit" || s == "Exit")
                {
                    if (stacks.Count > 1)
                    {
                        stacks.Pop();
                    }
                }
                else
                if (s == "[")
                {
                    stacks.Push(new TSRPStack());
                }
                else
                if (s == "]")
                {
                    if (stacks.Count > 1)
                    {
                        TSRPStack stack = Stack;
                        stacks.Pop();
                        Stack.Push(new StackArray(stack));
                    } //else error
                }
                else
                if (s == "clear")
                {
                    Stack.Clear();
                }
                else
                if (s == "import")
                {
                    if (Stack.Top is StackCustom)
                    {
                        var el = (StackCustom)Stack.Pop();
                        imports.Add(el);
                    }
                }
                else
                if (s.ToLower().StartsWith("repeat"))
                {
                    var k = s.Split(':');


                    if (Stack.Top is StackNumber && Stack.FromTop(1) is StackFunction)
                    {
                        int           times = ((StackNumber)Stack.Pop()).ToInt();
                        StackFunction func  = (StackFunction)Stack.Pop();
                        for (int i = 0; i < times; i++)
                        {
                            if (k.Length != 1)
                            {
                                if (local_variables == null)
                                {
                                    local_variables = new Dictionary <string, StackElement>();
                                }
                                local_variables[k[1]] = new StackInt(i);
                            }

                            Parse(func.Value, true, local_variables, held_variables);
                        }
                    }
                }
                else
                if (s == "if")
                {
                    if (Stack.Top is StackBool && Stack.FromTop(1) is StackFunction)
                    {
                        bool          v    = ((StackBool)Stack.Pop()).Value;
                        StackFunction func = (StackFunction)Stack.Pop();
                        if (v)
                        {
                            Parse(func.Value, true, local_variables, held_variables);
                        }
                    }
                }
                else
                if (s == "while")
                {
                    if (Stack.Top is StackFunction && Stack.FromTop(1) is StackFunction)
                    {
                        StackFunction statement = (StackFunction)Stack.Pop();
                        StackFunction func      = (StackFunction)Stack.Pop();
                        Parse(statement.Value, true, local_variables, held_variables);

                        while (Stack.Top is StackBool && ((StackBool)Stack.Pop()).Value)
                        {
                            Parse(func.Value, true, local_variables, held_variables);
                            Parse(statement.Value, true, local_variables, held_variables);
                        }
                    }
                }
                else
                if (s == "exec" || s == "execute" || s == "run")
                {
                    if (Stack.Top is StackFunction)
                    {
                        Parse(((StackFunction)Stack.Pop()).Value, true, local_variables, held_variables);
                    }
                }
                else
                if (s == "@size")
                {
                    Stack.Push(Stack.Count);
                }
                else
                if (s == "dup")
                {
                    if (Stack.Top is StackArray)
                    {
                        Stack.Push(Stack.Top);
                    }
                    Stack.Push((StackElement)Stack.Top.Clone());
                }
                else
                if (s == "pop" || s == "del")
                {
                    Stack.Pop();
                }
                else
                if (s == "swap")
                {
                    var t  = Stack.Pop();
                    var t2 = Stack.Pop();
                    Stack.Push(t);
                    Stack.Push(t2);
                }
                else
                if (s.Length > 2 && s.StartsWith(":>"))
                {
                    var k = s.Substring(2);
                    if (variables.ContainsKey(k) && variables[k] is StackArray)
                    {
                        var j = (StackArray)variables[k];

                        if (Stack.Count > 0)
                        {
                            j.Push(Stack.Pop());
                        }
                    }
                }
                else
                if (s.Length > 2 && s.StartsWith(":<"))
                {
                    var k = s.Substring(2);
                    if (variables.ContainsKey(k) && variables[k] is StackArray)
                    {
                        var j = (StackArray)variables[k];
                        if (j.Count > 0)
                        {
                            Stack.Push(j.Pop());
                        }
                    }
                }
                else
                if (s.Length > 2 && s.StartsWith("::"))
                {
                    var k = s.Substring(2);
                    if (variables.ContainsKey(k) && variables[k] is StackArray)
                    {
                        var j = (StackArray)variables[k];
                        Stack.Push(j.Count);
                    }
                }
                else
                if (s.Length > 1 && s.StartsWith("$"))
                {
                    if (variables.ContainsKey(s.Substring(1)))
                    {
                        if (variables[s.Substring(1)] is StackArray)
                        {
                            Stack.Push(variables[s.Substring(1)]); // Don't clone the stack array.
                        }
                        else
                        {
                            Stack.Push((StackElement)variables[s.Substring(1)].Clone());
                        }
                    }
                }
                else
                if (s.Length > 1 && s.StartsWith("=") && s[1] != '=')
                {
                    if (Stack.Top is StackArray)
                    {
                        variables[s.Substring(1)] = Stack.Top;     // Don't Clone the Stack Array.
                    }
                    else
                    {
                        variables[s.Substring(1)] = (StackElement)Stack.Top.Clone();
                    }
                }
                else
                if (s.Length > 2 && s.StartsWith("_=")) //Local Variables
                {
                    if (local_variables == null)
                    {
                        local_variables = new Dictionary <string, StackElement>();
                    }

                    if (hold)
                    {
                        if (held_variables == null)
                        {
                            held_variables       = new List <string>();
                            local_held_variables = new List <string>();
                        }
                        local_held_variables.Add(s.Substring(2));
                        held_variables.Add(s.Substring(2));
                    }

                    if (changes != null)
                    {
                        if (held_variables.Contains(s.Substring(2)) && !changes.ContainsKey(s.Substring(2)))
                        {
                            changes[s.Substring(2)] = local_variables[s.Substring(2)]; // Makes sure any changes don't affect the caller.
                        }
                    }



                    if (Stack.Top is StackArray)
                    {
                        local_variables[s.Substring(2)] = Stack.Top; // Don't Clone the Stack Array.
                    }
                    else
                    {
                        local_variables[s.Substring(2)] = (StackElement)Stack.Top.Clone();
                    }
                }
                else
                if (s.Length > 2 && s.StartsWith("_$")) //Local Variables
                {
                    if (local_variables == null)
                    {
                        local_variables = new Dictionary <string, StackElement>();
                    }

                    if (local_variables.ContainsKey(s.Substring(2)))
                    {
                        if (local_variables[s.Substring(2)] is StackArray)
                        {
                            Stack.Push(local_variables[s.Substring(2)]); // Don't clone the stack array.
                        }
                        else
                        {
                            Stack.Push((StackElement)local_variables[s.Substring(2)].Clone());
                        }
                    }
                }
                else
                if (s == "False" || s == "false")
                {
                    Stack.Push(false);
                }
                else
                if (s == "True" || s == "true")
                {
                    Stack.Push(true);
                }
                else
                if (s.StartsWith("\"") && s.EndsWith("\""))
                {
                    Stack.Push(ParseInlineString(s));
                }
                else
                if (s.Contains('.') && double.TryParse(s, out b))
                {
                    Stack.Push(b);
                }
                else
                if (int.TryParse(s, out a))
                {
                    Stack.Push(a);
                }
                else
                {
                    if (Stack.Top != null && Stack.Top.Execute(s, Stack))
                    {
                    }
                    else
                    {
                        if (variables.ContainsKey(s) && variables[s] is StackFunction)
                        {
                            Parse(((StackFunction)variables[s]).Value, true, local_variables, held_variables);
                        }
                        else
                        {
                            if (Stack.TopCustom != null && Stack.TopCustom.Execute(s, Stack))
                            {
                            }
                            else
                            {
                                bool got_one = false;
                                foreach (var im in imports)
                                {
                                    if (im.Execute(s, Stack))
                                    {
                                        got_one = true;
                                        break;
                                    }
                                }

                                if (!got_one)
                                {
                                    //Failure.
                                }
                            }
                        }
                    }
                }
            }

            if (hold)
            {
                foreach (string s in local_held_variables)
                {
                    held_variables.Remove(s);
                }
            }

            //Reversing any changes to local variables.
            if (changes != null)
            {
                var keys = changes.Keys;
                foreach (String s in keys)
                {
                    local_variables[s] = changes[s];
                }
            }
        }