From 0dc774db21f528fd6e611926f811cd98479034c5 Mon Sep 17 00:00:00 2001 From: Juraj Kirchheim Date: Sun, 24 Dec 2017 12:23:10 +0100 Subject: [PATCH] Add support for scoping. --- src/tink/macro/Exprs.hx | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/tink/macro/Exprs.hx b/src/tink/macro/Exprs.hx index ca0c2d9..8bcca04 100644 --- a/src/tink/macro/Exprs.hx +++ b/src/tink/macro/Exprs.hx @@ -311,10 +311,39 @@ class Exprs { static public inline function resolve(s:String, ?pos) return drill(s.split('.'), pos); + + static var scopes = new Array>(); + + static function inScope(a:Array, f:Void->T) { + scopes.push(a); + + inline function leave() + scopes.pop(); + try { + var ret = f(); + leave(); + return ret; + } + catch (e:Dynamic) { + leave(); + return Error.rethrow(e); + } + } + + static public function scoped(f:Void->T) + return inScope([], f); + + static public function inSubScope(f:Void->T, a:Array) + return inScope(switch scopes[scopes.length - 1] { + case null: a; + case v: v.concat(a); + }, f); static public function typeof(expr:Expr, ?locals) return try { + if (locals == null) + locals = scopes[scopes.length - 1]; if (locals != null) expr = [EVars(locals).at(expr.pos), expr].toMBlock(expr.pos); Success(Context.typeof(expr));