Example #1
0
                protected override void Run()
                {
                    if (Argv.Count == 1)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }
                    bool    symbolic = true;
                    NixPath source   = MainSession.WorkingDirectory.Combine(Argv[1]);
                    NixPath destination;

                    if (Argv.Count == 2)
                    {
                        destination = new NixPath(source.TopPath());
                    }
                    else
                    {
                        destination = MainSession.WorkingDirectory.Combine(Argv[2]);
                    }

                    if (MainSystem.RootDrive.MakeLink(source, MainSession.WorkingDirectory.Combine(destination)) != 1)
                    {
                        StdOut.WriteLine("Error attempting to write link.");
                    }
                }
Example #2
0
 protected override void Run()
 {
     if (Argv.Count == 1)
     {
         // Read from stdin
     }
     else
     {
         for (int i = 1; i < Argv.Count; i++)
         {
             NixPath path = OpenPath(Argv[i]);
             if (MainSystem.RootDrive.IsFileOrDirectory(path))
             {
                 try
                 {
                     using (Stream file = MainSystem.RootDrive.OpenFile(path, FileAccess.Read, FileMode.Open))
                     {
                         StreamReader reader = new StreamReader(file);
                         StdOut.Write(reader.ReadToEnd());
                     }
                 }
                 catch (System.Exception exp)
                 {
                     StdOut.WriteLine("Error reading file for cat: " + exp.Message);
                 }
             }
             else
             {
                 StdOut.Write(GetCommand() + ": " + Argv[i] + ": No such file or directory\n");
             }
         }
     }
 }
Example #3
0
        public void TestRedBlackBst()
        {
            var bst = new RedBlackBST <string, string>();

            bst.Put("A", "A");
            bst.Put("B", "B");
            bst.Put("C", "C");
            bst.Put("D", "D");
            bst.Put("E", "E");
            bst.Put("F", "F");
            bst.Put("G", "G");
            bst.Put("H", "H");
            bst.Put("I", "I");
            bst.Put("J", "J");

            Assert.IsFalse(bst.IsEmpty);
            Assert.AreEqual(10, bst.Count);
            Assert.AreEqual(3, bst.Height);

            StdOut.WriteLine(bst.LevelOrder());

            //bst.DeleteMax();
            //StdOut.WriteLine(bst.LevelOrder());

            while (bst.Count > 1)
            {
                bst.DeleteMin();
                StdOut.WriteLine(bst.LevelOrder());
            }

            bst.DeleteMin();
        }
Example #4
0
                protected override void Run()
                {
                    if (Argv.Count <= 2)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }

                    NixPath fromPath = MainSession.PhysicalDirectory.Combine(Argv[1]);
                    NixPath toPath   = OpenPath(Argv[2]);

                    StdOut.WriteLine("Copying from: " + fromPath + "\nCopying to: " + toPath);
                    try
                    {
                        MainSystem.RootDrive.Copy(fromPath, toPath);
                    }
                    catch (System.IO.FileNotFoundException exp)
                    {
                        StdOut.WriteLine("No such file or directory: " + Argv[1]);
                    }
                    catch (System.Exception exp)
                    {
                        StdOut.WriteLine(exp.Message);
                    }

                    //StdOut.Write ("No such file or directory: " + Argv[1]);

                    /*if (MainSystem.RootDrive.IsDirectory(newPath.ToString())) {
                     *  MainSession.SetWorkingDirectory(newPath);
                     * }
                     * else {
                     *  StdOut.Write(newPath.ToString() + " is not a directory.\n");
                     * }*/
                    return;
                }
Example #5
0
                protected override void Run()
                {
                    if (Argv.Count <= 2)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }

                    NixPath fromPath = MainSession.PhysicalDirectory.Combine(Argv[1]);
                    NixPath toPath   = OpenPath(Argv[2]);

                    try
                    {
                        MainSystem.RootDrive.Move(fromPath, toPath);
                    }
                    catch (System.IO.FileNotFoundException exp)
                    {
                        StdOut.WriteLine("No such file or directory: " + Argv[1]);
                    }
                    catch (System.Exception exp)
                    {
                        StdOut.WriteLine(exp.Message);
                    }
                    return;
                }
Example #6
0
 public static void Info(string message)
 {
     lock (Sync)
     {
         SetMode("default");
         StdOut.WriteLine(message);
     }
 }
Example #7
0
 public static void Verbose(string message)
 {
     lock (Sync)
     {
         SetMode("verbose");
         StdOut.WriteLine(message);
     }
 }
Example #8
0
 public static void Warn(string message)
 {
     lock (Sync)
     {
         SetMode("warning");
         StdOut.WriteLine(message);
     }
 }
Example #9
0
 static void SetMode(string mode)
 {
     if (stdOutMode == mode)
     {
         return;
     }
     StdOut.WriteLine("##octopus[stdout-" + mode + "]");
     stdOutMode = mode;
 }
Example #10
0
        public void TestHeapSort()
        {
            var a = CreateRandomArray(10);

            StdOut.WriteLine(a);
            Heap <int> .Sort(a);

            StdOut.WriteLine(a);
        }
Example #11
0
        public void TestQuick3Sort()
        {
            var a = CreateRandomArray(16);

            StdOut.WriteLine(a);
            Quick3 <int> .Sort(a);

            StdOut.WriteLine(a);
        }
Example #12
0
        public void TestShuffle()
        {
            var a = new int[10];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = StdRandom.Uniform(100);
            }
            StdOut.WriteLine(a);
            StdRandom.Shuffle(a);
            StdOut.WriteLine(a);
        }
Example #13
0
        public void TestBottomUpMerge()
        {
            var a = new int[16];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = StdRandom.Uniform(100);
            }
            StdOut.WriteLine(a);
            Merge <int> .SortBottomUp(a);

            StdOut.WriteLine(a);
        }
Example #14
0
        public void TestSelectionSort()
        {
            var a = new int[10];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = StdRandom.Uniform(10);
            }
            StdOut.WriteLine(a);
            Selection <int> .Sort(a);

            StdOut.WriteLine(a);
        }
Example #15
0
            public int RenameFileHandler(string fromname, string toname)
            {
                NixPath frompath = MainSession.PhysicalDirectory.Combine(new NixPath(fromname));
                NixPath topath   = MainSession.PhysicalDirectory.Combine(new NixPath(toname));

                try
                {
                    MainSystem.RootDrive.Rename(frompath, topath);
                }
                catch (Exception exp)
                {
                    StdOut.WriteLine("Error renaming file from Lua: " + exp.Message);
                    return(-1);
                }
                return(0);
            }
Example #16
0
        public void TestQuickSort()
        {
            var a = new int[16];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = StdRandom.Uniform(100);
            }
            StdOut.WriteLine(a);
            Quick <int> .Sort(a);

            StdOut.WriteLine(a);

            var item4 = Quick <int> .Select(a, 9);

            StdOut.WriteLine("item7 = {0}", item4);
        }
Example #17
0
        public void TestNormalMergeSort()
        {
            var a = new int[10];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = StdRandom.Uniform(10);
            }
            StdOut.WriteLine(a);
            Merge <int> .Sort(a);

            StdOut.WriteLine(a);

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = StdRandom.Uniform(10);
            }
        }
Example #18
0
                protected override void Run()
                {
                    bool physical = false;
                    bool error    = false;

                    for (int i = 1; i < Argv.Count; i++)
                    {
                        string arg = Argv[i];
                        if (arg[0] == '-')
                        {
                            for (int j = 1; j < arg.Length; j++)
                            {
                                if (arg[j] == 'P')
                                {
                                    physical = true;
                                }
                                else
                                {
                                    StdOut.WriteLine("Unknown argument: " + arg[j]);
                                    error = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (error)
                    {
                        return;
                    }
                    string pwd = "No session";

                    if (MainSession != null)
                    {
                        if (!physical && MainSession.WorkingDirectory != null)
                        {
                            pwd = MainSession.WorkingDirectory.ToString();
                        }
                        else if (physical && MainSession.PhysicalDirectory != null)
                        {
                            pwd = MainSession.PhysicalDirectory.ToString();
                        }
                    }
                    StdOut.WriteLine(pwd);
                }
                protected override void Run()
                {
                    if (Argv.Count < 4)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }

                    string type = "lua";

                    for (int i = 1; i < Argv.Count; i++)
                    {
                        switch (Argv[i])
                        {
                        case "-t":
                            type = Argv[i + 1];
                            break;
                        }
                    }

                    NixPath sourceFile = OpenPath(Argv[Argv.Count - 2]);
                    NixPath destFile   = OpenPath(Argv[Argv.Count - 1]);

                    if (type != "lua")
                    {
                        StdOut.WriteLine("Unknown device type: " + type);
                        return;
                    }

                    if (!MainSystem.RootDrive.IsFile(sourceFile))
                    {
                        StdOut.WriteLine("Cannot find file: " + sourceFile.ToString());
                        return;
                    }

                    if (type == "lua")
                    {
                        Device.LuaDevice device = new Device.LuaDevice(MainSession, MainSystem);
                        device.LoadFile(sourceFile);
                        MainSystem.MainDeviceManager.AddDevice(device);
                        MainSystem.RootDrive.MakeCharacterDevice(destFile, device.Id);
                    }
                }
Example #20
0
        public void TestMinPQ()
        {
            var arr = CreateRandomArray(10);

            Insertion <int> .Sort(arr);

            StdOut.WriteLine(arr);

            var pq = new MinPQ <int>();

            foreach (int i in arr)
            {
                pq.Insert(i);
            }

            while (!pq.IsEmpty)
            {
                StdOut.WriteLine("delete min: {0}", pq.DelMin());
            }
        }
Example #21
0
        public void TestBst()
        {
            var bst = new BST <string, string>();

            bst.Put("A", "A");
            bst.Put("B", "B");
            bst.Put("C", "C");
            bst.Put("D", "D");
            bst.Put("E", "E");
            bst.Put("F", "F");
            bst.Put("G", "G");
            bst.Put("H", "H");
            bst.Put("I", "I");
            bst.Put("J", "J");

            Assert.IsFalse(bst.IsEmpty);
            Assert.AreEqual(10, bst.Count);
            Assert.AreEqual(9, bst.Height);

            StdOut.WriteLine(bst.LevelOrder());
        }
Example #22
0
            public int RemoveFileHandler(string filename)
            {
                NixPath path = MainSession.PhysicalDirectory.Combine(new NixPath(filename));

                try
                {
                    if (MainSystem.RootDrive.IsDirectory(path))
                    {
                        MainSystem.RootDrive.DeleteDirectory(path, false);
                    }
                    else
                    {
                        MainSystem.RootDrive.DeleteFile(path);
                    }
                }
                catch (Exception exp)
                {
                    StdOut.WriteLine("Error removing file from Lua: " + exp.Message);
                    return(-1);
                }
                return(0);
            }
Example #23
0
                protected override void Run()
                {
                    LuaSystem luaSys = new LuaSystem(MainSession, MainSystem, StdOut, StdIn, StdErr);
                    Lua       l      = luaSys.Lua;

                    try
                    {
                        if (Argv.Count > 1)
                        {
                            NixPath newPath = OpenPath(Argv[1]);
                            string  file    = MainSystem.RootDrive.GetPathTo(newPath.ToString());
                            Debug.Log("File to load: " + newPath.ToString());
                            if (File.Exists(file))
                            {
                                string argStr = "arg={}\n";
                                argStr += "arg[0]=\"" + Argv[1].Replace("\\", "/") + "\"\n";
                                for (int i = 2; i < Argv.Count; i++)
                                {
                                    argStr += "arg[" + (i - 1) + "]=\"" + Argv[i] + "\"\n";
                                }
                                l.DoString(argStr);
                                l.DoFile(newPath.ToString());
                            }
                            else
                            {
                                StdOut.WriteLine("Unable to find file: " + Argv[1]);
                            }
                        }
                        else
                        {
                            // Do stdin stuff
                        }
                    }
                    catch (Exception exp)
                    {
                        StdOut.WriteLine("Exception executing Lua: " + exp.Message);
                    }
                }
Example #24
0
                protected override void Run()
                {
                    if (Argv.Count <= 1)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }
                    try{
                        NixPath newPath      = MainSession.WorkingDirectory.Combine(Argv[1]).ResolvePath();
                        NixPath followedPath = MainSystem.RootDrive.FollowLinks(MainSession.PhysicalDirectory.Combine(Argv[1]));

                        if (MainSystem.RootDrive.IsDirectory(followedPath))
                        {
                            MainSession.SetWorkingDirectory(newPath);
                        }
                        else
                        {
                            StdOut.Write(newPath.ToString() + " is not a directory.\n");
                        }
                    }
                    catch (System.Exception exp) {
                        Debug.Log("CD EXP: " + exp.Message);
                    }
                }
Example #25
0
                protected override void Run()
                {
                    if (Argv.Count == 1)
                    {
                        // Read from stdin
                        StdOut.WriteLine(GetCommand() + ": missing operand");
                    }
                    else
                    {
                        bool createParents = false;
                        //string[] copy = new string[Argv.Count - 1];
                        List <NixPath> copy  = new List <NixPath>();
                        bool           error = false;
                        for (int i = 1; i < Argv.Count; i++)
                        {
                            string arg = Argv[i];
                            if (arg[0] == '-')
                            {
                                for (int j = 1; j < arg.Length; j++)
                                {
                                    if (arg[j] == 'p')
                                    {
                                        createParents = true;
                                    }
                                    else
                                    {
                                        StdOut.WriteLine("Unknown argument: " + arg[j]);
                                        error = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                copy.Add(MainSession.PhysicalDirectory.Combine(arg));
                            }
                        }
                        if (error)
                        {
                            return;
                        }

                        /*
                         * for (int i = 1; i < Argv.Count; i++)
                         * {
                         *  if (Argv[i] == "-p")
                         *  {
                         *      createParents = true;
                         *  }
                         *  else
                         *  {
                         *      copy[j++] = MainSession.PhysicalDirectory.Combine(Argv[i]).ToString();
                         *  }
                         * }
                         */
                        for (int i = 0; i < copy.Count; i++)
                        {
                            Debug.Log("Copy arg " + copy[i]);
                            if (copy[i].ToString().Length == 0)
                            {
                                break;
                            }
                            try
                            {
                                MainSystem.RootDrive.MakeDirectory(copy[i], createParents);
                            }
                            catch (Exception exp)
                            {
                                StdOut.WriteLine("Error creating directory: " + exp.Message);
                                //Debug.Log("Error creating directory: " + exp.Message);
                            }
                        }
                    }
                }
Example #26
0
        InterpretResult Run()
        {
            Frame = frames[FrameCount - 1];

            while (true)
            {
#if TRACE
                System.Diagnostics.Debug.WriteLine(CurrentChunk.DisassembleInstruction(Frame.IP));
#endif

                InstructionCounter++;
                var instruction = (OpCode)ReadByte();
                switch (instruction)
                {
                case OpCode.Nop:
                    break;

                case OpCode.Constant:
                {
                    var constant = ReadConstant();
                    Push(constant);
                }
                break;

                case OpCode.True:
                    Push(Values.Bool(true));
                    break;

                case OpCode.False:
                    Push(Values.Bool(false));
                    break;

                case OpCode.Pop:
                    Pop();
                    break;

                case OpCode.PopN:
                    Pop(ReadByte());
                    break;

                case OpCode.LoadGlobal:
                {
                    var name = ReadConstant().AsString();
                    if (!globals.TryGetValue(name, out var value))
                    {
                        throw new RuntimeExecutionException($"Undefined variable '{name}'", CreateStackTrace());
                    }
                    Push(value);
                }
                break;

                case OpCode.LoadLocal:
                {
                    var slot = ReadByte();
                    Push(stack[Frame.StackBase + slot]);
                }
                break;

                case OpCode.AssignGlobal:
                {
                    var name = ReadConstant().AsString();
                    if (!globals.ContainsKey(name))
                    {
                        throw new RuntimeExecutionException($"Undefined variable '{name}'", CreateStackTrace());
                    }
                    globals[name] = Peek();
                }
                break;

                case OpCode.AssignLocal:
                {
                    var slot = ReadByte();
                    stack[Frame.StackBase + slot] = Peek();
                }
                break;

                case OpCode.DefineGlobal:
                {
                    var globalName = ReadConstant().AsString();
                    globals[globalName] = Peek();
                    Pop();
                }
                break;

                case OpCode.Equal:
                {
                    var a      = Pop();
                    var b      = Pop();
                    var equals = a.EqualsValue(b);
                    Push(Values.Bool(equals));
                }
                break;

                case OpCode.Greater:
                    BinaryOp((a, b) => Values.Bool(a > b));
                    break;

                case OpCode.Less:
                    BinaryOp((a, b) => Values.Bool(a < b));
                    break;

                case OpCode.Add:
                    BinaryOp((a, b) => Values.Number(a + b));
                    break;

                case OpCode.Subtract:
                    BinaryOp((a, b) => Values.Number(a - b));
                    break;

                case OpCode.Multiply:
                    BinaryOp((a, b) => Values.Number(a * b));
                    break;

                case OpCode.Divide:
                    BinaryOp((a, b) => Values.Number(a / b));
                    break;

                case OpCode.Exp:
                    BinaryOp((a, b) => Values.Number((decimal)Math.Pow((double)a, (double)b)));
                    break;

                case OpCode.Modulo:
                    BinaryOp((a, b) => Values.Number(decimal.Remainder(a, b)));
                    break;

                case OpCode.Not:
                {
                    var falsey = Pop().IsFalsey();
                    Push(Values.Bool(falsey));
                }
                break;

                case OpCode.Negate:
                {
                    if (!Peek(0).IsNumber())
                    {
                        throw new RuntimeExecutionException("Operand must be a number", CreateStackTrace());
                    }
                    Push(Values.Number(-Pop().AsNumber()));
                }
                break;

                case OpCode.Print:
                    StdOut.WriteLine(Pop().ToString());
                    break;

                case OpCode.Jump:
                {
                    var offset = ReadShort();
                    Frame.IP += offset;
                }
                break;

                case OpCode.JumpIfFalse:
                {
                    var offset = ReadShort();
                    if (Peek().IsFalsey())
                    {
                        Frame.IP += offset;
                    }
                }
                break;

                case OpCode.Loop:
                {
                    var offset = ReadShort();
                    Frame.IP -= offset;
                }
                break;

                case OpCode.Call:
                {
                    var argCount = ReadByte();
                    CallValue(Peek(argCount), argCount);
                    Frame = frames[FrameCount - 1];
                }
                break;

                case OpCode.Return:
                {
                    var result = Pop();
                    FrameCount--;
                    if (FrameCount == 0)
                    {
                        Pop();
                        return(InterpretResult.Success);
                    }

                    StackPointer = Frame.StackBase;
                    Push(result);

                    Frame = frames[FrameCount - 1];
                }
                break;

                default:
                    throw new RuntimeExecutionException($"Unknown instruction value '{instruction}' at ip offset {Frame.IP,4:D4}", CreateStackTrace());
                }
            }
        }
Example #27
0
                protected override void Run()
                {
                    if (Argv.Count <= 1)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }

                    bool recursive = false;
                    bool force     = false;
                    bool error     = false;

                    List <string> files = new List <string>();

                    for (int i = 1; !error && i < Argv.Count; i++)
                    {
                        string arg = Argv[i];
                        if (arg[0] == '-')
                        {
                            for (int j = 1; j < arg.Length; j++)
                            {
                                if (arg[j] == 'r')
                                {
                                    recursive = true;
                                }
                                else if (arg[j] == 'f')
                                {
                                    force = true;
                                }
                                else
                                {
                                    StdOut.WriteLine(GetCommand() + ": unknown command -- " + arg[j]);
                                    error = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            files.Add(arg);
                        }
                    }

                    try
                    {
                        for (int i = 0; i < files.Count; i++)
                        {
                            NixPath file = MainSession.PhysicalDirectory.Combine(new NixPath(files[i]));
                            if (MainSystem.RootDrive.IsDirectory(file))
                            {
                                if (!recursive)
                                {
                                    StdOut.WriteLine(GetCommand() + ": cannot remove '" + files[i] + "': Is a directory");
                                    return;
                                }
                                else if (!MainSystem.RootDrive.IsDirectoryEmpty(file))
                                {
                                    if (force)
                                    {
                                        MainSystem.RootDrive.DeleteDirectory(file, true);
                                    }
                                    else
                                    {
                                        StdOut.WriteLine(GetCommand() + ": cannot remove '" + files[i] + "': Is not empty");
                                    }
                                }
                                else
                                {
                                    MainSystem.RootDrive.DeleteDirectory(file, false);
                                }
                            }
                            else
                            {
                                MainSystem.RootDrive.DeleteFile(file);
                            }
                        }
                    }
                    catch (System.Exception exp)
                    {
                        Debug.Log("Error removing: " + exp.Message);
                    }

                    /*NixPath fromPath = MainSession.WorkingDirectory.Combine(Argv[1]);
                     * NixPath toPath = MainSession.WorkingDirectory.Combine(Argv[2]);
                     *
                     * try {
                     *  MainSystem.RootDrive.Move(fromPath, toPath);
                     * }
                     * catch (System.IO.FileNotFoundException exp) {
                     *  StdOut.WriteLine("No such file or directory: " + Argv[1]);
                     * }
                     * catch (System.Exception exp) {
                     *  StdOut.WriteLine(exp.Message);
                     * }
                     * return;
                     */
                }