Ejemplo n.º 1
0
        /// <summary>
        /// Resolve a symbol that is either a module or function.
        /// </summary>
        /// <param name="symscope"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MemberAccess ResolveSymbol(ISymbols symscope, string name)
        {
            var symbol = symscope.GetSymbol(name);

            // Case 2: Module.
            if (symbol.Category == SymbolCategory.Module)
            {
                var msym = symbol as SymbolModule;
                var mem  = new MemberAccess(MemberMode.Module);
                mem.Type  = msym.DataType;
                mem.Scope = msym.Scope;
                return(mem);
            }

            // Case 3: Function.
            if (symbol.Category == SymbolCategory.Func)
            {
                var sfunc = symbol as SymbolFunction;
                var mem   = new MemberAccess(MemberMode.FunctionScript);
                mem.MemberName = name;
                mem.Type       = sfunc.DataType;
                mem.Expr       = sfunc.FuncExpr as Expr;
                return(mem);
            }
            return(null);
        }
Ejemplo n.º 2
0
        private void PushParametersInScope(FunctionExpr expr)
        {
            // 1. Validate : any arguments.
            if (expr.ArgumentValues == null || expr.ArgumentValues.Count == 0)
            {
                return;
            }
            if (expr.Meta.Arguments == null || expr.Meta.Arguments.Count == 0)
            {
                return;
            }

            // 2. Check if there is an parameter named "arguments"
            var hasParameterNamedArguments = false;

            if (expr.Meta.Arguments != null && expr.Meta.Arguments.Count > 0)
            {
                if (expr.Meta.ArgumentsLookup.ContainsKey("arguments"))
                {
                    hasParameterNamedArguments = true;
                }
            }

            // 3. Get the symbolscope of the inside of the function and see if any statements.
            ISymbols symscope      = null;
            var      hasStatements = expr.Statements != null && expr.Statements.Count > 0;

            if (hasStatements)
            {
                symscope = expr.Statements[0].SymScope;
            }

            // 3. Add function arguments to scope
            for (var ndx = 0; ndx < expr.Meta.Arguments.Count; ndx++)
            {
                var val = expr.ArgumentValues[ndx] as LObject;
                var arg = expr.Meta.Arguments[ndx];

                // 4. Clone primitive datatypes.
                if (val.Type.IsPrimitiveType())
                {
                    var copied = val.Clone();
                    expr.ArgumentValues[ndx] = copied;
                }

                // 5. Now, set the memory value of the parameter.
                this.Ctx.Memory.SetValue(arg.Name, val);

                // 6. Finally, update the symbol type
                if (hasStatements)
                {
                    var sym = symscope.GetSymbol(arg.Name);
                    if (sym != null && val.Type.TypeVal == TypeConstants.Function &&
                        sym.Category != SymbolCategory.Func)
                    {
                        SymbolHelper.ResetSymbolAsFunction(symscope, arg.Name, val);
                    }
                }
            }

            // Finally add the arguments.
            // NOTE: Any extra arguments will be part of the implicit "arguments" array.
            if (!hasParameterNamedArguments)
            {
                var argArray = new LArray(expr.ArgumentValues);
                expr.Ctx.Memory.SetValue("arguments", argArray);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Resolve a symbol that is either a module or function.
        /// </summary>
        /// <param name="symscope"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MemberAccess ResolveSymbol(ISymbols symscope, string name)
        {
            var symbol = symscope.GetSymbol(name);

            // Case 2: Module.
            if (symbol.Category == SymbolCategory.Module)
            {
                var msym = symbol as SymbolModule;
                var mem = new MemberAccess(MemberMode.Module);
                mem.Type = msym.DataType;
                mem.Scope = msym.Scope;
                return mem;
            }

            // Case 3: Function.
            if (symbol.Category == SymbolCategory.Func)
            {
                var sfunc = symbol as SymbolFunction;
                var mem = new MemberAccess(MemberMode.FunctionScript);
                mem.MemberName = name;
                mem.Type = sfunc.DataType;
                mem.Expr = sfunc.FuncExpr as Expr;
                return mem;
            }
            return null;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Get the symbol associated with the supplied name
 /// </summary>
 /// <param name="name">Name of the symbol</param>
 /// <returns></returns>
 public Symbol GetSymbol(string name)
 {
     return(_current.GetSymbol(name));
 }