diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/naml/compiler/ScopedInterpreterImpl.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,45 @@
+package nabble.naml.compiler;
+
+import java.util.Map;
+
+
+final class ScopedInterpreterImpl<T> extends InterpreterImpl implements ScopedInterpreter<T> {
+	private final Map<String,Chunk> dynamicArgs;
+
+	ScopedInterpreterImpl(CommandSpec cmdSpec,RunState runState,Map<Class,Integer> inStack,Map<String,?> args,Map<String,Chunk> dynamicArgs) {
+		super(cmdSpec,runState,inStack,args);
+		this.dynamicArgs = dynamicArgs;
+	}
+
+	public Object getArg(final T scope,String param) {
+		if( scope == null )
+			throw new TemplateRuntimeException("scope is null");
+		if( !cmdSpec.scopedParameters.contains(param) )
+			throw new RuntimeException("not scoped parameter");
+		Chunk chunk = dynamicArgs.get(param);
+		if( chunk != null ) {
+			return new BlockWrapper(chunk,runState) {
+				void printTo(IPrintWriter out) {
+					int pushed = runState.push(scope);
+					try {
+						super.printTo(out);
+					} finally {
+						runState.pop(pushed);
+					}
+				}
+			};
+		}
+		Object obj = getArg(param);
+		if( obj != null )
+			return obj;
+		if( scope instanceof Primitive )
+			return scope;
+		return runState.saveNamespace(scope);
+	}
+
+	public String getArgString(T scope,String param) {
+		Object val = getArg(scope,param);
+		return val==null ? null : val.toString();
+	}
+
+}