public static MineDefineExecutable Compile(Stream input) { Console.WriteLine("Lexing"); var tokens = new MineDefineLexer(input).Lex(); Console.WriteLine("\nTokens:"); Console.WriteLine(string.Join(", ", new MineDefineLexer(input).Lex())); Console.WriteLine("\nParsing"); var ast = new MineDefineParser(tokens).Parse(); Console.WriteLine("\nAST:"); Console.WriteLine(ast.ToString()); Console.WriteLine("\nLowering"); ast = new StandaloneTransformSugar().Transform(ast); Console.WriteLine("Planning"); var layout = new MineDefineLayout().Layout(ast); return new MineDefineExecutable(ast,layout); }
public void ItParsesAFullProgram() { var input = @"@building: { @chair: { 2x1 @wood; 1,1 @wood; } @floor: { 10x10x1 @stone; up 1; 4,5 @chair; 10x10x4 @wood; } @roof: triangular-prisim 10x10x5 @wood; 1x1x10 @floor; top @roof; box 1x1x3 @wood; } @building;"; var tokens = new MineDefineLexer(new MemoryStream(Encoding.UTF8.GetBytes(input))).Lex(); var statements = new MineDefineParser(tokens).Parse().Statements; Assert.AreEqual(2,statements.Count); var buildingDef = (ElementDefinition)statements.First(); Assert.AreEqual("building",buildingDef.Name); Assert.AreEqual(6, buildingDef.Statements.Count); var chairDef = (ElementDefinition)buildingDef.Statements.First(); Assert.AreEqual("chair",chairDef.Name); Assert.AreEqual(2, chairDef.Statements.Count); var chairBase = (BuildByIdentifier) chairDef.Statements.First(); Assert.AreEqual(BuildShape.Box, chairBase.Shape); Assert.AreEqual(new Dimension(2,1,1), chairBase.Dimension); Assert.AreEqual("wood", chairBase.Identifier); var chairTop = (BuildByIdentifier) chairDef.Statements.Last(); Assert.AreEqual(BuildShape.Box, chairTop.Shape); Assert.AreEqual(new Location(1,1,0), chairTop.Location); Assert.AreEqual("wood", chairTop.Identifier); var floorDef = (ElementDefinition) buildingDef.Statements[1]; var floorCover = (BuildByIdentifier) floorDef.Statements.First(); Assert.AreEqual(BuildShape.Box, floorCover.Shape); Assert.AreEqual(new Dimension(10,10,1), floorCover.Dimension); Assert.AreEqual("stone", floorCover.Identifier); var buildingCall = (BuildByIdentifier)statements.Last();// etc Assert.AreEqual("building",buildingCall.Identifier); }