Пример #1
0
        private static FLScriptData LoadScriptData(string file, CLBufferInfo inBuffer, int width, int height, int depth, int channelCount,
                                                   KernelDatabase db, Dictionary <string, FLFunctionInfo> funcs)
        {
            Logger.Log("Loading Script Data for File: " + file, DebugChannel.Log | DebugChannel.OpenFL | DebugChannel.IO, 6);

            FLScriptData ret = new FLScriptData(LoadSource(file, channelCount));


            ret.Defines.Add(InputBufferName, inBuffer);

            Logger.Log("Parsing Texture Defines for File: " + file, DebugChannel.Log | DebugChannel.OpenFL | DebugChannel.IO, 5);
            ParseDefines(DefineKey, DefineTexture, ret.Source, ret.Defines, width, height, depth, channelCount, db);

            Logger.Log("Parsing Script Defines for File: " + file, DebugChannel.Log | DebugChannel.OpenFL | DebugChannel.IO, 5);
            ParseDefines(ScriptDefineKey, DefineScript, ret.Source, ret.Defines, width, height, depth, channelCount, db);

            Logger.Log("Parsing JumpLocations for File: " + file, DebugChannel.Log | DebugChannel.OpenFL | DebugChannel.IO, 5);
            ret.JumpLocations = ParseJumpLocations(ret.Source);

            Logger.Log("Parsing Instruction Data for File: " + file, DebugChannel.Log | DebugChannel.OpenFL | DebugChannel.IO, 5);
            foreach (string line in ret.Source)
            {
                Logger.Log("Parsing Instruction Data for Line: " + line, DebugChannel.Log | DebugChannel.OpenFL | DebugChannel.IO, 3);
                FLInstructionData data = GetInstructionData(line, ret.Defines, ret.JumpLocations, funcs, db);

                Logger.Log("Parsed Instruction Data: " + Enum.GetName(typeof(FLInstructionType), data.InstructionType), DebugChannel.Log | DebugChannel.OpenFL | DebugChannel.IO, 2);

                ret.ParsedSource.Add(data);
            }


            return(ret);
        }
Пример #2
0
        /// <summary>
        /// Detects if the Interpreter has reached the end of the current function
        /// </summary>
        private void DetectEnd()
        {
            if (_currentIndex == Data.ParsedSource.Count || Data.ParsedSource[_currentIndex].InstructionType == FLInstructionType.FunctionHeader)
            {
                if (_jumpStack.Count == 0)
                {
                    Logger.Log("Reached End of Code", DebugChannel.Log | DebugChannel.OpenFL, 9);

                    Terminated = true;
                }
                else
                {
                    InterpreterState lastState = _jumpStack.Pop();

                    Logger.Log("Returning to location: " + Data.Source[lastState.Line], DebugChannel.Log | DebugChannel.OpenFL, 6);
                    _currentIndex = lastState.Line;


                    if (lastState.ArgumentStack.Count != 0 && lastState.ArgumentStack.Peek() == null)
                    {
                        _leaveStack = true;
                        lastState.ArgumentStack.Pop();
                        lastState.ArgumentStack.Push(_currentBuffer);
                    }

                    _currentArgStack = lastState.ArgumentStack;
                    _currentBuffer   = lastState.ActiveBuffer;

                    _currentWord = lastState.ArgumentStack.Count;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Resets the Interpreter to work with a new script
        /// </summary>
        /// <param name="file">The file containing the source</param>
        /// <param name="input">The input buffer</param>
        /// <param name="width">Width of the input buffer</param>
        /// <param name="height">Height of the input buffer</param>
        /// <param name="depth">Depth of the input buffer</param>
        /// <param name="channelCount">The Channel Count</param>
        /// <param name="kernelDB">The Kernel DB that will be used</param>
        /// <param name="ignoreDebug">a flag to ignore the brk statement</param>
        public void Reset(string file, MemoryBuffer input, int width, int height, int depth, int channelCount,
                          KernelDatabase kernelDB, bool ignoreDebug)
        {
            //Clear old stuff

            ReleaseResources();

            //Setting variables
            _currentBuffer = new CLBufferInfo(input, false);
            _currentBuffer.SetKey(InputBufferName);

            _ignoreDebug     = ignoreDebug;
            _width           = width;
            _height          = height;
            _depth           = depth;
            _channelCount    = channelCount;
            _kernelDb        = kernelDB;
            _activeChannels  = new byte[_channelCount];
            _currentArgStack = new Stack <object>();
            for (int i = 0; i < _channelCount; i++)
            {
                _activeChannels[i] = 1;
            }

            _activeChannelBuffer =
                CLAPI.CreateBuffer(_activeChannels, MemoryFlag.ReadOnly | MemoryFlag.CopyHostPointer);

            //Parsing File
            _currentBuffer.SetKey(InputBufferName);
            Data = LoadScriptData(file, _currentBuffer, width, height, depth, channelCount, _kernelDb, _flFunctions);

            Reset();
        }
Пример #4
0
        /// <summary>
        /// Jumps the interpreter to the specified index
        /// </summary>
        /// <param name="index">the index of the line to jump to</param>
        /// <param name="leaveBuffer">a flag to optionally keep the current buffer</param>
        private void JumpTo(int index, bool leaveBuffer = false)
        {
            Logger.Log("Jumping To Function: " + Data.Source[index], DebugChannel.OpenFL | DebugChannel.Log, 6);
            _jumpStack.Push(new InterpreterState(_currentIndex, _currentBuffer, _currentArgStack));
            _stepResult.HasJumped = true;

#if NO_CL
            int size = 1;
#else
            int size = (int)_currentBuffer.Buffer.Size;
#endif


            if (!leaveBuffer)
            {
                _currentBuffer =
                    new CLBufferInfo(CLAPI.CreateEmpty <byte>(size, MemoryFlag.ReadWrite | MemoryFlag.CopyHostPointer),
                                     true);
                _currentBuffer.SetKey("Internal_JumpBuffer_Stack_Index" + (_jumpStack.Count - 1));
            }

            _currentIndex = index + 1; //+1 because the index is the function header
            _currentWord  = 0;
        }
Пример #5
0
        /// <summary>
        /// Define handler that loads defined textures
        /// </summary>
        /// <param name="arg">The Line of the definition</param>
        private static void DefineTexture(string[] arg, Dictionary <string, CLBufferInfo> defines, int width, int height,
                                          int depth, int channelCount, KernelDatabase kernelDb)
        {
            if (arg.Length < 2)
            {
                Logger.Crash(new FLInvalidFunctionUseException(ScriptDefineKey, "Invalid Define statement"), true);
                return;
            }

            string varname = arg[0].Trim();


            if (defines.ContainsKey(varname))
            {
                Logger.Log("Overwriting " + varname, DebugChannel.Warning | DebugChannel.OpenFL, 10);
                defines.Remove(varname);
            }

            MemoryFlag flags = MemoryFlag.ReadWrite;

            string[] flagTest = varname.Split(' ');
            if (flagTest.Length > 1)
            {
                varname = flagTest[1];
                if (flagTest[0] == "r")
                {
                    flags = MemoryFlag.ReadOnly;
                }

                else if (flagTest[0] == "w")
                {
                    flags = MemoryFlag.WriteOnly;
                }
            }

            string[] args = arg[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);


            string filename = args[0].Trim();

            byte[] activeChannels = new byte[channelCount];
            for (int i = 0; i < activeChannels.Length; i++)
            {
                activeChannels[i] = 1;
            }

            int InputBufferSize = width * height * depth * channelCount;

            if (IsSurroundedBy(filename, FilepathIndicator))
            {
                string fn = filename.Replace(FilepathIndicator, "");
                if (File.Exists(fn))
                {
                    Bitmap       bmp  = new Bitmap((Bitmap)System.Drawing.Image.FromFile(fn), width, height);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateFromImage(bmp,
                                                                               MemoryFlag.CopyHostPointer | flags), true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else
                {
                    Logger.Crash(
                        new FLInvalidFunctionUseException(DefineKey, "Invalid Filepath",
                                                          new InvalidFilePathException(fn)), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
            }
            else if (filename == "rnd")
            {
                MemoryBuffer buf = CLAPI.CreateEmpty <byte>(InputBufferSize, flags | MemoryFlag.CopyHostPointer);
                CLAPI.WriteRandom(buf, randombytesource, activeChannels, false);

                CLBufferInfo info = new CLBufferInfo(buf, true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
            else if (filename == "urnd")
            {
                MemoryBuffer buf = CLAPI.CreateEmpty <byte>(InputBufferSize, flags | MemoryFlag.CopyHostPointer);
                CLAPI.WriteRandom(buf, randombytesource, activeChannels, true);

                CLBufferInfo info = new CLBufferInfo(buf, true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
            else if (filename == "empty")
            {
                CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, flags), true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
            else if (filename == "wfc" || filename == "wfcf")
            {
                bool force = filename == "wfcf";
                if (args.Length < 10)
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[2], out int n))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[3], out int widh))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[4], out int heigt))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!bool.TryParse(args[5], out bool periodicInput))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!bool.TryParse(args[6], out bool periodicOutput))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[7], out int symmetry))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[8], out int ground))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else if (!int.TryParse(args[9], out int limit))
                {
                    Logger.Crash(new FLInvalidFunctionUseException("wfc", "Invalid WFC Define statement"), true);
                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
                else
                {
                    string fn = args[1].Trim().Replace(FilepathIndicator, "");
                    if (File.Exists(fn))
                    {
                        Bitmap         bmp;
                        WFCOverlayMode wfc = new WFCOverlayMode(fn, n, widh,
                                                                heigt, periodicInput, periodicOutput, symmetry, ground);
                        if (force)
                        {
                            do
                            {
                                wfc.Run(limit);
                                bmp = new Bitmap(wfc.Graphics(), new Size(width, height)); //Apply scaling
                            } while (!wfc.Success);
                        }
                        else
                        {
                            wfc.Run(limit);
                            bmp = new Bitmap(wfc.Graphics(), new Size(width, height)); //Apply scaling
                        }

                        CLBufferInfo info = new CLBufferInfo(CLAPI.CreateFromImage(bmp,
                                                                                   MemoryFlag.CopyHostPointer | flags), true);
                        info.SetKey(varname);
                        defines.Add(varname, info);
                    }
                    else
                    {
                        Logger.Crash(
                            new FLInvalidFunctionUseException("wfc", "Invalid WFC Image statement",
                                                              new InvalidFilePathException(fn)), true);
                        CLBufferInfo info =
                            new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite), true);
                        info.SetKey(varname);
                        defines.Add(varname, info);
                    }
                }
            }

            else
            {
                string s = "";
                foreach (string s1 in args)
                {
                    s += s1 + " ";
                }

                Logger.Crash(new FLInvalidFunctionUseException(DefineKey, "Define statement wrong: " + s), true);
                CLBufferInfo info =
                    new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite), true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
        }
Пример #6
0
 /// <summary>
 /// Creates an interpreter state
 /// </summary>
 /// <param name="line">The line where the interpreter is</param>
 /// <param name="activeBuffer">The active buffer</param>
 /// <param name="argumentStack">The unfinished argument stack</param>
 internal InterpreterState(int line, CLBufferInfo activeBuffer, Stack <object> argumentStack)
 {
     Line          = line;
     ActiveBuffer  = activeBuffer;
     ArgumentStack = argumentStack;
 }
Пример #7
0
        /// <summary>
        /// Define handler that loads defined scripts
        /// </summary>
        /// <param name="arg">The Line of the definition</param>
        private static void DefineScript(string[] arg, Dictionary <string, CLBufferInfo> defines, int width, int height,
                                         int depth, int channelCount, KernelDatabase kernelDb)
        {
            if (arg.Length < 2)
            {
                Logger.Crash(new FLInvalidFunctionUseException(ScriptDefineKey, "Invalid Define statement"), true);
                return;
            }

            string varname = arg[0].Trim();

            if (defines.ContainsKey(varname))
            {
                Logger.Log("Overwriting " + varname, DebugChannel.Warning | DebugChannel.OpenFL, 10);
                defines.Remove(varname);
            }

            string[] args = arg[1].Split(' ', StringSplitOptions.RemoveEmptyEntries);


            string filename = args[0].Trim();

            int InputBufferSize = width * height * depth * channelCount;

            if (IsSurroundedBy(filename, FilepathIndicator))
            {
                Logger.Log("Loading SubScript...", DebugChannel.Log | DebugChannel.OpenFL, 10);

                MemoryBuffer buf =
                    CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite);


                string fn = filename.Replace(FilepathIndicator, "");


                if (File.Exists(fn))
                {
                    Interpreter interpreter = new Interpreter(fn, buf, width, height,
                                                              depth, channelCount, kernelDb, true);

                    do
                    {
                        interpreter.Step();
                    } while (!interpreter.Terminated);

                    CLBufferInfo info = interpreter.GetActiveBufferInternal();
                    info.SetKey(varname);
                    defines.Add(varname, info);
                    interpreter.ReleaseResources();
                }
                else
                {
                    Logger.Crash(
                        new FLInvalidFunctionUseException(ScriptDefineKey, "Not a valid filepath as argument.",
                                                          new InvalidFilePathException(fn)),
                        true);

                    CLBufferInfo info = new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite),
                                                         true);
                    info.SetKey(varname);
                    defines.Add(varname, info);
                }
            }
            else
            {
                Logger.Crash(new FLInvalidFunctionUseException(ScriptDefineKey, "Not a valid filepath as argument."),
                             true);

                CLBufferInfo info =
                    new CLBufferInfo(CLAPI.CreateEmpty <byte>(InputBufferSize, MemoryFlag.ReadWrite), true);
                info.SetKey(varname);
                defines.Add(varname, info);
            }
        }