コード例 #1
0
ファイル: ParseBMP.cs プロジェクト: jay-tux/BMPScript
        public CodeChar[,] Recreate(string BMP)
        {
            byte[] fData = GetBytes(BMP, out int Width, out int Height);
            CodeChar[,] data = new CodeChar[Width, Height];
            OutWriter.Debug($"[ BMPLOAD ]  Image is {Width}x{Height}.");
            for (int field = 0; field < fData.Length; field += 4)
            {
                int      xPos = (field / 4) % Width;
                int      yPos = Height - 1 - (field / 4) / Width;
                CodeChar pos  = (CodeChar)(new byte[] { fData[field + 2], fData[field + 1], fData[field] });
                OutWriter.Debug($"[ BMPLOAD ]    Filling @{xPos};{yPos}={pos}");
                data[xPos, yPos] = pos;
            }

            OutWriter.Debug("   ====  IMAGE OVERVIEW  ====   ");
            for (int y = 0; y < Height; y++)
            {
                string str = "";
                for (int x = 0; x < Width; x++)
                {
                    str += data[x, y].ToString() + " ";
                }
                OutWriter.Debug(str);
            }
            OutWriter.Debug("   ====  END OF OVERVIEW  ====   ");
            return(data);
        }
コード例 #2
0
        protected void Overview(Point Entry)
        {
            OutWriter.Debug("   ====  PROGRAM OVERVIEW  ====   ");
            IEnumerator <Point> it = new Iteration2D(Entry.X, Entry.Y, Program.GetLength(0), Program.GetLength(1)).Snake(270, true);

            while (it.MoveNext())
            {
                OutWriter.Debug("    " + GetAt(it.Current).ToString());
            }
            OutWriter.Debug("   ====  END OF OVERVIEW  ====   ");
        }
コード例 #3
0
 protected void SysDump()
 {
     OutWriter.Debug("   ====  FULL SYS_DUMP  ====   ");
     OutWriter.Debug(" => Defined Labels:");
     Labels.Select(x => $"\t{x.Key}: ({x.Value.X}, {x.Value.Y})").ToList().ForEach(x => OutWriter.Debug(x));
     OutWriter.Debug(" ---- ---- ");
     OutWriter.Debug(" => Defined Variables:");
     OutWriter.Debug("   -> Defined Integers:");
     Integers.Select(x => $"\t{x.Key.ToString("D2")}: {x.Value}").ToList().ForEach(x => OutWriter.Debug(x));
     OutWriter.Debug("  --    --  ");
     OutWriter.Debug("   -> Defined Characters:");
     Characters.Select(x => $"\t{x.Key.ToString("D2")}: {x.Value}").ToList().ForEach(x => OutWriter.Debug(x));
     OutWriter.Debug("   ==== END OF SYS_DUMP ====   ");
 }
コード例 #4
0
ファイル: Loader.cs プロジェクト: jay-tux/BMPScript
 protected void Load(string Input, int Depth = 0)
 {
     if (Depth > 100)
     {
         return;
     }
     if (File.Exists(Input))
     {
         try
         {
             Entry = new Point(0, 0);
             OutWriter.Debug("[ LOAD    ]Trying to load/convert image...");
             Data = new ParseBMP().Recreate(Input);
             if (Data == null)
             {
                 OutWriter.Debug("[ LOAD ]  Failed to load."); return;
             }
             OutWriter.Debug($"[ LOAD    ]    Recreated image in RAM ({Data.GetLength(0)}x{Data.GetLength(1)})");
             for (int x = 0; x < Data.GetLength(0); x++)
             {
                 for (int y = 0; y < Data.GetLength(1); y++)
                 {
                     if (Data[x, y] == null)
                     {
                         OutWriter.Debug($"[  LOAD ]      CodeChar@({x},{y}) is null.");
                     }
                     if ((CodeChar.Order)(Data[x, y]) == CodeChar.Order.Entry)
                     {
                         Entry = new Point(x, y);
                     }
                 }
             }
             OutWriter.Debug("[ LOAD    ]    Ready");
             new Parser(Data, Depth).Start(Entry);
         }
         catch (ArgumentException ane)
         {
             Console.ForegroundColor = ConsoleColor.Red;
             OutWriter.Debug($"Argument Exception: {Input}: {ane.Message}\n\t{ane.StackTrace}");
         }
         catch (IOException ioe)
         {
             throw new BMPScriptException("preproc::loader", "FileError", $"Can't read file {Input}.");
         }
     }
     else
     {
         throw new BMPScriptException("preproc::loader", "FileError", $"Script/BMP file {Input} does not exist.");
     }
 }
コード例 #5
0
        protected void PreProcess(Point Entry)
        {
            //Uncomment to show Program overview at start of execution
            //Overview(Entry);
            OutWriter.Debug("[ PREPROC ]  Started Preprocessor...");
            IEnumerator <Point> it = new Iteration2D(Entry.X, Entry.Y, Program.GetLength(0), Program.GetLength(1))
                                     .Snake(270, true);

            while (it.MoveNext())
            {
                //Jump to labels only
                switch ((CodeChar.Order)GetAt(it.Current))
                {
                //Skip none
                case CodeChar.Order.Entry:
                case CodeChar.Order.WriteLn:
                case CodeChar.Order.Parse:
                case CodeChar.Order.Exit:
                    break;

                //Skip two
                case CodeChar.Order.If:
                case CodeChar.Order.Math:
                case CodeChar.Order.Not:
                    it.MoveNext(); it.MoveNext();
                    break;

                //Label: mark
                case CodeChar.Order.Label:
                    Point pt = it.Current;
                    it.MoveNext();
                    Labels[GetAt(it.Current)] = new Point(it.Current.X, it.Current.Y);
                    OutWriter.Debug($"[ PREPROC ]      @{pt.ToString()}: Encountered Mark Label: ({GetAt(it.Current).ToString()}) := {Labels[GetAt(it.Current)].ToString()}");
                    break;

                //Skip one
                default:
                    it.MoveNext();
                    break;
                }
            }
            OutWriter.Debug("[ PREPROC ]    Labels:");
            Labels.Select(x => $"{x.Key}: ({x.Value.X}, {x.Value.Y})").ToList().ForEach(x => OutWriter.Debug($"[ PREPROC ]      {x}"));
            OutWriter.Debug("[ PREPROC ]  Preprocessor Ready.");
        }
コード例 #6
0
 public static void Main(string[] args)
 {
     if (args.Length == 0)
     {
         Main(new string[] { Environment.CurrentDirectory + "/0.bmp" });
     }
     else
     {
         OutWriter.Debug($"[ BOOT    ]Trying to load {args[0]}.");
         if (File.Exists(args[0]))
         {
             OutWriter.Debug($"[ BOOT    ]Started parser on {args[0]}.");
             new Loader(args[0]);
             //new Loader(args[0]);
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Red;
             Console.Error.WriteLine("No image to parse.");
         }
     }
 }
コード例 #7
0
        public void Start(Point Entry)
        {
            Labels     = new Dictionary <CodeChar, Point>();
            Integers   = new Dictionary <int, int>();
            Characters = new Dictionary <int, char>();
            if (Program == null)
            {
                Console.Error.Write("Program is empty.");
            }
            PreProcess(Entry);
            OutWriter.Debug("");
            OutWriter.Debug("[ PARSE   ]Starting Parser.");
            Iteration2D         i2d = new Iteration2D(Entry.X, Entry.Y, Program.GetLength(0), Program.GetLength(1));
            bool                fin = false;
            CodeChar            cc;
            IEnumerator <Point> it = i2d.Snake(270);

            while (!fin && it.MoveNext())
            {
                OutWriter.Debug("[ PARSE   ]  @" + it.Current.ToString() + ": " + (string)GetAt(it.Current));
                switch ((CodeChar.Order)GetAt(it.Current))
                {
                case CodeChar.Order.Entry:
                    continue;

                case CodeChar.Order.Exit:
                    fin = true;
                    break;

                case CodeChar.Order.If:
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    if (EvaluateCheck(cc.GetField(CodeChar.Part.R), cc.GetField(CodeChar.Part.G), cc.GetField(CodeChar.Part.B)))
                    {
                        OutWriter.Debug("[ PARSE   ]    Check Succeeded");
                        it.MoveNext();
                        if (Labels.ContainsKey(GetAt(it.Current)))
                        {
                            i2d.XPos = Labels[GetAt(it.Current)].X;
                            i2d.YPos = Labels[GetAt(it.Current)].Y;
                        }
                    }
                    else
                    {
                        OutWriter.Debug("[ PARSE   ]    Check Failed");
                        it.MoveNext();
                    }
                    break;

                case CodeChar.Order.Jump:
                    it.MoveNext();
                    if (Labels.ContainsKey(GetAt(it.Current)))
                    {
                        i2d.XPos = Labels[GetAt(it.Current)].X;
                        i2d.YPos = Labels[GetAt(it.Current)].Y;
                    }
                    break;

                case CodeChar.Order.Label:
                    break;

                case CodeChar.Order.Math:
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    it.MoveNext();
                    Integers[GetAt(it.Current).GetField(CodeChar.Part.R)] =
                        EvaluateMath(cc.GetField(CodeChar.Part.R), cc.GetField(CodeChar.Part.G), cc.GetField(CodeChar.Part.B));
                    break;

                case CodeChar.Order.Not:
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    if (!EvaluateCheck(cc.GetField(CodeChar.Part.R), cc.GetField(CodeChar.Part.G), cc.GetField(CodeChar.Part.B)))
                    {
                        OutWriter.Debug("[ PARSE    ]    Check Succeeded");
                        it.MoveNext();
                        if (Labels.ContainsKey(GetAt(it.Current)))
                        {
                            i2d.XPos = Labels[GetAt(it.Current)].X;
                            i2d.YPos = Labels[GetAt(it.Current)].Y;
                        }
                    }
                    else
                    {
                        OutWriter.Debug("[ PARSE   ]    Check Failed");
                        it.MoveNext();
                    }
                    break;

                case CodeChar.Order.Parse:
                    Read++;
                    new Loader(Environment.CurrentDirectory + $"/{Read}.bmp", Depth);
                    break;

                case CodeChar.Order.Read:
                    Console.Write("? ");
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    if (cc.GetField(CodeChar.Part.R) >= 128)
                    {
                        string i = Console.ReadLine();
                        while (!int.TryParse(i, out int tmp))
                        {
                            Console.Error.Write("Input error: Expected an integer: "); i = Console.ReadLine();
                        }
                        Integers[cc.GetField(CodeChar.Part.G)] = tmp;
                    }
                    else
                    {
                        string i = Console.ReadLine();
                        while (i.Length < 1)
                        {
                            Console.Error.Write("Input error: Expected a character: "); i = Console.ReadLine();
                        }
                        Characters[cc.GetField(CodeChar.Part.B)] = i[0];
                    }
                    break;

                case CodeChar.Order.RNG:
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    Integers[cc.GetField(CodeChar.Part.R)] = RNG.Next(
                        (cc.GetField(CodeChar.Part.G) < cc.GetField(CodeChar.Part.B) ? cc.GetField(CodeChar.Part.G) : cc.GetField(CodeChar.Part.B)),
                        (cc.GetField(CodeChar.Part.G) > cc.GetField(CodeChar.Part.B) ? cc.GetField(CodeChar.Part.G) : cc.GetField(CodeChar.Part.B))
                        );
                    break;

                case CodeChar.Order.RGNV:
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    int e1 = (Integers.ContainsKey(cc.GetField(CodeChar.Part.G)) ? Integers[cc.GetField(CodeChar.Part.G)] :
                              Characters.ContainsKey(cc.GetField(CodeChar.Part.G)) ? (int)Characters[cc.GetField(CodeChar.Part.G)] :
                              cc.GetField(CodeChar.Part.G));
                    int e2 = (Integers.ContainsKey(cc.GetField(CodeChar.Part.B)) ? Integers[cc.GetField(CodeChar.Part.B)] :
                              Characters.ContainsKey(cc.GetField(CodeChar.Part.B)) ? (int)Characters[cc.GetField(CodeChar.Part.B)] :
                              cc.GetField(CodeChar.Part.B));
                    Integers[cc.GetField(CodeChar.Part.R)] = RNG.Next((e1 < e2) ? e1 : e2, (e1 > e2) ? e2 : e1);
                    break;

                case CodeChar.Order.Var:
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    if (cc.GetField(CodeChar.Part.R) >= 128)
                    {
                        Integers[cc.GetField(CodeChar.Part.G)] = cc.GetField(CodeChar.Part.B);
                    }
                    else
                    {
                        Characters[cc.GetField(CodeChar.Part.G)] = (char)cc.GetField(CodeChar.Part.B);
                    }
                    break;

                case CodeChar.Order.VarCP:
                    it.MoveNext();
                    cc = GetAt(it.Current);
                    if (cc.GetField(CodeChar.Part.R) >= 128)
                    {
                        Integers[cc.GetField(CodeChar.Part.G)] =
                            (Integers.ContainsKey(cc.GetField(CodeChar.Part.B)) ? Integers[cc.GetField(CodeChar.Part.B)] :
                             (Characters.ContainsKey(cc.GetField(CodeChar.Part.B)) ? (int)Characters[cc.GetField(CodeChar.Part.B)] :
                              cc.GetField(CodeChar.Part.B)));
                    }
                    else
                    {
                        Characters[cc.GetField(CodeChar.Part.G)] =
                            (Integers.ContainsKey(cc.GetField(CodeChar.Part.B)) ? (char)Integers[cc.GetField(CodeChar.Part.B)] :
                             (Characters.ContainsKey(cc.GetField(CodeChar.Part.B)) ? Characters[cc.GetField(CodeChar.Part.B)] :
                              (char)cc.GetField(CodeChar.Part.B)));
                    }
                    break;

                case CodeChar.Order.WriteC:
                    it.MoveNext();
                    cc = this.GetAt(it.Current);
                    Writer.Write($"{(char)cc.GetField(CodeChar.Part.R)}{(char)cc.GetField(CodeChar.Part.G)}{(char)cc.GetField(CodeChar.Part.B)}");
                    break;

                case CodeChar.Order.WriteLn:
                    Writer.Write("");
                    break;

                case CodeChar.Order.WriteV:
                    it.MoveNext();
                    int vl = this.GetAt(it.Current).GetField(CodeChar.Part.R);
                    Writer.Write(((Integers.ContainsKey(vl) ? Integers[vl] : Characters.ContainsKey(vl) ? Characters[vl] : vl)).ToString());
                    break;
                }
            }
            OutWriter.Debug("[ PARSE   ]Parser finished.");
            OutWriter.Debug("");
            SysDump();
        }