예제 #1
0
        public FunctionSyntax BuildFunction(string name, ReadOnlyArray<string> parameters, BodySyntax body, SourceLocation location)
        {
            IIdentifier identifier = null;

            if (name != null)
            {
                identifier = _scope.CreateIdentifier(name);
                _scope.DeclareIdentifier(name);
            }

            return new FunctionSyntax(
                identifier,
                parameters,
                body,
                location
            );
        }
예제 #2
0
            public Scope(Scope parent, BodySyntax body, IScriptBuilder scriptBuilder)
            {
                Parent = parent;
                TypeManager = new BoundTypeManager();

                if (body.Closure != null)
                {
                    _sourceClosure = body.Closure;

                    Closure = new BoundClosure(
                        FindParentClosure(),
                        body.Closure.Fields.Select(p => TypeManager.CreateType(p, BoundTypeKind.ClosureField)),
                        scriptBuilder
                    );
                }

                foreach (var variable in body.Identifiers)
                {
                    if (variable.Index.HasValue)
                    {
                        BoundClosure closure = null;
                        if (variable.Closure != null)
                            closure = GetClosure(variable.Closure);

                        _arguments.Add(variable, new BoundArgument(variable.Name, variable.Index.Value, closure));
                    }
                    else if (variable.Closure == null)
                    {
                        BoundLocalBase local;
                        if (variable.Type == IdentifierType.Global)
                            local = new BoundGlobal(variable.IsDeclared, TypeManager.CreateType(variable.Name, BoundTypeKind.Global));
                        else
                            local = new BoundLocal(variable.IsDeclared, TypeManager.CreateType(variable.Name, BoundTypeKind.Local));

                        _locals.Add(variable, local);
                    }
                }
            }
예제 #3
0
 public ProgramSyntax BuildProgram(BodySyntax body)
 {
     return new ProgramSyntax(body);
 }
예제 #4
0
        private JsFunction CompileFunction(string sourceCode, ReadOnlyArray<string> parameters)
        {
            BodySyntax newBody;

            if (sourceCode == String.Empty)
                newBody = new BodySyntax(BodyType.Function, ReadOnlyArray<SyntaxNode>.Empty, ReadOnlyArray<IIdentifier>.Empty, false, null);
            else
                newBody = ParseBlockStatement(sourceCode, parameters);

            var function = new FunctionSyntax(null, parameters, newBody, null);

            var scriptBuilder = TypeSystem.CreateScriptBuilder(null);
            var bindingVisitor = new BindingVisitor(scriptBuilder);

            var boundFunction = bindingVisitor.DeclareFunction(function);

            DefiniteAssignmentPhase.Perform(boundFunction);
            TypeMarkerPhase.Perform(boundFunction);

            boundFunction = SquelchPhase.Perform(boundFunction);

            return (JsFunction)Delegate.CreateDelegate(
                typeof(JsFunction),
                new CodeGenerator(Global, scriptBuilder).BuildFunction(boundFunction, sourceCode)
            );
        }