Mercurial Hosting > nabble
view src/nabble/naml/compiler/InterpreterImpl.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 source
package nabble.naml.compiler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Map; class InterpreterImpl implements Interpreter { private static final Logger logger = LoggerFactory.getLogger(Compiler.class); final CommandSpec cmdSpec; final RunState runState; private final Map<Class,Integer> inStack; private final Map<String,?> args; private final Encoder oldEncoder; InterpreterImpl(CommandSpec cmdSpec,RunState runState,Map<Class,Integer> inStack,Map<String,?> args) { this.cmdSpec = cmdSpec; this.runState = runState; this.inStack = inStack; this.args = args; this.oldEncoder = runState.getEncoder(); } public Object getArg(String param) { return args.get(param); } public String getArgString(String param) { return Template.stringValue( getArg(param) ); } public Template template() { return runState.template(); } public <T> T getFromStack(Class<T> cls) { Integer i = inStack.get(cls); if( i == null ) throw new RuntimeException(""+cls+" not found in stack"); return cls.cast(runState.getFromStack(i)); } public int callDepth() { return runState.callDepth(); } public final <T> T getArgAsNamespace(Class<T> cls,String param) { String s = getArgString(param); if( s == null ) return null; return cls.cast(runState.getNamespace(s)); } public boolean hasNamespace(String namespace) { return runState.hasNamespace(namespace); } public boolean isInCommandStack(String commandName) { return runState.isInCommandStack(commandName); } // from AbstractInterpreter public final boolean hasModule(String moduleName) { return runState.program().moduleNames().contains(moduleName); } public final boolean getArgAsBoolean(String param) throws BooleanFormatException { try { return Template.booleanValue( getArg(param) ); } catch(BooleanFormatException e) { throw new BooleanFormatException( newMsg(param,e) ); } } public final boolean getArgAsBoolean(String param,boolean defaultValue) throws BooleanFormatException { try { return Template.booleanValue( getArg(param), defaultValue ); } catch(BooleanFormatException e) { throw new BooleanFormatException( newMsg(param,e) ); } } public final int getArgAsInt(String param) throws NumberFormatException { try { return Template.intValue( getArg(param) ); } catch(NumberFormatException e) { throw new NumberFormatException( newMsg(param,e) ); } } public int getArgAsInt(String param, int defaultValue) throws NumberFormatException { try { return Template.intValue( getArg(param), defaultValue ); } catch(NumberFormatException e) { throw new NumberFormatException( newMsg(param,e) ); } } public final long getArgAsLong(String param) throws NumberFormatException { try { return Template.longValue( getArg(param) ); } catch(NumberFormatException e) { throw new NumberFormatException( newMsg(param,e) ); } } private static String newMsg(String param,Exception e) { return "For argument: \"" + param + "\" - " + e.getMessage(); } public String encode(String s) { return s==null ? null : getEncoder().encode(s); } public Encoder getEncoder() { return runState.getEncoder(); } public void setEncoder(Encoder encoder) { runState.setEncoder(encoder); } void close() { runState.setEncoder(oldEncoder); } }