aboutsummaryrefslogtreecommitdiff
path: root/project/QBarInterpreter/src/com/modulus/qbar/core/QBFunction.java
blob: 398e21a263b60cd9de2671dfa359295d7745df07 (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
package com.modulus.qbar.core;

import com.modulus.access.ArrayFunction;
import com.modulus.common.collections.Stack;
import com.modulus.qbar.core.parser.ByteOperation;

public abstract class QBFunction extends QBObject implements ArrayFunction< QBObject, QBObject >{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7422585311092161908L;

	private boolean global;
	
	public static final QBStruct functionStruct = new QBStruct("function"){
		/**
		 * 
		 */
		private static final long serialVersionUID = -2742542386017721944L;

		@Override
		public QBObject newInstance() {
			return this;
		}
	};
	
	protected QBObject returnedValue;
	
	private int argc;
	
	public QBFunction( int argc ) {
		super(functionStruct);
		this.argc = argc;
	}
	
	public int getArgc(){
		return argc;
	}
	
	public String getName(){
		return this.toString();
	}
	
	@Override
	public abstract QBObject execute(QBObject[] args);
	
	protected void setGlobal( boolean global ){
		this.global = global;
	}
	
	protected void setArgc( int argc ){
		this.argc = argc;
	}
	
	public boolean isGlobal(){
		return global;
	}
	
	/**
	 * Relatively low level function call that pushes the object
	 * it returned onto the stack.
	 * @param func the function to call
	 * @param stack the stack to call it on
	 */
	public static void callFunction( QBFunction func, Stack<QBObject> stack ){
		int argc = func.getArgc();
		QBObject[] nextArgs = new QBObject[ argc ];
		
		for( int i = 0; i < argc; i ++ ){
			//System.out.println(func + " argc: " + argc + " i: " + i );
			nextArgs[ argc - i - 1 ] = stack.pop();
		}
		
		stack.push( func.execute(nextArgs) );
	}
	
	/*
	 * Very, very low level computation, deals with all
	 * of the individual statements that need to be executed.
	 */
	public void exec( ByteOperation[] arr, Stack<QBObject> stack){
//		System.out.println(Arrays.toString(arr));
		if(arr == null)
			return;
		
		for( ByteOperation op : arr ){
			switch( op.getOperator() ){
			case CALLG:
				//System.out.println(op.getArg().toString());
				callFunction( (QBFunction)this.get(op.getArg().toString()), stack);
				break;
			case CALL:
				QBObject caller = stack.peek();
				
				if(caller instanceof QBUtilityNamespace)
					stack.pop();
				//System.out.println("CALL " + caller);
				callFunction( (QBFunction)caller.get(op.getArg().toString()), stack);
				break;
			case PUSH:
				String[] str = (String[]) op.getArgs();
				
				stack.push( this.hardGet(str) );
				break;
			case PUSHO:
				QBObject obj = (QBObject) op.getArg();
				stack.push(obj);
				break;
				
			case STO:
				QBObject pop = stack.peek();
				this.hardSet((String[]) op.getArgs(), pop);
				break;
				
			case ITR:
				QBObject popped = stack.peek();
				String name = op.getArg().toString();
				stack.push(popped.iterator(name, this));
				
			}
		}
		
		// this method does not return via the stack, but pops the value
		// into a holder to help prevent stack memory leaks.
		returnedValue = stack.pop();
	}
	
	public QBObject getReturnedValue(){
		return returnedValue;
	}
}