Пример #1
0
        public CodePiece CopyNormalized()
        {
            CodePiece result = Copy();

            result.Normalize();
            return(result);
        }
Пример #2
0
        public void ReverseX(bool nonpedantic)
        {
            CodePiece p = this.Copy();

            this.Clear();

            for (int x = p.MinX; x < p.MaxX; x++)
            {
                for (int y = p.MinY; y < p.MaxY; y++)
                {
                    if (!p[x, y].IsXDeltaIndependent())
                    {
                        if (nonpedantic && p[x, y].Type == BefungeCommandType.PCLeft)
                        {
                            this[-x, y] = BCHelper.PCRight;
                        }
                        else if (nonpedantic && p[x, y].Type == BefungeCommandType.PCRight)
                        {
                            this[-x, y] = BCHelper.PCLeft;
                        }
                        else
                        {
                            throw new CodePieceReverseException(p);
                        }
                    }
                    else
                    {
                        this[-x, y] = p[x, y];
                    }
                }
            }

            this.NormalizeX();
        }
Пример #3
0
        public void AppendLeft(int row, BefungeCommand c)
        {
            CodePiece p = new CodePiece();

            p[0, row] = c;

            AppendLeft(p);
        }
Пример #4
0
        public void AppendTop(int col, BefungeCommand c)
        {
            CodePiece p = new CodePiece();

            p[col, 0] = c;

            AppendTop(p);
        }
Пример #5
0
        public static CodePiece Repeat(int count, BefungeCommand cmd)
        {
            CodePiece p = new CodePiece();

            for (int i = 0; i < count; i++)
            {
                p[i, 0] = cmd;
            }
            return(p);
        }
Пример #6
0
 public void Append(CodePiece c, bool reverse)
 {
     if (reverse)
     {
         AppendLeft(c);
     }
     else
     {
         AppendRight(c);
     }
 }
Пример #7
0
        public static CodePiece CombineHorizontal(CodePiece first, params CodePiece[] other)
        {
            CodePiece p = first.Copy();

            foreach (CodePiece pO in other)
            {
                p.AppendRight(pO);
            }

            return(p);
        }
Пример #8
0
        public static CodePiece DoCompressHorizontally(CodePiece l, CodePiece r)
        {
            if (l.Width == 0 || r.Width == 0)
            {
                return(null);
            }

            CodePiece connect = new CodePiece();

            int xL = l.MaxX - 1;
            int xR = r.MinX;

            for (int y = Math.Min(l.MinY, r.MinY); y < Math.Max(l.MaxY, r.MaxY); y++)
            {
                CodeTag tag = null;

                if (l[xL, y].Tag != null && r[xR, y].Tag != null)
                {
                    return(null);                    // Can't compress - two tags would need to be merged
                }

                tag = l[xL, y].Tag ?? r[xR, y].Tag;

                if (l[xL, y].Type == BefungeCommandType.NOP && r[xR, y].Type == BefungeCommandType.NOP)
                {
                    connect[0, y] = new BefungeCommand(BefungeCommandType.NOP, tag);
                }
                else if (l[xL, y].Type != BefungeCommandType.NOP && r[xR, y].Type != BefungeCommandType.NOP)
                {
                    if (l[xL, y].Type == r[xR, y].Type && l[xL, y].Param == r[xR, y].Param && l[xL, y].IsCompressable())
                    {
                        connect[0, y] = new BefungeCommand(l[xL, y].Type, l[xL, y].Param, tag);
                    }
                    else
                    {
                        return(null);                        // Can't compress - two commands are colliding
                    }
                }
                else if (l[xL, y].Type != BefungeCommandType.NOP)
                {
                    connect[0, y] = new BefungeCommand(l[xL, y].Type, l[xL, y].Param, tag);
                }
                else if (r[xR, y].Type != BefungeCommandType.NOP)
                {
                    connect[0, y] = new BefungeCommand(r[xR, y].Type, r[xR, y].Param, tag);
                }
                else
                {
                    throw new WTFException();
                }
            }

            return(connect);
        }
Пример #9
0
        public static CodePiece CreateFromVerticalList(List <CodePiece> cplist)
        {
            CodePiece p = new CodePiece();

            foreach (var cp in cplist)
            {
                p.AppendBottom(cp);
            }

            return(p);
        }
Пример #10
0
        public static CodePiece DoCompressVertically(CodePiece t, CodePiece b)
        {
            if (t.Width == 0 || b.Width == 0)
            {
                return(null);
            }

            CodePiece connect = new CodePiece();

            int yT = t.MaxY - 1;
            int yB = b.MinY;

            for (int x = Math.Min(t.MinX, b.MinX); x < Math.Max(t.MaxX, b.MaxX); x++)
            {
                CodeTag tag = null;

                if (t[x, yT].Tag != null && b[x, yB].Tag != null)
                {
                    return(null);                    // Can't compress - two tags would need to be merged
                }

                tag = t[x, yT].Tag ?? b[x, yB].Tag;

                if (t[x, yT].Type == BefungeCommandType.NOP && b[x, yB].Type == BefungeCommandType.NOP)
                {
                    connect[x, 0] = new BefungeCommand(BefungeCommandType.NOP, tag);
                }
                else if (t[x, yT].Type != BefungeCommandType.NOP && b[x, yB].Type != BefungeCommandType.NOP)
                {
                    if (t[x, yT].Type == b[x, yB].Type && t[x, yT].Param == b[x, yB].Param && t[x, yT].IsCompressable())
                    {
                        connect[x, 0] = new BefungeCommand(t[x, yT].Type, t[x, yT].Param, tag);
                    }
                    else
                    {
                        return(null);                        // Can't compress - two commands are colliding
                    }
                }
                else if (t[x, yT].Type != BefungeCommandType.NOP)
                {
                    connect[x, 0] = new BefungeCommand(t[x, yT].Type, t[x, yT].Param, tag);
                }
                else if (b[x, yB].Type != BefungeCommandType.NOP)
                {
                    connect[x, 0] = new BefungeCommand(b[x, yB].Type, b[x, yB].Param, tag);
                }
                else
                {
                    throw new WTFException();
                }
            }

            return(connect);
        }
Пример #11
0
        public static CodePiece CombineVertical(CodePiece top, CodePiece bottom)
        {
            CodePiece cT = top.Copy();
            CodePiece cB = bottom.Copy();

            cT.NormalizeY();
            cB.NormalizeY();

            cT.AppendBottom(cB);

            return(cT);
        }
Пример #12
0
        public static CodePiece CombineHorizontal(CodePiece left, CodePiece right)
        {
            CodePiece cL = left.Copy();
            CodePiece cR = right.Copy();

            cL.NormalizeX();
            cR.NormalizeX();

            cL.AppendRight(cR);

            return(cL);
        }
Пример #13
0
        private void AppendRightDirect(CodePiece right)
        {
            right.NormalizeX();

            int offset = MaxX;

            for (int x = right.MinX; x < right.MaxX; x++)
            {
                for (int y = right.MinY; y < right.MaxY; y++)
                {
                    this[offset + x, y] = right[x, y];
                }
            }
        }
Пример #14
0
        private void AppendBottomDirect(CodePiece bot)
        {
            bot.NormalizeY();

            int offset = MaxY;

            for (int x = bot.MinX; x < bot.MaxX; x++)
            {
                for (int y = bot.MinY; y < bot.MaxY; y++)
                {
                    this[x, offset + y] = bot[x, y];
                }
            }
        }
Пример #15
0
        private void AppendTopDirect(CodePiece top)
        {
            top.NormalizeY();

            int offset = MinY - top.MaxY;

            for (int x = top.MinX; x < top.MaxX; x++)
            {
                for (int y = top.MinY; y < top.MaxY; y++)
                {
                    this[x, offset + y] = top[x, y];
                }
            }
        }
Пример #16
0
        private void AppendLeftDirect(CodePiece left)
        {
            left.NormalizeX();

            int offset = MinX - left.MaxX;

            for (int x = left.MinX; x < left.MaxX; x++)
            {
                for (int y = left.MinY; y < left.MaxY; y++)
                {
                    this[offset + x, y] = left[x, y];
                }
            }
        }
Пример #17
0
        public void SetAt(int paramX, int paramY, CodePiece lit, bool skipNop = false)
        {
            for (int x = lit.MinX; x < lit.MaxX; x++)
            {
                for (int y = lit.MinY; y < lit.MaxY; y++)
                {
                    if (skipNop && lit[x, y].Type == BefungeCommandType.NOP)
                    {
                        continue;
                    }

                    this[x + paramX, y + paramY] = lit[x, y];
                }
            }
        }
Пример #18
0
        public void AppendTop(CodePiece top)
        {
            top = top.Copy();

            CodePiece compressConn;

            if (ASTObject.CGO.CompressVerticalCombining && (compressConn = DoCompressVertically(top, this)) != null)
            {
                this.RemoveRow(this.MinY);
                top.RemoveRow(top.MaxY - 1);

                this.AppendTopDirect(compressConn);
            }

            AppendTopDirect(top);
        }
Пример #19
0
        public void AppendBottom(CodePiece bot)
        {
            bot = bot.Copy();

            CodePiece compressConn;

            if (ASTObject.CGO.CompressVerticalCombining && (compressConn = DoCompressVertically(this, bot)) != null)
            {
                this.RemoveRow(this.MaxY - 1);
                bot.RemoveRow(bot.MinY);

                this.AppendBottomDirect(compressConn);
            }

            AppendBottomDirect(bot);
        }
Пример #20
0
        public void AppendLeft(CodePiece left)
        {
            left = left.Copy();

            CodePiece compressConn;

            if (ASTObject.CGO.CompressHorizontalCombining && (compressConn = DoCompressHorizontally(left, this)) != null)
            {
                this.RemoveFirstColumn();
                left.RemoveLastColumn();

                this.AppendLeftDirect(compressConn);
            }

            AppendLeftDirect(left);
        }
Пример #21
0
        public void AppendRight(CodePiece right)
        {
            right = right.Copy();

            CodePiece compressConn;

            if (ASTObject.CGO.CompressHorizontalCombining && (compressConn = DoCompressHorizontally(this, right)) != null)
            {
                this.RemoveLastColumn();
                right.RemoveFirstColumn();

                this.AppendRightDirect(compressConn);
            }

            AppendRightDirect(right);
        }
Пример #22
0
        public static CodePiece ParseFromLine(string l, bool interpretSpaceAsWalkway = false, bool interpretAtAsNOP = false)
        {
            CodePiece p = new CodePiece();

            for (int i = 0; i < l.Length; i++)
            {
                char c = l[i];

                BefungeCommand cmd;

                if (c == ' ')
                {
                    if (interpretSpaceAsWalkway)
                    {
                        cmd = BCHelper.Walkway;
                    }
                    else
                    {
                        throw new InternalCodeGenException();                         // Space is undefinied: NOP <> Walkway
                    }
                }
                else if (c == '@')
                {
                    if (interpretAtAsNOP)
                    {
                        cmd = BCHelper.Unused;
                    }
                    else
                    {
                        cmd = BCHelper.FindCommand(c);
                    }
                }
                else
                {
                    cmd = BCHelper.FindCommand(c);
                }

                p[i, 0] = cmd;
            }

            return(p);
        }
Пример #23
0
        public CodePiece Copy()
        {
            CodePiece result = new CodePiece();

            for (int x = 0; x < commandArr.Count; x++)
            {
                for (int y = 0; y < commandArr[x].Count; y++)
                {
                    result[x, y] = commandArr[x][y];
                }
            }

            result.MinX = MinX;
            result.MinY = MinY;

            result.MaxX = MaxX;
            result.MaxY = MaxY;

            return(result);
        }
Пример #24
0
 public TwoDirectionCodePiece(CodePiece norm, CodePiece rev)
 {
     content = Tuple.Create(norm, rev);
 }