blob: bf4e8d8a3325e0fd40f2cfc785e344ef4909ac87 (
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
|
package com.modulus.dataread.expressions;
/**
* This class is used to help to assemble the code into more
* readable code for the computer, it sets the system based on
* a more of a stack-ish way so the virtual machine can linearly
* push/pop arguments and call functions.
*
* @author jrahm
*
*/
public abstract class ExpressionPart {
// Null if function is global
private ExpressionPart caller;
private ExpressionPart[] args;
private String function;
private boolean primary = false;
private ExpressionPart(){}
/**
* This creates a new Expression part meant to handle multi-part
* expressions such as expressions with parenthesis, functions
* inside of functions and so on.
*
* Q-Bar is set up to be very functional, to the point where every entity is made
* of functions being called on arguments and such.
*
* @param caller the caller of the function (null if the function is global)
* @param function the function to be called
* @param args the arguments of the function
* @return an expression part that describes this layout
*/
public static ExpressionPart makePrimaryExpressionPart( ExpressionPart caller, String function, ExpressionPart[] args ){
ExpressionPart ths = new ExpressionPart(){
private String compile;
@Override
public String compilePart() {
if( compile == null ){
StringBuffer buf = new StringBuffer();
ExpressionPart caller = this.getCaller();
// else
// buf.append( "$global$" );
for(ExpressionPart part : this.getArgs()){
buf.append(' ');
buf.append( part.compilePart() );
}
if(caller != null)
buf.append( " " + caller.compilePart() );
buf.append(' ');
if(this.getFunction().trim().length() > 0){
if(caller != null){
// \u00FF is the marker for what is an instance function
buf.append( "\u00FF" + this.getFunction() );
}
else{
// \u00FE is the marker for a global function
buf.append( "\u00FE" + this.getFunction() );
}
}
compile = buf.toString().replaceAll("\\s+", " ");
}
return compile;
}
};
ths.caller = caller;
ths.function = function;
ths.args = args;
ths.primary = true;
return ths;
}
/**
* This method returns an expression part that is made entirely up
* of one single variable name. On a side note, these parts are the
* recursive base case for the compilation.
*
* The name of the variable is stored in the function value, as a variable
* name can be thought of as a function that returns that point in
* memory.
*
* @param name the name of the variable.
* @return A base case ExpressionPart
*/
public static ExpressionPart makeExpressionPart( String name ){
ExpressionPart ths = new ExpressionPart(){
@Override
public String compilePart() {
return this.getFunction();
}
};
ths.function = name;
return ths;
}
/**
* @return the caller
*/
public ExpressionPart getCaller() {
return caller;
}
/**
* @return the args
*/
public ExpressionPart[] getArgs() {
return args;
}
/**
* @return the function
*/
public String getFunction() {
return function;
}
/**
* @return the primary
*/
public boolean isPrimary() {
return primary;
}
public abstract String compilePart();
}
|