From 6a70c4bc522a7b839f6a13d48e80358181b36065 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Mon, 8 Mar 2021 13:52:29 +0100 Subject: [PATCH] Add legacy output for itinero1.0 formats --- AspectedRouting.Test/LuaPrinterTest.cs | 4 +- AspectedRouting.Test/Snippets/SnippetTests.cs | 8 ++-- .../IO/LuaSkeleton/LuaSkeleton.Expressions.cs | 2 +- .../IO/LuaSkeleton/LuaSkeleton.Function.cs | 41 +++++++++++-------- AspectedRouting/IO/LuaSkeleton/LuaSkeleton.cs | 4 +- .../LuaPrinter1.RelationPreprocessor.cs | 2 +- AspectedRouting/IO/itinero1/LuaPrinter1.cs | 2 +- AspectedRouting/IO/itinero2/LuaPrinter2.cs | 2 +- AspectedRouting/IO/lua/memberOf.lua | 2 +- 9 files changed, 39 insertions(+), 28 deletions(-) diff --git a/AspectedRouting.Test/LuaPrinterTest.cs b/AspectedRouting.Test/LuaPrinterTest.cs index 54ca94c..940b9f3 100644 --- a/AspectedRouting.Test/LuaPrinterTest.cs +++ b/AspectedRouting.Test/LuaPrinterTest.cs @@ -22,7 +22,7 @@ namespace AspectedRouting.Test } ); - var luaPrinter = new LuaSkeleton(new Context()); + var luaPrinter = new LuaSkeleton(new Context(), false); var result = luaPrinter.MappingToLua(mapping); Assert.Equal( @@ -45,7 +45,7 @@ namespace AspectedRouting.Test ) } ); - var luaPrinter = new LuaSkeleton(new Context()); + var luaPrinter = new LuaSkeleton(new Context(), false); var result = luaPrinter.MappingToLua(mapping); Assert.Equal("{\n a = {\n b = 42\n }\n}", result); } diff --git a/AspectedRouting.Test/Snippets/SnippetTests.cs b/AspectedRouting.Test/Snippets/SnippetTests.cs index 801ad93..438edbe 100644 --- a/AspectedRouting.Test/Snippets/SnippetTests.cs +++ b/AspectedRouting.Test/Snippets/SnippetTests.cs @@ -15,7 +15,7 @@ namespace AspectedRouting.Test.Snippets public void DefaultSnippet_SimpleDefault_GetsLua() { var gen = new DefaultSnippet(); - var lua = new LuaSkeleton(new Context()); + var lua = new LuaSkeleton(new Context(), true); var code = gen.Convert(lua, "result", new List { new Constant("the_default_value"), Funcs.Id, @@ -29,7 +29,7 @@ namespace AspectedRouting.Test.Snippets public void FirstOfSnippet_SimpleFirstOf_GetLua() { var gen = new FirstMatchOfSnippet(); - var lua = new LuaSkeleton(new Context()); + var lua = new LuaSkeleton(new Context(), true); // FirstMatchOf: [a] -> (Tags -> [a]) -> Tags -> a @@ -77,7 +77,7 @@ namespace AspectedRouting.Test.Snippets } ); var gen = new SimpleMappingSnippet(mapping); - var code = gen.Convert(new LuaSkeleton(new Context()), "result", new List { + var code = gen.Convert(new LuaSkeleton(new Context(), true), "result", new List { new LuaLiteral(Typs.String, "tags.oneway") }); @@ -96,7 +96,7 @@ namespace AspectedRouting.Test.Snippets ); - var code = gen.Convert(new LuaSkeleton(new Context()), + var code = gen.Convert(new LuaSkeleton(new Context(), true), "result", new List() ); diff --git a/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Expressions.cs b/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Expressions.cs index 54b57ee..7a834b7 100644 --- a/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Expressions.cs +++ b/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Expressions.cs @@ -57,7 +57,7 @@ namespace AspectedRouting.IO.LuaSkeleton ).Invoke(bare)) { AddDep("memberOf"); - return "member_of(funcName, parameters, tags, result)"; + return "memberOf(funcName, parameters, tags, result)"; } diff --git a/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Function.cs b/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Function.cs index 65d69b3..a3436a5 100644 --- a/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Function.cs +++ b/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.Function.cs @@ -10,11 +10,9 @@ namespace AspectedRouting.IO.LuaSkeleton { public partial class LuaSkeleton { - public void AddFunction(AspectMetadata meta) { - if (_alreadyAddedFunctions.Contains(meta.Name)) - { + if (_alreadyAddedFunctions.Contains(meta.Name)) { // already added return; } @@ -29,10 +27,8 @@ namespace AspectedRouting.IO.LuaSkeleton var funcNameDeclaration = ""; - meta.Visit(e => - { - if (e is Function f && f.Name.Equals(Funcs.MemberOf.Name)) - { + meta.Visit(e => { + if (e is Function f && f.Name.Equals(Funcs.MemberOf.Name)) { funcNameDeclaration = $"\n local funcName = \"{meta.Name.AsLuaIdentifier()}\""; } @@ -40,13 +36,28 @@ namespace AspectedRouting.IO.LuaSkeleton }); var expression = meta.ExpressionImplementation; - if (expression.Types.First() is Curry c) { - expression = expression.Apply(new LuaLiteral(Typs.Tags, "tags")); - } var ctx = Context; - this._context = _context.WithAspectName(meta.Name); - var impl = string.Join("\n", + _context = _context.WithAspectName(meta.Name); + + var body = ""; + if (_useSnippets) { + if (expression.Types.First() is Curry c) { + expression = expression.Apply(new LuaLiteral(Typs.Tags, "tags")); + } + + body = Utils.Lines( + " local r = nil", + " " + Snippets.Convert(this, "r", expression).Indent(), + " return r" + ); + } + else { + body = " return " + ToLua(expression); + } + + + var impl = Utils.Lines( "--[[", meta.Description, "", @@ -59,13 +70,11 @@ namespace AspectedRouting.IO.LuaSkeleton "Returns values: ", "]]", "function " + meta.Name.AsLuaIdentifier() + "(parameters, tags, result)" + funcNameDeclaration, - " local r = nil", - " "+Snippets.Convert(this, "r", expression).Indent(), - " return r" , + body, "end" ); - this._context = ctx; + _context = ctx; _functionImplementations.Add(impl); } } diff --git a/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.cs b/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.cs index 2668676..fa6e7f7 100644 --- a/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.cs +++ b/AspectedRouting/IO/LuaSkeleton/LuaSkeleton.cs @@ -18,6 +18,7 @@ namespace AspectedRouting.IO.LuaSkeleton private readonly List _constants = new List(); private Context _context; + private readonly bool _useSnippets; private readonly HashSet _dependencies = new HashSet(); private readonly List _functionImplementations = new List(); @@ -32,9 +33,10 @@ namespace AspectedRouting.IO.LuaSkeleton public Context Context => _context; - public LuaSkeleton(Context context, bool staticTables = false) + public LuaSkeleton(Context context, bool useSnippets, bool staticTables = false) { _context = context; + _useSnippets = useSnippets; _staticTables = staticTables; } diff --git a/AspectedRouting/IO/itinero1/LuaPrinter1.RelationPreprocessor.cs b/AspectedRouting/IO/itinero1/LuaPrinter1.RelationPreprocessor.cs index 5fd6465..445962b 100644 --- a/AspectedRouting/IO/itinero1/LuaPrinter1.RelationPreprocessor.cs +++ b/AspectedRouting/IO/itinero1/LuaPrinter1.RelationPreprocessor.cs @@ -68,7 +68,7 @@ namespace AspectedRouting.IO.itinero1 func.Add(" end"); - if (usedParameters.Count() == 0) + if (!usedParameters.Any()) { // Every behaviour uses the default parameters for this one func.Add(" -- No parameter dependence for aspect " + calledInFunction); diff --git a/AspectedRouting/IO/itinero1/LuaPrinter1.cs b/AspectedRouting/IO/itinero1/LuaPrinter1.cs index fb2a378..3404fd4 100644 --- a/AspectedRouting/IO/itinero1/LuaPrinter1.cs +++ b/AspectedRouting/IO/itinero1/LuaPrinter1.cs @@ -26,7 +26,7 @@ namespace AspectedRouting.IO.itinero1 _context = context; _aspectTestSuites = aspectTestSuites; _profileTests = profileTests; - _skeleton = new LuaSkeleton.LuaSkeleton(context); + _skeleton = new LuaSkeleton.LuaSkeleton(context, false); _parameterPrinter = new LuaParameterPrinter(profile, _skeleton); } diff --git a/AspectedRouting/IO/itinero2/LuaPrinter2.cs b/AspectedRouting/IO/itinero2/LuaPrinter2.cs index ef7495e..5d26083 100644 --- a/AspectedRouting/IO/itinero2/LuaPrinter2.cs +++ b/AspectedRouting/IO/itinero2/LuaPrinter2.cs @@ -40,7 +40,7 @@ namespace AspectedRouting.IO.itinero2 List aspectTests, IEnumerable behaviourTestSuite, DateTime lastChangeTime) { - _skeleton = new LuaSkeleton.LuaSkeleton(context, false); + _skeleton = new LuaSkeleton.LuaSkeleton(context, true); _profile = profile; _behaviourName = behaviourName; _context = context; diff --git a/AspectedRouting/IO/lua/memberOf.lua b/AspectedRouting/IO/lua/memberOf.lua index 321a748..7ca56be 100644 --- a/AspectedRouting/IO/lua/memberOf.lua +++ b/AspectedRouting/IO/lua/memberOf.lua @@ -1,6 +1,6 @@ function memberOf(calledIn, parameters, tags, result) local k = "_relation:" .. calledIn - -- This tag is conventiently setup by all the preprocessors, which take the parameters into account + -- This tag is conveniently setup by all the preprocessors, which take the parameters into account local doesMatch = tags[k] if (doesMatch == "yes") then result.attributes_to_keep[k] = "yes"