├── luablock ├── Test │ ├── obj │ │ └── Debug │ │ │ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs │ │ │ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs │ │ │ ├── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs │ │ │ └── Test.csproj.FileListAbsolute.txt │ ├── bin │ │ └── Debug │ │ │ ├── tt.schematic │ │ │ ├── Tree.schematic │ │ │ ├── test.schematic │ │ │ ├── cmd │ │ │ ├── tp.lua │ │ │ ├── xp.lua │ │ │ ├── clear.lua │ │ │ ├── clone.lua │ │ │ ├── debug.lua │ │ │ ├── fill.lua │ │ │ ├── give.lua │ │ │ ├── kill.lua │ │ │ ├── seed.lua │ │ │ ├── time.lua │ │ │ ├── effect.lua │ │ │ ├── enchant.lua │ │ │ ├── execute.lua │ │ │ ├── gamemode.lua │ │ │ ├── gamerule.lua │ │ │ ├── particle.lua │ │ │ ├── publish.lua │ │ │ ├── setblock.lua │ │ │ ├── summon.lua │ │ │ ├── tellraw.lua │ │ │ ├── testfor.lua │ │ │ ├── trigger.lua │ │ │ ├── weather.lua │ │ │ ├── blockdata.lua │ │ │ ├── difficulty.lua │ │ │ ├── playsound.lua │ │ │ ├── scoreboard.lua │ │ │ ├── spawnpoint.lua │ │ │ ├── achievement.lua │ │ │ ├── setworldspawn.lua │ │ │ ├── spreadplayers.lua │ │ │ ├── testforblock.lua │ │ │ ├── testforblocks.lua │ │ │ ├── worldborder.lua │ │ │ ├── defaultgamemode.lua │ │ │ ├── toggledownfall.lua │ │ │ └── print.lua │ │ │ ├── Test.exe.config │ │ │ ├── Test.vshost.exe.config │ │ │ ├── tes1t.lua │ │ │ ├── test.lua │ │ │ └── Test.vshost.exe.manifest │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Test.csproj ├── luablock │ ├── obj │ │ └── Debug │ │ │ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs │ │ │ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs │ │ │ ├── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs │ │ │ └── luablock.csproj.FileListAbsolute.txt │ ├── CmdJson.cs │ ├── packages.config │ ├── Command.cs │ ├── Commands │ │ ├── Say.cs │ │ └── LuaCmd.cs │ ├── CommandManager.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── NetLua │ │ ├── LuaContext.cs │ │ ├── Libraries │ │ │ ├── MathLibrary.cs │ │ │ └── IoLibrary.cs │ │ ├── Ast │ │ │ └── Ast.cs │ │ ├── LuaGrammar.cs │ │ ├── Lua.cs │ │ ├── LuaEvents.cs │ │ ├── LuaObject.cs │ │ ├── Parser.cs │ │ └── LuaCompiler.cs │ ├── luablock.csproj │ ├── Schematic.cs │ └── Compiler.cs ├── packages │ ├── fNbt.0.6.3 │ │ └── fNbt.0.6.3.nupkg │ ├── Irony.0.9.0 │ │ ├── Irony.0.9.0.nupkg │ │ └── tools │ │ │ └── Irony.GrammarExplorer.exe.config │ └── MoonSharp.0.9.8.0 │ │ └── MoonSharp.0.9.8.0.nupkg └── luablock.sln └── README.md /luablock/Test/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /luablock/Test/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /luablock/Test/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /luablock/luablock/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /luablock/luablock/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /luablock/luablock/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/tt.schematic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/Test/bin/Debug/tt.schematic -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/Tree.schematic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/Test/bin/Debug/Tree.schematic -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/test.schematic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/Test/bin/Debug/test.schematic -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/tp.lua: -------------------------------------------------------------------------------- 1 | ret = "/tp" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/xp.lua: -------------------------------------------------------------------------------- 1 | ret = "/xp" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/packages/fNbt.0.6.3/fNbt.0.6.3.nupkg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/packages/fNbt.0.6.3/fNbt.0.6.3.nupkg -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/clear.lua: -------------------------------------------------------------------------------- 1 | ret = "/clear" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/clone.lua: -------------------------------------------------------------------------------- 1 | ret = "/clone" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/debug.lua: -------------------------------------------------------------------------------- 1 | ret = "/debug" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/fill.lua: -------------------------------------------------------------------------------- 1 | ret = "/fill" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/give.lua: -------------------------------------------------------------------------------- 1 | ret = "/give" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/kill.lua: -------------------------------------------------------------------------------- 1 | ret = "/kill" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/seed.lua: -------------------------------------------------------------------------------- 1 | ret = "/seed" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/time.lua: -------------------------------------------------------------------------------- 1 | ret = "/time" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/packages/Irony.0.9.0/Irony.0.9.0.nupkg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/packages/Irony.0.9.0/Irony.0.9.0.nupkg -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/effect.lua: -------------------------------------------------------------------------------- 1 | ret = "/effect" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/enchant.lua: -------------------------------------------------------------------------------- 1 | ret = "/enchant" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/execute.lua: -------------------------------------------------------------------------------- 1 | ret = "/execute" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/gamemode.lua: -------------------------------------------------------------------------------- 1 | ret = "/gamemode" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/gamerule.lua: -------------------------------------------------------------------------------- 1 | ret = "/gamerule" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/particle.lua: -------------------------------------------------------------------------------- 1 | ret = "/particle" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/publish.lua: -------------------------------------------------------------------------------- 1 | ret = "/publish" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/setblock.lua: -------------------------------------------------------------------------------- 1 | ret = "/setblock" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/summon.lua: -------------------------------------------------------------------------------- 1 | ret = "/summon" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/tellraw.lua: -------------------------------------------------------------------------------- 1 | ret = "/tellraw" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/testfor.lua: -------------------------------------------------------------------------------- 1 | ret = "#/testfor" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/trigger.lua: -------------------------------------------------------------------------------- 1 | ret = "/trigger" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/weather.lua: -------------------------------------------------------------------------------- 1 | ret = "/weather" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/blockdata.lua: -------------------------------------------------------------------------------- 1 | ret = "/blockdata" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/difficulty.lua: -------------------------------------------------------------------------------- 1 | ret = "/difficulty" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/playsound.lua: -------------------------------------------------------------------------------- 1 | ret = "/playsound" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/scoreboard.lua: -------------------------------------------------------------------------------- 1 | ret = "/scoreboard" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/spawnpoint.lua: -------------------------------------------------------------------------------- 1 | ret = "/spawnpoint" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/achievement.lua: -------------------------------------------------------------------------------- 1 | ret = "/achievement" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/setworldspawn.lua: -------------------------------------------------------------------------------- 1 | ret = "/setworldspawn" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/spreadplayers.lua: -------------------------------------------------------------------------------- 1 | ret = "/spreadplayers" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/testforblock.lua: -------------------------------------------------------------------------------- 1 | ret = "#/testforblock" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/testforblocks.lua: -------------------------------------------------------------------------------- 1 | ret = "/testforblocks" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/worldborder.lua: -------------------------------------------------------------------------------- 1 | ret = "/worldborder" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/defaultgamemode.lua: -------------------------------------------------------------------------------- 1 | ret = "/defaultgamemode" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/toggledownfall.lua: -------------------------------------------------------------------------------- 1 | ret = "/toggledownfall" 2 | 3 | for i=0,argsl,1 4 | do 5 | ret = ret .. " " .. args[i] 6 | end 7 | 8 | return ret -------------------------------------------------------------------------------- /luablock/packages/MoonSharp.0.9.8.0/MoonSharp.0.9.8.0.nupkg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Myvar/LuaBlock/HEAD/luablock/packages/MoonSharp.0.9.8.0/MoonSharp.0.9.8.0.nupkg -------------------------------------------------------------------------------- /luablock/Test/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/Test.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/Test.vshost.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /luablock/luablock/CmdJson.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace luablock 8 | { 9 | public class CmdJson 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /luablock/luablock/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/cmd/print.lua: -------------------------------------------------------------------------------- 1 | ret = "/tellraw " .. args[0] .. " [ " 2 | --/tellraw @a [ {text:"Your score is "} , {score:{name:lua,objective:lua_if_1}} , {text:"."} ] 3 | for i=1,argsl - 1,1 4 | do 5 | ret = ret .. "{text:\" " .. args[i] .. "\"}," 6 | end 7 | 8 | return ret .. "{score:{name:lua,objective:" .. args[argsl] .."}}]" -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/tes1t.lua: -------------------------------------------------------------------------------- 1 | 2 | function war() 3 | if item == 0 then 4 | give("@a","minecraft:diamond_sword", 1, 0,"{Unbreakable:1}") 5 | end 6 | end 7 | 8 | function hunger() 9 | if item == 1 then 10 | give("@a","minecraft:beef", 1, 0) 11 | end 12 | end 13 | 14 | function redstone() 15 | if item == 2 then 16 | give("@a","minecraft:redstone_block", 64, 0) 17 | end 18 | end 19 | 20 | war() 21 | hunger() 22 | redstone() -------------------------------------------------------------------------------- /luablock/luablock/Command.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace luablock 8 | { 9 | public class Command 10 | { 11 | public string Name { get; set; } 12 | 13 | public virtual string Parse(string Name, List args) 14 | { 15 | return "/say [ §4Error ] Identifier " + Name + " Does not exzist"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/test.lua: -------------------------------------------------------------------------------- 1 | 2 | function isten() 3 | if vara == 10 then 4 | 5 | print("@a", "§aVara is: ", vara) 6 | 7 | end 8 | end 9 | 10 | function main() 11 | 12 | vara = 10 13 | 14 | print("@a", "The vlaue is: ", vara) 15 | 16 | vara = vara + 50 + 15 17 | 18 | print("@a", "The vlaue is: ", vara) 19 | 20 | vara = vara - 70 21 | 22 | print("@a", "The vlaue is: ", vara) 23 | 24 | vara = vara * 2 25 | 26 | print("@a", "The vlaue is: ", vara) 27 | 28 | isten() 29 | end 30 | 31 | main() 32 | -------------------------------------------------------------------------------- /luablock/Test/bin/Debug/Test.vshost.exe.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /luablock/Test/Program.cs: -------------------------------------------------------------------------------- 1 | using luablock; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Test 10 | { 11 | class Program 12 | { 13 | static void Main(string[] args) 14 | { 15 | // Schematic s1 = new Schematic(); 16 | // s1.Load("tt.schematic"); 17 | 18 | 19 | 20 | 21 | 22 | Compiler c = new Compiler(); 23 | c.Convert(File.ReadAllText(args[0]), args[1]); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /luablock/luablock/Commands/Say.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace luablock.Commands 8 | { 9 | public class Say : Command 10 | { 11 | public Say() 12 | { 13 | Name = "say"; 14 | } 15 | 16 | public override string Parse(string Name, List args) 17 | { 18 | string re = "/say"; 19 | 20 | foreach(var i in args) 21 | { 22 | re += " " + i; 23 | } 24 | 25 | return re; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /luablock/luablock/CommandManager.cs: -------------------------------------------------------------------------------- 1 | using luablock.Commands; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace luablock 9 | { 10 | public static class CommandManager 11 | { 12 | public static List Cmds = new List() 13 | { 14 | new Say() 15 | }; 16 | 17 | public static Command GetCommand(string name) 18 | { 19 | foreach(var i in Cmds) 20 | { 21 | if(i.Name == name) 22 | { 23 | return i; 24 | } 25 | } 26 | 27 | return new Command(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LuaBlock 2 | A compiler that compiles Lua to Command blocks in the form of a schematic, for minecraft 3 | 4 | **Please Feel free to contribute** 5 | 6 | [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Myvar/LuaBlock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) 7 | 8 | #Usage 9 | 1. Download the latest archive, and unzip it. 10 | 2. Open command promt where you unzipped LuaBlock (Tip: Make sure you don't have anything selected, hold shift and right click in the folder. Now click "Open command prompt here") 11 | 3. You can now run the compiler with the following command "LBC [input lua file path] [output schematic file path]" 12 | 4. You can now import the schematic file with your favourite editor(mcedit, worldedit etc) 13 | -------------------------------------------------------------------------------- /luablock/luablock/Commands/LuaCmd.cs: -------------------------------------------------------------------------------- 1 | using MoonSharp.Interpreter; 2 | using NetLua; 3 | using NetLua.Ast; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace luablock.Commands 11 | { 12 | public class LuaCmd : Command 13 | { 14 | public string Lua { get; set; } 15 | 16 | public override string Parse(string Name, List args) 17 | { 18 | Script script = new Script(); 19 | UserData.RegisterType>(); 20 | 21 | script.Globals.SetAsObject("args", args); 22 | script.Globals.SetAsObject("argsl", args.Count - 1); 23 | return script.DoString(Lua).String; 24 | } 25 | 26 | private string TypeToString(IExpression e) 27 | { 28 | //this feels so cheety(whatt ever you do dont show this too kenny! xD) 29 | dynamic z = e; 30 | try 31 | { 32 | try 33 | { 34 | return z.Value; 35 | } 36 | catch 37 | { 38 | return z.Name; 39 | } 40 | } 41 | catch 42 | { 43 | return ""; 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /luablock/luablock.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "luablock", "luablock\luablock.csproj", "{B4FC2990-A026-47D6-930D-2724FF54C308}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{99857049-CF9A-4791-90DD-8C14B0EF4BBE}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {B4FC2990-A026-47D6-930D-2724FF54C308}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /luablock/Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Test")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Test")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("99857049-cf9a-4791-90dd-8c14b0ef4bbe")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /luablock/luablock/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("luablock")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("luablock")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("b4fc2990-a026-47d6-930d-2724ff54c308")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /luablock/packages/Irony.0.9.0/tools/Irony.GrammarExplorer.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 0 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | False 24 | 25 | 26 | False 27 | 28 | 29 | True 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /luablock/luablock/obj/Debug/luablock.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\luablock.dll 2 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\luablock.pdb 3 | C:\Users\Myvar\Desktop\luablock\luablock\obj\Debug\luablock.csprojResolveAssemblyReference.cache 4 | C:\Users\Myvar\Desktop\luablock\luablock\obj\Debug\luablock.dll 5 | C:\Users\Myvar\Desktop\luablock\luablock\obj\Debug\luablock.pdb 6 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\Irony.dll 7 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\Irony.pdb 8 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\Irony.xml 9 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\fNbt.dll 10 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\fNbt.pdb 11 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\MoonSharp.Interpreter.dll 12 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\MoonSharp.Interpreter.pdb 13 | C:\Users\Myvar\Desktop\luablock\luablock\bin\Debug\MoonSharp.Interpreter.xml 14 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\luablock.dll 15 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\luablock.pdb 16 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\fNbt.dll 17 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\Irony.dll 18 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\MoonSharp.Interpreter.dll 19 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\fNbt.pdb 20 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\Irony.pdb 21 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\Irony.xml 22 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\MoonSharp.Interpreter.pdb 23 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\bin\Debug\MoonSharp.Interpreter.xml 24 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\obj\Debug\luablock.csprojResolveAssemblyReference.cache 25 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\obj\Debug\luablock.dll 26 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\luablock\obj\Debug\luablock.pdb 27 | -------------------------------------------------------------------------------- /luablock/Test/obj/Debug/Test.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Test.exe.config 2 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Test.exe 3 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Test.pdb 4 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\luablock.dll 5 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\luablock.pdb 6 | C:\Users\Myvar\Desktop\luablock\Test\obj\Debug\Test.csprojResolveAssemblyReference.cache 7 | C:\Users\Myvar\Desktop\luablock\Test\obj\Debug\Test.exe 8 | C:\Users\Myvar\Desktop\luablock\Test\obj\Debug\Test.pdb 9 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Irony.dll 10 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Irony.pdb 11 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\Irony.xml 12 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\fNbt.dll 13 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\fNbt.pdb 14 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\MoonSharp.Interpreter.dll 15 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\MoonSharp.Interpreter.pdb 16 | C:\Users\Myvar\Desktop\luablock\Test\bin\Debug\MoonSharp.Interpreter.xml 17 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Test.exe.config 18 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\obj\Debug\Test.exe 19 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\obj\Debug\Test.pdb 20 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Test.exe 21 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Test.pdb 22 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\luablock.dll 23 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Irony.dll 24 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\fNbt.dll 25 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\MoonSharp.Interpreter.dll 26 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\luablock.pdb 27 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Irony.pdb 28 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\Irony.xml 29 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\fNbt.pdb 30 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\MoonSharp.Interpreter.pdb 31 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\bin\Debug\MoonSharp.Interpreter.xml 32 | C:\Users\Myvar\Documents\GitHub\LuaBlock\luablock\Test\obj\Debug\Test.csprojResolveAssemblyReference.cache 33 | -------------------------------------------------------------------------------- /luablock/Test/Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {99857049-CF9A-4791-90DD-8C14B0EF4BBE} 8 | Exe 9 | Properties 10 | Test 11 | Test 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | {b4fc2990-a026-47d6-930d-2724ff54c308} 55 | luablock 56 | 57 | 58 | 59 | 66 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/LuaContext.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file 3 | */ 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | 10 | using System.Dynamic; 11 | 12 | namespace NetLua 13 | { 14 | /// 15 | /// Holds a scope and its variables 16 | /// 17 | public class LuaContext : DynamicObject 18 | { 19 | LuaContext parent; 20 | Dictionary variables; 21 | LuaArguments varargs; 22 | 23 | /// 24 | /// Used to create scopes 25 | /// 26 | public LuaContext(LuaContext Parent) 27 | { 28 | parent = Parent; 29 | variables = new Dictionary(); 30 | varargs = new LuaArguments(new LuaObject[] { }); 31 | } 32 | 33 | /// 34 | /// Creates a base context 35 | /// 36 | public LuaContext() : this(null) { } 37 | 38 | /// 39 | /// Sets or creates a variable in the local scope 40 | /// 41 | public void SetLocal(string Name, LuaObject Value) 42 | { 43 | variables[Name] = Value; 44 | } 45 | 46 | /// 47 | /// Sets or creates a variable in the global scope 48 | /// 49 | public void SetGlobal(string Name, LuaObject Value) 50 | { 51 | if (parent == null) 52 | variables[Name] = Value; 53 | else 54 | parent.SetGlobal(Name, Value); 55 | } 56 | 57 | /// 58 | /// Returns the nearest declared variable value or nil 59 | /// 60 | public LuaObject Get(string Name) 61 | { 62 | var obj = LuaObject.Nil; 63 | if (variables.TryGetValue(Name, out obj) || parent == null) 64 | { 65 | if (obj == null) 66 | return LuaObject.Nil; 67 | else 68 | return obj; 69 | } 70 | else 71 | { 72 | return parent.Get(Name); 73 | } 74 | } 75 | 76 | /// 77 | /// Sets the nearest declared variable or creates a new one 78 | /// 79 | public void Set(string Name, LuaObject Value) 80 | { 81 | var obj = LuaObject.Nil; 82 | if (parent == null || variables.TryGetValue(Name, out obj)) 83 | variables[Name] = Value; 84 | else 85 | parent.Set(Name, Value); 86 | } 87 | 88 | internal LuaArguments Varargs 89 | { 90 | get 91 | { 92 | return varargs; 93 | } 94 | 95 | set 96 | { 97 | varargs = value; 98 | } 99 | } 100 | 101 | #region DynamicObject 102 | public override bool TryGetMember(GetMemberBinder binder, out object result) 103 | { 104 | result = Get(binder.Name); 105 | if (result == LuaObject.Nil) 106 | return false; 107 | return true; 108 | } 109 | 110 | public override bool TrySetMember(SetMemberBinder binder, object value) 111 | { 112 | Set(binder.Name, LuaObject.FromObject(value)); 113 | return true; 114 | } 115 | #endregion 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /luablock/luablock/luablock.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {B4FC2990-A026-47D6-930D-2724FF54C308} 8 | Library 9 | Properties 10 | luablock 11 | luablock 12 | v4.5.2 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\fNbt.0.6.3\lib\net35-client\fNbt.dll 35 | True 36 | 37 | 38 | ..\packages\Irony.0.9.0\lib\net45\Irony.dll 39 | True 40 | 41 | 42 | ..\packages\MoonSharp.0.9.8.0\lib\net40-client\MoonSharp.Interpreter.dll 43 | True 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 85 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/Libraries/MathLibrary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace NetLua 7 | { 8 | public static class MathLibrary 9 | { 10 | static LuaArguments math_abs(LuaArguments args) 11 | { 12 | return Lua.Return(Math.Abs(args[0])); 13 | } 14 | 15 | static LuaArguments math_acos(LuaArguments args) 16 | { 17 | return Lua.Return(Math.Acos(args[0])); 18 | } 19 | 20 | static LuaArguments math_asin(LuaArguments args) 21 | { 22 | return Lua.Return(Math.Asin(args[0])); 23 | } 24 | 25 | static LuaArguments math_atan(LuaArguments args) 26 | { 27 | return Lua.Return(Math.Atan(args[0])); 28 | } 29 | 30 | static LuaArguments math_atan2(LuaArguments args) 31 | { 32 | return Lua.Return(Math.Atan2(args[0], args[1])); 33 | } 34 | 35 | static LuaArguments math_ceil(LuaArguments args) 36 | { 37 | return Lua.Return(Math.Ceiling(args[0])); 38 | } 39 | 40 | static LuaArguments math_cos(LuaArguments args) 41 | { 42 | return Lua.Return(Math.Cos(args[0])); 43 | } 44 | 45 | static LuaArguments math_cosh(LuaArguments args) 46 | { 47 | return Lua.Return(Math.Cosh(args[0])); 48 | } 49 | 50 | static LuaArguments math_exp(LuaArguments args) 51 | { 52 | return Lua.Return(Math.Exp(args[0])); 53 | } 54 | 55 | static LuaArguments math_floor(LuaArguments args) 56 | { 57 | return Lua.Return(Math.Floor(args[0])); 58 | } 59 | 60 | static LuaArguments math_log(LuaArguments args) 61 | { 62 | return Lua.Return(Math.Log(args[0], args[1] | Math.E)); 63 | } 64 | 65 | static LuaArguments math_max(LuaArguments args) 66 | { 67 | var max = args[0]; 68 | foreach (LuaObject o in args) 69 | { 70 | max = Math.Max(max, o); 71 | } 72 | return Lua.Return(max); 73 | } 74 | 75 | static LuaArguments math_min(LuaArguments args) 76 | { 77 | var min = args[0]; 78 | foreach (LuaObject o in args) 79 | { 80 | min = Math.Min(min, o); 81 | } 82 | return Lua.Return(min); 83 | } 84 | 85 | static LuaArguments math_pow(LuaArguments args) 86 | { 87 | return Lua.Return(Math.Pow(args[0], args[1])); 88 | } 89 | 90 | static LuaArguments math_sin(LuaArguments args) 91 | { 92 | return Lua.Return(Math.Sin(args[0])); 93 | } 94 | 95 | static LuaArguments math_sinh(LuaArguments args) 96 | { 97 | return Lua.Return(Math.Sinh(args[0])); 98 | } 99 | 100 | static LuaArguments math_sqrt(LuaArguments args) 101 | { 102 | return Lua.Return(Math.Sqrt(args[0])); 103 | } 104 | 105 | static LuaArguments math_tan(LuaArguments args) 106 | { 107 | return Lua.Return(Math.Tan(args[0])); 108 | } 109 | 110 | static LuaArguments math_tanh(LuaArguments args) 111 | { 112 | return Lua.Return(Math.Tanh(args[0])); 113 | } 114 | 115 | public static void AddMathLibrary(LuaContext Context) 116 | { 117 | dynamic math = LuaObject.NewTable(); 118 | math.abs = (LuaFunction)math_abs; 119 | math.acos = (LuaFunction)math_acos; 120 | math.asin = (LuaFunction)math_asin; 121 | math.atan = (LuaFunction)math_atan; 122 | math.atan2 = (LuaFunction)math_atan2; 123 | math.ceil = (LuaFunction)math_ceil; 124 | math.cos = (LuaFunction)math_cos; 125 | math.cosh = (LuaFunction)math_cosh; 126 | math.exp = (LuaFunction)math_exp; 127 | math.floor = (LuaFunction)math_floor; 128 | math.log = (LuaFunction)math_log; 129 | math.max = (LuaFunction)math_max; 130 | math.min = (LuaFunction)math_min; 131 | math.pow = (LuaFunction)math_pow; 132 | math.sin = (LuaFunction)math_sin; 133 | math.sinh = (LuaFunction)math_sinh; 134 | math.sqrt = (LuaFunction)math_sqrt; 135 | math.tan = (LuaFunction)math_tan; 136 | math.tanh = (LuaFunction)math_tanh; 137 | 138 | math.pi = Math.PI; 139 | 140 | Context.Set("math", math); 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /luablock/luablock/Schematic.cs: -------------------------------------------------------------------------------- 1 | using fNbt; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace luablock 9 | { 10 | public class Schematic 11 | { 12 | 13 | 14 | public short Width { get; set; } 15 | public short Height { get; set; } 16 | public short Length { get; set; } 17 | 18 | private byte[] _data { get; set; } 19 | private byte[] _biomes { get; set; } 20 | private byte[] _blocks { get; set; } 21 | 22 | public List Blocks { get; set; } = new List(); 23 | 24 | 25 | public void Fill() 26 | { 27 | var size = Width * Height * Length; 28 | 29 | for (int i = 0; i < size; i++) 30 | { 31 | Blocks.Add(new Block()); 32 | } 33 | 34 | } 35 | 36 | 37 | public void SetBlock(int x, int y, int z, Block b) 38 | { 39 | //(y * Length + z)* Width + x 40 | Blocks[(y * Length + z) * Width + x] = b; 41 | } 42 | 43 | public Block GetBlock(int x, int y, int z) 44 | { 45 | //(y * Length + z)* Width + x 46 | return Blocks[(y * Length + z) * Width + x]; 47 | } 48 | 49 | public void Load(string p) 50 | { 51 | var myFile = new NbtFile(); 52 | myFile.LoadFromFile(p); 53 | var root = myFile.RootTag; 54 | 55 | Width = root.Get("Width").ShortValue; 56 | Height = root.Get("Height").ShortValue; 57 | Length = root.Get("Length").ShortValue; 58 | 59 | _data = root.Get("Data").ByteArrayValue; 60 | _biomes = root.Get("Biomes").ByteArrayValue; 61 | _blocks = root.Get("Blocks").ByteArrayValue; 62 | 63 | Fill(); 64 | 65 | 66 | 67 | for (int y = 0; y < Height; y++) 68 | { 69 | for (int z = 0; z < Length; z++) 70 | { 71 | for (int x = 0; x < Width; x++) 72 | { 73 | SetBlock(x, y, z, new Block() { ID = _blocks[(y * Length + z) * Width + x], Metta = _data[(y * Length + z) * Width + x] & 0x0F }); 74 | } 75 | } 76 | } 77 | 78 | 79 | } 80 | 81 | public void Save(string p) 82 | { 83 | var myFile = new NbtFile(); 84 | 85 | var root = myFile.RootTag; 86 | root.Name = "Schematic"; 87 | 88 | root.Add(new NbtShort("Height", Height)); 89 | root.Add(new NbtShort("Length", Length)); 90 | root.Add(new NbtShort("Width", Width)); 91 | 92 | 93 | var nbttile = new NbtList("TileEntities", NbtTagType.Compound); 94 | var size = Width * Height * Length; 95 | _data = new byte[size]; 96 | _blocks = new byte[size]; 97 | 98 | for (int y = 0; y < Height; y++) 99 | { 100 | for (int z = 0; z < Length; z++) 101 | { 102 | for (int x = 0; x < Width; x++) 103 | { 104 | var d = GetBlock(x, y, z); 105 | _blocks[(y * Length + z) * Width + x] = (byte)d.ID; 106 | _data[(y * Length + z) * Width + x] = (byte)(d.Metta & 0x0F); 107 | 108 | if (d.Command != null) 109 | { 110 | var nbtc = new NbtCompound(); 111 | nbtc.Add(new NbtString("CustomName", "LuaBlock")); 112 | nbtc.Add(new NbtString("Command", d.Command)); 113 | nbtc.Add(new NbtString("id", "Control")); 114 | nbtc.Add(new NbtInt("x", x)); 115 | nbtc.Add(new NbtInt("y", y)); 116 | nbtc.Add(new NbtInt("z", z)); 117 | 118 | nbttile.Add(nbtc); 119 | } 120 | } 121 | } 122 | } 123 | root.Add(new NbtList("Entities", NbtTagType.Byte)); 124 | root.Add(nbttile); 125 | root.Add(new NbtList("TileTicks", NbtTagType.Byte)); 126 | 127 | root.Add(new NbtString("Materials", "Alpha")); 128 | 129 | 130 | 131 | root.Add(new NbtByteArray("Data", _data)); 132 | // root.Add(new NbtByteArray("Biomes", new byte[Width * Length])); 133 | root.Add(new NbtByteArray("Blocks", _blocks)); 134 | 135 | 136 | myFile.SaveToFile(p, NbtCompression.None); 137 | } 138 | } 139 | 140 | public class Block 141 | { 142 | public int ID { get; set; } = 0; 143 | public int Metta { get; set; } = 0; 144 | public string Command { get; set; } = null; 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/Ast/Ast.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2013 Francesco Bertolaccini 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | * this software and associated documentation files (the "Software"), to deal in 8 | * the Software without restriction, including without limitation the rights to 9 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | * the Software, and to permit persons to whom the Software is furnished to do so, 11 | * subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Linq; 27 | using System.Text; 28 | 29 | namespace NetLua.Ast 30 | { 31 | public enum BinaryOp 32 | { 33 | Addition, 34 | Subtraction, 35 | Multiplication, 36 | Division, 37 | Power, 38 | Modulo, 39 | Concat, 40 | GreaterThan, 41 | GreaterOrEqual, 42 | LessThan, 43 | LessOrEqual, 44 | Equal, 45 | Different, 46 | And, 47 | Or 48 | } 49 | 50 | public enum UnaryOp 51 | { 52 | Negate, 53 | Invert, 54 | Length 55 | } 56 | 57 | public interface IStatement { } 58 | 59 | public interface IExpression { } 60 | 61 | public interface IAssignable : IExpression 62 | { } 63 | 64 | public class Variable : IExpression, IAssignable 65 | { 66 | // Prefix.Name 67 | public IExpression Prefix; 68 | public string Name; 69 | } 70 | 71 | public class Argument 72 | { 73 | public string Name; 74 | } 75 | 76 | public class StringLiteral : IExpression 77 | { 78 | public string Value; 79 | } 80 | 81 | public class NumberLiteral : IExpression 82 | { 83 | public double Value; 84 | } 85 | 86 | public class NilLiteral : IExpression 87 | { } 88 | 89 | public class BoolLiteral : IExpression 90 | { 91 | public bool Value; 92 | } 93 | 94 | public class VarargsLiteral : IExpression 95 | { } 96 | 97 | public class FunctionCall : IStatement, IExpression 98 | { 99 | public IExpression Function; 100 | public List Arguments = new List(); 101 | } 102 | 103 | public class TableAccess : IExpression, IAssignable 104 | { 105 | // Expression[Index] 106 | public IExpression Expression; 107 | public IExpression Index; 108 | } 109 | 110 | public class FunctionDefinition : IExpression 111 | { 112 | // function(Arguments) Body end 113 | public List Arguments = new List(); 114 | public Block Body; 115 | } 116 | 117 | public class BinaryExpression : IExpression 118 | { 119 | public IExpression Left, Right; 120 | public BinaryOp Operation; 121 | } 122 | 123 | public class UnaryExpression : IExpression 124 | { 125 | public IExpression Expression; 126 | public UnaryOp Operation; 127 | } 128 | 129 | public class TableConstructor : IExpression 130 | { 131 | public Dictionary Values = new Dictionary(); 132 | } 133 | 134 | public class Assignment : IStatement 135 | { 136 | // Var1, Var2, Var3 = Exp1, Exp2, Exp3 137 | //public Variable[] Variables; 138 | //public IExpression[] Expressions; 139 | 140 | public List Variables = new List(); 141 | public List Expressions = new List(); 142 | } 143 | 144 | public class ReturnStat : IStatement 145 | { 146 | public List Expressions = new List(); 147 | } 148 | 149 | public class BreakStat : IStatement { } 150 | 151 | public class LocalAssignment : IStatement 152 | { 153 | public List Names = new List(); 154 | public List Values = new List(); 155 | } 156 | 157 | public class Block : IStatement 158 | { 159 | public List Statements = new List(); 160 | } 161 | 162 | public class WhileStat : IStatement 163 | { 164 | public IExpression Condition; 165 | public Block Block; 166 | } 167 | 168 | public class RepeatStat : IStatement 169 | { 170 | public Block Block; 171 | public IExpression Condition; 172 | } 173 | 174 | public class NumericFor : IStatement 175 | { 176 | public IExpression Var, Limit, Step; 177 | public string Variable; 178 | public Block Block; 179 | } 180 | 181 | public class GenericFor : IStatement 182 | { 183 | public List Variables = new List(); 184 | public List Expressions = new List(); 185 | public Block Block; 186 | } 187 | 188 | public class IfStat : IStatement 189 | { 190 | public IExpression Condition; 191 | public Block Block; 192 | public List ElseIfs = new List(); 193 | public Block ElseBlock; 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/LuaGrammar.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file 3 | */ 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | 10 | using Irony.Parsing; 11 | using Irony.Ast; 12 | 13 | namespace NetLua 14 | { 15 | class LuaGrammar : Grammar 16 | { 17 | public LuaGrammar() 18 | : base(true) 19 | { 20 | #region Terminals 21 | Terminal Identifier = new IdentifierTerminal("identifier"); 22 | Terminal SingleString = new StringLiteral("string", "'", StringOptions.AllowsAllEscapes); 23 | Terminal DoubleString = new StringLiteral("string", "\"", StringOptions.AllowsAllEscapes); 24 | Terminal Number = new NumberLiteral("number", NumberOptions.AllowSign); 25 | 26 | Terminal LineComment = new CommentTerminal("Comment", "--", "\n", "\r"); 27 | Terminal LongComment = new CommentTerminal("LongComment", "--[[", "]]"); 28 | 29 | base.NonGrammarTerminals.Add(LineComment); 30 | base.NonGrammarTerminals.Add(LongComment); 31 | #endregion 32 | 33 | #region Nonterminals 34 | NonTerminal Chunk = new NonTerminal("Chunk"); 35 | 36 | NonTerminal Prefix = new NonTerminal("Prefix"); 37 | NonTerminal Variable = new NonTerminal("Variable"); 38 | NonTerminal FunctionCall = new NonTerminal("FunctionCall"); 39 | NonTerminal CallArguments = new NonTerminal("FunctionCallArguments"); 40 | NonTerminal OopCall = new NonTerminal("OopCall"); 41 | NonTerminal CallArgumentsFragment = new NonTerminal(""); 42 | NonTerminal Expression = new NonTerminal("Expression"); 43 | NonTerminal FunctionDef = new NonTerminal("FunctionDef"); 44 | NonTerminal DefArguments = new NonTerminal("FunctionDefArguments"); 45 | NonTerminal DefArgumentsFragment = new NonTerminal(""); 46 | 47 | NonTerminal Statement = new NonTerminal("Statement"); 48 | NonTerminal ReturnStatement = new NonTerminal("ReturnStat"); 49 | NonTerminal BreakStatement = new NonTerminal("BreakStat"); 50 | NonTerminal Assignment = new NonTerminal("Assignment"); 51 | NonTerminal LocalAssignment = new NonTerminal("LocalAssignment"); 52 | NonTerminal FunctionDecl = new NonTerminal("FunctionDecl"); 53 | NonTerminal DoBlock = new NonTerminal("DoBlock"); 54 | NonTerminal If = new NonTerminal("If"); 55 | NonTerminal Elseif = new NonTerminal("Elseif"); 56 | NonTerminal Else = new NonTerminal("Else"); 57 | NonTerminal While = new NonTerminal("While"); 58 | NonTerminal Repeat = new NonTerminal("Repeat"); 59 | NonTerminal For = new NonTerminal("For"); 60 | 61 | NonTerminal PowerOp = new NonTerminal("PowerOp"); 62 | NonTerminal MulOp = new NonTerminal("MulOp"); 63 | NonTerminal AddOp = new NonTerminal("AddOp"); 64 | NonTerminal ConcatOp = new NonTerminal("ConcatOp"); 65 | NonTerminal RelOp = new NonTerminal("RelOp"); 66 | NonTerminal AndOp = new NonTerminal("AndOp"); 67 | NonTerminal OrOp = new NonTerminal("OrOp"); 68 | NonTerminal UnaryExpr = new NonTerminal("UnaryExpr"); 69 | 70 | NonTerminal TableConstruct = new NonTerminal("TableConstruct"); 71 | NonTerminal TableConstructFragment = new NonTerminal("TableConstructFragment"); 72 | #endregion 73 | 74 | #region Fragments 75 | CallArgumentsFragment.Rule = Expression | Expression + "," + CallArgumentsFragment; 76 | 77 | CallArguments.Rule = "(" + (CallArgumentsFragment | Empty) + ")"; 78 | 79 | DefArgumentsFragment.Rule = Identifier | Identifier + "," + DefArgumentsFragment; 80 | 81 | DefArguments.Rule = "(" + (DefArgumentsFragment | Empty) + ")"; 82 | 83 | Chunk.Rule = MakeStarRule(Chunk, Statement); 84 | #endregion 85 | 86 | #region Expressions 87 | PowerOp.Rule = Expression + ("^" + Expression | Empty); 88 | MulOp.Rule = PowerOp + ((ToTerm("*") | "/" | "%") + PowerOp | Empty); 89 | AddOp.Rule = MulOp + ((ToTerm("+") | "-") + MulOp | Empty); 90 | ConcatOp.Rule = AddOp + (".." + AddOp | Empty); 91 | RelOp.Rule = ConcatOp + ((ToTerm(">") | ">=" | "<" | "<=" | "==" | "~=") + ConcatOp | Empty); 92 | AndOp.Rule = RelOp + ("and" + RelOp | Empty); 93 | OrOp.Rule = AndOp + ("or" + AndOp | Empty); 94 | 95 | UnaryExpr.Rule = (ToTerm("not") | "-" | "#") + Expression; 96 | 97 | Prefix.Rule = 98 | OopCall 99 | | FunctionCall 100 | | Variable 101 | | "(" + Expression + ")"; 102 | 103 | Variable.Rule = 104 | Prefix + "." + Identifier 105 | | Prefix + "[" + Expression + "]" 106 | | Identifier; 107 | 108 | FunctionCall.Rule = Prefix + CallArguments; 109 | OopCall.Rule = Prefix + ":" + Identifier + CallArguments; 110 | 111 | FunctionDef.Rule = 112 | ToTerm("function") + DefArguments 113 | + Chunk + ToTerm("end"); 114 | 115 | var tableSep = new NonTerminal(""); 116 | tableSep.Rule = ToTerm(";") | ","; 117 | TableConstructFragment.Rule = 118 | ( 119 | ( 120 | ( 121 | (Identifier | "[" + Expression + "]") + "=" 122 | ) 123 | + Expression 124 | | Expression 125 | ) 126 | + (";" + TableConstructFragment | "," + TableConstructFragment | Empty) 127 | ) | Empty; 128 | TableConstruct.Rule = "{" + TableConstructFragment + "}"; 129 | 130 | var varargs = new NonTerminal("Varargs"); 131 | varargs.Rule = "..."; 132 | 133 | Expression.Rule = 134 | varargs 135 | | Prefix 136 | | OrOp 137 | | UnaryExpr 138 | | ToTerm("true") 139 | | "false" 140 | | "nil" 141 | | SingleString 142 | | DoubleString 143 | | Number 144 | | FunctionDef 145 | | TableConstruct; 146 | #endregion 147 | 148 | #region Statements 149 | FunctionDecl.Rule = "function" + Variable + (":" + Identifier | Empty) + DefArguments + Chunk + "end"; 150 | 151 | 152 | var RetChunk = new NonTerminal("RetChunk"); 153 | RetChunk.Rule = Expression + (("," + RetChunk) | Empty); 154 | 155 | ReturnStatement.Rule = 156 | "return" + (RetChunk | Empty); 157 | 158 | var AssignExpChunk = new NonTerminal("AssignExpChunk"); 159 | AssignExpChunk.Rule = Expression + (("," + AssignExpChunk) | Empty); 160 | var AssignVarChunk = new NonTerminal("AssignVarChunk"); 161 | AssignVarChunk.Rule = Variable + (("," + AssignVarChunk) | Empty); 162 | 163 | Assignment.Rule = 164 | AssignVarChunk + "=" + AssignExpChunk; 165 | 166 | var LocalAssignNameChunk = new NonTerminal("AssignNameChunk"); 167 | var LocalFunction = new NonTerminal("LocalFunction"); 168 | LocalAssignNameChunk.Rule = Identifier + (("," + LocalAssignNameChunk) | Empty); 169 | LocalFunction.Rule = "function" + Identifier + DefArguments + Chunk + "end"; 170 | LocalAssignment.Rule = "local" + (LocalAssignNameChunk + ("=" + AssignExpChunk | Empty) | LocalFunction); 171 | 172 | Elseif.Rule = "elseif" + Expression + "then" + Chunk + (Elseif | Empty); 173 | Else.Rule = "else" + Chunk; 174 | 175 | If.Rule = "if" + Expression + "then" + Chunk + (Elseif | Empty) + (Else | Empty) + "end"; 176 | 177 | While.Rule = "while" + Expression + DoBlock; 178 | 179 | DoBlock.Rule = "do" + Chunk + "end"; 180 | 181 | Repeat.Rule = "repeat" + Chunk + "until" + Expression; 182 | 183 | BreakStatement.Rule = "break"; 184 | 185 | var NumericFor = new NonTerminal("NumericFor"); 186 | NumericFor.Rule = Identifier + "=" + Expression + "," + Expression + ("," + Expression | Empty); 187 | var GenericFor = new NonTerminal("GenericFor"); 188 | var NameList = new NonTerminal("NameList"); 189 | var ExpList = new NonTerminal("ExpList"); 190 | NameList.Rule = Identifier + (("," + NameList) | Empty); 191 | ExpList.Rule = Expression + (("," + ExpList) | Empty); 192 | GenericFor.Rule = NameList + "in" + ExpList; 193 | 194 | For.Rule = "for" + (GenericFor | NumericFor) + DoBlock; 195 | 196 | Statement.Rule = 197 | ";" 198 | | ReturnStatement 199 | | BreakStatement 200 | | Assignment 201 | | LocalAssignment 202 | | FunctionCall 203 | | OopCall 204 | | FunctionDecl 205 | | For 206 | | If 207 | | While 208 | | DoBlock 209 | | Repeat; 210 | #endregion 211 | 212 | this.Root = Chunk; 213 | this.MarkReservedWords( 214 | "true", "false", 215 | "nil", "local", 216 | "function", "while", 217 | "if", "for", "repeat", "until", 218 | "end", "do", "return", "break"); 219 | 220 | this.MarkPunctuation(".", ",", ";", "(", ")", "[", "]", "{", "}", "=", ":"); 221 | this.MarkTransient(Statement); 222 | } 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/Lua.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file 3 | */ 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq.Expressions; 8 | using System.Text; 9 | using System.Dynamic; 10 | 11 | using NetLua.Ast; 12 | 13 | namespace NetLua 14 | { 15 | public class Lua : DynamicObject 16 | { 17 | LuaContext ctx = new LuaContext(); 18 | Parser p = new Parser(); 19 | 20 | /// 21 | /// Helper function for returning Nil from a function 22 | /// 23 | /// Nil 24 | public static LuaArguments Return() 25 | { 26 | return new LuaObject[] { LuaObject.Nil }; 27 | } 28 | 29 | /// 30 | /// Helper function for returning objects from a function 31 | /// 32 | /// The objects to return 33 | public static LuaArguments Return(params LuaObject[] values) 34 | { 35 | return values; 36 | } 37 | 38 | /// 39 | /// Creates a new Lua context with the base functions already set 40 | /// 41 | public Lua() 42 | { 43 | ctx.Set("assert", (LuaFunction)assert); 44 | ctx.Set("dofile", (LuaFunction)dofile); 45 | ctx.Set("error", (LuaFunction)error); 46 | ctx.Set("getmetatable", (LuaFunction)getmetatable); 47 | ctx.Set("setmetatable", (LuaFunction)setmetatable); 48 | ctx.Set("rawequal", (LuaFunction)rawequal); 49 | ctx.Set("rawget", (LuaFunction)rawget); 50 | ctx.Set("rawset", (LuaFunction)rawset); 51 | ctx.Set("rawlen", (LuaFunction)rawlen); 52 | ctx.Set("tonumber", (LuaFunction)tonumber); 53 | ctx.Set("tostring", (LuaFunction)tostring); 54 | ctx.Set("type", (LuaFunction)type); 55 | ctx.Set("ipairs", (LuaFunction)ipairs); 56 | ctx.Set("next", (LuaFunction)next); 57 | ctx.Set("pairs", (LuaFunction)pairs); 58 | } 59 | 60 | /// 61 | /// Parses and executes the specified file 62 | /// 63 | /// The file to execute 64 | public LuaArguments DoFile(string Filename) 65 | { 66 | FunctionDefinition def = new FunctionDefinition(); 67 | def.Arguments = new List(); 68 | def.Body = p.ParseFile(Filename); 69 | var function = LuaCompiler.CompileFunction(def, Expression.Constant(ctx)).Compile(); 70 | return function().Call(Lua.Return()); 71 | } 72 | 73 | /// 74 | /// Parses and executes the specified string 75 | /// 76 | public LuaArguments DoString(string Chunk) 77 | { 78 | FunctionDefinition def = new FunctionDefinition(); 79 | def.Arguments = new List(); 80 | def.Body = p.ParseString(Chunk); 81 | var function = LuaCompiler.CompileFunction(def, Expression.Constant(ctx)).Compile(); 82 | return function().Call(Lua.Return()); 83 | } 84 | 85 | /// 86 | /// The base context 87 | /// 88 | public LuaContext Context 89 | { 90 | get 91 | { 92 | return ctx; 93 | } 94 | } 95 | 96 | /// 97 | /// The base context 98 | /// 99 | public dynamic DynamicContext 100 | { 101 | get 102 | { 103 | return ctx; 104 | } 105 | } 106 | 107 | #region Dynamic methods 108 | 109 | public override bool TryGetMember(GetMemberBinder binder, out object result) 110 | { 111 | result = null; 112 | LuaObject obj = ctx.Get(binder.Name); 113 | if (obj.IsNil) 114 | return false; 115 | else 116 | { 117 | result = LuaObject.getObject(obj); 118 | return true; 119 | } 120 | } 121 | 122 | public override bool TrySetMember(SetMemberBinder binder, object value) 123 | { 124 | if (value is LuaObject) 125 | ctx.Set(binder.Name, value as LuaObject); 126 | else 127 | ctx.Set(binder.Name, LuaObject.FromObject(value)); 128 | return true; 129 | } 130 | 131 | #endregion 132 | 133 | #region Basic functions 134 | 135 | LuaArguments assert(LuaArguments args) 136 | { 137 | if (args.Length > 0) 138 | { 139 | if (args[0].AsBool() == false) 140 | { 141 | if (args.Length == 1) 142 | throw new LuaException("Assertion failed"); 143 | else 144 | throw new LuaException(args[1].ToString()); 145 | } 146 | } 147 | return Return(); 148 | } 149 | 150 | LuaArguments dofile(LuaArguments args) 151 | { 152 | return DoFile(args[0].ToString()); 153 | } 154 | 155 | LuaArguments error(LuaArguments args) 156 | { 157 | throw new LuaException(args[0].ToString()); 158 | } 159 | 160 | LuaArguments getmetatable(LuaArguments args) 161 | { 162 | return Return(args[0].Metatable); 163 | } 164 | 165 | LuaArguments setmetatable(LuaArguments args) 166 | { 167 | args[0].Metatable = args[1]; 168 | return Return(); 169 | } 170 | 171 | LuaArguments rawequal(LuaArguments args) 172 | { 173 | return Return(args[0] == args[1]); 174 | } 175 | 176 | LuaArguments rawget(LuaArguments args) 177 | { 178 | return Return(LuaEvents.rawget(args[0], args[1])); 179 | } 180 | 181 | LuaArguments rawset(LuaArguments args) 182 | { 183 | LuaEvents.rawset(args[0], args[1], args[2]); 184 | return Return(args[0]); 185 | } 186 | 187 | LuaArguments rawlen(LuaArguments args) 188 | { 189 | LuaObject obj = args[0]; 190 | if (obj.IsString) 191 | return Return(obj.AsString().Length); 192 | else if (obj.IsTable) 193 | return Return(obj.AsTable().Count); 194 | else 195 | throw new LuaException("invalid argument"); 196 | } 197 | 198 | LuaArguments tonumber(LuaArguments args) 199 | { 200 | LuaObject obj = args[0]; 201 | if (args.Length == 1) 202 | { 203 | double d = 0; 204 | if (obj.IsString) 205 | { 206 | if (double.TryParse(obj.AsString(), out d)) 207 | return Return(d); 208 | else 209 | return Return(); 210 | } 211 | else if (obj.IsNumber) 212 | { 213 | return Return(obj.AsNumber()); 214 | } 215 | else 216 | { 217 | return Return(); 218 | } 219 | } 220 | else 221 | { 222 | //TODO: Implement tonumber for bases different from 10 223 | throw new NotImplementedException(); 224 | } 225 | } 226 | 227 | LuaArguments tostring(LuaArguments args) 228 | { 229 | return Return(LuaEvents.tostring_event(args[0])); 230 | } 231 | 232 | LuaArguments type(LuaArguments args) 233 | { 234 | switch (args[0].Type) 235 | { 236 | case LuaType.boolean: 237 | return Return("boolean"); 238 | case LuaType.function: 239 | return Return("function"); 240 | case LuaType.nil: 241 | return Return("nil"); 242 | case LuaType.number: 243 | return Return("number"); 244 | case LuaType.@string: 245 | return Return("string"); 246 | case LuaType.table: 247 | return Return("table"); 248 | case LuaType.thread: 249 | return Return("thread"); 250 | case LuaType.userdata: 251 | return Return("userdata"); 252 | } 253 | return Return(); 254 | } 255 | 256 | LuaArguments ipairs(LuaArguments args) 257 | { 258 | LuaObject handler = LuaEvents.getMetamethod(args[0], "__ipairs"); 259 | if (!handler.IsNil) 260 | { 261 | return handler.Call(args); 262 | } 263 | else 264 | { 265 | if (args[0].IsTable) 266 | { 267 | LuaFunction f = delegate(LuaArguments x) 268 | { 269 | var s = x[0]; 270 | var var = x[1].AsNumber() + 1; 271 | 272 | var val = s[var]; 273 | if (val == LuaObject.Nil) 274 | return Return(LuaObject.Nil); 275 | else 276 | return Return(var, val); 277 | }; 278 | return Return(f, args[0], 0); 279 | } 280 | else 281 | { 282 | throw new LuaException("t must be a table"); 283 | } 284 | } 285 | } 286 | 287 | LuaArguments next(LuaArguments args) 288 | { 289 | var table = args[0]; 290 | var index = args[1]; 291 | if (!table.IsTable) 292 | { 293 | throw new LuaException("t must be a table"); 294 | } 295 | List keys = new List(table.AsTable().Keys); 296 | if (index.IsNil) 297 | { 298 | if (keys.Count == 0) 299 | return Return(); 300 | return Return(keys[0], table[keys[0]]); 301 | } 302 | else 303 | { 304 | int pos = keys.IndexOf(index); 305 | if (pos == keys.Count - 1) 306 | { 307 | return Return(); 308 | } 309 | else 310 | { 311 | return Return(keys[pos + 1], table[keys[pos + 1]]); 312 | } 313 | } 314 | } 315 | 316 | LuaArguments pairs(LuaArguments args) 317 | { 318 | LuaObject handler = LuaEvents.getMetamethod(args[0], "__pairs"); 319 | if (!handler.IsNil) 320 | { 321 | return handler.Call(args); 322 | } 323 | else 324 | { 325 | return Return((LuaFunction)next, args[0], LuaObject.Nil); 326 | } 327 | } 328 | 329 | #endregion 330 | } 331 | } 332 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/Libraries/IoLibrary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.IO; 6 | 7 | namespace NetLua 8 | { 9 | public static class IoLibrary 10 | { 11 | // TODO: implment lines, read 12 | class FileObject 13 | { 14 | public FileObject(Stream s) 15 | { 16 | stream = s; 17 | if (s.CanRead) 18 | reader = new StreamReader(s); 19 | if (s.CanWrite) 20 | writer = new StreamWriter(s); 21 | } 22 | 23 | public Stream stream; 24 | public StreamReader reader; 25 | public StreamWriter writer; 26 | } 27 | 28 | static dynamic FileMetatable = LuaObject.NewTable(); 29 | 30 | static LuaObject currentInput = LuaObject.Nil, currentOutput = LuaObject.Nil; 31 | 32 | static bool isStream(LuaObject obj) 33 | { 34 | return (obj.IsUserData && obj.luaobj is FileObject); 35 | } 36 | 37 | static LuaObject CreateFileObject(Stream stream) 38 | { 39 | LuaObject obj = LuaObject.FromObject(new FileObject(stream)); 40 | obj.Metatable = FileMetatable; 41 | 42 | return obj; 43 | } 44 | 45 | static LuaObject CreateFileObject(Stream stream, bool autoflush) 46 | { 47 | FileObject fobj = new FileObject(stream); 48 | fobj.writer.AutoFlush = autoflush; 49 | LuaObject obj = LuaObject.FromObject(fobj); 50 | obj.Metatable = FileMetatable; 51 | 52 | return obj; 53 | } 54 | 55 | public static void AddIoLibrary(LuaContext Context) 56 | { 57 | dynamic io = LuaObject.NewTable(); 58 | 59 | FileMetatable.__index = LuaObject.NewTable(); 60 | FileMetatable.__index.write = (LuaFunction)write; 61 | FileMetatable.__index.close = (LuaFunction)close; 62 | FileMetatable.__index.flush = (LuaFunction)flush; 63 | FileMetatable.__index.seek = (LuaFunction)seek; 64 | FileMetatable.__index.read = (LuaFunction)read; 65 | 66 | io.open = (LuaFunction)io_open; 67 | io.type = (LuaFunction)io_type; 68 | io.input = (LuaFunction)io_input; 69 | io.output = (LuaFunction)io_output; 70 | io.temp = (LuaFunction)io_temp; 71 | io.flush = (LuaFunction)io_flush; 72 | io.write = (LuaFunction)io_write; 73 | io.read = (LuaFunction)io_read; 74 | 75 | currentInput = CreateFileObject(Console.OpenStandardInput()); 76 | currentOutput = CreateFileObject(Console.OpenStandardOutput(), true); 77 | io.stdin = currentInput; 78 | io.stdout = currentOutput; 79 | io.stderr = CreateFileObject(Console.OpenStandardError(), true); 80 | 81 | Context.Set("io", io); 82 | } 83 | 84 | static LuaArguments io_open(LuaArguments args) 85 | { 86 | var file = args[0]; 87 | var mode = args[1]; 88 | 89 | if (file.IsString) 90 | { 91 | FileMode fmode = FileMode.Open; 92 | FileAccess faccess = FileAccess.Read; 93 | if (mode.IsString) 94 | { 95 | switch (mode.ToString()) 96 | { 97 | case "r": 98 | faccess = FileAccess.Read; break; 99 | case "w": 100 | fmode = FileMode.Create; faccess = FileAccess.ReadWrite; break; 101 | case "a": 102 | fmode = FileMode.Append; faccess = FileAccess.Write; break; 103 | case "r+": 104 | case "w+": 105 | case "a+": 106 | // TODO: Implement rwa+ 107 | throw new NotImplementedException(); 108 | } 109 | } 110 | FileStream stream = new FileStream(file.ToString(), fmode, faccess); 111 | 112 | return Lua.Return(CreateFileObject(stream)); 113 | } 114 | else 115 | return Lua.Return(); 116 | } 117 | 118 | static LuaArguments io_type(LuaArguments args) 119 | { 120 | var obj = args[0]; 121 | if (isStream(obj)) 122 | { 123 | FileObject fobj = obj.luaobj as FileObject; 124 | if (!fobj.stream.CanWrite && !fobj.stream.CanRead) 125 | return Lua.Return("closed file"); 126 | else 127 | return Lua.Return("file"); 128 | } 129 | return Lua.Return(); 130 | } 131 | 132 | static LuaArguments io_input(LuaArguments args) 133 | { 134 | var obj = args[0]; 135 | if (isStream(obj)) 136 | { 137 | currentInput = obj; 138 | return Lua.Return(currentInput); 139 | } 140 | else if (obj.IsString) 141 | { 142 | currentInput = io_open(args)[0]; 143 | return Lua.Return(currentInput); 144 | } 145 | else if (args.Length == 0) 146 | return Lua.Return(currentInput); 147 | else 148 | throw new LuaException("Invalid argument"); 149 | } 150 | 151 | static LuaArguments io_output(LuaArguments args) 152 | { 153 | var obj = args[0]; 154 | if (isStream(obj)) 155 | { 156 | currentOutput = obj; 157 | return Lua.Return(currentOutput); 158 | } 159 | else if (obj.IsString) 160 | { 161 | FileStream stream = new FileStream(obj.ToString(), FileMode.OpenOrCreate); 162 | currentOutput = CreateFileObject(stream); 163 | return Lua.Return(currentOutput); 164 | } 165 | else if (args.Length == 0) 166 | return Lua.Return(currentOutput); 167 | else 168 | throw new LuaException("Invalid argument"); 169 | } 170 | 171 | static LuaArguments io_temp(LuaArguments args) 172 | { 173 | string path = Path.GetTempFileName(); 174 | Stream s = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Write, Int16.MaxValue, FileOptions.DeleteOnClose); 175 | 176 | return Lua.Return(CreateFileObject(s)); 177 | } 178 | 179 | static LuaArguments io_write(LuaArguments args) 180 | { 181 | var obj = args[0]; 182 | if (!obj.IsNil) 183 | return currentOutput["write"].MethodCall(currentOutput, args); 184 | else 185 | return Lua.Return(); 186 | } 187 | 188 | static LuaArguments io_flush(LuaArguments args) 189 | { 190 | var obj = args[0]; 191 | if (obj.IsNil) 192 | return currentOutput["flush"].MethodCall(currentOutput, args); 193 | else 194 | return obj["flush"].MethodCall(obj, args); 195 | } 196 | 197 | static LuaArguments io_close(LuaArguments args) 198 | { 199 | var obj = args[0]; 200 | if (obj.IsNil) 201 | return currentOutput["close"].MethodCall(currentOutput, args); 202 | else 203 | return obj["close"].MethodCall(obj, args); 204 | } 205 | 206 | static LuaArguments io_read(LuaArguments args) 207 | { 208 | return currentInput["read"].MethodCall(currentInput, args); 209 | } 210 | 211 | static LuaArguments write(LuaArguments args) 212 | { 213 | var self = args[0]; 214 | if (isStream(self)) 215 | { 216 | FileObject fobj = self.luaobj as FileObject; 217 | foreach (var arg in args) 218 | { 219 | if (arg == self) 220 | continue; 221 | 222 | if (!(arg.IsString || arg.IsNumber)) 223 | Lua.Return(); 224 | 225 | if (fobj.stream.CanWrite) 226 | fobj.writer.Write(arg.ToString()); 227 | else 228 | Lua.Return(); 229 | } 230 | return Lua.Return(self); 231 | } 232 | else 233 | return Lua.Return(); 234 | } 235 | 236 | static LuaArguments close(LuaArguments args) 237 | { 238 | var obj = args[0]; 239 | if (isStream(obj)) 240 | { 241 | FileObject fobj = obj.luaobj as FileObject; 242 | fobj.stream.Close(); 243 | } 244 | return Lua.Return(); 245 | } 246 | 247 | static LuaArguments flush(LuaArguments args) 248 | { 249 | var obj = args[0]; 250 | if (isStream(obj)) 251 | { 252 | FileObject fobj = obj.luaobj as FileObject; 253 | fobj.writer.Flush(); 254 | } 255 | return Lua.Return(); 256 | } 257 | 258 | static LuaArguments seek(LuaArguments args) 259 | { 260 | var obj = args[0]; 261 | var whence = args[1] | "cur"; 262 | var offset = args[2] | 0; 263 | 264 | if (isStream(obj)) 265 | { 266 | var fobj = obj.luaobj as FileObject; 267 | switch (whence.ToString()) 268 | { 269 | case "cur": 270 | fobj.stream.Position += (long)offset; break; 271 | case "set": 272 | fobj.stream.Position = (long)offset; break; 273 | case "end": 274 | fobj.stream.Position = fobj.stream.Length + (long)offset; break; 275 | } 276 | return Lua.Return(fobj.stream.Position); 277 | } 278 | return Lua.Return(); 279 | } 280 | 281 | static LuaArguments read(LuaArguments args) 282 | { 283 | var self = args[0]; 284 | if (isStream(self)) 285 | { 286 | var fobj = self.luaobj as FileObject; 287 | if (args.Length == 1) 288 | { 289 | var line = fobj.reader.ReadLine(); 290 | 291 | return Lua.Return(line); 292 | } 293 | else 294 | { 295 | List ret = new List(); 296 | foreach (var arg in args) 297 | { 298 | if (arg == self) 299 | continue; 300 | if (arg.IsNumber) 301 | { 302 | StringBuilder bld = new StringBuilder(); 303 | for (int i = 0; i < arg; i++) 304 | { 305 | bld.Append((char)fobj.reader.Read()); 306 | } 307 | ret.Add(bld.ToString()); 308 | } 309 | else if (arg == "*a") 310 | ret.Add(fobj.reader.ReadToEnd()); 311 | else if (arg == "*l") 312 | ret.Add(fobj.reader.ReadLine()); 313 | else if (arg == "*n") 314 | { 315 | //TODO: Implement io.read("*n") 316 | throw new NotImplementedException(); 317 | } 318 | } 319 | return Lua.Return(ret.ToArray()); 320 | } 321 | } 322 | else 323 | return Lua.Return(); 324 | } 325 | } 326 | } 327 | -------------------------------------------------------------------------------- /luablock/luablock/Compiler.cs: -------------------------------------------------------------------------------- 1 | using luablock.Commands; 2 | using NetLua; 3 | using NetLua.Ast; 4 | using System; 5 | using System.Collections; 6 | using System.Collections.Generic; 7 | using System.IO; 8 | using System.Linq; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | 12 | namespace luablock 13 | { 14 | public class Compiler 15 | { 16 | public Dictionary> Methods = new Dictionary>(); 17 | 18 | public int ConstantCount = 0; 19 | 20 | public Compiler() 21 | { 22 | if(Directory.Exists("cmd")) 23 | { 24 | foreach(var i in Directory.GetFiles("cmd")) 25 | { 26 | if (i.EndsWith(".lua")) 27 | { 28 | var fl = new FileInfo(i); 29 | CommandManager.Cmds.Add(new LuaCmd() { Lua = File.ReadAllText(i), Name = fl.Name.Split('.')[0] }); 30 | } 31 | } 32 | } 33 | else 34 | { 35 | Directory.CreateDirectory("cmd"); 36 | } 37 | } 38 | 39 | public void Convert(string script, string schmaticpath) 40 | { 41 | Parser p = new Parser(); 42 | var z = p.ParseString(script); 43 | 44 | Methods.Add("classlevel", BuildBody(z.Statements)); 45 | 46 | //build clean up code 47 | List CleanUp = new List(); 48 | int total = 0; 49 | foreach(var i in Methods) 50 | { 51 | foreach(var x in i.Value) 52 | { 53 | total++; 54 | if (x.TrimStart('/').StartsWith("scoreboard objectives add")) 55 | { 56 | CleanUp.Add("/scoreboard objectives remove " + x.TrimStart('/').Split(' ')[3]); 57 | } 58 | } 59 | } 60 | Methods["classlevel"].Add("|" + (total / 4));//indicated we need more repeaters 61 | Methods["classlevel"].AddRange(CleanUp); 62 | BuildSchematic(schmaticpath); 63 | 64 | } 65 | 66 | public int IFsfound = 0; 67 | 68 | public void iterateLogic(List Lines, IfStat ifs) 69 | { 70 | 71 | var a = TypeToString((ifs.Condition as BinaryExpression).Left); 72 | var b = TypeToString((ifs.Condition as BinaryExpression).Right); 73 | 74 | Lines.Add("#/scoreboard players test lua " + a + " " + b + " " + b); 75 | } 76 | 77 | 78 | public List BuildBody(List Statments, List Linesa = null) 79 | { 80 | if(Linesa == null) 81 | { 82 | Linesa = new List(); 83 | } 84 | List Lines = Linesa; 85 | foreach (var i in Statments) 86 | { 87 | if(i is IfStat) 88 | { 89 | var x = i as IfStat; 90 | iterateLogic(Lines, x); 91 | Methods.Add("lua_if_" + IFsfound, BuildBody(x.Block.Statements)); 92 | var tmplines = Lines.ToArray().ToList(); 93 | //tmplines.Add(""); 94 | var bd = BuildBody(new List() { new FunctionCall() { Arguments = new List(), Function = new StringLiteral() { Value = "lua_if_" + IFsfound } } }, tmplines); 95 | bd[bd.Count - 1] = bd[bd.Count - 1].TrimStart('/');// 1 tick repeater 96 | Lines.Add(bd[bd.Count - 1]); 97 | IFsfound++; 98 | } 99 | 100 | if (i is FunctionCall) 101 | { 102 | // /setblock ~ ~ ~ minecraft:redstone_block 103 | var x = i as FunctionCall; 104 | if (Methods.ContainsKey(TypeToString(x.Function))) 105 | { 106 | // note: how there is no /, this is to say we need a delay on the repleater 107 | var ins = 0; 108 | if ((Methods.Keys.ToList().IndexOf(TypeToString(x.Function)) == 0)) 109 | { 110 | ins = Methods.Count * 2 + (Methods.Keys.ToList().IndexOf(TypeToString(x.Function))) * 2; 111 | } 112 | else 113 | { 114 | 115 | var index = (Methods.Keys.ToList().IndexOf(TypeToString(x.Function))); 116 | ins = (Methods.Count - index) * 2; 117 | 118 | 119 | } 120 | Lines.Add("setblock ~-" + (ins) + " ~ ~-" + ((Lines.Count * 2) + 2) + " minecraft:redstone_block"); 121 | } 122 | else 123 | { 124 | 125 | var y = CommandManager.GetCommand(TypeToString(x.Function)); 126 | Lines.Add(y.Parse(TypeToString(x.Function), ArgBuilder(x.Arguments))); 127 | } 128 | } 129 | if (i is Assignment) 130 | { 131 | var x = i as Assignment; 132 | 133 | if (x.Expressions[0] is FunctionDefinition) 134 | { 135 | var fnc = x.Expressions[0] as FunctionDefinition; 136 | Methods.Add(TypeToString((x.Variables[0] as Variable)), BuildBody(fnc.Body.Statements)); 137 | } 138 | else 139 | { 140 | if (x.Expressions[0] is BinaryExpression) 141 | { 142 | var name = TypeToString((x.Variables[0] as Variable)); 143 | var j = x.Expressions[0] as BinaryExpression; 144 | iterateBinOp(j, name , Lines); 145 | 146 | } 147 | else 148 | { 149 | var name = TypeToString((x.Variables[0] as Variable)); 150 | //Lines.Add("/scoreboard objectives remove " + name); 151 | Lines.Add("/scoreboard objectives add " + name + " dummy"); 152 | Lines.Add("/scoreboard players set lua " + name + " " + TypeToString(x.Expressions[0])); 153 | } 154 | } 155 | } 156 | } 157 | 158 | return Lines; 159 | } 160 | 161 | 162 | public int optc = 0; 163 | 164 | public string iterateBinOp(BinaryExpression bb, string name, List Lines) 165 | { 166 | var line = ""; 167 | string a = ""; 168 | string b = ""; 169 | 170 | if (bb.Left is BinaryExpression) 171 | { 172 | a = name + "_opt_" + optc; 173 | optc++; 174 | //Lines.Add("/scoreboard objectives remove " + a); 175 | Lines.Add("/scoreboard objectives add " + a + " dummy"); 176 | var ln = iterateBinOp(bb.Left as BinaryExpression, a , Lines); 177 | Lines.Add("/scoreboard players operation lua " + a + " " + "+=" + " lua " + ln); 178 | } 179 | else 180 | { 181 | if(char.IsDigit(TypeToString(bb.Left)[0])) 182 | { 183 | //and new var 184 | //Lines.Add("/scoreboard objectives remove con_" + ConstantCount); 185 | Lines.Add("/scoreboard objectives add con_" + ConstantCount + " dummy"); 186 | Lines.Add("/scoreboard players set lua con_" + ConstantCount + " " + TypeToString(bb.Left)); 187 | a = "con_" + ConstantCount; 188 | ConstantCount++; 189 | } 190 | else 191 | { 192 | a = TypeToString(bb.Left); 193 | } 194 | } 195 | 196 | if (bb.Right is BinaryExpression) 197 | { 198 | b = name + "_opt_" + optc; 199 | optc++; 200 | // Lines.Add("/scoreboard objectives remove " + b); 201 | Lines.Add("/scoreboard objectives add " + b + " dummy"); 202 | var ln = iterateBinOp(bb.Right as BinaryExpression, b, Lines); 203 | Lines.Add("/scoreboard players operation lua " + b + " " + "+=" + " lua " + ln); 204 | } 205 | else 206 | { 207 | if (char.IsDigit(TypeToString(bb.Right)[0])) 208 | { 209 | //and new var 210 | // Lines.Add("/scoreboard objectives remove con_" + ConstantCount); 211 | Lines.Add("/scoreboard objectives add con_" + ConstantCount + " dummy"); 212 | Lines.Add("/scoreboard players set lua con_" + ConstantCount + " " + TypeToString(bb.Right)); 213 | b = "con_" + ConstantCount; 214 | ConstantCount++; 215 | } 216 | else 217 | { 218 | b = TypeToString(bb.Right); 219 | } 220 | } 221 | 222 | 223 | Lines.Add("/scoreboard players operation lua " + a + " " + BuildBOp(bb.Operation) + " lua " + b ); 224 | line = a; 225 | return line; 226 | } 227 | 228 | public string BuildBOp(BinaryOp bo) 229 | { 230 | switch(bo) 231 | { 232 | case BinaryOp.Addition: 233 | return "+="; 234 | case BinaryOp.Subtraction: 235 | return "-="; 236 | case BinaryOp.Division: 237 | return "/="; 238 | case BinaryOp.Multiplication: 239 | return "*="; 240 | } 241 | return "+="; 242 | } 243 | 244 | public List ArgBuilder(List perams) 245 | { 246 | var re = new List(); 247 | foreach(var i in perams) 248 | { 249 | re.Add(TypeToString(i)); 250 | } 251 | return re; 252 | } 253 | 254 | private int getIntFromBitArray(BitArray bitArray) 255 | { 256 | 257 | if (bitArray.Length > 32) 258 | throw new ArgumentException("Argument length shall be at most 32 bits."); 259 | 260 | int[] array = new int[1]; 261 | bitArray.CopyTo(array, 0); 262 | return array[0]; 263 | 264 | } 265 | 266 | public void BuildSchematic(string schmaticpath) 267 | { 268 | int maxl = 0; 269 | int maxlw = 0; 270 | foreach (var i in Methods) 271 | { 272 | if (i.Value.Count > maxl) 273 | { 274 | maxl = i.Value.Count; 275 | } 276 | foreach (var iyy in i.Value) 277 | { 278 | var iz = iyy; 279 | if (iz.StartsWith("|")) 280 | { 281 | iz = iz.TrimStart('|'); 282 | maxlw += int.Parse(iz + 2); // 2 exstra repeaters to give script time 283 | 284 | } 285 | } 286 | } 287 | 288 | 289 | 290 | 291 | Schematic s = new Schematic(); 292 | s.Height = 1; 293 | s.Width = (short)((Methods.Count * 2) + maxlw + 5) ; 294 | s.Length = (short)(maxl * 2 + 2 + 5); 295 | s.Fill(); 296 | int x = 0; 297 | foreach (var ii in Methods) 298 | { 299 | int z = 1; 300 | s.SetBlock(x, 0, 0, new Block() { ID = 93, Metta = 2 }); 301 | foreach (var iyy in ii.Value) 302 | { 303 | var i = iyy; 304 | 305 | if (i.StartsWith("|")) 306 | { 307 | i = i.TrimStart('|'); 308 | for (int iu = 0; iu < int.Parse(i); iu++) 309 | { 310 | s.SetBlock(x, 0, z, new Block() { ID = 93, Metta = 14 }); // full tic repeater 311 | z++; 312 | } 313 | 314 | } 315 | else 316 | { 317 | int b = 2; 318 | if (!i.StartsWith("/")) 319 | { 320 | b = 6; 321 | } 322 | if (i.StartsWith("#")) 323 | { 324 | i = i.TrimStart('#'); 325 | s.SetBlock(x, 0, z + 1, new Block() { ID = 149, Metta = 2 }); 326 | } 327 | else 328 | { 329 | s.SetBlock(x, 0, z + 1, new Block() { ID = 93, Metta = b }); 330 | } 331 | s.SetBlock(x, 0, z, new Block() { ID = 137, Metta = 0, Command = i }); 332 | z += 2; 333 | } 334 | } 335 | s.SetBlock(x, 0, z, new Block() { ID = 137, Metta = 0, Command = "/setblock ~ ~ ~-" + (z + 1) + " minecraft:air" }); 336 | x += 2; 337 | } 338 | s.Save(schmaticpath); 339 | } 340 | 341 | 342 | 343 | 344 | private string TypeToString(IExpression e) 345 | { 346 | //this feels so cheety(whatt ever you do dont show this too kenny! xD) 347 | 348 | dynamic z = e; 349 | try 350 | { 351 | try 352 | { 353 | return z.Value.ToString(); 354 | } 355 | catch 356 | { 357 | return z.Name.ToString(); 358 | } 359 | } 360 | catch 361 | { 362 | return ""; 363 | } 364 | } 365 | } 366 | } 367 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/LuaEvents.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file 3 | */ 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | 10 | namespace NetLua 11 | { 12 | static class LuaEvents 13 | { 14 | // This is all based on the Lua 5.2 manual 15 | // http://www.lua.org/manual/5.2/manual.html#2.4 16 | 17 | public static LuaObject rawget(LuaObject table, LuaObject index) 18 | { 19 | if (table.IsTable) 20 | { 21 | LuaObject obj; 22 | if (table.AsTable().TryGetValue(index, out obj)) 23 | return obj; 24 | else 25 | return LuaObject.Nil; 26 | } 27 | else 28 | throw new LuaException("Invalid operation"); 29 | } 30 | 31 | public static void rawset(LuaObject table, LuaObject key, LuaObject value) 32 | { 33 | if (table.IsTable) 34 | { 35 | var t = table.AsTable(); 36 | if (t.ContainsKey(key)) 37 | t[key] = value; 38 | else 39 | t.Add(key, value); 40 | } 41 | else 42 | throw new LuaException("Invalid operation"); 43 | } 44 | 45 | static LuaObject getBinhandler(LuaObject a, LuaObject b, string f) 46 | { 47 | var f1 = getMetamethod(a, f); 48 | var f2 = getMetamethod(b, f); 49 | 50 | return f1 | f2; 51 | } 52 | 53 | static LuaObject getEqualhandler(LuaObject a, LuaObject b) 54 | { 55 | if ((a.Type != b.Type) || (a.IsTable && b.IsUserData)) 56 | return LuaObject.Nil; 57 | var mm1 = getMetamethod(a, "__eq"); 58 | var mm2 = getMetamethod(b, "__eq"); 59 | if (mm1 == mm2) 60 | return mm1; 61 | else 62 | return LuaObject.Nil; 63 | } 64 | 65 | internal static LuaObject toNumber(LuaObject obj) 66 | { 67 | if (obj.IsNumber) 68 | return LuaObject.FromNumber(obj.AsNumber()); 69 | else if (obj.IsString) 70 | { 71 | double d; 72 | if (double.TryParse(obj.AsString(), out d)) 73 | return LuaObject.FromNumber(d); 74 | else 75 | return LuaObject.Nil; 76 | } 77 | else 78 | return LuaObject.Nil; 79 | } 80 | 81 | internal static bool ConvertToNumber(LuaObject obj, out double value) 82 | { 83 | if (obj.IsNumber) 84 | { 85 | value = (double)obj.luaobj; 86 | return true; 87 | } 88 | else if (obj.IsString) 89 | { 90 | if (double.TryParse(obj.AsString(), out value)) 91 | return true; 92 | else 93 | return false; 94 | } 95 | else 96 | { 97 | value = 0d; 98 | return false; 99 | } 100 | } 101 | 102 | internal static LuaObject getMetamethod(LuaObject obj, string e) 103 | { 104 | if (obj.Metatable == null || obj.Metatable.IsNil) 105 | return LuaObject.Nil; 106 | else 107 | return obj.Metatable[e]; 108 | } 109 | 110 | internal static LuaObject add_event(LuaObject op1, LuaObject op2) 111 | { 112 | double a, b; 113 | 114 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b)) 115 | return LuaObject.FromNumber(a + b); 116 | else 117 | { 118 | var handler = getBinhandler(op1, op2, "__add"); 119 | if (!handler.IsNil) 120 | return handler.Call(op1, op2)[0]; 121 | } 122 | 123 | throw new LuaException("Invalid arithmetic operation"); 124 | } 125 | 126 | internal static LuaObject sub_event(LuaObject op1, LuaObject op2) 127 | { 128 | double a, b; 129 | 130 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b)) 131 | return LuaObject.FromNumber(a - b); 132 | else 133 | { 134 | var handler = getBinhandler(op1, op2, "__sub"); 135 | if (!handler.IsNil) 136 | return handler.Call(op1, op2)[0]; 137 | } 138 | 139 | throw new LuaException("Invalid arithmetic operation"); 140 | } 141 | 142 | internal static LuaObject mul_event(LuaObject op1, LuaObject op2) 143 | { 144 | double a, b; 145 | 146 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b)) 147 | return LuaObject.FromNumber(a * b); 148 | else 149 | { 150 | var handler = getBinhandler(op1, op2, "__mul"); 151 | if (!handler.IsNil) 152 | return handler.Call(op1, op2)[0]; 153 | } 154 | 155 | throw new LuaException("Invalid arithmetic operation"); 156 | } 157 | 158 | internal static LuaObject div_event(LuaObject op1, LuaObject op2) 159 | { 160 | double a, b; 161 | 162 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b)) 163 | return LuaObject.FromNumber(a / b); 164 | else 165 | { 166 | var handler = getBinhandler(op1, op2, "__div"); 167 | if (!handler.IsNil) 168 | return handler.Call(op1, op2)[0]; 169 | } 170 | 171 | throw new LuaException("Invalid arithmetic operation"); 172 | } 173 | 174 | internal static LuaObject mod_event(LuaObject op1, LuaObject op2) 175 | { 176 | double a, b; 177 | 178 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b)) 179 | return LuaObject.FromNumber(a - Math.Floor(a / b) * b); 180 | else 181 | { 182 | var handler = getBinhandler(op1, op2, "__mod"); 183 | if (!handler.IsNil) 184 | return handler.Call(op1, op2)[0]; 185 | } 186 | 187 | throw new LuaException("Invalid arithmetic operation"); 188 | } 189 | 190 | internal static LuaObject pow_event(LuaObject op1, LuaObject op2) 191 | { 192 | double a, b; 193 | 194 | if (ConvertToNumber(op1, out a) && ConvertToNumber(op2, out b)) 195 | return LuaObject.FromNumber(Math.Pow(a, b)); 196 | else 197 | { 198 | var handler = getBinhandler(op1, op2, "__pow"); 199 | if (!handler.IsNil) 200 | return handler.Call(op1, op2)[0]; 201 | } 202 | 203 | throw new LuaException("Invalid arithmetic operation"); 204 | } 205 | 206 | internal static LuaObject unm_event(LuaObject op) 207 | { 208 | double a; 209 | 210 | if (ConvertToNumber(op, out a)) 211 | { 212 | return LuaObject.FromNumber(-a); 213 | } 214 | else 215 | { 216 | var handler = getMetamethod(op, "__unm"); 217 | if (!handler.IsNil) 218 | return handler.Call(op)[0]; 219 | } 220 | 221 | throw new LuaException("Invalid arithmetic operation"); 222 | } 223 | 224 | internal static LuaObject index_event(LuaObject table, LuaObject key) 225 | { 226 | LuaObject handler; 227 | 228 | if (table.IsTable) 229 | { 230 | var v = rawget(table, key); 231 | if (!v.IsNil) 232 | return v; 233 | else 234 | { 235 | handler = getMetamethod(table, "__index"); 236 | if (handler.IsNil) 237 | return LuaObject.Nil; 238 | } 239 | } 240 | else 241 | { 242 | handler = getMetamethod(table, "__index"); 243 | if (handler.IsNil) 244 | throw new LuaException("Invalid argument"); 245 | } 246 | 247 | if (handler.IsFunction) 248 | return handler.AsFunction()(new LuaObject[]{table, key})[0]; 249 | else if (!handler.IsNil) 250 | return handler[key]; 251 | else 252 | return LuaObject.Nil; 253 | } 254 | 255 | internal static LuaObject newindex_event(LuaObject table, LuaObject key, LuaObject value) 256 | { 257 | LuaObject handler; 258 | if (table.IsTable) 259 | { 260 | var v = rawget(table, key); 261 | if (!v.IsNil) 262 | { 263 | rawset(table, key, value); 264 | return LuaObject.Nil; 265 | } 266 | handler = getMetamethod(table, "__newindex"); 267 | if (handler.IsNil) 268 | { 269 | rawset(table, key, value); 270 | return LuaObject.Nil; 271 | } 272 | } 273 | else 274 | { 275 | handler = getMetamethod(table, "__newindex"); 276 | if (handler.IsNil) 277 | throw new LuaException("Invalid op"); 278 | } 279 | 280 | if (handler.IsFunction) 281 | handler.AsFunction()(new LuaObject[]{table, key, value}); 282 | else 283 | handler[key] = value; 284 | return LuaObject.Nil; 285 | } 286 | 287 | internal static LuaArguments call_event(LuaObject func, LuaArguments args) 288 | { 289 | if (func.IsFunction) 290 | return func.AsFunction()(args); 291 | else 292 | { 293 | var handler = getMetamethod(func, "__call"); 294 | if (handler.IsFunction) 295 | { 296 | var argslist = new List(); 297 | argslist.Add(func); argslist.AddRange(args); 298 | return handler.AsFunction()(argslist.ToArray()); 299 | } 300 | else 301 | throw new LuaException("Cannot call nonfunction"); 302 | } 303 | } 304 | 305 | internal static LuaObject len_event(LuaObject op) 306 | { 307 | if (op.IsString) 308 | return op.AsString().Length; 309 | else 310 | { 311 | var handler = getMetamethod(op, "__len"); 312 | if (!handler.IsNil) 313 | return handler.Call(op)[0]; 314 | else if (op.IsTable) 315 | return op.AsTable().Count; 316 | else 317 | throw new LuaException("Invalid op"); 318 | } 319 | } 320 | 321 | internal static LuaObject concat_event(LuaObject op1, LuaObject op2) 322 | { 323 | if ((op1.IsString || op1.IsNumber) && (op2.IsString || op2.IsNumber)) 324 | return string.Concat(op1, op2); 325 | else 326 | { 327 | var handler = getBinhandler(op1, op2, "__concat"); 328 | if (!handler.IsNil) 329 | return handler.Call(op1, op2)[0]; 330 | else 331 | throw new LuaException("Invalid op"); 332 | } 333 | } 334 | 335 | internal static LuaObject eq_event(LuaObject op1, LuaObject op2) 336 | { 337 | if (op1 == op2) 338 | return true; 339 | var handler = getEqualhandler(op1, op2); 340 | if (!handler.IsNil) 341 | return !(!(handler.Call(op1, op2)[0].AsBool())); 342 | else 343 | return false; 344 | } 345 | 346 | internal static LuaObject lt_event(LuaObject op1, LuaObject op2) 347 | { 348 | if (op1.IsNumber && op2.IsNumber) 349 | return op1.AsNumber() < op2.AsNumber(); 350 | else if (op1.IsString && op2.IsString) 351 | { 352 | int n = StringComparer.CurrentCulture.Compare(op1.AsString(), op2.AsString()); 353 | return (n < 0); 354 | } 355 | else 356 | { 357 | var handler = getBinhandler(op1, op2, "__lt"); 358 | if (!handler.IsNil) 359 | return !(!(handler.Call(op1, op2)[0].AsBool())); 360 | else 361 | throw new ArgumentException("attempt to compare " + op1.type.ToString() + " with " + op2.type.ToString()); 362 | } 363 | } 364 | 365 | internal static LuaObject le_event(LuaObject op1, LuaObject op2) 366 | { 367 | if (op1.IsNumber && op2.IsNumber) 368 | return op1.AsNumber() <= op2.AsNumber(); 369 | else if (op1.IsString && op2.IsString) 370 | { 371 | int n = StringComparer.CurrentCulture.Compare(op1.AsString(), op2.AsString()); 372 | return (n <= 0); 373 | } 374 | else 375 | { 376 | var handler = getBinhandler(op1, op2, "__le"); 377 | if (!handler.IsNil) 378 | return !(!(handler.Call(op1, op2)[0].AsBool())); 379 | else 380 | throw new ArgumentException("attempt to compare " + op1.type.ToString() + " with " + op2.type.ToString()); 381 | } 382 | } 383 | 384 | internal static LuaObject gc_event(LuaObject op) 385 | { 386 | var handler = getMetamethod(op, "__gc"); 387 | if (handler.IsFunction) 388 | handler.AsFunction()(new LuaObject[] {op}); 389 | return LuaObject.Nil; 390 | } 391 | 392 | internal static LuaObject tostring_event(LuaObject op) 393 | { 394 | var handler = getMetamethod(op, "__tostring"); 395 | if (!handler.IsNil) 396 | return handler.Call(op)[0]; 397 | else 398 | return op.ToString(); 399 | } 400 | } 401 | } 402 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/LuaObject.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file 3 | */ 4 | 5 | using System; 6 | using System.Linq; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using System.Threading; 10 | 11 | using System.Dynamic; 12 | 13 | namespace NetLua 14 | { 15 | using LuaTable = IDictionary; 16 | using LuaTableImpl = Dictionary; 17 | using LuaTableItem = KeyValuePair; 18 | 19 | /// 20 | /// An exception thrown by the Lua interpreter 21 | /// 22 | public class LuaException : Exception 23 | { 24 | /// 25 | /// An exception thrown by a syntactical error 26 | /// 27 | /// The file wich contains the error 28 | /// The row of the error 29 | /// The column of the error 30 | /// The kind of the error 31 | public LuaException(string file, int row, int col, string message) 32 | : base(string.Format("Error in {0}({1},{2}): {3}", file, row, col, message)) 33 | { } 34 | 35 | public LuaException(string message) 36 | : base("Error (unknown context): " + message) 37 | { } 38 | } 39 | 40 | /// 41 | /// A Lua function 42 | /// 43 | public delegate LuaArguments LuaFunction(LuaArguments args); 44 | 45 | public class LuaArguments : IEnumerable 46 | { 47 | List list; 48 | 49 | public LuaArguments() 50 | { 51 | list = new List(); 52 | } 53 | 54 | public LuaArguments(List List) 55 | { 56 | list = List; 57 | } 58 | 59 | public LuaArguments(params LuaObject[] Objects) 60 | { 61 | list = new List(Objects); 62 | } 63 | 64 | public LuaArguments(params LuaArguments[] Objects) 65 | { 66 | foreach (LuaArguments arg in Objects) 67 | { 68 | if (list == null) 69 | list = new List(arg.list); 70 | else 71 | list.AddRange(arg.list); 72 | } 73 | } 74 | 75 | public int Length 76 | { 77 | get 78 | { 79 | return list.Count; 80 | } 81 | } 82 | 83 | public LuaObject this[int Index] 84 | { 85 | get 86 | { 87 | if (Index < list.Count) 88 | return list[Index]; 89 | else 90 | return LuaObject.Nil; 91 | } 92 | set 93 | { 94 | if (Index < list.Count) 95 | list[Index] = value; 96 | } 97 | } 98 | 99 | public void Add(LuaObject obj) 100 | { 101 | list.Add(obj); 102 | } 103 | 104 | public LuaArguments Concat(LuaArguments args) 105 | { 106 | list.AddRange(args.list); 107 | return this; 108 | } 109 | 110 | public IEnumerator GetEnumerator() 111 | { 112 | return list.GetEnumerator(); 113 | } 114 | 115 | IEnumerator IEnumerable.GetEnumerator() 116 | { 117 | return this.GetEnumerator(); 118 | } 119 | 120 | public static implicit operator LuaArguments(LuaObject[] array) 121 | { 122 | return new LuaArguments(array); 123 | } 124 | 125 | public static implicit operator LuaObject[](LuaArguments args) 126 | { 127 | return args.list.ToArray(); 128 | } 129 | 130 | public LuaArguments GetSubset(int startIndex) 131 | { 132 | if (startIndex >= list.Count) 133 | { 134 | return new LuaArguments(new LuaObject[] { }); 135 | } 136 | else 137 | { 138 | return new LuaArguments(list.GetRange(startIndex, list.Count - startIndex)); 139 | } 140 | } 141 | } 142 | 143 | // http://www.lua.org/pil/2.html 144 | /// 145 | /// A Lua type 146 | /// 147 | public enum LuaType : byte 148 | { 149 | nil = 0, 150 | boolean, 151 | number, 152 | @string, 153 | userdata, 154 | function, 155 | thread, 156 | table 157 | } 158 | 159 | /// 160 | /// A Lua object. Can be any of the standard Lua objects 161 | /// 162 | public class LuaObject : DynamicObject, IEnumerable> 163 | { 164 | internal object luaobj; 165 | internal LuaType type; 166 | private LuaObject metatable = Nil; 167 | 168 | public LuaObject() 169 | { 170 | this.metatable = Nil; 171 | } 172 | 173 | private LuaObject(object Obj, LuaType Type) 174 | { 175 | this.metatable = Nil; 176 | this.luaobj = Obj; 177 | this.type = Type; 178 | } 179 | 180 | ~LuaObject() 181 | { 182 | LuaEvents.gc_event(this); 183 | } 184 | 185 | /// 186 | /// Gets or sets the object's metatable 187 | /// 188 | public LuaObject Metatable 189 | { 190 | get 191 | { 192 | return metatable; 193 | } 194 | set 195 | { 196 | metatable = value; 197 | } 198 | } 199 | 200 | #region Common objects 201 | /// 202 | /// An empty/unset value 203 | /// 204 | public static readonly LuaObject Nil = new LuaObject() { luaobj = null, type = LuaType.nil }; 205 | 206 | /// 207 | /// A standard true boolean value 208 | /// 209 | public static readonly LuaObject True = new LuaObject { luaobj = true, type = LuaType.boolean, Metatable = Nil }; 210 | 211 | /// 212 | /// A standard false boolean value 213 | /// 214 | public static readonly LuaObject False = new LuaObject { luaobj = false, type = LuaType.boolean, Metatable = Nil }; 215 | 216 | /// 217 | /// Zero (number) 218 | /// 219 | public static readonly LuaObject Zero = new LuaObject { luaobj = 0d, type = LuaType.number, Metatable = Nil }; 220 | 221 | /// 222 | /// And empty string 223 | /// 224 | public static readonly LuaObject EmptyString = new LuaObject { luaobj = "", type = LuaType.@string, Metatable = Nil }; 225 | #endregion 226 | 227 | /// 228 | /// Gets the underlying Lua type 229 | /// 230 | public LuaType Type { get { return type; } internal set { type = value; } } 231 | 232 | /// 233 | /// Checks whether the type matches or not 234 | /// 235 | public bool Is(LuaType type) 236 | { 237 | return this.type == type; 238 | } 239 | 240 | /// 241 | /// Creates a Lua object from a .NET object 242 | /// Automatically checks if there is a matching Lua type. 243 | /// If not, creates a userdata value 244 | /// 245 | public static LuaObject FromObject(object obj) 246 | { 247 | if (obj == null) 248 | return Nil; 249 | 250 | if (obj is LuaObject) 251 | return (LuaObject)obj; 252 | 253 | if (obj is bool) 254 | return FromBool((bool)obj); 255 | 256 | { 257 | var str = obj as string; 258 | if (str != null) 259 | return FromString(str); 260 | } 261 | 262 | { 263 | var @delegate = obj as LuaFunction; 264 | if (@delegate != null) 265 | return FromFunction(@delegate); 266 | } 267 | 268 | { 269 | var dictionary = obj as LuaTable; 270 | if (dictionary != null) 271 | return FromTable(dictionary); 272 | } 273 | 274 | if (obj is double) return FromNumber((double)obj); 275 | if (obj is float) return FromNumber((float)obj); 276 | if (obj is int) return FromNumber((int)obj); 277 | if (obj is uint) return FromNumber((uint)obj); 278 | if (obj is short) return FromNumber((short)obj); 279 | if (obj is ushort) return FromNumber((ushort)obj); 280 | if (obj is long) return FromNumber((long)obj); 281 | if (obj is ulong) return FromNumber((ulong)obj); 282 | if (obj is byte) return FromNumber((byte)obj); 283 | if (obj is sbyte) return FromNumber((sbyte)obj); 284 | if (obj is Thread) return new LuaObject { luaobj = obj, type = LuaType.thread }; 285 | return FromUserData(obj); 286 | } 287 | 288 | #region Boolean 289 | /// 290 | /// Creates a Lua object from a boolean value 291 | /// 292 | public static LuaObject FromBool(bool bln) 293 | { 294 | if (bln) 295 | return True; 296 | 297 | return False; 298 | } 299 | 300 | public static implicit operator LuaObject(bool bln) 301 | { 302 | return FromBool(bln); 303 | } 304 | 305 | public static implicit operator bool(LuaObject obj) 306 | { 307 | return obj.AsBool(); 308 | } 309 | 310 | /// 311 | /// Gets whether this is a boolean object 312 | /// 313 | public bool IsBool { get { return type == LuaType.boolean; } } 314 | 315 | /// 316 | /// Converts this Lua object into a boolean 317 | /// 318 | /// 319 | public bool AsBool() 320 | { 321 | if (luaobj == null) 322 | return false; 323 | 324 | if (luaobj is bool && ((bool)luaobj) == false) 325 | return false; 326 | 327 | return true; 328 | } 329 | #endregion 330 | 331 | #region Number 332 | /// 333 | /// Creates a Lua object from a double 334 | /// 335 | public static LuaObject FromNumber(double number) 336 | { 337 | if (number == 0d) 338 | return Zero; 339 | 340 | return new LuaObject(number, LuaType.number); 341 | } 342 | 343 | public static implicit operator LuaObject(double number) 344 | { 345 | return FromNumber(number); 346 | } 347 | 348 | public static implicit operator double(LuaObject obj) 349 | { 350 | return obj.AsNumber(); 351 | } 352 | 353 | /// 354 | /// Gets whether this is a number object 355 | /// 356 | public bool IsNumber { get { return type == LuaType.number; } } 357 | 358 | /// 359 | /// Converts this object into a number 360 | /// 361 | public double AsNumber() 362 | { 363 | return (double)luaobj; 364 | } 365 | #endregion 366 | 367 | #region String 368 | /// 369 | /// Creates a Lua object from a string 370 | /// 371 | public static LuaObject FromString(string str) 372 | { 373 | if (str == null) 374 | return Nil; 375 | 376 | if (str.Length == 0) 377 | return EmptyString; 378 | 379 | return new LuaObject(str, LuaType.@string); 380 | } 381 | 382 | public static implicit operator LuaObject(string str) 383 | { 384 | return FromString(str); 385 | } 386 | 387 | public static implicit operator string(LuaObject obj) 388 | { 389 | return obj.AsString(); 390 | } 391 | 392 | public bool IsString { get { return type == LuaType.@string; } } 393 | 394 | public string AsString() 395 | { 396 | return luaobj.ToString(); 397 | } 398 | #endregion 399 | 400 | #region Function 401 | /// 402 | /// Creates a Lua object from a Lua function 403 | /// 404 | public static LuaObject FromFunction(LuaFunction fn) 405 | { 406 | if (fn == null) 407 | return Nil; 408 | 409 | return new LuaObject(fn, LuaType.function); 410 | } 411 | 412 | public static implicit operator LuaObject(LuaFunction fn) 413 | { 414 | return FromFunction(fn); 415 | } 416 | 417 | public bool IsFunction { get { return type == LuaType.function; } } 418 | 419 | public LuaFunction AsFunction() 420 | { 421 | var fn = luaobj as LuaFunction; 422 | if (fn == null) 423 | throw new LuaException("cannot call non-function"); 424 | 425 | return fn; 426 | } 427 | #endregion 428 | 429 | #region Table 430 | /// 431 | /// Creates a Lua object from a Lua table 432 | /// 433 | public static LuaObject FromTable(LuaTable table) 434 | { 435 | if (table == null) 436 | return Nil; 437 | 438 | return new LuaObject(table, LuaType.table); 439 | } 440 | 441 | /// 442 | /// Creates and initializes a Lua object with a table value 443 | /// 444 | /// The initial items of the table to create 445 | public static LuaObject NewTable(params LuaTableItem[] initItems) 446 | { 447 | var table = FromTable(new LuaTableImpl()); 448 | 449 | foreach (var item in initItems) 450 | table[item.Key] = item.Value; 451 | 452 | return table; 453 | } 454 | 455 | public bool IsTable { get { return type == LuaType.table; } } 456 | 457 | public LuaTable AsTable() 458 | { 459 | return luaobj as LuaTable; 460 | } 461 | #endregion 462 | 463 | #region Userdata 464 | /// 465 | /// Creates a Lua object from a .NET object 466 | /// 467 | public static LuaObject FromUserData(object userdata) 468 | { 469 | if (userdata == null) 470 | return Nil; 471 | 472 | //return new LuaObject { luaobj = userdata, type = LuaType.userdata }; 473 | return new LuaObject(userdata, LuaType.userdata); 474 | } 475 | 476 | /// 477 | /// Gets whether this object is nil 478 | /// 479 | public bool IsNil { get { return type == LuaType.nil; } } 480 | 481 | /// 482 | /// Gets whether this object is userdata 483 | /// 484 | public bool IsUserData { get { return type == LuaType.userdata; } } 485 | 486 | /// 487 | /// Returns the CLI object underneath the wrapper 488 | /// 489 | public object AsUserData() 490 | { 491 | return luaobj; 492 | } 493 | #endregion 494 | 495 | public static LuaObject operator +(LuaObject a, LuaObject b) 496 | { 497 | return LuaEvents.add_event(a, b); 498 | } 499 | 500 | public static LuaObject operator -(LuaObject a, LuaObject b) 501 | { 502 | return LuaEvents.sub_event(a, b); 503 | } 504 | 505 | public static LuaObject operator *(LuaObject a, LuaObject b) 506 | { 507 | return LuaEvents.mul_event(a, b); 508 | } 509 | 510 | public static LuaObject operator /(LuaObject a, LuaObject b) 511 | { 512 | return LuaEvents.div_event(a, b); 513 | } 514 | 515 | public static LuaObject operator %(LuaObject a, LuaObject b) 516 | { 517 | return LuaEvents.mod_event(a, b); 518 | } 519 | 520 | public static LuaObject operator ^(LuaObject a, LuaObject b) 521 | { 522 | return LuaEvents.pow_event(a, b); 523 | } 524 | 525 | public static LuaObject operator <(LuaObject a, LuaObject b) 526 | { 527 | return LuaEvents.lt_event(a, b); 528 | } 529 | 530 | public static LuaObject operator >(LuaObject a, LuaObject b) 531 | { 532 | return LuaEvents.lt_event(b, a); 533 | } 534 | 535 | public static LuaObject operator <=(LuaObject a, LuaObject b) 536 | { 537 | return LuaEvents.le_event(a, b); 538 | } 539 | 540 | public static LuaObject operator >=(LuaObject a, LuaObject b) 541 | { 542 | return LuaEvents.le_event(b, a); 543 | } 544 | 545 | public static bool operator ==(LuaObject a, object b) 546 | { 547 | if (b == null) 548 | return LuaObject.FromObject(a).IsNil; 549 | 550 | if (a.IsNil) 551 | { 552 | if (b == null) 553 | return true; 554 | else 555 | { 556 | if (b is LuaObject) 557 | return (b as LuaObject).IsNil; 558 | else 559 | return false; 560 | } 561 | } 562 | else 563 | { 564 | if (b == null) 565 | return false; 566 | else 567 | { 568 | if (b is LuaObject) 569 | { 570 | if ((b as LuaObject).IsNil) 571 | return a.IsNil; 572 | 573 | return (b as LuaObject).luaobj.Equals(a.luaobj); 574 | } 575 | else 576 | return a.luaobj.Equals(b); 577 | } 578 | } 579 | } 580 | 581 | public static bool operator !=(LuaObject a, object b) 582 | { 583 | return !(a == b); 584 | } 585 | 586 | public static LuaObject operator |(LuaObject a, object b) 587 | { 588 | if (a.IsNil || !a.AsBool()) 589 | { 590 | if (b is LuaObject) 591 | return b as LuaObject; 592 | else 593 | return LuaObject.FromObject(b); 594 | } 595 | else 596 | return a; 597 | } 598 | 599 | public static LuaObject operator &(LuaObject a, object b) 600 | { 601 | if (a.IsNil || !a.AsBool()) 602 | return a; 603 | else 604 | { 605 | if (b is LuaObject) 606 | return b as LuaObject; 607 | else 608 | return LuaObject.FromObject(b); 609 | } 610 | } 611 | 612 | public IEnumerator> GetEnumerator() 613 | { 614 | var table = luaobj as IEnumerable>; 615 | if (table == null) 616 | return null; 617 | 618 | return table.GetEnumerator(); 619 | } 620 | 621 | IEnumerator IEnumerable.GetEnumerator() 622 | { 623 | return GetEnumerator(); 624 | } 625 | 626 | public LuaObject this[LuaObject key] 627 | { 628 | get 629 | { 630 | if (IsTable) 631 | { 632 | var table = AsTable(); 633 | LuaObject obj; 634 | if (table.TryGetValue(key, out obj)) 635 | return obj; 636 | else 637 | return LuaEvents.index_event(this, key); 638 | } 639 | else 640 | return LuaEvents.index_event(this, key); 641 | } 642 | set 643 | { 644 | if (IsTable) 645 | { 646 | var table = AsTable(); 647 | LuaObject obj; 648 | if (table.TryGetValue(key, out obj)) 649 | table[key] = value; 650 | else 651 | LuaEvents.newindex_event(this, key, value); 652 | } 653 | else 654 | LuaEvents.newindex_event(this, key, value); 655 | } 656 | } 657 | 658 | // Unlike AsString, this will return string representations of nil, tables, and functions 659 | public override string ToString() 660 | { 661 | if (IsNil) 662 | return "nil"; 663 | 664 | if (IsTable) 665 | return "table"; 666 | 667 | if (IsFunction) 668 | return "function"; 669 | 670 | if (IsBool) 671 | return luaobj.ToString().ToLower(); 672 | 673 | return luaobj.ToString(); 674 | } 675 | 676 | public override bool Equals(object obj) 677 | { 678 | if (obj is LuaObject) 679 | return luaobj.Equals((obj as LuaObject).luaobj); 680 | else 681 | return luaobj.Equals(obj); 682 | } 683 | 684 | public override int GetHashCode() 685 | { 686 | unchecked 687 | { 688 | return (luaobj != null ? luaobj.GetHashCode() : 0) ^ (byte)type; 689 | } 690 | } 691 | 692 | /// 693 | /// Calls the object passing the instance as first argument. Uses the metafield __call 694 | /// 695 | /// The object to be passed as first argument 696 | /// Arguments to be passed after the object 697 | public LuaArguments MethodCall(LuaObject instance, LuaArguments args) 698 | { 699 | var objs = new LuaObject[args.Length + 1]; 700 | objs[0] = instance; 701 | for (int i = 0; i < args.Length; i++) 702 | { 703 | objs[i + 1] = args[i]; 704 | } 705 | 706 | return this.Call(objs); 707 | } 708 | 709 | /// 710 | /// Calls the object. If this is not a function, it calls the metatable field __call 711 | /// 712 | /// The arguments to pass 713 | public LuaArguments Call(params LuaObject[] args) 714 | { 715 | return this.Call(new LuaArguments(args)); 716 | } 717 | 718 | /// 719 | /// Calls the object. If this is not a function, it calls the metatable field __call 720 | /// 721 | /// The arguments to pass 722 | public LuaArguments Call(LuaArguments args) 723 | { 724 | return LuaEvents.call_event(this, args); 725 | } 726 | 727 | #region DynamicObject 728 | 729 | /// 730 | /// Gets a standard .NET value froma LuaObject 731 | /// 732 | /// The LuaObject is is a function or a table, its underlying luaobj if not 733 | internal static object getObject(LuaObject a) 734 | { 735 | if (a.Type != LuaType.table && a.Type != LuaType.function) 736 | return a.luaobj; 737 | else 738 | return a; 739 | } 740 | 741 | public override bool TryGetMember(GetMemberBinder binder, out object result) 742 | { 743 | result = null; 744 | LuaObject obj = this[binder.Name]; 745 | if (obj.IsNil) 746 | return false; 747 | else 748 | { 749 | result = getObject(obj); 750 | return true; 751 | } 752 | } 753 | 754 | public override bool TrySetMember(SetMemberBinder binder, object value) 755 | { 756 | if (value is LuaObject) 757 | this[binder.Name] = (LuaObject)value; 758 | else 759 | this[binder.Name] = LuaObject.FromObject(value); 760 | return true; 761 | } 762 | 763 | public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) 764 | { 765 | LuaObject[] passingArgs = Array.ConvertAll(args, 766 | x => (x is LuaObject ? (LuaObject)x : LuaObject.FromObject(x))); 767 | LuaArguments ret = this.Call(passingArgs); 768 | if (ret.Length == 1) 769 | { 770 | if (ret[0].IsNil) 771 | result = null; 772 | else 773 | result = getObject(ret[0]); 774 | return true; 775 | } 776 | else 777 | { 778 | object[] res = Array.ConvertAll(ret.ToArray(), x => getObject(x)); 779 | result = res; 780 | return true; 781 | } 782 | } 783 | #endregion 784 | } 785 | } 786 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/Parser.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file 3 | */ 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | 10 | using Irony.Parsing; 11 | using Irony.Ast; 12 | 13 | namespace NetLua 14 | { 15 | public class Parser 16 | { 17 | public Ast.Block block; 18 | 19 | LuaGrammar grammar = new LuaGrammar(); 20 | LanguageData language; 21 | Irony.Parsing.Parser parser; 22 | 23 | public Parser() 24 | { 25 | language = new LanguageData(grammar); 26 | parser = new Irony.Parsing.Parser(language); 27 | } 28 | 29 | public Ast.Block ParseString(string Chunk) 30 | { 31 | ParseTree parseTree = parser.Parse(Chunk); 32 | ParseTreeNode root = parseTree.Root; 33 | if (root == null) 34 | { 35 | Irony.LogMessage msg = parseTree.ParserMessages[0]; 36 | throw new LuaException("", msg.Location.Line, msg.Location.Column, msg.Message); 37 | } 38 | return (ParseBlock(root)); 39 | } 40 | 41 | public Ast.Block ParseFile(string Filename) 42 | { 43 | string source = System.IO.File.ReadAllText(Filename); 44 | ParseTree parseTree = parser.Parse(source, Filename); 45 | ParseTreeNode root = parseTree.Root; 46 | if (root == null) 47 | { 48 | Irony.LogMessage msg = parseTree.ParserMessages[0]; 49 | throw new LuaException(Filename, msg.Location.Line, msg.Location.Column, msg.Message); 50 | } 51 | return (ParseBlock(root)); 52 | } 53 | 54 | #region Binary expression tree 55 | Ast.IExpression ParseOrOp(ParseTreeNode node) 56 | { 57 | if (node.Term.Name == "OrOp") 58 | { 59 | ParseTreeNode left = node.ChildNodes[0]; 60 | Ast.IExpression lexpr = ParseAndOp(left); 61 | if (node.ChildNodes[1].ChildNodes.Count == 0) 62 | return lexpr; 63 | ParseTreeNode right = node.ChildNodes[1].ChildNodes[1]; 64 | Ast.IExpression rexpr = ParseAndOp(right); 65 | 66 | return new Ast.BinaryExpression() { Left = lexpr, Right = rexpr, Operation = Ast.BinaryOp.Or }; 67 | } 68 | throw new Exception("Invalid OrOp node"); 69 | } 70 | 71 | Ast.IExpression ParseAndOp(ParseTreeNode node) 72 | { 73 | if (node.Term.Name == "AndOp") 74 | { 75 | ParseTreeNode left = node.ChildNodes[0]; 76 | Ast.IExpression lexpr = ParseRelOp(left); 77 | if (node.ChildNodes[1].ChildNodes.Count == 0) 78 | return lexpr; 79 | ParseTreeNode right = node.ChildNodes[1].ChildNodes[1]; 80 | Ast.IExpression rexpr = ParseRelOp(right); 81 | 82 | return new Ast.BinaryExpression() { Left = lexpr, Right = rexpr, Operation = Ast.BinaryOp.And }; 83 | } 84 | throw new Exception("Invalid AndOp node"); 85 | } 86 | 87 | Ast.IExpression ParseRelOp(ParseTreeNode node) 88 | { 89 | if (node.Term.Name == "RelOp") 90 | { 91 | ParseTreeNode left = node.ChildNodes[0]; 92 | Ast.IExpression lexpr = ParseConcatOp(left); 93 | if (node.ChildNodes[1].ChildNodes.Count == 0) 94 | return lexpr; 95 | string opstring = node.ChildNodes[1].ChildNodes[0].ChildNodes[0].Token.ValueString; 96 | Ast.BinaryOp op; 97 | switch (opstring) 98 | { 99 | case ">": 100 | op = Ast.BinaryOp.GreaterThan; break; 101 | case ">=": 102 | op = Ast.BinaryOp.GreaterOrEqual; break; 103 | case "<": 104 | op = Ast.BinaryOp.LessThan; break; 105 | case "<=": 106 | op = Ast.BinaryOp.LessOrEqual; break; 107 | case "==": 108 | op = Ast.BinaryOp.Equal; break; 109 | case "~=": 110 | op = Ast.BinaryOp.Different; break; 111 | default: 112 | throw new Exception("Invalid operator"); 113 | } 114 | 115 | ParseTreeNode right = node.ChildNodes[1].ChildNodes[1]; 116 | Ast.IExpression rexpr = ParseConcatOp(right); 117 | 118 | return new Ast.BinaryExpression() { Left = lexpr, Right = rexpr, Operation = op }; 119 | } 120 | throw new Exception("Invalid RelOp node"); 121 | } 122 | 123 | Ast.IExpression ParseConcatOp(ParseTreeNode node) 124 | { 125 | if (node.Term.Name == "ConcatOp") 126 | { 127 | ParseTreeNode left = node.ChildNodes[0]; 128 | Ast.IExpression lexpr = ParseAddOp(left); 129 | if (node.ChildNodes[1].ChildNodes.Count == 0) 130 | return lexpr; 131 | string opstring = node.ChildNodes[1].ChildNodes[0].Token.ValueString; 132 | Ast.BinaryOp op = Ast.BinaryOp.Concat; 133 | 134 | ParseTreeNode right = node.ChildNodes[1].ChildNodes[1]; 135 | Ast.IExpression rexpr = ParseAddOp(right); 136 | 137 | return new Ast.BinaryExpression() { Left = lexpr, Right = rexpr, Operation = op }; 138 | } 139 | throw new Exception("Invalid ConcatOp node"); 140 | } 141 | 142 | Ast.IExpression ParseAddOp(ParseTreeNode node) 143 | { 144 | if (node.Term.Name == "AddOp") 145 | { 146 | ParseTreeNode left = node.ChildNodes[0]; 147 | Ast.IExpression lexpr = ParseMulOp(left); 148 | if (node.ChildNodes[1].ChildNodes.Count == 0) 149 | return lexpr; 150 | string opstring = node.ChildNodes[1].ChildNodes[0].ChildNodes[0].Token.ValueString; 151 | Ast.BinaryOp op; 152 | switch (opstring) 153 | { 154 | case "+": 155 | op = Ast.BinaryOp.Addition; break; 156 | case "-": 157 | op = Ast.BinaryOp.Subtraction; break; 158 | default: 159 | throw new Exception("Invalid operator"); 160 | } 161 | 162 | ParseTreeNode right = node.ChildNodes[1].ChildNodes[1]; 163 | Ast.IExpression rexpr = ParseMulOp(right); 164 | 165 | return new Ast.BinaryExpression() { Left = lexpr, Right = rexpr, Operation = op }; 166 | } 167 | throw new Exception("Invalid AddOp node"); 168 | } 169 | 170 | Ast.IExpression ParseMulOp(ParseTreeNode node) 171 | { 172 | if (node.Term.Name == "MulOp") 173 | { 174 | ParseTreeNode left = node.ChildNodes[0]; 175 | Ast.IExpression lexpr = ParsePowerOp(left); 176 | if (node.ChildNodes[1].ChildNodes.Count == 0) 177 | return lexpr; 178 | string opstring = node.ChildNodes[1].ChildNodes[0].ChildNodes[0].Token.ValueString; 179 | Ast.BinaryOp op; 180 | switch (opstring) 181 | { 182 | case "*": 183 | op = Ast.BinaryOp.Multiplication; break; 184 | case "/": 185 | op = Ast.BinaryOp.Division; break; 186 | case "%": 187 | op = Ast.BinaryOp.Modulo; break; 188 | default: 189 | throw new Exception("Invalid operator"); 190 | } 191 | 192 | ParseTreeNode right = node.ChildNodes[1].ChildNodes[1]; 193 | Ast.IExpression rexpr = ParsePowerOp(right); 194 | 195 | return new Ast.BinaryExpression() { Left = lexpr, Right = rexpr, Operation = op }; 196 | } 197 | throw new Exception("Invalid MulOp node"); 198 | } 199 | 200 | Ast.IExpression ParsePowerOp(ParseTreeNode node) 201 | { 202 | if (node.Term.Name == "PowerOp") 203 | { 204 | ParseTreeNode left = node.ChildNodes[0]; 205 | Ast.IExpression lexpr = ParseExpression(left); 206 | if (node.ChildNodes[1].ChildNodes.Count == 0) 207 | return lexpr; 208 | 209 | ParseTreeNode right = node.ChildNodes[1].ChildNodes[1]; 210 | Ast.IExpression rexpr = ParseExpression(right); 211 | 212 | return new Ast.BinaryExpression() { Left = lexpr, Right = rexpr, Operation = Ast.BinaryOp.Power }; 213 | } 214 | throw new Exception("Invalid PowerOp node"); 215 | } 216 | #endregion 217 | 218 | #region Statements 219 | Ast.FunctionCall ParseFunctionCall(ParseTreeNode node) 220 | { 221 | if (node.Term.Name == "FunctionCall") 222 | { 223 | Ast.IExpression expr = ParsePrefix(node.ChildNodes[0]); 224 | Ast.FunctionCall call = new Ast.FunctionCall(); 225 | call.Arguments = new List(); 226 | call.Function = expr; 227 | 228 | var root = node.ChildNodes[1].ChildNodes[0]; 229 | if (root.ChildNodes.Count != 0) 230 | { 231 | root = root.ChildNodes[0]; 232 | while (true) 233 | { 234 | call.Arguments.Add(ParseExpression(root.ChildNodes[0])); 235 | if (root.ChildNodes.Count == 1) 236 | break; 237 | else 238 | root = root.ChildNodes[1]; 239 | } 240 | } 241 | return call; 242 | } 243 | throw new Exception("Invalid FunctionCall node"); 244 | } 245 | 246 | Ast.FunctionCall ParseOopCall(ParseTreeNode node) 247 | { 248 | if (node.Term.Name == "OopCall") 249 | { 250 | Ast.IExpression expr = ParsePrefix(node.ChildNodes[0]); 251 | string name = node.ChildNodes[1].Token.ValueString; 252 | Ast.FunctionCall call = new Ast.FunctionCall(); 253 | call.Arguments.Add(expr); 254 | call.Function = new Ast.Variable() { Name = name, Prefix = expr }; 255 | 256 | var root = node.ChildNodes[2].ChildNodes[0]; 257 | if (root.ChildNodes.Count != 0) 258 | { 259 | root = root.ChildNodes[0]; 260 | while (true) 261 | { 262 | call.Arguments.Add(ParseExpression(root.ChildNodes[0])); 263 | if (root.ChildNodes.Count == 1) 264 | break; 265 | else 266 | root = root.ChildNodes[1]; 267 | } 268 | } 269 | return call; 270 | } 271 | throw new Exception("Invalid OopCall node"); 272 | } 273 | 274 | Ast.Assignment ParseAssign(ParseTreeNode node) 275 | { 276 | if (node.Term.Name == "Assignment") 277 | { 278 | Ast.Assignment assign = new Ast.Assignment(); 279 | 280 | var left = node.ChildNodes[0]; 281 | var right = node.ChildNodes[1]; 282 | 283 | assign.Variables.Add(ParseVariable(left.ChildNodes[0])); 284 | assign.Expressions.Add(ParseExpression(right.ChildNodes[0])); 285 | 286 | left = left.ChildNodes[1]; 287 | right = right.ChildNodes[1]; 288 | 289 | while (left.ChildNodes.Count > 0) 290 | { 291 | left = left.ChildNodes[0]; 292 | assign.Variables.Add(ParseVariable(left.ChildNodes[0])); 293 | left = left.ChildNodes[1]; 294 | } 295 | 296 | while (right.ChildNodes.Count > 0) 297 | { 298 | right = right.ChildNodes[0]; 299 | assign.Expressions.Add(ParseExpression(right.ChildNodes[0])); 300 | right = right.ChildNodes[1]; 301 | } 302 | 303 | return assign; 304 | } 305 | throw new Exception("Invalid Assignment node"); 306 | } 307 | 308 | Ast.LocalAssignment ParseLocalAssign(ParseTreeNode node) 309 | { 310 | if (node.Term.Name == "LocalAssignment") 311 | { 312 | Ast.LocalAssignment assign = new Ast.LocalAssignment(); 313 | 314 | 315 | var child = node.ChildNodes[1]; 316 | 317 | if (child.ChildNodes[0].Term.Name == "LocalFunction") 318 | { 319 | child = child.ChildNodes[0]; 320 | 321 | var argsNode = child.ChildNodes[2]; 322 | var blockNode = child.ChildNodes[3]; 323 | 324 | assign.Names.Add(child.ChildNodes[1].Token.ValueString); 325 | var func = new Ast.FunctionDefinition(); 326 | 327 | if (argsNode.ChildNodes.Count > 0) 328 | { 329 | argsNode = argsNode.ChildNodes[0]; 330 | while (argsNode.ChildNodes.Count > 0) 331 | { 332 | string ident = argsNode.ChildNodes[0].Token.ValueString; 333 | func.Arguments.Add(new Ast.Argument() { Name = ident }); 334 | if (argsNode.ChildNodes.Count == 1) 335 | break; 336 | argsNode = argsNode.ChildNodes[1]; 337 | } 338 | } 339 | 340 | func.Body = ParseBlock(blockNode); 341 | 342 | assign.Values.Add(func); 343 | return assign; 344 | } 345 | 346 | var left = child.ChildNodes[0]; 347 | var right = child.ChildNodes[1]; 348 | 349 | assign.Names.Add(left.ChildNodes[0].Token.ValueString); 350 | 351 | left = left.ChildNodes[1]; 352 | 353 | while (left.ChildNodes.Count > 0) 354 | { 355 | left = left.ChildNodes[0]; 356 | assign.Names.Add(left.ChildNodes[0].Token.ValueString); 357 | left = left.ChildNodes[1]; 358 | } 359 | 360 | while (right.ChildNodes.Count > 0) 361 | { 362 | right = right.ChildNodes[0]; 363 | assign.Values.Add(ParseExpression(right.ChildNodes[0])); 364 | right = right.ChildNodes[1]; 365 | } 366 | 367 | return assign; 368 | } 369 | throw new Exception("Invalid LocalAssignment node"); 370 | } 371 | 372 | Ast.ReturnStat ParseReturnStat(ParseTreeNode node) 373 | { 374 | if (node.Term.Name == "ReturnStat") 375 | { 376 | Ast.ReturnStat ret = new Ast.ReturnStat(); 377 | var child = node.ChildNodes[1]; 378 | while (child.ChildNodes.Count > 0) 379 | { 380 | child = child.ChildNodes[0]; 381 | ret.Expressions.Add(ParseExpression(child.ChildNodes[0])); 382 | child = child.ChildNodes[1]; 383 | } 384 | return ret; 385 | } 386 | throw new Exception("Invalid ReturnStat node"); 387 | } 388 | 389 | Ast.Block ParseDoBlock(ParseTreeNode node) 390 | { 391 | if (node.Term.Name == "DoBlock") 392 | return ParseBlock(node.ChildNodes[1]); 393 | throw new Exception("Invalid DoBlock node"); 394 | } 395 | 396 | Ast.RepeatStat ParseRepeat(ParseTreeNode node) 397 | { 398 | if (node.Term.Name == "Repeat") 399 | { 400 | Ast.Block block = ParseBlock(node.ChildNodes[1]); 401 | Ast.IExpression condition = ParseExpression(node.ChildNodes[3]); 402 | 403 | return new Ast.RepeatStat() 404 | { 405 | Block = block, 406 | Condition = condition 407 | }; 408 | } 409 | throw new Exception("Invalid Repeat node"); 410 | } 411 | 412 | Ast.WhileStat ParseWhile(ParseTreeNode node) 413 | { 414 | if (node.Term.Name == "While") 415 | { 416 | return new Ast.WhileStat() 417 | { 418 | Condition = ParseExpression(node.ChildNodes[1]), 419 | Block = ParseDoBlock(node.ChildNodes[2]) 420 | }; 421 | } 422 | throw new Exception("Invalid While node"); 423 | } 424 | 425 | Ast.IfStat ParseIf(ParseTreeNode node) 426 | { 427 | if (node.Term.Name == "If") 428 | { 429 | Ast.IExpression condition = ParseExpression(node.ChildNodes[1]); 430 | Ast.Block block = ParseBlock(node.ChildNodes[3]); 431 | 432 | Ast.IfStat If = new Ast.IfStat(); 433 | If.Block = block; 434 | If.Condition = condition; 435 | If.ElseIfs = new List(); 436 | 437 | ParseTreeNode ElseifNode = node.ChildNodes[4]; 438 | ParseTreeNode ElseNode = node.ChildNodes[5]; 439 | 440 | while (ElseifNode.ChildNodes.Count != 0) 441 | { 442 | var childnode = ElseifNode.ChildNodes[0]; 443 | Ast.IfStat elseif = new Ast.IfStat(); 444 | elseif.Condition = ParseExpression(childnode.ChildNodes[1]); 445 | elseif.Block = ParseBlock(childnode.ChildNodes[3]); 446 | 447 | If.ElseIfs.Add(elseif); 448 | 449 | ElseifNode = childnode.ChildNodes[4]; 450 | } 451 | 452 | if (ElseNode.ChildNodes.Count != 0) 453 | If.ElseBlock = ParseBlock(ElseNode.ChildNodes[0].ChildNodes[1]); 454 | 455 | return If; 456 | } 457 | throw new Exception("Invalid If node"); 458 | } 459 | 460 | Ast.Assignment ParseFunctionDecl(ParseTreeNode node) 461 | { 462 | if (node.Term.Name == "FunctionDecl") 463 | { 464 | Ast.IAssignable expr = ParseVariable(node.ChildNodes[1]); 465 | 466 | ParseTreeNode argsNode = node.ChildNodes[3].ChildNodes[0]; 467 | ParseTreeNode chunkNode = node.ChildNodes[4]; 468 | 469 | Ast.Block block = ParseBlock(chunkNode); 470 | Ast.FunctionDefinition def = new Ast.FunctionDefinition(); 471 | def.Arguments = new List(); 472 | 473 | var nameNode = node.ChildNodes[2]; 474 | if (nameNode.ChildNodes.Count > 0) 475 | { 476 | def.Arguments.Add(new Ast.Argument() { Name = "self" }); 477 | expr = new Ast.Variable() 478 | { 479 | Name = nameNode.ChildNodes[0].Token.ValueString, 480 | Prefix = expr 481 | }; 482 | } 483 | def.Body = block; 484 | if (argsNode.ChildNodes.Count > 0) 485 | { 486 | argsNode = argsNode.ChildNodes[0]; 487 | while (argsNode.ChildNodes.Count > 0) 488 | { 489 | string ident = argsNode.ChildNodes[0].Token.ValueString; 490 | def.Arguments.Add(new Ast.Argument() { Name = ident }); 491 | if (argsNode.ChildNodes.Count == 1) 492 | break; 493 | argsNode = argsNode.ChildNodes[1]; 494 | } 495 | } 496 | Ast.Assignment assign = new Ast.Assignment(); 497 | assign.Variables.Add(expr); 498 | assign.Expressions.Add(def); 499 | return assign; 500 | } 501 | throw new Exception("Invalid FunctionDecl node"); 502 | } 503 | #endregion 504 | 505 | #region Expressions 506 | Ast.UnaryExpression ParseUnaryExpr(ParseTreeNode node) 507 | { 508 | if (node.Term.Name == "UnaryExpr") 509 | { 510 | Ast.IExpression expr = ParseExpression(node.ChildNodes[1]); 511 | var opNode = node.ChildNodes[0].ChildNodes[0]; 512 | Ast.UnaryOp op = Ast.UnaryOp.Invert; 513 | switch (opNode.Token.ValueString) 514 | { 515 | case "not": 516 | op = Ast.UnaryOp.Negate; break; 517 | case "-": 518 | op = Ast.UnaryOp.Invert; break; 519 | case "#": 520 | op = Ast.UnaryOp.Length; break; 521 | } 522 | return new Ast.UnaryExpression() 523 | { 524 | Expression = expr, 525 | Operation = op 526 | }; 527 | } 528 | throw new Exception("Invalid UnaryExpr node"); 529 | } 530 | 531 | Ast.FunctionDefinition ParseFunctionDef(ParseTreeNode node) 532 | { 533 | if (node.Term.Name == "FunctionDef") 534 | { 535 | ParseTreeNode argsNode = node.ChildNodes[1].ChildNodes[0]; 536 | ParseTreeNode chunkNode = node.ChildNodes[2]; 537 | 538 | Ast.Block block = ParseBlock(chunkNode); 539 | Ast.FunctionDefinition def = new Ast.FunctionDefinition(); 540 | def.Body = block; 541 | def.Arguments = new List(); 542 | 543 | if (argsNode.ChildNodes.Count == 0) 544 | return def; 545 | if (argsNode.ChildNodes.Count > 0) 546 | { 547 | argsNode = argsNode.ChildNodes[0]; 548 | while (argsNode.ChildNodes.Count > 0) 549 | { 550 | string ident = argsNode.ChildNodes[0].Token.ValueString; 551 | def.Arguments.Add(new Ast.Argument() { Name = ident }); 552 | if (argsNode.ChildNodes.Count == 1) 553 | break; 554 | argsNode = argsNode.ChildNodes[1]; 555 | } 556 | } 557 | return def; 558 | } 559 | throw new Exception("Invalid FunctionDef node"); 560 | } 561 | 562 | Ast.IExpression ParsePrefix(ParseTreeNode node) 563 | { 564 | if (node.Term != null) 565 | { 566 | if (node.Term.Name == "Prefix") 567 | { 568 | ParseTreeNode child = node.ChildNodes[0]; 569 | if (child.Term.Name == "Variable") 570 | { 571 | return ParseVariable(child); 572 | } 573 | else if (child.Term.Name == "Expression") 574 | { 575 | return ParseExpression(child); 576 | } 577 | else if (child.Term.Name == "FunctionCall") 578 | { 579 | return ParseFunctionCall(child); 580 | } 581 | else if (child.Term.Name == "OopCall") 582 | { 583 | return ParseOopCall(child); 584 | } 585 | } 586 | } 587 | throw new Exception("Invalid Prefix node"); 588 | } 589 | 590 | Ast.IAssignable ParseVariable(ParseTreeNode node) 591 | { 592 | if (node.Term != null) 593 | { 594 | if (node.ChildNodes.Count == 1) 595 | { 596 | string name = node.ChildNodes[0].Token.ValueString; 597 | return new Ast.Variable() { Name = name }; 598 | } 599 | else 600 | { 601 | Ast.IExpression prefix = ParsePrefix(node.ChildNodes[0]); 602 | if (node.ChildNodes[1].Term.Name == "Expression") 603 | { 604 | Ast.IExpression index = ParseExpression(node.ChildNodes[1]); 605 | return new Ast.TableAccess() { Expression = prefix, Index = index }; 606 | } 607 | else 608 | { 609 | string name = node.ChildNodes[1].Token.ValueString; 610 | return new Ast.Variable() { Name = name, Prefix = prefix }; 611 | } 612 | } 613 | } 614 | throw new Exception("Invalid Variable node"); 615 | } 616 | 617 | Ast.TableConstructor ParseTableConstruct(ParseTreeNode node) 618 | { 619 | if (node.Term.Name == "TableConstruct") 620 | { 621 | if (node.ChildNodes.Count == 0) 622 | { 623 | return new Ast.TableConstructor() { Values = new Dictionary() }; 624 | } 625 | else 626 | { 627 | var child = node.ChildNodes[0]; 628 | Ast.TableConstructor t = new Ast.TableConstructor(); 629 | t.Values = new Dictionary(); 630 | 631 | int i = 1; 632 | while (true) 633 | { 634 | if (child.ChildNodes.Count == 0) 635 | break; 636 | 637 | var value = child.ChildNodes[0]; 638 | 639 | if (value.ChildNodes.Count == 1) 640 | { 641 | t.Values.Add(new Ast.NumberLiteral() { Value = i }, ParseExpression(value.ChildNodes[0])); 642 | i++; 643 | } 644 | else 645 | { 646 | var prefix = value.ChildNodes[0]; 647 | 648 | Ast.IExpression key; 649 | if (prefix.ChildNodes[0].Term.Name == "identifier") 650 | key = new Ast.StringLiteral() { Value = prefix.ChildNodes[0].Token.ValueString }; 651 | else 652 | key = ParseExpression(prefix.ChildNodes[0]); 653 | 654 | var expr = value.ChildNodes[1]; 655 | Ast.IExpression val = ParseExpression(expr); 656 | 657 | t.Values.Add(key, val); 658 | } 659 | 660 | //child = child.ChildNodes[1].ChildNodes[0]; 661 | child = child.ChildNodes[1]; 662 | if (child.ChildNodes.Count == 0) 663 | break; 664 | child = child.ChildNodes[0]; 665 | } 666 | return t; 667 | } 668 | } 669 | throw new Exception("Invalid TableConstruct node"); 670 | } 671 | 672 | Ast.IExpression ParseExpression(ParseTreeNode node) 673 | { 674 | if (node.Term.Name == "Expression") 675 | { 676 | ParseTreeNode child = node.ChildNodes[0]; 677 | if (child.Token != null && child.Token.Terminal is NumberLiteral) 678 | { 679 | return new Ast.NumberLiteral() { Value = (child.Token.Value is double ? (double)(child.Token.Value) : (int)(child.Token.Value)) }; 680 | } 681 | else if (child.Token != null && child.Token.Terminal is StringLiteral) 682 | { 683 | return new Ast.StringLiteral() { Value = (string)(child.Token.Value) }; 684 | } 685 | else if (child.Token != null && child.Token.Terminal is KeyTerm) 686 | { 687 | string val = child.Token.ValueString; 688 | if (val == "true") 689 | return new Ast.BoolLiteral() { Value = true }; 690 | else if (val == "false") 691 | return new Ast.BoolLiteral() { Value = false }; 692 | else if (val == "nil") 693 | return new Ast.NilLiteral(); 694 | } 695 | else if (child.Term != null && child.Term.Name == "Prefix") 696 | { 697 | return ParsePrefix(child); 698 | } 699 | else if (child.Term != null && child.Term.Name == "OrOp") 700 | { 701 | return ParseOrOp(child); 702 | } 703 | else if (child.Term != null && child.Term.Name == "FunctionDef") 704 | { 705 | return ParseFunctionDef(child); 706 | } 707 | else if (child.Term != null && child.Term.Name == "UnaryExpr") 708 | { 709 | return ParseUnaryExpr(child); 710 | } 711 | else if (child.Term != null && child.Term.Name == "TableConstruct") 712 | { 713 | return ParseTableConstruct(child); 714 | } 715 | else if (child.Term != null && child.Term.Name == "OopCall") 716 | { 717 | return ParseOopCall(child); 718 | } 719 | else if (child.Term != null && child.Term.Name == "Varargs") 720 | { 721 | return new Ast.VarargsLiteral(); 722 | } 723 | } 724 | throw new Exception("Invalid Expression node"); 725 | } 726 | #endregion 727 | 728 | Ast.Block ParseBlock(ParseTreeNode node) 729 | { 730 | Ast.Block block = new Ast.Block(); 731 | block.Statements = new List(); 732 | foreach (ParseTreeNode child in node.ChildNodes) 733 | { 734 | switch (child.Term.Name) 735 | { 736 | case "Assignment": 737 | block.Statements.Add(ParseAssign(child)); break; 738 | case "LocalAssignment": 739 | block.Statements.Add(ParseLocalAssign(child)); break; 740 | case "FunctionCall": 741 | block.Statements.Add(ParseFunctionCall(child)); break; 742 | case "ReturnStat": 743 | block.Statements.Add(ParseReturnStat(child)); break; 744 | case "BreakStat": 745 | block.Statements.Add(new Ast.BreakStat()); break; 746 | case "DoBlock": 747 | block.Statements.Add(ParseDoBlock(child)); break; 748 | case "If": 749 | block.Statements.Add(ParseIf(child)); break; 750 | case "While": 751 | block.Statements.Add(ParseWhile(child)); break; 752 | case "Repeat": 753 | block.Statements.Add(ParseRepeat(child)); break; 754 | case "FunctionDecl": 755 | block.Statements.Add(ParseFunctionDecl(child)); break; 756 | case "For": 757 | block.Statements.Add(ParseFor(child)); break; 758 | case "OopCall": 759 | block.Statements.Add(ParseOopCall(child)); break; 760 | case ";": 761 | break; 762 | default: 763 | throw new NotImplementedException("Node not yet implemented"); 764 | } 765 | } 766 | return block; 767 | } 768 | 769 | Ast.IStatement ParseFor(ParseTreeNode node) 770 | { 771 | if (node.Term.Name == "For") 772 | { 773 | var block = ParseDoBlock(node.ChildNodes[2]); 774 | var type = node.ChildNodes[1].ChildNodes[0]; 775 | if (type.Term.Name == "NumericFor") 776 | { 777 | var cycle = new Ast.NumericFor(); 778 | cycle.Block = block; 779 | cycle.Variable = type.ChildNodes[0].Token.ValueString; 780 | cycle.Var = ParseExpression(type.ChildNodes[1]); 781 | cycle.Limit = ParseExpression(type.ChildNodes[2]); 782 | cycle.Step = new Ast.NumberLiteral() { Value = 1 }; 783 | if (type.ChildNodes[3].ChildNodes.Count > 0) 784 | { 785 | var child = type.ChildNodes[3].ChildNodes[0]; 786 | cycle.Step = ParseExpression(child); 787 | } 788 | 789 | return cycle; 790 | } 791 | else 792 | { 793 | var cycle = new Ast.GenericFor(); 794 | cycle.Block = block; 795 | 796 | var nameList = type.ChildNodes[0]; 797 | var exprList = type.ChildNodes[2]; 798 | 799 | while (true) 800 | { 801 | var name = nameList.ChildNodes[0].Token.ValueString; 802 | cycle.Variables.Add(name); 803 | var child = nameList.ChildNodes[1]; 804 | if (child.ChildNodes.Count > 0) 805 | nameList = child.ChildNodes[0]; 806 | else 807 | break; 808 | } 809 | 810 | while (true) 811 | { 812 | var expr = ParseExpression(exprList.ChildNodes[0]); 813 | cycle.Expressions.Add(expr); 814 | var child = exprList.ChildNodes[1]; 815 | if (child.ChildNodes.Count > 0) 816 | exprList = child.ChildNodes[0]; 817 | else 818 | break; 819 | } 820 | 821 | return cycle; 822 | } 823 | } 824 | throw new Exception("Invalid For node"); 825 | } 826 | } 827 | } 828 | -------------------------------------------------------------------------------- /luablock/luablock/NetLua/LuaCompiler.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * See LICENSE file 3 | */ 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Linq.Expressions; 10 | using System.Reflection; 11 | 12 | using NetLua.Ast; 13 | 14 | namespace NetLua 15 | { 16 | public static class LuaCompiler 17 | { 18 | static Type LuaContext_Type = typeof(LuaContext); 19 | static Type LuaArguments_Type = typeof(LuaArguments); 20 | static Type LuaEvents_Type = typeof(LuaEvents); 21 | static Type LuaObject_Type = typeof(LuaObject); 22 | 23 | static MethodInfo LuaContext_Get = LuaContext_Type.GetMethod("Get"); 24 | static MethodInfo LuaContext_SetLocal = LuaContext_Type.GetMethod("SetLocal"); 25 | static MethodInfo LuaContext_SetGlobal = LuaContext_Type.GetMethod("SetGlobal"); 26 | static MethodInfo LuaContext_Set = LuaContext_Type.GetMethod("Set"); 27 | 28 | static MethodInfo LuaArguments_Concat = LuaArguments_Type.GetMethod("Concat"); 29 | static MethodInfo LuaArguments_Add = LuaArguments_Type.GetMethod("Add"); 30 | 31 | static ConstructorInfo LuaContext_New_parent = LuaContext_Type.GetConstructor(new[] { typeof(LuaContext) }); 32 | static ConstructorInfo LuaArguments_New = LuaArguments_Type.GetConstructor(new[] { typeof(LuaObject[]) }); 33 | static ConstructorInfo LuaArguments_New_arglist = LuaArguments_Type.GetConstructor(new[] { typeof(LuaArguments[]) }); 34 | static ConstructorInfo LuaArguments_New_void = LuaArguments_Type.GetConstructor(new Type[] { }); 35 | 36 | static MethodInfo LuaEvents_eq = LuaEvents_Type.GetMethod("eq_event", BindingFlags.NonPublic | BindingFlags.Static); 37 | static MethodInfo LuaEvents_concat = LuaEvents_Type.GetMethod("concat_event", BindingFlags.NonPublic | BindingFlags.Static); 38 | static MethodInfo LuaEvents_len = LuaEvents_Type.GetMethod("len_event", BindingFlags.NonPublic | BindingFlags.Static); 39 | static MethodInfo LuaEvents_toNumber = LuaEvents_Type.GetMethod("toNumber", BindingFlags.NonPublic | BindingFlags.Static); 40 | 41 | static MethodInfo LuaObject_Call = LuaObject_Type.GetMethod("Call", new[] { LuaArguments_Type }); 42 | static MethodInfo LuaObject_AsBool = LuaObject_Type.GetMethod("AsBool"); 43 | 44 | static LuaArguments VoidArguments = new LuaArguments(); 45 | 46 | #region Helpers 47 | static Expression ToNumber(Expression Expression) 48 | { 49 | return Expression.Call(LuaEvents_toNumber, Expression); 50 | } 51 | 52 | static Expression CreateLuaArguments(params Expression[] Expressions) 53 | { 54 | var array = Expression.NewArrayInit(typeof(LuaObject), Expressions); 55 | return Expression.New(LuaArguments_New, array); 56 | } 57 | 58 | static Expression GetFirstArgument(Expression Expression) 59 | { 60 | if (Expression.Type == typeof(LuaArguments)) 61 | return Expression.Property(Expression, "Item", Expression.Constant(0)); 62 | else 63 | return Expression.ArrayAccess(Expression, Expression.Constant(0)); 64 | } 65 | 66 | static Expression GetFirstArgumentAsBool(Expression Expression) 67 | { 68 | Expression e = GetFirstArgument(Expression); 69 | return Expression.Call(e, LuaObject_AsBool); 70 | } 71 | 72 | static Expression GetAsBool(Expression Expression) 73 | { 74 | return Expression.Call(Expression, LuaObject_AsBool); 75 | } 76 | 77 | static Expression GetArgument(Expression Expression, int n) 78 | { 79 | if (Expression.Type == typeof(LuaArguments)) 80 | return Expression.Property(Expression, "Item", Expression.Constant(n)); 81 | else 82 | return Expression.ArrayAccess(Expression, Expression.Constant(n)); 83 | } 84 | #endregion 85 | 86 | #region Expressions 87 | static Expression CompileBinaryExpression(Ast.BinaryExpression expr, Expression Context) 88 | { 89 | var left = CompileSingleExpression(expr.Left, Context); 90 | var right = CompileSingleExpression(expr.Right, Context); 91 | switch (expr.Operation) 92 | { 93 | case BinaryOp.Addition: 94 | return Expression.Add(left, right); 95 | case BinaryOp.And: 96 | return Expression.And(left, right); 97 | case BinaryOp.Concat: 98 | return Expression.Call(LuaEvents_concat, left, right); 99 | case BinaryOp.Different: 100 | return Expression.Negate(Expression.Call(LuaEvents_eq, left, right)); 101 | case BinaryOp.Division: 102 | return Expression.Divide(left, right); 103 | case BinaryOp.Equal: 104 | return Expression.Call(LuaEvents_eq, left, right); 105 | case BinaryOp.GreaterOrEqual: 106 | return Expression.GreaterThanOrEqual(left, right); 107 | case BinaryOp.GreaterThan: 108 | return Expression.GreaterThan(left, right); 109 | case BinaryOp.LessOrEqual: 110 | return Expression.LessThanOrEqual(left, right); 111 | case BinaryOp.LessThan: 112 | return Expression.LessThan(left, right); 113 | case BinaryOp.Modulo: 114 | return Expression.Modulo(left, right); 115 | case BinaryOp.Multiplication: 116 | return Expression.Multiply(left, right); 117 | case BinaryOp.Or: 118 | return Expression.Or(left, right); 119 | case BinaryOp.Power: 120 | return Expression.ExclusiveOr(left, right); 121 | case BinaryOp.Subtraction: 122 | return Expression.Subtract(left, right); 123 | default: 124 | throw new NotImplementedException(); 125 | } 126 | } 127 | 128 | static Expression CompileUnaryExpression(Ast.UnaryExpression expr, Expression Context) 129 | { 130 | var e = CompileSingleExpression(expr.Expression, Context); 131 | switch (expr.Operation) 132 | { 133 | case UnaryOp.Invert: 134 | return Expression.Negate(e); 135 | case UnaryOp.Negate: 136 | return Expression.Not(e); 137 | case UnaryOp.Length: 138 | return Expression.Call(LuaEvents_len, e); 139 | default: 140 | throw new NotImplementedException(); 141 | } 142 | } 143 | 144 | static Expression GetVariable(Ast.Variable expr, Expression Context) 145 | { 146 | if (expr.Prefix == null) 147 | return Expression.Call(Context, LuaContext_Get, Expression.Constant(expr.Name)); 148 | else 149 | { 150 | Expression p = CompileSingleExpression(expr.Prefix, Context); 151 | return Expression.Property(p, "Item", Expression.Convert(Expression.Constant(expr.Name), LuaObject_Type)); 152 | } 153 | } 154 | 155 | static Expression GetTableAccess(Ast.TableAccess expr, Expression Context) 156 | { 157 | var e = CompileSingleExpression(expr.Expression, Context); 158 | var i = CompileSingleExpression(expr.Index, Context); 159 | 160 | return Expression.Property(e, "Item", i); 161 | } 162 | 163 | // This function returns a Expression with type LuaArguments. Similar to CompileSingleExpression 164 | static Expression CompileExpression(IExpression expr, Expression Context) 165 | { 166 | if (expr is NumberLiteral) 167 | return CreateLuaArguments(Expression.Constant((LuaObject)((expr as NumberLiteral).Value))); 168 | if (expr is StringLiteral) 169 | return CreateLuaArguments(Expression.Constant((LuaObject)((expr as StringLiteral).Value))); 170 | if (expr is BoolLiteral) 171 | return CreateLuaArguments(Expression.Constant((LuaObject)((expr as BoolLiteral).Value))); 172 | if (expr is NilLiteral) 173 | return CreateLuaArguments(Expression.Constant(LuaObject.Nil)); 174 | 175 | if (expr is Ast.BinaryExpression) 176 | return CreateLuaArguments(CompileBinaryExpression(expr as Ast.BinaryExpression, Context)); 177 | if (expr is Ast.UnaryExpression) 178 | return CreateLuaArguments(CompileUnaryExpression(expr as Ast.UnaryExpression, Context)); 179 | if (expr is Ast.Variable) 180 | return CreateLuaArguments(GetVariable(expr as Ast.Variable, Context)); 181 | if (expr is Ast.TableAccess) 182 | return CreateLuaArguments(GetTableAccess(expr as Ast.TableAccess, Context)); 183 | if (expr is Ast.FunctionCall) 184 | return CompileFunctionCallExpr(expr as Ast.FunctionCall, Context); 185 | if (expr is Ast.FunctionDefinition) 186 | return CreateLuaArguments(CompileFunctionDef(expr as Ast.FunctionDefinition, Context)); 187 | if (expr is Ast.VarargsLiteral) 188 | return CompileVarargs(Context); 189 | if (expr is Ast.TableConstructor) 190 | return CreateLuaArguments(CompileTableConstructor(expr as Ast.TableConstructor, Context)); 191 | throw new NotImplementedException(); 192 | } 193 | 194 | // This function returns a Expression with type LuaObject. Similar to CompileExpression 195 | static Expression CompileSingleExpression(IExpression expr, Expression Context) 196 | { 197 | if (expr is NumberLiteral) 198 | return (Expression.Constant((LuaObject)((expr as NumberLiteral).Value))); 199 | if (expr is StringLiteral) 200 | return (Expression.Constant((LuaObject)((expr as StringLiteral).Value))); 201 | if (expr is BoolLiteral) 202 | return (Expression.Constant((LuaObject)((expr as BoolLiteral).Value))); 203 | if (expr is NilLiteral) 204 | return (Expression.Constant(LuaObject.Nil)); 205 | 206 | if (expr is Ast.BinaryExpression) 207 | return (CompileBinaryExpression(expr as Ast.BinaryExpression, Context)); 208 | if (expr is Ast.UnaryExpression) 209 | return (CompileUnaryExpression(expr as Ast.UnaryExpression, Context)); 210 | if (expr is Ast.Variable) 211 | return (GetVariable(expr as Ast.Variable, Context)); 212 | if (expr is Ast.TableAccess) 213 | return (GetTableAccess(expr as Ast.TableAccess, Context)); 214 | if (expr is Ast.FunctionCall) 215 | return GetFirstArgument(CompileFunctionCallExpr(expr as Ast.FunctionCall, Context)); 216 | if (expr is Ast.FunctionDefinition) 217 | return (CompileFunctionDef(expr as Ast.FunctionDefinition, Context)); 218 | if (expr is Ast.VarargsLiteral) 219 | return GetFirstArgument(CompileVarargs(Context)); 220 | if (expr is Ast.TableConstructor) 221 | return (CompileTableConstructor(expr as Ast.TableConstructor, Context)); 222 | throw new NotImplementedException(); 223 | } 224 | 225 | static Expression CompileTableConstructor(Ast.TableConstructor table, Expression Context) 226 | { 227 | var values = new List>(); 228 | int i = 0; 229 | var exprs = new List(); 230 | var type = typeof(Dictionary); 231 | var add = type.GetMethod("Add", new[] { LuaObject_Type, LuaObject_Type }); 232 | var variable = Expression.Parameter(type); 233 | var assign = Expression.Assign(variable, Expression.New(type.GetConstructor(new Type[] { }))); 234 | exprs.Add(assign); 235 | foreach (KeyValuePair kvpair in table.Values) 236 | { 237 | if (i == table.Values.Count - 1) 238 | { 239 | var k = CompileSingleExpression(kvpair.Key, Context); 240 | var v = CompileExpression(kvpair.Value, Context); 241 | var singlev = GetFirstArgument(v); 242 | var ifFalse = Expression.Call(variable, add, k, singlev); 243 | 244 | var counter = Expression.Parameter(typeof(int)); 245 | var value = Expression.Parameter(LuaArguments_Type); 246 | var @break = Expression.Label(); 247 | var breakLabel = Expression.Label(@break); 248 | var assignValue = Expression.Assign(value, v); 249 | var assignCounter = Expression.Assign(counter, Expression.Constant(0)); 250 | var incrementCounter = Expression.Assign(counter, Expression.Increment(counter)); 251 | var loopCondition = Expression.LessThan(counter, Expression.Property(v, "Length")); 252 | var addValue = Expression.Call(variable, add, 253 | Expression.Add(k, 254 | Expression.Call(LuaObject_Type.GetMethod("FromNumber"), 255 | Expression.Convert(counter, typeof(double) ) ) ), 256 | Expression.Property(value, "Item", counter) ); 257 | 258 | var check = Expression.IfThenElse(loopCondition, Expression.Block(addValue, incrementCounter), Expression.Break(@break)); 259 | var loopBody = Expression.Loop(check); 260 | var ifTrue = Expression.Block(new[] { counter, value }, assignCounter, assignValue, loopBody, breakLabel); 261 | 262 | var condition = Expression.IsTrue(Expression.Property(k, "IsNumber")); 263 | var ifblock = Expression.IfThenElse(condition, ifTrue, ifFalse); 264 | exprs.Add(ifblock); 265 | } 266 | else 267 | { 268 | var k = CompileSingleExpression(kvpair.Key, Context); 269 | var v = CompileSingleExpression(kvpair.Value, Context); 270 | 271 | exprs.Add(Expression.Call(variable, add, k, v)); 272 | } 273 | i++; 274 | } 275 | exprs.Add(Expression.Call(LuaObject_Type.GetMethod("FromTable"), variable)); 276 | var block = Expression.Block(new[] { variable }, exprs.ToArray()); 277 | return Expression.Invoke(Expression.Lambda>(block)); 278 | } 279 | 280 | static Expression CompileVarargs(Expression Context) 281 | { 282 | return Expression.Property(Context, "Varargs"); 283 | } 284 | 285 | static Expression CompileFunctionDef(FunctionDefinition def, Expression Context) 286 | { 287 | return Expression.Invoke(CompileFunction(def, Context)); 288 | } 289 | 290 | static Expression CompileFunctionCallExpr(Ast.FunctionCall expr, Expression Context) 291 | { 292 | var function = CompileSingleExpression(expr.Function, Context); 293 | var args = new List(); 294 | Expression lastArg = null; 295 | int i = 0; 296 | foreach (IExpression e in expr.Arguments) 297 | { 298 | if (i == expr.Arguments.Count - 1) 299 | lastArg = CompileExpression(e, Context); 300 | else 301 | args.Add(CompileSingleExpression(e, Context)); 302 | i++; 303 | } 304 | var arg = Expression.NewArrayInit(LuaObject_Type, args.ToArray()); 305 | var luaarg = Expression.New(LuaArguments_New, arg); 306 | 307 | if (lastArg == null) 308 | return Expression.Call(function, LuaObject_Call, luaarg); 309 | else 310 | return Expression.Call(function, LuaObject_Call, Expression.Call(luaarg, LuaArguments_Concat, lastArg)); 311 | } 312 | 313 | public static Expression> CompileFunction(Ast.FunctionDefinition func, Expression Context) 314 | { 315 | var exprs = new List(); 316 | 317 | var args = Expression.Parameter(LuaArguments_Type, "args"); 318 | var label = Expression.Label(LuaArguments_Type, "exit"); 319 | var @break = Expression.Label("break"); 320 | 321 | var scopeVar = Expression.Parameter(LuaContext_Type, "funcScope"); 322 | var assignScope = Expression.Assign(scopeVar, Expression.New(LuaContext_New_parent, Context)); 323 | 324 | #region Arguments init 325 | var len = Expression.Property(args, "Length"); 326 | var argLen = Expression.Constant(func.Arguments.Count); 327 | var argCount = Expression.Constant(func.Arguments.Count); 328 | 329 | var i = Expression.Parameter(typeof(int), "i"); 330 | var assignI = Expression.Assign(i, Expression.Constant(0)); 331 | var names = Expression.Parameter(typeof(string[]), "names"); 332 | var assignNames = Expression.Assign(names, Expression.Constant(Array.ConvertAll(func.Arguments.ToArray(), x => x.Name))); 333 | 334 | var innerCond = Expression.LessThan(i, argLen); 335 | var outerCond = Expression.LessThan(i, len); 336 | 337 | var innerIf = Expression.Call(scopeVar, LuaContext_SetLocal, Expression.ArrayAccess(names, i), Expression.Property(args, "Item", i)); 338 | var varargs = Expression.Property(scopeVar, "Varargs"); 339 | var innerElse = Expression.Call(varargs, LuaArguments_Add, Expression.Property(args, "Item", i)); 340 | 341 | var outerIf = Expression.Block(Expression.IfThenElse(innerCond, innerIf, innerElse), Expression.Assign(i, Expression.Add(i, Expression.Constant(1)))); 342 | var outerElse = Expression.Break(@break); 343 | 344 | var loopBody = Expression.IfThenElse(outerCond, outerIf, outerElse); 345 | var loop = Expression.Loop(loopBody); 346 | 347 | var breakLabel = Expression.Label(@break); 348 | #endregion 349 | 350 | var body = CompileBlock(func.Body, label, null, scopeVar); 351 | 352 | exprs.Add(assignScope); 353 | exprs.Add(assignI); 354 | exprs.Add(assignNames); 355 | exprs.Add(loop); 356 | exprs.Add(breakLabel); 357 | exprs.Add(body); 358 | exprs.Add(Expression.Label(label, Expression.Constant(VoidArguments))); 359 | 360 | var funcBody = Expression.Block(new[] { i, names, scopeVar }, exprs.ToArray()); 361 | 362 | var function = Expression.Lambda(funcBody, args); 363 | var returnValue = Expression.Lambda>(Expression.Convert(function, LuaObject_Type), null); 364 | 365 | return returnValue; 366 | } 367 | #endregion 368 | 369 | #region Statements 370 | static Expression SetVariable(Ast.Variable expr, Expression value, Expression Context) 371 | { 372 | if (expr.Prefix == null) 373 | return Expression.Call(Context, LuaContext_Set, Expression.Constant(expr.Name), value); 374 | else 375 | { 376 | var prefix = CompileSingleExpression(expr.Prefix, Context); 377 | var index = Expression.Constant((LuaObject)(expr.Name)); 378 | var set = Expression.Property(prefix, "Item", index); 379 | return Expression.Assign(set, value); 380 | } 381 | } 382 | 383 | static Expression CompileAssignment(Ast.Assignment assign, Expression Context) 384 | { 385 | var variable = Expression.Parameter(typeof(LuaArguments), "vars"); 386 | var stats = new List(); 387 | 388 | stats.Add(Expression.Assign(variable, Expression.New(LuaArguments_New_void))); 389 | foreach (IExpression expr in assign.Expressions) 390 | { 391 | var ret = CompileExpression(expr, Context); 392 | stats.Add(Expression.Call(variable, LuaArguments_Concat, ret)); 393 | } 394 | int i = 0; 395 | foreach (IAssignable var in assign.Variables) 396 | { 397 | var arg = GetArgument(variable, i); 398 | if (var is Ast.Variable) 399 | { 400 | var x = var as Ast.Variable; 401 | stats.Add(SetVariable(x, arg, Context)); 402 | } 403 | else if (var is Ast.TableAccess) 404 | { 405 | var x = var as Ast.TableAccess; 406 | 407 | var expression = CompileSingleExpression(x.Expression, Context); 408 | var index = CompileSingleExpression(x.Index, Context); 409 | 410 | var set = Expression.Property(expression, "Item", index); 411 | stats.Add(Expression.Assign(set, arg)); 412 | } 413 | i++; 414 | } 415 | 416 | return Expression.Block(new[] { variable }, stats.ToArray()); 417 | } 418 | 419 | static Expression CompileLocalAssignment(Ast.LocalAssignment assign, Expression Context) 420 | { 421 | var variable = Expression.Parameter(typeof(LuaArguments), "vars"); 422 | var stats = new List(); 423 | 424 | stats.Add(Expression.Assign(variable, Expression.New(LuaArguments_New_void))); 425 | foreach (IExpression expr in assign.Values) 426 | { 427 | var ret = CompileExpression(expr, Context); 428 | stats.Add(Expression.Call(variable, LuaArguments_Concat, ret)); 429 | } 430 | int i = 0; 431 | foreach (string var in assign.Names) 432 | { 433 | var arg = GetArgument(variable, i); 434 | stats.Add(Expression.Call(Context, LuaContext_SetLocal, Expression.Constant(var), arg)); 435 | i++; 436 | } 437 | 438 | return Expression.Block(new[] { variable }, stats.ToArray()); 439 | } 440 | 441 | static Expression CompileFunctionCallStat(Ast.FunctionCall call, Expression Context) 442 | { 443 | var variable = Expression.Parameter(typeof(LuaArguments), "vars"); 444 | var stats = new List(); 445 | 446 | stats.Add(Expression.Assign(variable, Expression.New(LuaArguments_New_void))); 447 | 448 | var expression = CompileSingleExpression(call.Function, Context); 449 | int i = 0; 450 | 451 | foreach (Ast.IExpression arg in call.Arguments) 452 | { 453 | if (i == call.Arguments.Count - 1) 454 | { 455 | var exp = CompileExpression(arg, Context); 456 | stats.Add(Expression.Call(variable, LuaArguments_Concat, exp)); 457 | } 458 | else 459 | { 460 | var exp = CompileSingleExpression(arg, Context); 461 | stats.Add(Expression.Call(variable, LuaArguments_Add, exp)); 462 | } 463 | i++; 464 | } 465 | stats.Add(Expression.Call(expression, LuaObject_Call, variable)); 466 | 467 | return Expression.Block(new[] { variable }, stats.ToArray()); 468 | } 469 | 470 | static Expression CompileBlock(Ast.Block block, LabelTarget returnTarget, LabelTarget breakTarget, Expression Context) 471 | { 472 | var exprs = new List(); 473 | var scope = Expression.Parameter(LuaContext_Type); 474 | exprs.Add(Expression.Assign(scope, Context)); 475 | 476 | foreach (IStatement s in block.Statements) 477 | { 478 | exprs.Add(CompileStatement(s, returnTarget, breakTarget, scope)); 479 | } 480 | 481 | return Expression.Block(new[] { scope }, exprs.ToArray()); 482 | } 483 | 484 | static Expression CompileWhileStat(Ast.WhileStat stat, LabelTarget returnTarget, Expression Context) 485 | { 486 | var cond = GetAsBool(CompileSingleExpression(stat.Condition, Context)); 487 | var breakTarget = Expression.Label("break"); 488 | var loopBody = CompileBlock(stat.Block, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context)); 489 | var condition = Expression.IfThenElse(cond, loopBody, Expression.Break(breakTarget)); 490 | var loop = Expression.Loop(condition); 491 | 492 | return Expression.Block(loop, Expression.Label(breakTarget)); 493 | } 494 | 495 | static Expression CompileIfStat(Ast.IfStat ifstat, LabelTarget returnTarget, LabelTarget breakTarget, Expression Context) 496 | { 497 | var condition = GetAsBool(CompileSingleExpression(ifstat.Condition, Context)); 498 | var block = CompileBlock(ifstat.Block, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context)); 499 | 500 | if (ifstat.ElseIfs.Count == 0) 501 | { 502 | if (ifstat.ElseBlock == null) 503 | return Expression.IfThen(condition, block); 504 | else 505 | { 506 | var elseBlock = CompileBlock(ifstat.ElseBlock, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context)); 507 | return Expression.IfThenElse(condition, block, elseBlock); 508 | } 509 | } 510 | else 511 | { 512 | Expression b = null; 513 | for (int i = ifstat.ElseIfs.Count - 1; i >= 0; i--) 514 | { 515 | IfStat branch = ifstat.ElseIfs[i]; 516 | var cond = GetAsBool(CompileSingleExpression(branch.Condition, Context)); 517 | var body = CompileBlock(branch.Block, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context)); 518 | if (b == null) 519 | { 520 | if (ifstat.ElseBlock == null) 521 | b = Expression.IfThen(cond, body); 522 | else 523 | { 524 | var elseBlock = CompileBlock(ifstat.ElseBlock, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context)); 525 | b = Expression.IfThenElse(cond, body, elseBlock); 526 | } 527 | } 528 | else 529 | b = Expression.IfThenElse(cond, body, b); 530 | } 531 | 532 | var tree = Expression.IfThenElse(condition, block, b); 533 | return tree; 534 | } 535 | } 536 | 537 | static Expression CompileReturnStat(Ast.ReturnStat ret, LabelTarget returnTarget, Expression Context) 538 | { 539 | var variable = Expression.Parameter(LuaArguments_Type); 540 | var body = new List(); 541 | var ctor = Expression.New(LuaArguments_New_void); 542 | body.Add(Expression.Assign(variable, ctor)); 543 | 544 | int i = 0; 545 | foreach (IExpression expr in ret.Expressions) 546 | { 547 | if (i == ret.Expressions.Count - 1) 548 | { 549 | var exp = CompileExpression(expr, Context); 550 | body.Add(Expression.Call(variable, LuaArguments_Concat, exp)); 551 | } 552 | else 553 | { 554 | var exp = CompileSingleExpression(expr, Context); 555 | body.Add(Expression.Call(variable, LuaArguments_Add, exp)); 556 | } 557 | } 558 | 559 | body.Add(Expression.Return(returnTarget, variable)); 560 | 561 | return Expression.Block(new[] { variable }, body.ToArray()); 562 | } 563 | 564 | static Expression CompileRepeatStatement(Ast.RepeatStat stat, LabelTarget returnTarget, Expression Context) 565 | { 566 | var ctx = Expression.New(LuaContext_New_parent, Context); 567 | var scope = Expression.Parameter(LuaContext_Type); 568 | var assignScope = Expression.Assign(scope, ctx); 569 | var @break = Expression.Label(); 570 | 571 | var condition = GetAsBool(CompileSingleExpression(stat.Condition, scope)); 572 | var body = CompileBlock(stat.Block, returnTarget, @break, scope); 573 | var check = Expression.IfThen(condition, Expression.Break(@break)); 574 | var loop = Expression.Loop(Expression.Block(body, check)); 575 | var block = Expression.Block(new[] { scope }, assignScope, loop, Expression.Label(@break)); 576 | return block; 577 | } 578 | 579 | static Expression CompileNumericFor(Ast.NumericFor stat, LabelTarget returnTarget, Expression Context) 580 | { 581 | var varValue = ToNumber(CompileSingleExpression(stat.Var, Context)); 582 | var limit = ToNumber(CompileSingleExpression(stat.Limit, Context)); 583 | var step = ToNumber(CompileSingleExpression(stat.Step, Context)); 584 | 585 | var var = Expression.Parameter(LuaObject_Type); 586 | var scope = Expression.Parameter(LuaContext_Type); 587 | var @break = Expression.Label(); 588 | var assignScope = Expression.Assign(scope, Expression.New(LuaContext_New_parent, Context)); 589 | var assignVar = Expression.Assign(var, varValue); 590 | 591 | var condition = 592 | Expression.Or( 593 | Expression.And( 594 | Expression.GreaterThan(step, Expression.Constant((LuaObject)0)), 595 | Expression.LessThanOrEqual(var, limit)), 596 | Expression.And( 597 | Expression.LessThanOrEqual(step, Expression.Constant((LuaObject)0)), 598 | Expression.GreaterThanOrEqual(var, limit))); 599 | var setLocalVar = Expression.Call(scope, LuaContext_SetLocal, Expression.Constant(stat.Variable), var); 600 | var innerBlock = CompileBlock(stat.Block, returnTarget, @break, scope); 601 | var sum = Expression.Assign(var, Expression.Add(var, step)); 602 | var check = Expression.IfThenElse(GetAsBool(condition), Expression.Block(setLocalVar, innerBlock, sum), Expression.Break(@break)); 603 | var loop = Expression.Loop(check); 604 | 605 | var body = Expression.Block(new[]{var, scope}, assignScope, assignVar, loop, Expression.Label(@break)); 606 | 607 | return body; 608 | } 609 | 610 | static Expression CompileGenericFor(Ast.GenericFor stat, LabelTarget returnTarget, Expression Context) 611 | { 612 | var body = new List(); 613 | var args = Expression.Parameter(LuaArguments_Type); 614 | var f = GetArgument(args, 0); 615 | var s = GetArgument(args, 1); 616 | var var = GetArgument(args, 2); 617 | var fVar = Expression.Parameter(LuaObject_Type); 618 | var sVar = Expression.Parameter(LuaObject_Type); 619 | var varVar = Expression.Parameter(LuaObject_Type); 620 | var scope = Expression.Parameter(LuaContext_Type); 621 | 622 | var @break = Expression.Label(); 623 | 624 | body.Add(Expression.Assign(args, Expression.New(LuaArguments_New_void))); 625 | foreach (IExpression expr in stat.Expressions) 626 | { 627 | body.Add(Expression.Call(args, LuaArguments_Concat,CompileExpression(expr, Context))); 628 | } 629 | body.Add(Expression.Assign(fVar, f)); 630 | body.Add(Expression.Assign(sVar, s)); 631 | body.Add(Expression.Assign(varVar, var)); 632 | 633 | body.Add(Expression.Assign(scope, Expression.New(LuaContext_New_parent, Context))); 634 | 635 | var res = Expression.Parameter(LuaArguments_Type); 636 | var buildArgs = Expression.New(LuaArguments_New, Expression.NewArrayInit(typeof(LuaObject), sVar, varVar)); 637 | var resAssign = Expression.Assign(res, Expression.Call(fVar, LuaObject_Call, buildArgs)); 638 | List exprs = new List(); 639 | exprs.Add(resAssign); 640 | for (int i = 0; i < stat.Variables.Count; i++) 641 | { 642 | var val = GetArgument(res, i); 643 | exprs.Add(Expression.Call(scope, LuaContext_SetLocal, Expression.Constant(stat.Variables[i]), val)); 644 | } 645 | var check = Expression.IfThen(Expression.Property(GetArgument(res, 0), "IsNil"), Expression.Break(@break)); 646 | exprs.Add(check); 647 | exprs.Add(Expression.Assign(varVar, GetFirstArgument(res))); 648 | exprs.Add(CompileBlock(stat.Block, returnTarget, @break, scope)); 649 | 650 | var loopBody = Expression.Block(new[] { res }, exprs.ToArray()); 651 | var loop = Expression.Loop(loopBody); 652 | body.Add(loop); 653 | body.Add(Expression.Label(@break)); 654 | 655 | var block = Expression.Block(new[] { args, scope, fVar, sVar, varVar }, body.ToArray()); 656 | 657 | return block; 658 | } 659 | 660 | static Expression CompileStatement(IStatement stat, LabelTarget returnTarget, LabelTarget breakTarget, Expression Context) 661 | { 662 | if (stat is Ast.Assignment) 663 | return CompileAssignment(stat as Ast.Assignment, Context); 664 | else if (stat is Ast.LocalAssignment) 665 | return CompileLocalAssignment(stat as Ast.LocalAssignment, Context); 666 | else if (stat is Ast.FunctionCall) 667 | return CompileFunctionCallStat(stat as Ast.FunctionCall, Context); 668 | else if (stat is Ast.Block) 669 | return CompileBlock(stat as Ast.Block, returnTarget, breakTarget, Context); 670 | else if (stat is Ast.IfStat) 671 | return CompileIfStat(stat as Ast.IfStat, returnTarget, breakTarget, Context); 672 | else if (stat is Ast.ReturnStat) 673 | return CompileReturnStat(stat as Ast.ReturnStat, returnTarget, Context); 674 | else if (stat is Ast.BreakStat) 675 | return Expression.Break(breakTarget); 676 | else if (stat is Ast.WhileStat) 677 | return CompileWhileStat(stat as Ast.WhileStat, returnTarget, Context); 678 | else if (stat is Ast.RepeatStat) 679 | return CompileRepeatStatement(stat as Ast.RepeatStat, returnTarget, Context); 680 | else if (stat is Ast.GenericFor) 681 | return CompileGenericFor(stat as Ast.GenericFor, returnTarget, Context); 682 | else if (stat is Ast.NumericFor) 683 | return CompileNumericFor(stat as Ast.NumericFor, returnTarget, Context); 684 | else 685 | throw new NotImplementedException(); 686 | } 687 | #endregion 688 | } 689 | } 690 | --------------------------------------------------------------------------------