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); } }
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)); }
private LookupCompilationDelegate _target, _tracingTarget; // lazily compiled targets public TotemScriptCode(TotemAst ast) : base(ast) { Assert.NotNull(ast); Debug.Assert(ast.Type == typeof(Expression<LookupCompilationDelegate>)); }
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); }
internal override void RewriteBody(TotemAst.LookupVisitor visitor) { _funcCode = null; }
internal virtual void RewriteBody(TotemAst.LookupVisitor visitor) { }
public virtual ScriptCode MakeScriptCode(TotemAst ast) { return new RuntimeScriptCode(ast, ast.ModuleContext.GlobalContext); }
public RunnableScriptCode(TotemAst ast) : base(ast.SourceUnit) { _ast = ast; }
public void SetLoc(TotemAst globalParent, IndexSpan span) { _span = span; _parent = globalParent; }
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; } }
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); } }
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); } }
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; }
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); }
public LookupVisitor(TotemAst ast, Expression globalContext) { _globalContext = globalContext; _curScope = ast; }
// TotemAst public override bool Walk(TotemAst node) { return false; }
public void SetLoc(TotemAst globalParent, int start, int end) { _span = new IndexSpan(start, end > start ? end - start : start); _parent = globalParent; }
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)); }
// TotemAst public virtual bool Walk(TotemAst node) { return true; }
public abstract LightLambdaExpression ReduceAst(TotemAst instance, string name);
public virtual void PostWalk(TotemAst node) { }
public virtual void PrepareScope(TotemAst ast, ReadOnlyCollectionBuilder<ParameterExpression> locals, List<Expression> init) { }
public override ScriptCode MakeScriptCode(TotemAst ast) { return new TotemScriptCode(ast); }