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

import com.modulus.common.collections.Stack;
import com.modulus.qbar.core.QBFunction;
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.QBPrimitive;

public class IfThenElseFunction extends QBFunction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6694512700264765780L;
	private ByteOperation[] ifPart, thenPart, elsePart;
	

	public IfThenElseFunction(ByteOperation[] ifPart,
			ByteOperation[] thenPart, ByteOperation[] elsePart) {
		super(0);
		this.ifPart = ifPart;
		this.thenPart = thenPart;
		this.elsePart = elsePart;
	}


	@Override
	public QBObject execute(QBObject[] args) {
		Stack<QBObject> stack = QBInterpreter.instance().getStack();
		this.exec(ifPart, stack);
		
		if(returnedValue instanceof QBPrimitive && ((QBPrimitive) returnedValue).intValue() != 0){
			this.exec(thenPart, stack);
			return returnedValue;
		} else{
			this.exec(elsePart, stack);
			return returnedValue;
		}
	}

}