Exemple #1
0
        public override string Convert(LuaSkeleton.LuaSkeleton lua, string assignTo, List <IExpression> args)
        {
            var neededKeysExpr = args[0];
            var funcExpr       = args[1];
            var tagsExpr       = args[2];

            var result     = "";
            var neededKeys = lua.FreeVar("neededKeys");
            var tags       = "";

            if (tagsExpr is LuaLiteral literal)
            {
                tags = literal.Lua;
            }
            else
            {
                tags    = lua.FreeVar("tags");
                result += $"local {tags}";
                result += Snippets.Convert(lua, tags, tagsExpr);
            }


            result += $"local {neededKeys}\n";
            result += Snippets.Convert(lua, neededKeys, neededKeysExpr);
            var key   = lua.FreeVar("key");
            var value = lua.FreeVar("value");

            result += $"for _, {key} in ipairs({neededKeys}) do\n";
            result += $"   local {value} = {tags}[{key}]\n";
            result += $"   if ({value} == nil) then\n";
            result += $"       -- The value is nil, so mustmatch probably fails...\n";

            throw new System.NotImplementedException();
        }
        public override string Convert(LuaSkeleton.LuaSkeleton lua, string assignTo, List <IExpression> args)
        {
            // Note how we totally throw away args[0]
            var tagsToken = args[1];

            var tags   = "";
            var result = "";

            if (tagsToken is LuaLiteral lit)
            {
                tags = lit.Lua;
            }
            else
            {
                tags    = lua.FreeVar("tags");
                result += "local " + tags + "\n";
                result += Snippets.Convert(lua, tags, tagsToken);
            }

            var r = lua.FreeVar("relationValue");

            result += "local " + r + " = " + tags + "[\"_relation:" + lua.Context.AspectName.Replace(".", "_") + "\"]\n";
            result += assignTo + " = " + r + " == \"yes\"";
            return(result);
        }
        public override string Convert(LuaSkeleton.LuaSkeleton lua, string assignTo, List <IExpression> args)
        {
            var arg  = args[0];
            var v    = lua.FreeVar("v");
            var vLua = new LuaLiteral(Typs.String, v);

            var mappings = new List <string>();

            foreach (var kv in _mapping.StringToResultFunctions)
            {
                var f = kv.Value;
                if (f.Types.First() is Curry)
                {
                    f = f.Apply(vLua);
                }
                mappings.Add("if (" + v + " == \"" + kv.Key + "\") then\n    " + assignTo + " = " + lua.ToLua(f));
            }


            return(Utils.Lines(
                       "local " + v,
                       Snippets.Convert(lua, v, arg),
                       string.Join("\nelse", mappings),
                       "end"
                       ));
        }
        public override string Convert(LuaSkeleton.LuaSkeleton lua, string assignTo, List <IExpression> args)
        {
            var         fCond  = args[0].Optimize();
            var         fValue = args[1];
            IExpression fElse  = null;
            var         arg    = args[2];

            if (args.Count == 4)
            {
                arg   = args[3];
                fElse = args[2];
            }

            var c      = lua.FreeVar("cond");
            var result = "";

            result += "local " + c + "\n";
            var condApplied = fCond.Apply(arg);
            var isString    = condApplied.Types.First().Equals(Typs.String);

            result += Snippets.Convert(lua, c, condApplied) + "\n";
            result += $"if ( {c} or {c} == \"yes\" ) then \n";
            result += "    " + Snippets.Convert(lua, assignTo, fValue.Apply(arg)).Indent();

            if (fElse != null)
            {
                result += "else\n";
                result += "    " + Snippets.Convert(lua, assignTo, fElse.Apply(arg)).Indent();
            }

            result += "end\n";
            return(result);
        }
Exemple #5
0
        public override string Convert(LuaSkeleton.LuaSkeleton lua, string assignTo, List <IExpression> args)
        {
            var actualArgs = new List <IExpression>();
            var mappings   = new List <Mapping>();

            if (UnApply(
                    UnApply(IsFunc(Funcs.StringStringToTags),
                            IsMapping(mappings)),
                    Assign(actualArgs)
                    ).Invoke(args[0]))
            {
                var actualArg = actualArgs.First();
                var mapping   = mappings.First();

                if (mapping.StringToResultFunctions.Count != 1)
                {
                    return(null);
                }

                var(key, func) = mapping.StringToResultFunctions.ToList().First();
                var result = "";
                var tags   = "";
                if (actualArg is LuaLiteral l)
                {
                    tags = l.Lua;
                }
                else
                {
                    tags    = lua.FreeVar("tags");
                    result += "local " + tags + "\n";
                    result += Snippets.Convert(lua, tags, actualArg);
                }



                var v = lua.FreeVar("value");
                result += "local " + v + " = " + tags + "[\"" + key + "\"]\n";
                result += Snippets.Convert(lua, assignTo, func.Apply(new LuaLiteral(Typs.String, v)));
                return(result);
            }

            return(null);
        }
        public override string Convert(LuaSkeleton.LuaSkeleton lua, string assignTo, List <IExpression> args)
        {
            // Multiply multiplies a list of values - we thus have to handle _each_ arg
            // Note: we get a single argument which is an expression resulting in a list of values
            var listToMultiply = args[0];
            var mappings       = new List <Mapping>();
            var arg            = new List <IExpression>();

            if (UnApply(UnApply(
                            IsFunc(Funcs.StringStringToTags),
                            IsMapping(mappings)),
                        Assign(arg)
                        ).Invoke(listToMultiply))
            {
                var mapping = mappings.First();

                var result     = assignTo + " = " + _neutralValue + "\n";
                var mappingArg = arg.First();
                if (!Equals(mappingArg.Types.First(), Typs.Tags))
                {
                    return(null);
                }

                string tags;
                if (mappingArg is LuaLiteral literal)
                {
                    tags = literal.Lua;
                }
                else
                {
                    tags    = lua.FreeVar("tags");
                    result += "local " + tags + " = nil\n";
                    result += Snippets.Convert(lua, tags, mappingArg);
                }

                var m = lua.FreeVar("m");
                result += "    local " + m + " = nil\n";

                foreach (var(key, func) in mapping.StringToResultFunctions)
                {
                    result += "if (" + tags + "[\"" + key + "\"] ~= nil) then\n";
                    result += m + " = nil\n";
                    result += "    " +
                              Snippets.Convert(lua, m,
                                               func.Apply(new LuaLiteral(Typs.String, tags + "[\"" + key + "\"]"))).Indent() + "\n";
                    result += "\n\n    if (" + m + " ~= nil) then\n        " +
                              Combine(assignTo, m) +
                              "\n    end\n";
                    result += "end\n";
                }

                return(result);
            }


            var listDotArgs = new List <IExpression>();

            if (UnApply(
                    UnApply(IsFunc(Funcs.ListDot),
                            Assign(listDotArgs)),
                    Assign(arg)
                    ).Invoke(listToMultiply))
            {
                var listDotArg = arg.First();
                if (!(listDotArgs.First().Evaluate(lua.Context) is List <IExpression> functionsToApply))
                {
                    return(null);
                }

                var    result = "    " + assignTo + " = " + _neutralValue + "\n";
                string tags;
                if (listDotArg is LuaLiteral literal)
                {
                    tags = literal.Lua;
                }
                else
                {
                    tags    = lua.FreeVar("tags");
                    result += "    local " + tags + "\n";
                    result += Snippets.Convert(lua, tags, listDotArg);
                }

                var m = lua.FreeVar("m");
                result += "    local " + m + "\n";
                foreach (var func in functionsToApply)
                {
                    result += "    " + m + " = nil\n";
                    var subMapping = ExtractSubMapping(func);
                    if (subMapping != null)
                    {
                        var(key, f) = subMapping.Value;
                        var e = f.Apply(new LuaLiteral(Typs.String, tags + "[\"" + key + "\"]"));
                        e       = e.Optimize();
                        result += Snippets.Convert(lua, m, e).Indent();
                    }
                    else
                    {
                        result += Snippets.Convert(lua, m, func.Apply(new LuaLiteral(Typs.Tags, "tags")));
                    }


                    result += "\n\n    if (" + m + " ~= nil) then\n        " + Combine(assignTo, m) + "\n    end\n";
                }


                return(result);
            }


            throw new NotImplementedException();
        }