Mercurial Hosting > nabble
comparison src/nabble/naml/compiler/ScopedInterpreterImpl.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.naml.compiler; | |
2 | |
3 import java.util.Map; | |
4 | |
5 | |
6 final class ScopedInterpreterImpl<T> extends InterpreterImpl implements ScopedInterpreter<T> { | |
7 private final Map<String,Chunk> dynamicArgs; | |
8 | |
9 ScopedInterpreterImpl(CommandSpec cmdSpec,RunState runState,Map<Class,Integer> inStack,Map<String,?> args,Map<String,Chunk> dynamicArgs) { | |
10 super(cmdSpec,runState,inStack,args); | |
11 this.dynamicArgs = dynamicArgs; | |
12 } | |
13 | |
14 public Object getArg(final T scope,String param) { | |
15 if( scope == null ) | |
16 throw new TemplateRuntimeException("scope is null"); | |
17 if( !cmdSpec.scopedParameters.contains(param) ) | |
18 throw new RuntimeException("not scoped parameter"); | |
19 Chunk chunk = dynamicArgs.get(param); | |
20 if( chunk != null ) { | |
21 return new BlockWrapper(chunk,runState) { | |
22 void printTo(IPrintWriter out) { | |
23 int pushed = runState.push(scope); | |
24 try { | |
25 super.printTo(out); | |
26 } finally { | |
27 runState.pop(pushed); | |
28 } | |
29 } | |
30 }; | |
31 } | |
32 Object obj = getArg(param); | |
33 if( obj != null ) | |
34 return obj; | |
35 if( scope instanceof Primitive ) | |
36 return scope; | |
37 return runState.saveNamespace(scope); | |
38 } | |
39 | |
40 public String getArgString(T scope,String param) { | |
41 Object val = getArg(scope,param); | |
42 return val==null ? null : val.toString(); | |
43 } | |
44 | |
45 } |