Exemplo n.º 1
0
        public DynValue Move(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectTypeAtIndex(1, DynValueType.String);

            var source = args[0].String;
            var target = args[1].String;

            try
            {
                if (Directory.Exists(source))
                {
                    Directory.Move(source, target);
                }
                else if (File.Exists(source))
                {
                    File.Move(source, target);
                }
                else
                {
                    return(new DynValue(-2));
                }

                return(DynValue.Zero);
            }
            catch
            {
                return(new DynValue(-1));
            }
        }
Exemplo n.º 2
0
        public static DynValue Length(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            return(new DynValue(args[0].String.Length));
        }
Exemplo n.º 3
0
        public DynValue LinkToDevice(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var addr   = Address.Parse(args[0].String);
            var device = UserProfile.Instance.Internet.GetDevice(addr.ToStringWithoutNode());

            if (device == null)
            {
                return(new DynValue(SystemReturnCodes.Network.NoRouteToHost));
            }

            if (Kernel.Instance.NetworkConnectionStack.TryPeek(out var entity))
            {
                if (entity is Node)
                {
                    return(new DynValue(SystemReturnCodes.Network.BoundToNode));
                }
            }

            if (Kernel.Instance.NetworkConnectionStack.Contains(device))
            {
                return(new DynValue(SystemReturnCodes.Network.AlreadyLinked));
            }


            Kernel.Instance.LinkToDevice(device);
            return(new DynValue(SystemReturnCodes.Success));
        }
Exemplo n.º 4
0
        public DynValue Import(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var libName = args[0].String;

            var path    = "/lib/" + libName + ".lib";
            var file    = File.Get(path);
            var absPath = file.GetAbsolutePath();

            var process = Kernel.Instance.ProcessManager.GetProcess(interpreter);

            if (process.ImportedLibraryPaths.Contains(absPath))
            {
                interpreter.TerminateWithKernelMessage($"{absPath} WAS ALREADY IMPORTED");
                return(new DynValue(-1));
            }

            try
            {
                var content = file.GetData();
                process.ImportedLibraryPaths.Add(absPath);

                return(interpreter.ExecuteAsync(content, Kernel.Instance.InteractionCancellation.Token).GetAwaiter().GetResult());
            }
            catch
            {
                interpreter.TerminateWithKernelMessage($"FAILED TO IMPORT {absPath}");
                return(new DynValue(-2));
            }
        }
Exemplo n.º 5
0
        public DynValue PingServiceNode(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectByteAtIndex(0);

            var port = (byte)args[0].Number;

            if (!Kernel.Instance.NetworkConnectionStack.TryPeek(out var entity))
            {
                return(new DynValue(SystemReturnCodes.Network.NotLinked));
            }

            if (!(entity is Device device))
            {
                return(new DynValue(SystemReturnCodes.Network.NotADevice));
            }

            var serviceNode = device.GetServiceNode(port);

            if (serviceNode != null)
            {
                if (serviceNode.IsActive)
                {
                    return(new DynValue(SystemReturnCodes.Network.ServiceNodeActive));
                }

                return(new DynValue(SystemReturnCodes.Network.ServiceNodeInactive));
            }

            return(new DynValue(SystemReturnCodes.Success));
        }
Exemplo n.º 6
0
        public DynValue SetXY(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(6)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectIntegerAtIndex(1)
            .ExpectIntegerAtIndex(2)
            .ExpectByteAtIndex(3)
            .ExpectByteAtIndex(4)
            .ExpectByteAtIndex(5);

            var str = args[0].String;
            var x   = args[1].Number;
            var y   = args[2].Number;
            var r   = (byte)args[3].Number;
            var g   = (byte)args[4].Number;
            var b   = (byte)args[5].Number;

            if (x >= Kernel.Instance.Vga.TotalColumns)
            {
                return(new DynValue(-1));
            }

            if (y >= Kernel.Instance.Vga.TotalRows)
            {
                return(new DynValue(-1));
            }

            if (str.Length != 1)
            {
                throw new ClrFunctionException("Expected a single-character string.");
            }

            Kernel.Instance.Vga.PutCharAt(str[0], new Color(r, g, b), Color.Black, (int)x, (int)y);
            return(DynValue.Zero);
        }
Exemplo n.º 7
0
        public DynValue SetCursorPosition(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectIntegerAtIndex(0)
            .ExpectIntegerAtIndex(1);

            var x = args[0].Number;
            var y = args[1].Number;

            if (x >= Kernel.Instance.Vga.TotalColumns - Kernel.Instance.Vga.Margins.Right)
            {
                x = Kernel.Instance.Vga.Margins.Right;
            }

            if (y >= Kernel.Instance.Vga.TotalRows - Kernel.Instance.Vga.Margins.Bottom)
            {
                y = Kernel.Instance.Vga.Margins.Bottom;
            }

            if (x < Kernel.Instance.Vga.Margins.Left)
            {
                x = Kernel.Instance.Vga.Margins.Left;
            }

            if (y < Kernel.Instance.Vga.Margins.Top)
            {
                y = Kernel.Instance.Vga.Margins.Top;
            }

            Kernel.Instance.Vga.CursorX = (int)x;
            Kernel.Instance.Vga.CursorY = (int)y;

            return(DynValue.Zero);
        }
Exemplo n.º 8
0
        private DynValue Abs(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.Number);

            return(new DynValue(Math.Abs(args[0].Number)));
        }
Exemplo n.º 9
0
        public DynValue Margins(Interpreter interpreter, ClrFunctionArguments args)
        {
            if (args.Count == 0)
            {
                var tbl = new Table();

                tbl["left"]   = new DynValue(Kernel.Instance.Vga.Margins.Left);
                tbl["top"]    = new DynValue(Kernel.Instance.Vga.Margins.Top);
                tbl["right"]  = new DynValue(Kernel.Instance.Vga.Margins.Right);
                tbl["bottom"] = new DynValue(Kernel.Instance.Vga.Margins.Bottom);

                return(new DynValue(tbl));
            }
            else
            {
                args.ExpectExactly(4)
                .ExpectByteAtIndex(0)
                .ExpectByteAtIndex(1)
                .ExpectByteAtIndex(2)
                .ExpectByteAtIndex(3);

                Kernel.Instance.Vga.SetMargins(
                    (byte)args[0].Number,
                    (byte)args[1].Number,
                    (byte)args[2].Number,
                    (byte)args[3].Number
                    );

                return(DynValue.Zero);
            }
        }
Exemplo n.º 10
0
        public static DynValue ToChar(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectIntegerAtIndex(0);

            return(new DynValue(((char)args[0].Number).ToString()));
        }
Exemplo n.º 11
0
        public DynValue GetAttributes(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var path = args[0].String;

            if (!File.Exists(path))
            {
                return(new DynValue(-1));
            }

            var attrs = File.Get(path).Attributes;
            var ret   = string.Empty;

            if ((attrs & FileAttributes.Executable) != 0)
            {
                ret += "x";
            }
            else
            {
                ret += "-";
            }

            if ((attrs & FileAttributes.Hidden) != 0)
            {
                ret += "h";
            }
            else
            {
                ret += "-";
            }

            return(new DynValue(ret));
        }
Exemplo n.º 12
0
        public static DynValue LowerCase(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var str = args[0].String;

            return(new DynValue(str.ToLowerInvariant()));
        }
Exemplo n.º 13
0
        public DynValue KeyUp(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.Number);

            var keyCode = args[0].Number;

            return(new DynValue(Keyboard.IsKeyUp((KeyCode)keyCode) ? 1 : 0));
        }
Exemplo n.º 14
0
        public static DynValue ToNumber(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            if (!decimal.TryParse(args[0].String, out var result))
            {
                throw new ClrFunctionException("The number was in an invalid format.");
            }

            return(new DynValue(result));
        }
Exemplo n.º 15
0
        public DynValue Wait(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectIntegerAtIndex(0);

            var timeExpires = DateTime.Now + TimeSpan.FromMilliseconds(args[0].Number);

            while (!interpreter.BreakExecution && DateTime.Now < timeExpires)
            {
            }

            return(DynValue.Zero);
        }
Exemplo n.º 16
0
        public static DynValue IsGlobal(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var name = args[0].String;

            if (interpreter.Environment.GlobalScope.HasMember(name))
            {
                return(new DynValue(1));
            }

            return(new DynValue(0));
        }
Exemplo n.º 17
0
        public DynValue SetAttributes(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectTypeAtIndex(1, DynValueType.String);

            var path  = args[0].String;
            var attrs = args[1].String.Distinct();

            if (!File.Exists(path))
            {
                return(new DynValue(-1));
            }

            FileAttributes fileAttrs = 0;

            foreach (var c in attrs)
            {
                if (!File.SupportedAttributeDescriptors.Contains(c) && !(c == '-'))
                {
                    return(new DynValue(-2));
                }

                if (c == 'x')
                {
                    fileAttrs |= FileAttributes.Executable;
                    continue;
                }
                else if (c == '-')
                {
                    fileAttrs &= ~(FileAttributes.Executable);
                    continue;
                }

                if (c == 'h')
                {
                    fileAttrs |= FileAttributes.Hidden;
                }
                else if (c == '-')
                {
                    fileAttrs &= ~(FileAttributes.Hidden);
                }
            }

            var file = File.Get(path);

            file.Attributes = fileAttrs;

            return(DynValue.Zero);
        }
Exemplo n.º 18
0
        public DynValue IsFunction(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var name = args[0].String;

            if (interpreter.Environment.Functions.ContainsKey(name))
            {
                return(new DynValue(1));
            }

            return(new DynValue(0));
        }
Exemplo n.º 19
0
        public static DynValue ToCharCode(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var str = args[0].String;

            if (str.Length != 1)
            {
                throw new ClrFunctionException("Expected a single character.");
            }

            return(new DynValue(str[0]));
        }
Exemplo n.º 20
0
        public DynValue WaitForProcess(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectIntegerAtIndex(0);

            interpreter.SuspendExecution = true;

            while (!interpreter.BreakExecution && Kernel.Instance.ProcessManager.IsProcessRunning((int)args[0].Number))
            {
            }

            interpreter.SuspendExecution = false;

            return(DynValue.Zero);
        }
Exemplo n.º 21
0
        public DynValue SetTextModeBackground(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(3)
            .ExpectByteAtIndex(0)
            .ExpectByteAtIndex(1)
            .ExpectByteAtIndex(2);

            var r = (byte)args[0].Number;
            var g = (byte)args[1].Number;
            var b = (byte)args[2].Number;

            Kernel.Instance.Vga.ActiveBackgroundColor = new Color(r, g, b);

            return(DynValue.Zero);
        }
Exemplo n.º 22
0
        public DynValue ToCharTable(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var str   = args[0].String;
            var table = new Table();

            for (var i = 0; i < str.Length; i++)
            {
                table[i] = new DynValue(str[i].ToString());
            }

            return(new DynValue(table));
        }
Exemplo n.º 23
0
        public static DynValue CharAt(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectIntegerAtIndex(1);

            var str   = args[0].String;
            var index = (int)args[1].Number;

            if (index >= str.Length)
            {
                throw new ClrFunctionException("Requested index is outside the provided string.");
            }

            return(new DynValue(str[index].ToString()));
        }
Exemplo n.º 24
0
        public static DynValue Insert(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(3)
            .ExpectTypeAtIndex(0, DynValueType.Table);

            var tbl = args[0].Table;

            if (tbl.ContainsKey(args[1]))
            {
                throw new ClrFunctionException("Cannot insert a value into a table when the same key already exists.");
            }

            tbl[args[1]] = args[2];

            return(DynValue.Zero);
        }
Exemplo n.º 25
0
        public DynValue Kill(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectIntegerAtIndex(0);

            var pid = (int)args[0].Number;

            if (Kernel.Instance.ProcessManager.Processes.ContainsKey(pid))
            {
                Kernel.Instance.ProcessManager.Kill(pid);
                return(new DynValue(1));
            }
            else
            {
                return(DynValue.Zero);
            }
        }
Exemplo n.º 26
0
        public static DynValue Remove(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.Table);

            var tbl = args[0].Table;
            var key = tbl.GetKeyByDynValue(args[1]);

            if (key == null)
            {
                throw new ClrFunctionException("The requested key was not found in the table.");
            }

            tbl.Remove(key);

            return(DynValue.Zero);
        }
Exemplo n.º 27
0
        public DynValue MakeDir(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var target = args[0].String;

            try
            {
                Directory.CreateDirectory(target);
                return(DynValue.Zero);
            }
            catch
            {
                return(new DynValue(-1));
            }
        }
Exemplo n.º 28
0
        public DynValue ChangeDirectory(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var path = args[0].String;

            try
            {
                Directory.ChangeWorkingDirectory(path);
                return(DynValue.Zero);
            }
            catch
            {
                return(new DynValue(-1));
            }
        }
Exemplo n.º 29
0
        public DynValue Copy(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectTypeAtIndex(1, DynValueType.String);

            var source = args[0].String;
            var target = args[1].String;

            try
            {
                File.Copy(source, target);
                return(DynValue.Zero);
            }
            catch
            {
                return(new DynValue(-1));
            }
        }
Exemplo n.º 30
0
        public DynValue Write(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectTypeAtIndex(1, DynValueType.String);

            var path = args[0].String;
            var data = args[1].String;

            try
            {
                File.Create(path, true).SetData(data);
                return(DynValue.Zero);
            }
            catch
            {
                return(new DynValue(-1));
            }
        }