aboutsummaryrefslogtreecommitdiff
path: root/project/QBarInterpreter/src/com/modulus/qbar/lang/QBLazyRangeListConstructor.java
blob: 5465515b20f96580dc2a99fe20f1aabac7f1005c (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
package com.modulus.qbar.lang;

import com.modulus.common.collections.Stack;
import com.modulus.qbar.core.QBObject;
import com.modulus.qbar.core.interpreter.QBInterpreter;
import com.modulus.qbar.core.parser.ByteOperation;
import com.modulus.qbar.core.primitive.QBDouble;
import com.modulus.qbar.core.primitive.QBInt;
import com.modulus.qbar.core.primitive.QBPrimitive;

public class QBLazyRangeListConstructor extends ListConstructor{
	private static final long serialVersionUID = -3617305937103054888L;

	private ByteOperation[] start;
	private ByteOperation[] second;
	private ByteOperation[] end;
	
	public QBLazyRangeListConstructor(ByteOperation[] start,
			ByteOperation[] second, ByteOperation[] end) {
		super();
		this.start = start;
		this.second = second;
		this.end = end;
	}

	@Override
	protected QBList makeList() {
		Stack<QBObject> stack = QBInterpreter.instance().getStack();
		
		QBPrimitive startValue;
		if(start == null){
			startValue = null;
		} else{
			exec( start, stack );
			startValue = (QBPrimitive) returnedValue;
		}
		
		QBPrimitive secondValue;
		if(second == null){
			secondValue = null;
		} else {
			exec( second, stack );
			secondValue = (QBPrimitive) returnedValue;
		}
		
		QBPrimitive endValue;
		if(end == null){
			endValue = null;
		} else{
			exec( end, stack );
			endValue = (QBPrimitive) returnedValue;
		}
		
		double change = secondValue == null ? 1 : secondValue.doubleValue() - startValue.doubleValue();
		
		QBPrimitive changeValue;
		if(change % 1 == 0 && startValue instanceof QBInt)
			changeValue = new QBInt((int)change);
		else
			changeValue = new QBDouble(change);
		
		return new QBLazyRangeList(startValue, endValue, changeValue);
	}

}