0
|
1 package nabble.naml.compiler;
|
|
2
|
|
3 import org.slf4j.Logger;
|
|
4 import org.slf4j.LoggerFactory;
|
|
5
|
|
6 import java.util.Map;
|
|
7
|
|
8
|
|
9 class InterpreterImpl implements Interpreter {
|
|
10 private static final Logger logger = LoggerFactory.getLogger(Compiler.class);
|
|
11
|
|
12 final CommandSpec cmdSpec;
|
|
13 final RunState runState;
|
|
14 private final Map<Class,Integer> inStack;
|
|
15 private final Map<String,?> args;
|
|
16 private final Encoder oldEncoder;
|
|
17
|
|
18 InterpreterImpl(CommandSpec cmdSpec,RunState runState,Map<Class,Integer> inStack,Map<String,?> args) {
|
|
19 this.cmdSpec = cmdSpec;
|
|
20 this.runState = runState;
|
|
21 this.inStack = inStack;
|
|
22 this.args = args;
|
|
23 this.oldEncoder = runState.getEncoder();
|
|
24 }
|
|
25
|
|
26 public Object getArg(String param) {
|
|
27 return args.get(param);
|
|
28 }
|
|
29
|
|
30 public String getArgString(String param) {
|
|
31 return Template.stringValue( getArg(param) );
|
|
32 }
|
|
33
|
|
34 public Template template() {
|
|
35 return runState.template();
|
|
36 }
|
|
37
|
|
38 public <T> T getFromStack(Class<T> cls) {
|
|
39 Integer i = inStack.get(cls);
|
|
40 if( i == null )
|
|
41 throw new RuntimeException(""+cls+" not found in stack");
|
|
42 return cls.cast(runState.getFromStack(i));
|
|
43 }
|
|
44
|
|
45 public int callDepth() {
|
|
46 return runState.callDepth();
|
|
47 }
|
|
48
|
|
49 public final <T> T getArgAsNamespace(Class<T> cls,String param) {
|
|
50 String s = getArgString(param);
|
|
51 if( s == null )
|
|
52 return null;
|
|
53 return cls.cast(runState.getNamespace(s));
|
|
54 }
|
|
55
|
|
56 public boolean hasNamespace(String namespace) {
|
|
57 return runState.hasNamespace(namespace);
|
|
58 }
|
|
59
|
|
60 public boolean isInCommandStack(String commandName) {
|
|
61 return runState.isInCommandStack(commandName);
|
|
62 }
|
|
63
|
|
64
|
|
65 // from AbstractInterpreter
|
|
66
|
|
67 public final boolean hasModule(String moduleName) {
|
|
68 return runState.program().moduleNames().contains(moduleName);
|
|
69 }
|
|
70
|
|
71 public final boolean getArgAsBoolean(String param)
|
|
72 throws BooleanFormatException
|
|
73 {
|
|
74 try {
|
|
75 return Template.booleanValue( getArg(param) );
|
|
76 } catch(BooleanFormatException e) {
|
|
77 throw new BooleanFormatException( newMsg(param,e) );
|
|
78 }
|
|
79 }
|
|
80
|
|
81 public final boolean getArgAsBoolean(String param,boolean defaultValue)
|
|
82 throws BooleanFormatException
|
|
83 {
|
|
84 try {
|
|
85 return Template.booleanValue( getArg(param), defaultValue );
|
|
86 } catch(BooleanFormatException e) {
|
|
87 throw new BooleanFormatException( newMsg(param,e) );
|
|
88 }
|
|
89 }
|
|
90
|
|
91 public final int getArgAsInt(String param)
|
|
92 throws NumberFormatException
|
|
93 {
|
|
94 try {
|
|
95 return Template.intValue( getArg(param) );
|
|
96 } catch(NumberFormatException e) {
|
|
97 throw new NumberFormatException( newMsg(param,e) );
|
|
98 }
|
|
99 }
|
|
100
|
|
101 public int getArgAsInt(String param, int defaultValue)
|
|
102 throws NumberFormatException
|
|
103 {
|
|
104 try {
|
|
105 return Template.intValue( getArg(param), defaultValue );
|
|
106 } catch(NumberFormatException e) {
|
|
107 throw new NumberFormatException( newMsg(param,e) );
|
|
108 }
|
|
109 }
|
|
110
|
|
111 public final long getArgAsLong(String param)
|
|
112 throws NumberFormatException
|
|
113 {
|
|
114 try {
|
|
115 return Template.longValue( getArg(param) );
|
|
116 } catch(NumberFormatException e) {
|
|
117 throw new NumberFormatException( newMsg(param,e) );
|
|
118 }
|
|
119 }
|
|
120
|
|
121 private static String newMsg(String param,Exception e) {
|
|
122 return "For argument: \"" + param + "\" - " + e.getMessage();
|
|
123 }
|
|
124
|
|
125 public String encode(String s) {
|
|
126 return s==null ? null : getEncoder().encode(s);
|
|
127 }
|
|
128
|
|
129 public Encoder getEncoder() {
|
|
130 return runState.getEncoder();
|
|
131 }
|
|
132
|
|
133 public void setEncoder(Encoder encoder) {
|
|
134 runState.setEncoder(encoder);
|
|
135 }
|
|
136
|
|
137 void close() {
|
|
138 runState.setEncoder(oldEncoder);
|
|
139 }
|
|
140
|
|
141 }
|