Пример #1
0
        /// <summary>
        /// Adds a dependency using the "require" function (for loading Lua modules)
        /// </summary>
        /// <param name="c"></param>
        /// <param name="name">The name of the module to reference</param>
        /// <param name="varName">Optional. The variable name to assign to the module</param>
        public static void AddModuleDependency(Chunk c, string name, string varName = "")
        {
            if (varName == "")
            {
                // FunctionCall
                StringCallExpr call = new StringCallExpr();

                Variable require = c.Scope.GetVariable("require");
                VariableExpression v = new VariableExpression();
                if (require == null)
                {
                    require = c.Scope.CreateGlobal("require");
                    require.IsGlobal = true;
                }
                v.Var = require;
                call.Base = v;

                call.Arguments.Add(new StringExpr(name) { StringType = TokenType.DoubleQuoteString });

                c.Body.Insert(0, new CallStatement() { Expression = call, Scope = c.Scope });
            }
            else
            {
                // Assignment with FunctionCall
                AssignmentStatement a = new AssignmentStatement();

                StringCallExpr call = new StringCallExpr();
                call.Scope = c.Scope;
                Variable require = c.Scope.GetVariable("require");
                VariableExpression v = new VariableExpression();
                if (require == null)
                {
                    require = c.Scope.CreateGlobal("require");
                    require.IsGlobal = true;
                }
                v.Var = require;
                call.Base = v;
                call.Arguments.Add(new StringExpr(name) { StringType = TokenType.DoubleQuoteString });
                a.IsLocal = true; // local import
                a.Rhs.Add(call);

                Variable var = c.Scope.GetVariable(varName);
                VariableExpression v2 = new VariableExpression();
                if (var == null)
                {
                    var = c.Scope.CreateLocal(varName);
                }
                v2.Var = var;
                a.Lhs.Add(v2);

                c.Body.Insert(0, a);
            }
        }
Пример #2
0
        // clr.load "<assembly>"

        // ? clr.usingns(<ns>)

        // local __ns = clr.getns "<type.ns>"
        // local <type> = __ns.<type.typename>

        /// <summary>
        /// Adds a dependency to a .NET/CLR library using clr.load
        /// </summary>
        /// <param name="c"></param>
        /// <param name="assembly"></param>
        public static void AddClrDependency(Chunk c, string assembly)
        {
            // FunctionCall
            StringCallExpr call = new StringCallExpr();

            Variable require = c.Scope.GetVariable("clr");
            VariableExpression v = new VariableExpression();
            if (require == null)
            {
                require = c.Scope.CreateGlobal("clr");
                require.IsGlobal = true;
            }
            v.Var = require;
            MemberExpr me = new MemberExpr();
            me.Base = v;
            me.Indexer = ".";
            me.Ident = "load";
            call.Base = me;

            call.Arguments.Add(new StringExpr(assembly) { StringType = TokenType.DoubleQuoteString });

            c.Body.Insert(0, new CallStatement() { Expression = call, Scope = c.Scope });
        }
Пример #3
0
        /// <summary>
        /// Adds a dependency to a .NET/CLR library using clr.load,
        /// then it sets type to the type in the assembly.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="assembly"></param>
        /// <param name="type"></param>
        public static void AddClrDependency(Chunk c, string assembly, string type)
        {
            AddClrDependency(c, assembly);

            // Assignment with FunctionCall
            AssignmentStatement a = new AssignmentStatement();

            CallExpr call = new CallExpr();
            call.Scope = c.Scope;
            Variable require = c.Scope.GetVariable("clr");
            VariableExpression v = new VariableExpression();
            if (require == null)
            {
                require = c.Scope.CreateGlobal("clr");
                require.IsGlobal = true;
            }

            string name = "", varName = "";

            if (type.Contains('.'))
            {
                name = type.Substring(0, type.LastIndexOf('.'));
                varName = type.Substring(type.LastIndexOf('.') + 1);
            }
            else
            {
                name = assembly;
                varName = type;
            }

            v.Var = require;
            MemberExpr me = new MemberExpr();
            me.Base = v;
            me.Indexer = ".";
            me.Ident = "getns";
            call.Base = me;
            call.Arguments.Add(new StringExpr(name) { StringType = TokenType.DoubleQuoteString });
            a.IsLocal = true; // local import
            MemberExpr me2 = new MemberExpr();
            me2.Base = call;
            me2.Indexer = ".";
            me2.Ident = varName;
            a.Rhs.Add(me2);

            Variable var = c.Scope.GetVariable(varName);
            VariableExpression v2 = new VariableExpression();
            if (var == null)
            {
                var = c.Scope.CreateLocal(varName);
            }
            v2.Var = var;
            a.Lhs.Add(v2);

            // Insert after the load
            c.Body.Insert(1, a);
        }
Пример #4
0
        Expression ParsePrimaryExpr(Scope c)
        {
            //Console.WriteLine(tok.Peek().Type + " " + tok.Peek().Data);
            if (reader.ConsumeSymbol('('))
            {
                Expression ex = ParseExpr(c);
                if (!reader.ConsumeSymbol(')'))
                    error("')' expected");

                // save the information about parenthesized expressions somewhere
                ex.ParenCount = ex.ParenCount + 1;
                return ex;
            }
            else if (reader.Is(TokenType.Ident))
            {
                Token id = reader.Get();
                VariableExpression v = new VariableExpression();
                Variable var = c.GetLocal(id.Data);
                if (var == null)
                {
                    var = c.GetGlobal(id.Data);
                    if (var == null)
                    {
                        v.Var = c.CreateGlobal(id.Data);
                    }
                    else
                    {
                        v.Var = var;
                        v.Var.References++;
                    }
                }
                else
                {
                    v.Var = var;
                    v.Var.References++;
                }
                return v;
            }
            else
                error("primary expression expected");

            return null; // satisfy the C# compiler, but this will never happen
        }