aboutsummaryrefslogtreecommitdiff
path: root/project/QBarInterpreter/src/com/modulus/qbar/core/QBSyntheticFunction.java
blob: fc8b3174a36d9515630b3ec73f6bf12c3649b8a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.modulus.qbar.core;

import java.util.HashMap;
import java.util.Map;

import com.modulus.common.collections.ArrayStack;
import com.modulus.common.collections.Stack;
import com.modulus.dataread.expressions.FlowStatement;
import com.modulus.dataread.expressions.Statement;
import com.modulus.qbar.core.exceptions.ParseException;
import com.modulus.qbar.core.interpreter.QBInterpreter;
import com.modulus.qbar.core.parser.ByteOperation;
import com.modulus.qbar.core.parser.QBParser;
import com.modulus.qbar.core.parser.stmt.QBStatement;
import com.modulus.qbar.core.primitive.QBPrimitive;
import com.modulus.qbar.lang.QBIterator;

public class QBSyntheticFunction extends QBFunction{
	private Stack<QBNamespace> namespaceCalls = new ArrayStack<QBNamespace>();
	/**
	 * 
	 */
	private static final long serialVersionUID = 2890479116324397754L;
	private QBStatement code;
	private String name;
	private String[] args;
	private Map<String, QBObject> namespaceTemplate = new HashMap<String,QBObject>();
	private QBObject defaultThis;
	
	private Map<String, QBObject> subs = new HashMap<String, QBObject>();
	
	public QBSyntheticFunction( QBStatement code, QBParser parser ){
		super( -1 );
		this.code = code;
		
		namespaceCalls.push(new QBNamespace(namespaceTemplate));
		parse(parser);
	}
	
	public void parse(QBParser parser){
		String header = code.getHeader(); // should return header, e.g. let someFunction(a,b,c) = do
		
		if(!header.startsWith("let")){
			throw new ParseException("Invalid Syntax For Function, expected `let`", code);
		}
		
		// this means that code is a one-line function and we need to break it up
		if( !code.hasChildren() ){
			String[] arr = header.split("=", 2);
			header = arr[0] + " = ";

			QBStatement onlyChild = new QBStatement(QBParser.formatter);
			onlyChild.setLineNumber(code.getLineNumber());
			onlyChild.setHeader(arr[1]);
			
		
			code.addChild(onlyChild);
			code.setHeader(header);
		}
		
		this.args = chompArgs( header );
		this.name = chompName( header );
		
		for( Statement stmt : code.getChildren() ){
			String expression = stmt.getHeader();
			
			//System.out.println("EXPPRS: "+expression);
			
			stmt.setHeader(expression);
			
			parser.readStatement( stmt, this );
		}
	}
	
	/*
	 * Return the args of a function
	 */
	private String[] chompArgs(String header){
	//	System.out.println(header);
		int idx1 = header.indexOf('(');
		int idx2 = header.lastIndexOf(')');
		
		if(idx1 == idx2 && idx1 == -1){
			return new String[]{"this"};
		} else if( idx1 == -1 || idx2 == -1){
			throw new ParseException("Syntax Error", code);
		}
		// should parse header, like the a,b,c part of
		// let someFunction(a,b,c) = do
		String args = header.substring( idx1 + 1, idx2 ).trim();
	
		// split the arguments into string[], trimming all unnecessary whitespace
		String[] ret = (args + ",this").split("\\s*,\\s*");
		
		
		return ret;
	}
	
	/*
	 * return the name of the function
	 */
	static private String chompName(String header){
		// given header is to the effect of, `let someFunction(a,b,c) = do`
		
		// Chop the `let` from the string
		header = header.substring(3);
		int idx = header.indexOf('(');
		if(idx == -1)
			idx = header.indexOf('=');
		// get the name
		return header.substring(0, idx).trim();
	}
	
	private boolean ifBit;
//	private QBObject returnedValue;
	
	@Override
	public QBObject execute(QBObject[] args) {
	//	System.out.println("Executing: " + this.getName());
		QBNamespace layer = new QBNamespace(new HashMap<String, QBObject>());
		layer.setSuper(this.getSuper());
		
		for( String str : subs.keySet() ) {
			QBObject clone = subs.get(str).clone();
			layer.set(str,  clone);
			clone.setSuper(layer);
		}
		
	//	System.out.println(layer.toString2());
		//System.out.println("push");
		namespaceCalls.push(layer);
		
		mapArgs( args );
		Stack<QBObject> stack = QBInterpreter.instance().getStack();
		Statement[] statements = code.getChildren();
	//	System.out.println(Arrays.toString(statements));
		execute(statements, stack);
		
		//System.out.println("pop");
		namespaceCalls.pop();
		return stack.pop();
	}
	
	/**
	 * Guts of the exection process, when it operates on this low of of level
	 */
	public void execute(Statement[] statements, Stack <QBObject> stack){
		QBStatement exe = null;
		Map<QBStatement, QBIterator> iterators = new HashMap<QBStatement, QBIterator>();
		Stack<QBStatement> toProcess = new ArrayStack<QBStatement>();
		try{
		// push all of the statements onto the stack
		// the pushAll method pushes the statements on backwards
		pushAll(statements, toProcess);

		// execute the statements
		while(!toProcess.isEmpty()){
			
			//if(toProcess.size() == 1 && namespaceCalls.size() == 2)
			//	System.out.println("Processing: " + toProcess.peek().getHeader() + " :: " + namespaceCalls.size() );
			// get the next statement to execute
			exe = toProcess.peek(); // look at the statement
		//	System.out.println("EXE " + exe + ", " + exe.getFlowStatement());
			// is there some kind of a flow statement attached with this function?
			FlowStatement flow = exe.getFlowStatement();
			
			if( flow == FlowStatement.FOR){
				QBIterator<?> iter = iterators.get(exe);
				
				if(iter == null){
					ByteOperation[] ops = exe.getOperations();
					exec(ops, stack);
					iter = (QBIterator<?>) returnedValue;
					
					iterators.put(exe, iter);
				}
				
				if(!iter.hasNext()){
					toProcess.pop();
					iterators.put(exe, null);
				} else{
					iter.next();
					pushAll( exe.getChildren(), toProcess );
				}
			}
			// if and whiles are very similar, the only difference being that one repeats and the other does not
			else if(flow == FlowStatement.IF || flow == FlowStatement.WHILE){
				
				// if statements do not repeat
				if(flow == FlowStatement.IF)
					toProcess.pop(); // pop it off to make it not repeat.
				
				// these are the operations
				ByteOperation[] ops = exe.getOperations();
				
				// execute the operations
				exec(ops, stack);
				
				// the method exec pops what it returns in returnedValue;
				QBObject obj = returnedValue;
				
				// check to see if the boolean evaluated to true
				if( obj instanceof QBPrimitive &&((QBPrimitive) obj).intValue() > 0){ // TODO fix this to use method in Core
					pushAll( exe.getChildren(), toProcess );
					ifBit = true;
				}
				// if not
				else{
					// when the while statement is false, it is time to move on
					if(flow == FlowStatement.WHILE){
						toProcess.pop();
					} else{
						ifBit = false;
					}
				}
			} else if( flow == FlowStatement.ELSE){
				// just pop it
				toProcess.pop();
				
				// if the if bit was false
				if( !ifBit ){
					
					// push all the children
					pushAll( exe.getChildren(), toProcess );
				}
			}
			// if there is a return statement stop right now
			else if ( flow == FlowStatement.RETURN ){
				toProcess.pop();
				exec( exe.getOperations(), stack );
				
				stack.push( returnedValue );
				return;
			}
			// if there is no flow, then just pop and execute.
			else {
				toProcess.pop();
				exec( exe.getOperations(), stack );
			}
		}
		
		// if this is the end, then it is time just to return the value that was executed by the last command,
		// this means that if there is no return statement, then the last command becomes the return statement.
		stack.push( returnedValue );
		} catch(RuntimeException e){
			e.printStackTrace();
			throw new ParseException(e.getMessage(), e, exe);
		}
	}
	
	/*
	 * This function pushes all of the elements in an array
	 * onto the stack in reverse order.
	 */
	private void pushAll( Statement[] arr, Stack<QBStatement> toProcess ){
		for ( int i = arr.length - 1; i >= 0; i --){
			toProcess.push( (QBStatement) arr[i]);
		}
	}
	
	
	

	
	
	private void mapArgs(QBObject[] args){

		for( int i = 0;i < args.length; i ++){
			this.set(this.args[i], args[i]);
		}
		
		if( defaultThis != null )
			this.set("this", defaultThis);
	}
	
	@Override
	public int getArgc(){
		return this.args.length;
	}
	
	@Override
	public String getName(){
		return name;
	}
	
	public String toString2(){
		return code.toString();
	}
	
	@Override
	public void setGlobal( boolean global ){
		super.setGlobal(global);

		if( global ){
			if(args[args.length - 1].equals("this")){
				String[] next = new String[args.length - 1];
				System.arraycopy(this.args, 0, next, 0, args.length - 1);
				args = next;
			} else if(!args[args.length - 1].equals("this")){
				String[] next = new String[args.length + 1];
				System.arraycopy(this.args, 0, next, 0, args.length);
				next[args.length] = "this";
				args = next;
			} 
		}

	}
	
	@Override
	public void setSuper( QBNamespace space){
		super.setSuper(space);
	//	System.out.println("Setting Space To " + space);
		
		if( space instanceof QBObject && !(space instanceof QBUtilityNamespace)){
			setGlobal(false);
		} else {
			setGlobal(true);
		}
		
		if(space instanceof QBUtilityNamespace){
			this.defaultThis = (QBObject) space;
		}
	}
	
	@Override
	public QBObject get(String name){
		return namespaceCalls.peek().get(name);
	}
	
	@Override
	public void set(String name, QBObject obj){
		namespaceCalls.peek().set(name, obj);
	}
	
	public void addSubObject( String name, QBObject obj ){
		this.subs.put(name, obj);
	}
}