Esempio n. 1
0
        public void ToCSharp_SyncReturnValueCapture_OutputScript()
        {
            var block = BlockFactory.GetBlock <AutoBlockInstance>("Substring");

            block.OutputVariable = "myOutput";
            block.IsCapture      = true;
            var input  = block.Settings["input"];
            var index  = block.Settings["index"];
            var length = block.Settings["length"];

            input.InputMode         = SettingInputMode.Variable;
            input.InputVariableName = "myInput";

            index.InputMode = SettingInputMode.Fixed;
            (index.FixedSetting as IntSetting).Value = 3;

            length.InputMode = SettingInputMode.Fixed;
            (length.FixedSetting as IntSetting).Value = 5;

            var declaredVariables = new List <string> {
            };

            var expected = "string myOutput = Substring(data, myInput.AsString(), 3, 5);\r\ndata.MarkForCapture(nameof(myOutput));\r\n";

            Assert.Equal(expected, block.ToCSharp(declaredVariables, new ConfigSettings()));
        }
Esempio n. 2
0
        public void ToCSharp_SyncReturnValueAlreadyDeclared_OutputScript()
        {
            var block = BlockFactory.GetBlock <AutoBlockInstance>("Substring");

            block.OutputVariable = "myOutput";
            var input  = block.Settings["input"];
            var index  = block.Settings["index"];
            var length = block.Settings["length"];

            input.InputMode           = SettingInputMode.Interpolated;
            input.InterpolatedSetting = new InterpolatedStringSetting {
                Value = "my <interp> string"
            };

            index.InputMode = SettingInputMode.Fixed;
            (index.FixedSetting as IntSetting).Value = 3;

            length.InputMode         = SettingInputMode.Variable;
            length.InputVariableName = "myLength";

            var declaredVariables = new List <string> {
                "myOutput"
            };

            var expected = "myOutput = Substring(data, $\"my {interp} string\", 3, myLength.AsInt());\r\n";

            Assert.Equal(expected, block.ToCSharp(declaredVariables, new ConfigSettings()));
        }
Esempio n. 3
0
        public void ParseSetting_Variable_Parse()
        {
            var block        = BlockFactory.GetBlock <AutoBlockInstance>("ConstantString");
            var valueSetting = block.Settings["value"];

            var input = "value = @myVariable";

            LoliCodeParser.ParseSetting(ref input, block.Settings, block.Descriptor);
            Assert.Equal(SettingInputMode.Variable, valueSetting.InputMode);
            Assert.Equal("myVariable", valueSetting.InputVariableName);
        }
Esempio n. 4
0
        public void ParseSetting_InterpolatedString_Parse()
        {
            var block        = BlockFactory.GetBlock <AutoBlockInstance>("ConstantString");
            var valueSetting = block.Settings["value"];

            var input = "value = $\"my <interp> string\"";

            LoliCodeParser.ParseSetting(ref input, block.Settings, block.Descriptor);
            Assert.Equal(SettingInputMode.Interpolated, valueSetting.InputMode);
            Assert.IsType <InterpolatedStringSetting>(valueSetting.InterpolatedSetting);
            Assert.Equal("my <interp> string", ((InterpolatedStringSetting)valueSetting.InterpolatedSetting).Value);
        }
Esempio n. 5
0
        public void ParseSetting_FixedStringSetting_Parse()
        {
            var block        = BlockFactory.GetBlock <AutoBlockInstance>("ConstantString");
            var valueSetting = block.Settings["value"];

            var input = "value = \"myValue\"";

            LoliCodeParser.ParseSetting(ref input, block.Settings, block.Descriptor);
            Assert.Equal(SettingInputMode.Fixed, valueSetting.InputMode);
            Assert.IsType <StringSetting>(valueSetting.FixedSetting);
            Assert.Equal("myValue", (valueSetting.FixedSetting as StringSetting).Value);
        }
Esempio n. 6
0
    private void Spawn()
    {
        GameObject temp = null;

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                temp = blockFactory.GetBlock();
                temp.transform.position = new Vector3(x - width / 2 + 0.5f, -y, 0);
            }
        }
    }
Esempio n. 7
0
        public IBlock GetBlock(int x, int y, int z)
        {
            //  try
            //  {
            IChunkColumn chunk;

            if (ChunkManager.TryGetChunk(new ChunkCoordinates(x >> 4, z >> 4), out chunk))
            {
                return(chunk.GetBlock(x & 0xf, y & 0xff, z & 0xf));
            }
            //}
            //	catch { }

            return(BlockFactory.GetBlock(0));
        }
Esempio n. 8
0
        public void ToCSharp_AsyncNoReturnValue_OutputScript()
        {
            var block   = BlockFactory.GetBlock <AutoBlockInstance>("TcpConnect");
            var url     = block.Settings["host"];
            var port    = block.Settings["port"];
            var ssl     = block.Settings["useSSL"];
            var timeout = block.Settings["timeoutMilliseconds"];

            (url.FixedSetting as StringSetting).Value  = "example.com";
            (port.FixedSetting as IntSetting).Value    = 80;
            (ssl.FixedSetting as BoolSetting).Value    = false;
            (timeout.FixedSetting as IntSetting).Value = 1000;

            var declaredVariables = new List <string> {
            };

            var expected = "await TcpConnect(data, \"example.com\", 80, false, 1000).ConfigureAwait(false);\r\n";

            Assert.Equal(expected, block.ToCSharp(declaredVariables, new ConfigSettings()));
        }
Esempio n. 9
0
        public void FromLC_ParseLRBlock_BuildBlock()
        {
            var block      = BlockFactory.GetBlock <AutoBlockInstance>("Substring");
            var script     = "DISABLED\r\nLABEL:My Label\r\n  input = @myInput\r\n  index = 3\r\n  => CAP @myOutput\r\n";
            int lineNumber = 0;

            block.FromLC(ref script, ref lineNumber);

            Assert.True(block.Disabled);
            Assert.Equal("My Label", block.Label);
            Assert.Equal("myOutput", block.OutputVariable);
            Assert.True(block.IsCapture);

            var input  = block.Settings["input"];
            var index  = block.Settings["index"];
            var length = block.Settings["length"];

            Assert.Equal(SettingInputMode.Variable, input.InputMode);
            Assert.Equal("myInput", input.InputVariableName);
            Assert.Equal(SettingInputMode.Fixed, index.InputMode);
            Assert.Equal(3, (index.FixedSetting as IntSetting).Value);
        }
Esempio n. 10
0
        public void ToLC_ParseLRBlock_OutputScript()
        {
            var block = BlockFactory.GetBlock <AutoBlockInstance>("Substring");

            block.OutputVariable = "myOutput";
            block.IsCapture      = false;
            block.Disabled       = true;
            block.Label          = "My Label";
            var input  = block.Settings["input"];
            var index  = block.Settings["index"];
            var length = block.Settings["length"];

            input.InputMode         = SettingInputMode.Variable;
            input.InputVariableName = "myInput";

            index.InputMode = SettingInputMode.Fixed;
            (index.FixedSetting as IntSetting).Value = 3;

            length.InputMode = SettingInputMode.Fixed;

            var expected = "DISABLED\r\nLABEL:My Label\r\n  input = @myInput\r\n  index = 3\r\n  => VAR @myOutput\r\n";

            Assert.Equal(expected, block.ToLC());
        }
Esempio n. 11
0
            public override void AddBlocks()
            {// Tik kaip pvz. Reikia sugalvoti algoritma kuris sugeneruos visus blokus zemelapije.
                for (int i = 0; i < Map.SizeX; i++)
                {
                    this.Map.Blocks[i, 0] = BlockFactory.GetBlock(BlockType.Border);
                    this.Map.Blocks[i, 0].setPosXY(i, 0);

                    this.Map.Blocks[0, i] = BlockFactory.GetBlock(BlockType.Border);
                    this.Map.Blocks[0, i].setPosXY(0, i);

                    this.Map.Blocks[Map.SizeX - 1, i] = BlockFactory.GetBlock(BlockType.Border);
                    this.Map.Blocks[Map.SizeX - 1, i].setPosXY(Map.SizeX - 1, i);

                    this.Map.Blocks[i, Map.SizeY - 1] = BlockFactory.GetBlock(BlockType.Border);
                    this.Map.Blocks[i, Map.SizeY - 1].setPosXY(i, Map.SizeY - 1);
                }
                Random rnd      = new Random();
                int    waterCnt = rnd.Next(1, 3);

                for (int x = 0; x < waterCnt; x++)
                {
                    int x1 = rnd.Next(1, 19);
                    int x2 = rnd.Next(1, 19);
                    int y1 = rnd.Next(1, 19);
                    int y2 = rnd.Next(1, 19);

                    this.Map.Blocks[x1, y1] = BlockFactory.GetBlock(BlockType.Water);
                    this.Map.Blocks[x1, y1].setPosXY(x1, y1);

                    this.Map.Blocks[x2, y2] = BlockFactory.GetBlock(BlockType.Water);
                    this.Map.Blocks[x2, y2].setPosXY(x2, y2);

                    int numOfBlocksTodrawX = 0;
                    int numOfBlocksTodrawY = 0;
                    int tempX = 0;

                    if (x1 < x2)
                    {
                        numOfBlocksTodrawX = x2 - x1;
                        for (int i = 1; i < numOfBlocksTodrawX + 1; i++)
                        {
                            if (this.Map.Blocks[x1 + i, y1] == null)
                            {
                                this.Map.Blocks[x1 + i, y1] = BlockFactory.GetBlock(BlockType.Water);
                                this.Map.Blocks[x1 + i, y1].setPosXY(x1 + i, y1);
                            }
                        }
                        tempX = x2;
                    }
                    else
                    {
                        numOfBlocksTodrawX = x1 - x2;
                        for (int i = 1; i < numOfBlocksTodrawX + 1; i++)
                        {
                            if (this.Map.Blocks[x2 + i, y1] == null)
                            {
                                this.Map.Blocks[x2 + i, y2] = BlockFactory.GetBlock(BlockType.Water);
                                this.Map.Blocks[x2 + i, y2].setPosXY(x2 + i, y2);
                            }
                        }
                        tempX = x1;
                    }

                    if (y1 < y2)
                    {
                        numOfBlocksTodrawY = y2 - y1;
                        for (int i = 1; i < numOfBlocksTodrawY; i++)
                        {
                            if (this.Map.Blocks[tempX, y1 + i] == null)
                            {
                                this.Map.Blocks[tempX, y1 + i] = BlockFactory.GetBlock(BlockType.Water);
                                this.Map.Blocks[tempX, y1 + i].setPosXY(tempX, y1 + i);
                            }
                        }
                    }
                    else
                    {
                        numOfBlocksTodrawY = y1 - y2;
                        for (int i = 1; i < numOfBlocksTodrawY; i++)
                        {
                            if (this.Map.Blocks[tempX, y2 + i] == null)
                            {
                                this.Map.Blocks[tempX, y2 + i] = BlockFactory.GetBlock(BlockType.Water);
                                this.Map.Blocks[tempX, y2 + i].setPosXY(tempX, y2 + i);
                            }
                        }
                    }
                }
                int bricksPatter = rnd.Next(0, 2);

                if (bricksPatter == 0)
                {
                    for (int i = 2; i < Map.SizeX - 2; i++)
                    {
                        this.Map.Blocks[i, i] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[i, i].setPosXY(i, i);

                        this.Map.Blocks[Map.SizeX - i, i] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[Map.SizeX - i, i].setPosXY(Map.SizeX - i, i);
                    }
                }
                if (bricksPatter == 1)
                {
                    for (int i = Map.SizeX / 5; i < (Map.SizeX - Map.SizeX / 5) + 1; i++)
                    {
                        this.Map.Blocks[i, Map.SizeX / 5] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[i, Map.SizeX / 5].setPosXY(i, Map.SizeX / 5);

                        this.Map.Blocks[Map.SizeX / 5, i] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[Map.SizeX / 5, i].setPosXY(Map.SizeX / 5, i);

                        this.Map.Blocks[i, Map.SizeX - Map.SizeX / 5] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[i, Map.SizeX - Map.SizeX / 5].setPosXY(i, Map.SizeX - Map.SizeX / 5);


                        this.Map.Blocks[Map.SizeX - Map.SizeX / 5, i] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[Map.SizeX - Map.SizeX / 5, i].setPosXY(Map.SizeX - Map.SizeX / 5, i);
                    }


                    for (int i = 1; i < Map.SizeX - 1; i++)
                    {
                        this.Map.Blocks[i, Map.SizeX - Map.SizeX / 2] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[i, Map.SizeX - Map.SizeX / 2].setPosXY(i, Map.SizeX - Map.SizeX / 2);


                        this.Map.Blocks[Map.SizeX - Map.SizeX / 2, i] = BlockFactory.GetBlock(BlockType.Brick);
                        this.Map.Blocks[Map.SizeX - Map.SizeX / 2, i].setPosXY(Map.SizeX - Map.SizeX / 2, i);
                    }
                }


                for (int i = 0; i < Map.SizeX; i++)
                {
                    for (int j = 0; j < Map.SizeY; j++)
                    {
                        if (Map.Blocks[i, j] == null)
                        {
                            Map.Blocks[i, j] = BlockFactory.GetBlock(BlockType.Ground);
                            Map.Blocks[i, j].setPosXY(i, j);
                        }
                    }
                }
            }
        /// <summary>
        /// Creates a list of <see cref="BlockInstance"/> objects from a LoliCode <paramref name="script"/>.
        /// </summary>
        public static List <BlockInstance> Transpile(string script)
        {
            if (string.IsNullOrWhiteSpace(script))
            {
                return(new List <BlockInstance>());
            }

            var lines = script.Split(new string[] { "\n", "\r\n" }, System.StringSplitOptions.None);
            var stack = new List <BlockInstance>();

            var    localLineNumber = 0;
            var    lineNumber = 0;
            string line, trimmedLine;

            while (localLineNumber < lines.Length)
            {
                line        = lines[localLineNumber];
                trimmedLine = line.Trim();
                localLineNumber++;
                lineNumber++;

                // If it's a block directive
                if (trimmedLine.StartsWith("BLOCK:"))
                {
                    /*
                     *  BLOCK:Id
                     *  ...
                     *  ENDBLOCK
                     */

                    var match = Regex.Match(trimmedLine, $"^BLOCK:({validTokenRegex})$");

                    if (!match.Success)
                    {
                        throw new LoliCodeParsingException(lineNumber, "Could not parse the block id");
                    }

                    var blockId = match.Groups[1].Value;

                    // Create the block
                    var block = BlockFactory.GetBlock <BlockInstance>(blockId);

                    var sb = new StringBuilder();

                    // As long as we don't find the ENDBLOCK token, add lines to the StringBuilder
                    while (localLineNumber < lines.Length)
                    {
                        line        = lines[localLineNumber];
                        trimmedLine = line.Trim();
                        localLineNumber++;

                        if (trimmedLine.StartsWith("ENDBLOCK"))
                        {
                            break;
                        }

                        sb.AppendLine(line);
                    }

                    var blockOptions = sb.ToString();
                    block.FromLC(ref blockOptions, ref lineNumber); // This can throw a LoliCodeParsingException
                    lineNumber++;                                   // Add one line for the ENDBLOCK statement

                    stack.Add(block);
                }

                // If it's not a block directive, build a LoliCode block
                else
                {
                    var descriptor = new LoliCodeBlockDescriptor();
                    var block      = new LoliCodeBlockInstance(descriptor);

                    var sb = new StringBuilder();

                    sb.Append(line);

                    // As long as we don't find a BLOCK directive, add lines to the StringBuilder
                    while (localLineNumber < lines.Length)
                    {
                        sb.AppendLine(); // Print a newline character
                        line        = lines[localLineNumber];
                        trimmedLine = line.Trim();
                        lineNumber++;
                        localLineNumber++;

                        // If we find a block directive, stop reading lines without consuming it
                        if (trimmedLine.StartsWith("BLOCK:"))
                        {
                            lineNumber--;
                            localLineNumber--;
                            break;
                        }

                        sb.Append(line);
                    }

                    block.Script = sb.ToString();

                    // Make sure the script is not empty
                    if (!string.IsNullOrWhiteSpace(block.Script.Replace("\n", "").Replace("\r\n", "")))
                    {
                        stack.Add(block);
                    }
                }
            }

            return(stack);
        }