Example #1
0
        private void Bind(TotemAst unboundAst)
        {
            Assert.NotNull(unboundAst);

            _currentScope = _globalScope = unboundAst;
            _finallyCount.Add(0);

            // Find all scopes and variables
            unboundAst.Walk(this);

            // Bind
            foreach (var scope in _scopes)
            {
                scope.Bind(this);
            }

            // Bind the globals
            unboundAst.Bind(this);

            // Finish Binding w/ outer most scopes first.
            for (int i = _scopes.Count - 1; i >= 0; i--)
            {
                _scopes[i].FinishBind(this);
            }

            // Finish the globals
            unboundAst.FinishBind(this);

            // Run flow checker
            foreach (var scope in _scopes)
            {
                //FlowChecker.Check(totemCode);
            }
        }
Example #2
0
        internal static void BindAst(TotemAst ast, CompilerContext context)
        {
            Assert.NotNull(ast, context);

            TotemNameBinder binder = new TotemNameBinder(context);
            binder.Bind(ast);
        }
        public RuntimeScriptCode(TotemAst ast, CodeContext codeContext)
            : base(ast)
        {
            Debug.Assert(codeContext.GlobalScope.GetExtension(codeContext.LanguageContext.ContextId) != null);
            Debug.Assert(ast.Type == typeof(Expression<Func<FunctionCode, object>>));

            _optimizedContext = codeContext;
        }
 public override LightLambdaExpression ReduceAst(TotemAst instance, string name)
 {
     return Utils.LightLambda<LookupCompilationDelegate>(
         typeof(object),
         Utils.Convert(instance.ReduceWorker(), typeof(object)),
         name,
         TotemAst._arrayFuncParams
     );
 }
 public override LightLambdaExpression ReduceAst(TotemAst instance, string name)
 {
     return Utils.LightLambda<Func<FunctionCode, object>>(
             typeof(object),
             Expression.Block(
                 new[] { TotemAst._globalArray, TotemAst._globalContext },
                 Expression.Assign(TotemAst._globalArray, instance.GlobalArrayInstance),
                 Expression.Assign(TotemAst._globalContext, Expression.Constant(instance.ModuleContext.GlobalContext)),
                 Utils.Convert(instance.ReduceWorker(), typeof(object))
             ),
             name,
             new[] { TotemAst._functionCode }
         );
 }
 public override void PrepareScope(TotemAst ast, ReadOnlyCollectionBuilder<ParameterExpression> locals, List<Expression> init)
 {
     locals.Add(TotemAst._globalArray);
     init.Add(Expression.Assign(TotemAst._globalArray, ast._arrayExpression));
 }
Example #7
0
        private LookupCompilationDelegate _target, _tracingTarget; // lazily compiled targets

        public TotemScriptCode(TotemAst ast)
            : base(ast)
        {
            Assert.NotNull(ast);
            Debug.Assert(ast.Type == typeof(Expression<LookupCompilationDelegate>));
        }
Example #8
0
 public override void PostWalk(TotemAst node)
 {
     // Do not add the global suite to the list of processed nodes,
     // the publishing must be done after the class local binding.
     Debug.Assert(_currentScope == node);
     _currentScope = _currentScope.Parent;
     _finallyCount.RemoveAt(_finallyCount.Count - 1);
 }
		// TotemAst
		public override bool Walk(TotemAst node)
		{
			node.Parent = _currentScope;
			
			return base.Walk(node);
		}
Example #10
0
 internal override void RewriteBody(TotemAst.LookupVisitor visitor)
 {
     _funcCode = null;
 }
Example #11
0
 internal virtual void RewriteBody(TotemAst.LookupVisitor visitor)
 { }
Example #12
0
 public virtual ScriptCode MakeScriptCode(TotemAst ast)
 {
     return new RuntimeScriptCode(ast, ast.ModuleContext.GlobalContext);
 }
 public RunnableScriptCode(TotemAst ast)
     : base(ast.SourceUnit)
 {
     _ast = ast;
 }
Example #14
0
 public void SetLoc(TotemAst globalParent, IndexSpan span)
 {
     _span = span;
     _parent = globalParent;
 }
Example #15
0
        public TotemAst ParseInteractiveCode(out ScriptCodeParseResult properties)
        {
            bool parsingMultiLineCmpdStmt;
            bool isEmptyStmt = false;

            properties = ScriptCodeParseResult.Complete;

            _globalParent = new TotemAst(false, _languageFeatures, true, _context);
            StartParsing();
            Stmt ret = InternalParseInteractiveInput(out parsingMultiLineCmpdStmt, out isEmptyStmt);

            if (_errorCode == 0)
            {
                if (isEmptyStmt)
                {
                    properties = ScriptCodeParseResult.Empty;
                }
                else if (parsingMultiLineCmpdStmt)
                {
                    properties = ScriptCodeParseResult.IncompleteStatement;
                }

                if (isEmptyStmt)
                {
                    return null;
                }

                return FinishParsing(ret);
            }
            else
            {
                if ((_errorCode & ErrorCodes.IncompleteMask) != 0)
                {
                    if ((_errorCode & ErrorCodes.IncompleteToken) != 0)
                    {
                        properties = ScriptCodeParseResult.IncompleteToken;
                        return null;
                    }

                    if ((_errorCode & ErrorCodes.IncompleteStatement) != 0)
                    {
                        if (parsingMultiLineCmpdStmt)
                        {
                            properties = ScriptCodeParseResult.IncompleteStatement;
                        }
                        else
                        {
                            properties = ScriptCodeParseResult.IncompleteToken;
                        }
                    }
                }

                properties = ScriptCodeParseResult.Invalid;
                return null;
            }
        }
Example #16
0
 internal TotemAst ParseTopExpression()
 {
     try
     {
         _globalParent = new TotemAst(false, _languageFeatures, false, _context);
         ReturnStmt ret = new ReturnStmt(ParseExpr());
         ret.SetLoc(_globalParent, 0, 0);
         return FinishParsing(ret);
     }
     catch (BadSourceException bse)
     {
         throw BadSourceError(bse);
     }
 }
Example #17
0
        internal TotemAst ParseSingleStatement()
        {
            try
            {
                _globalParent = new TotemAst(false, _languageFeatures, true, _context);
                StartParsing();

                MaybeEatNewLine();
                Stmt statement = ParseStmt();
                EatEndOfInput();
                return FinishParsing(statement);
            }
            catch (BadSourceException bse)
            {
                throw BadSourceError(bse);
            }
        }
Example #18
0
        private TotemAst FinishParsing(Stmt ret)
        {
            var res = _globalParent;
            _globalParent = null;
            var lineLocs = _tokenizer.GetLineLocations();
            // update line mapping
            if (_sourceUnit.HasLineMapping)
            {
                List<int> newLineMapping = new List<int>();
                int last = 0;
                for (int i = 0; i < lineLocs.Length; i++)
                {
                    while (newLineMapping.Count < i)
                    {
                        newLineMapping.Add(last);
                    }
                    last = lineLocs[i] + 1;
                    newLineMapping.Add(lineLocs[i]);
                }

                lineLocs = newLineMapping.ToArray();
            }
            res.ParsingFinished(lineLocs, ret, _languageFeatures);

            return res;
        }
Example #19
0
        private TotemAst ParseFileWorker(bool makeModule, bool returnValue)
        {
            _globalParent = new TotemAst(makeModule, _languageFeatures, false, _context);
            StartParsing();

            List<Stmt> l = new List<Stmt>();
            List<string> imports = new List<string>();

            // import-statements are only allowed in the start of the module
            while (PeekToken(TokenType.KeywordImport))
            {
                imports.Add(ParseImport());
            }
            _importAllowed = false;

            while (true)
            {
                if (MaybeEat(TokenType.EndOfFile)) break;
                if (MaybeEatNewLine()) continue;

                Stmt s = ParseStmt();
                l.Add(s);
            }


            Stmt[] stmts = l.ToArray();
            string[] imps = imports.ToArray();

            if (returnValue && stmts.Length > 0)
            {
                ExprStmt exprStmt = stmts[stmts.Length - 1] as ExprStmt;
                if (exprStmt != null)
                {
                    var retStmt = new ReturnStmt(exprStmt.Expr);
                    stmts[stmts.Length - 1] = retStmt;
                    retStmt.SetLoc(_globalParent, exprStmt.Expr.IndexSpan);
                }
            }

            BlockStmt ret = new BlockStmt(stmts);
            ret.SetLoc(_globalParent, 0, GetEnd());
            return FinishParsing(ret);
        }
Example #20
0
 public LookupVisitor(TotemAst ast, Expression globalContext)
 {
     _globalContext = globalContext;
     _curScope = ast;
 }
Example #21
0
		// TotemAst
		public override bool Walk(TotemAst node) { return false; }
Example #22
0
 public void SetLoc(TotemAst globalParent, int start, int end)
 {
     _span = new IndexSpan(start, end > start ? end - start : start);
     _parent = globalParent;
 }
Example #23
0
		public override void PostWalk(TotemAst node) { }
        internal override void RewriteBody(TotemAst.LookupVisitor visitor)
        {
            _dlrBody = null;    // clear the cached body if we've been reduced

            Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
            FuncCodeExpr = funcCode;

            Body = new TotemAst.RewrittenBodyStmt(Body, visitor.Visit(Body));
        }
Example #25
0
		// TotemAst
		public virtual bool Walk(TotemAst node) { return true; }
Example #26
0
 public abstract LightLambdaExpression ReduceAst(TotemAst instance, string name);
Example #27
0
		public virtual void PostWalk(TotemAst node) { }
Example #28
0
 public virtual void PrepareScope(TotemAst ast, ReadOnlyCollectionBuilder<ParameterExpression> locals, List<Expression> init)
 {
 }
 public override ScriptCode MakeScriptCode(TotemAst ast)
 {
     return new TotemScriptCode(ast);
 }