mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-18 19:29:08 +00:00
format all the things!
This commit is contained in:
1 parent
54ec75fd3d
commit
b36c6dade6
377 files changed
+6833
-6772
No files matched your search
@@ -11,62 +11,64 @@ package stanhebben.zenscript.parser;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class ArrayListI {
|
||||
private int[] data;
|
||||
private int size;
|
||||
private int[] data;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new, empty ArrayList of integers.
|
||||
*/
|
||||
public ArrayListI() {
|
||||
data = new int[8];
|
||||
}
|
||||
/**
|
||||
* Creates a new, empty ArrayList of integers.
|
||||
*/
|
||||
public ArrayListI() {
|
||||
data = new int[8];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a value.
|
||||
*
|
||||
* @param value integer value
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size == data.length) expand();
|
||||
data[size++] = value;
|
||||
}
|
||||
/**
|
||||
* Adds a value.
|
||||
*
|
||||
* @param value integer value
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size == data.length)
|
||||
expand();
|
||||
data[size++] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value at the specified position.
|
||||
*
|
||||
* @param index position
|
||||
* @return value at this position
|
||||
*/
|
||||
public int get(int index) {
|
||||
if (index >= size) throw new ArrayIndexOutOfBoundsException();
|
||||
return data[index];
|
||||
}
|
||||
/**
|
||||
* Gets the value at the specified position.
|
||||
*
|
||||
* @param index position
|
||||
* @return value at this position
|
||||
*/
|
||||
public int get(int index) {
|
||||
if (index >= size)
|
||||
throw new ArrayIndexOutOfBoundsException();
|
||||
return data[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of this ArrayList.
|
||||
*
|
||||
* @return size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
/**
|
||||
* Gets the size of this ArrayList.
|
||||
*
|
||||
* @return size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
/**
|
||||
* Doubles the size of this array.
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newdata = new int[data.length * 2];
|
||||
System.arraycopy(data, 0, newdata, 0, size);
|
||||
data = newdata;
|
||||
}
|
||||
/**
|
||||
* Doubles the size of this array.
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newdata = new int[data.length * 2];
|
||||
System.arraycopy(data, 0, newdata, 0, size);
|
||||
data = newdata;
|
||||
}
|
||||
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
System.arraycopy(data, 0, result, 0, size);
|
||||
return result;
|
||||
}
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
System.arraycopy(data, 0, result, 0, size);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -13,107 +13,111 @@ package stanhebben.zenscript.parser;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CharStream {
|
||||
private char[] data;
|
||||
private int index;
|
||||
private char[] data;
|
||||
private int index;
|
||||
|
||||
/**
|
||||
* Constructs a character stream from a data string.
|
||||
*
|
||||
* @param data data string
|
||||
*/
|
||||
public CharStream(String data) {
|
||||
this.data = data.toCharArray();
|
||||
index = 0;
|
||||
}
|
||||
/**
|
||||
* Constructs a character stream from a data string.
|
||||
*
|
||||
* @param data data string
|
||||
*/
|
||||
public CharStream(String data) {
|
||||
this.data = data.toCharArray();
|
||||
index = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the next character in the stream equals the specified character.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean peek(char ch) {
|
||||
if (index == data.length) return false;
|
||||
return data[index] == ch;
|
||||
}
|
||||
/**
|
||||
* Checks if the next character in the stream equals the specified
|
||||
* character.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean peek(char ch) {
|
||||
if (index == data.length)
|
||||
return false;
|
||||
return data[index] == ch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an optional character. If the next character does not equal
|
||||
* the specified character, nothing will happen.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean optional(char ch) {
|
||||
if (index == data.length) return false;
|
||||
if (data[index] == ch) {
|
||||
index++;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Processes an optional character. If the next character does not equal the
|
||||
* specified character, nothing will happen.
|
||||
*
|
||||
* @param ch checked character
|
||||
* @return true if the next character equals the specified character
|
||||
*/
|
||||
public boolean optional(char ch) {
|
||||
if (index == data.length)
|
||||
return false;
|
||||
if (data[index] == ch) {
|
||||
index++;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an optional range of characters. If the next character is not
|
||||
* within the specified bounds, nothing will happen.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char optional(char from, char to) {
|
||||
if (index == data.length) return 0;
|
||||
if (data[index] >= from && data[index] <= to) {
|
||||
return data[index++];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Processes an optional range of characters. If the next character is not
|
||||
* within the specified bounds, nothing will happen.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char optional(char from, char to) {
|
||||
if (index == data.length)
|
||||
return 0;
|
||||
if (data[index] >= from && data[index] <= to) {
|
||||
return data[index++];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a required character. If the next character does not equal the
|
||||
* specified character, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param ch required character
|
||||
*/
|
||||
public void required(char ch) {
|
||||
if (data[index] != ch) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
/**
|
||||
* Processes a required character. If the next character does not equal the
|
||||
* specified character, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param ch required character
|
||||
*/
|
||||
public void required(char ch) {
|
||||
if (data[index] != ch) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a required character range. If the next character is not within
|
||||
* the specified bounds, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char required(char from, char to) {
|
||||
if (data[index] < from || data[index] > to) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
return data[index++];
|
||||
}
|
||||
/**
|
||||
* Processes a required character range. If the next character is not within
|
||||
* the specified bounds, an IllegalArgumentException is thrown.
|
||||
*
|
||||
* @param from lower character value bound, inclusive
|
||||
* @param to upper character value bound, inclusive
|
||||
* @return the processed character
|
||||
*/
|
||||
public char required(char from, char to) {
|
||||
if (data[index] < from || data[index] > to) {
|
||||
throw new IllegalArgumentException("Unexpected character: " + data[index]);
|
||||
}
|
||||
return data[index++];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next character in the stream.
|
||||
*
|
||||
* @return the next character
|
||||
*/
|
||||
public char next() {
|
||||
return data[index++];
|
||||
}
|
||||
/**
|
||||
* Returns the next character in the stream.
|
||||
*
|
||||
* @return the next character
|
||||
*/
|
||||
public char next() {
|
||||
return data[index++];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the end of the stream has been reached.
|
||||
*
|
||||
* @return true if there are more characters in the stream
|
||||
*/
|
||||
public boolean hasMore() {
|
||||
return index < data.length;
|
||||
}
|
||||
/**
|
||||
* Checks if the end of the stream has been reached.
|
||||
*
|
||||
* @return true if there are more characters in the stream
|
||||
*/
|
||||
public boolean hasMore() {
|
||||
return index < data.length;
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ package stanhebben.zenscript.parser;
|
||||
import static java.util.Arrays.sort;
|
||||
|
||||
/**
|
||||
* Represents a compiled DFA. A compiled DFA has a compact representation and
|
||||
* is immediately usable for efficient processing.
|
||||
* Represents a compiled DFA. A compiled DFA has a compact representation and is
|
||||
* immediately usable for efficient processing.
|
||||
*
|
||||
* A compiled DFA can be converted to a compact integer array. This array can
|
||||
* then be hardcoded in an application.
|
||||
@@ -17,136 +17,137 @@ import static java.util.Arrays.sort;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CompiledDFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
public HashMapII[] transitions;
|
||||
public int[] finals;
|
||||
public HashMapII[] transitions;
|
||||
public int[] finals;
|
||||
|
||||
/**
|
||||
* Constructs a compiled DFA from the specified transition graph and finals
|
||||
* arrays.
|
||||
*
|
||||
* The transition array specifies all transitions for each state. The finals
|
||||
* array specifies the final class index of each state, or NOFINAL if the state
|
||||
* is not a final. There can multiple final classes, which can, for example,
|
||||
* be used to distinguish token types.
|
||||
*
|
||||
* @param transitions transitions graph
|
||||
* @param finals finals
|
||||
*/
|
||||
public CompiledDFA(HashMapII[] transitions, int[] finals) {
|
||||
this.transitions = transitions;
|
||||
this.finals = finals;
|
||||
}
|
||||
/**
|
||||
* Constructs a compiled DFA from the specified transition graph and finals
|
||||
* arrays.
|
||||
*
|
||||
* The transition array specifies all transitions for each state. The finals
|
||||
* array specifies the final class index of each state, or NOFINAL if the
|
||||
* state is not a final. There can multiple final classes, which can, for
|
||||
* example, be used to distinguish token types.
|
||||
*
|
||||
* @param transitions transitions graph
|
||||
* @param finals finals
|
||||
*/
|
||||
public CompiledDFA(HashMapII[] transitions, int[] finals) {
|
||||
this.transitions = transitions;
|
||||
this.finals = finals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an integer array with a DFA definition (generated by toArray())
|
||||
* to a CompiledDFA.
|
||||
*
|
||||
* @param definition definition array
|
||||
*/
|
||||
public CompiledDFA(int[] definition) {
|
||||
int ix = 0;
|
||||
int numStates = definition[ix++];
|
||||
transitions = new HashMapII[numStates];
|
||||
finals = new int[numStates];
|
||||
/**
|
||||
* Converts an integer array with a DFA definition (generated by toArray())
|
||||
* to a CompiledDFA.
|
||||
*
|
||||
* @param definition definition array
|
||||
*/
|
||||
public CompiledDFA(int[] definition) {
|
||||
int ix = 0;
|
||||
int numStates = definition[ix++];
|
||||
transitions = new HashMapII[numStates];
|
||||
finals = new int[numStates];
|
||||
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
transitions[i] = new HashMapII();
|
||||
finals[i] = definition[ix++];
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
transitions[i] = new HashMapII();
|
||||
finals[i] = definition[ix++];
|
||||
|
||||
int numRanges = definition[ix++];
|
||||
for (int j = 0; j < numRanges; j++) {
|
||||
int from = definition[ix++];
|
||||
int to = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
for (int k = from; k <= to; k++) {
|
||||
transitions[i].put(k, state);
|
||||
}
|
||||
}
|
||||
int numRanges = definition[ix++];
|
||||
for (int j = 0; j < numRanges; j++) {
|
||||
int from = definition[ix++];
|
||||
int to = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
for (int k = from; k <= to; k++) {
|
||||
transitions[i].put(k, state);
|
||||
}
|
||||
}
|
||||
|
||||
int numSingles = definition[ix++];
|
||||
for (int j = 0; j < numSingles; j++) {
|
||||
int label = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
transitions[i].put(label, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
int numSingles = definition[ix++];
|
||||
for (int j = 0; j < numSingles; j++) {
|
||||
int label = definition[ix++];
|
||||
int state = definition[ix++];
|
||||
transitions[i].put(label, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the DFA to an integer array.
|
||||
*
|
||||
* @return an integer array representation of the DFA
|
||||
*/
|
||||
public int[] toArray() {
|
||||
ArrayListI result = new ArrayListI();
|
||||
result.add(finals.length);
|
||||
/**
|
||||
* Converts the DFA to an integer array.
|
||||
*
|
||||
* @return an integer array representation of the DFA
|
||||
*/
|
||||
public int[] toArray() {
|
||||
ArrayListI result = new ArrayListI();
|
||||
result.add(finals.length);
|
||||
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
result.add(finals[i]);
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
result.add(finals[i]);
|
||||
|
||||
ArrayListI singles = new ArrayListI();
|
||||
ArrayListI rangeFrom = new ArrayListI();
|
||||
ArrayListI rangeTo = new ArrayListI();
|
||||
ArrayListI singles = new ArrayListI();
|
||||
ArrayListI rangeFrom = new ArrayListI();
|
||||
ArrayListI rangeTo = new ArrayListI();
|
||||
|
||||
int[] keys = transitions[i].keysArray();
|
||||
sort(keys);
|
||||
for (int j = 0; j < keys.length; j++) {
|
||||
int from = j;
|
||||
int state = transitions[i].get(j);
|
||||
while (j + 1 < keys.length && keys[j + 1] == keys[j] + 1 && transitions[i].get(j + 1) == state) j++;
|
||||
if (j == from) {
|
||||
singles.add(keys[j]);
|
||||
} else {
|
||||
rangeFrom.add(keys[from]);
|
||||
rangeTo.add(keys[j]);
|
||||
}
|
||||
}
|
||||
int[] keys = transitions[i].keysArray();
|
||||
sort(keys);
|
||||
for (int j = 0; j < keys.length; j++) {
|
||||
int from = j;
|
||||
int state = transitions[i].get(j);
|
||||
while (j + 1 < keys.length && keys[j + 1] == keys[j] + 1 && transitions[i].get(j + 1) == state)
|
||||
j++;
|
||||
if (j == from) {
|
||||
singles.add(keys[j]);
|
||||
} else {
|
||||
rangeFrom.add(keys[from]);
|
||||
rangeTo.add(keys[j]);
|
||||
}
|
||||
}
|
||||
|
||||
result.add(rangeFrom.size());
|
||||
for (int j = 0; j < rangeFrom.size(); j++) {
|
||||
result.add(rangeFrom.get(j));
|
||||
result.add(rangeTo.get(j));
|
||||
result.add(transitions[i].get(rangeFrom.get(j)));
|
||||
}
|
||||
result.add(singles.size());
|
||||
for (int j = 0; j < singles.size(); j++) {
|
||||
result.add(singles.get(j));
|
||||
result.add(transitions[i].get(singles.get(j)));
|
||||
}
|
||||
}
|
||||
return result.toArray();
|
||||
}
|
||||
result.add(rangeFrom.size());
|
||||
for (int j = 0; j < rangeFrom.size(); j++) {
|
||||
result.add(rangeFrom.get(j));
|
||||
result.add(rangeTo.get(j));
|
||||
result.add(transitions[i].get(rangeFrom.get(j)));
|
||||
}
|
||||
result.add(singles.size());
|
||||
for (int j = 0; j < singles.size(); j++) {
|
||||
result.add(singles.get(j));
|
||||
result.add(transitions[i].get(singles.get(j)));
|
||||
}
|
||||
}
|
||||
return result.toArray();
|
||||
}
|
||||
|
||||
/* Used for debugging */
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < transitions.length; i++) {
|
||||
HashMapII map = transitions[i];
|
||||
/* Used for debugging */
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < transitions.length; i++) {
|
||||
HashMapII map = transitions[i];
|
||||
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
if (finals[i] != DFA.NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < finals.length; i++) {
|
||||
if (finals[i] != DFA.NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
@@ -16,234 +16,235 @@ import java.util.Queue;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class DFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
private DFAState initial;
|
||||
private DFAState initial;
|
||||
|
||||
/**
|
||||
* Constructs a new DFA with the specified initial state.
|
||||
*
|
||||
* @param initial
|
||||
*/
|
||||
public DFA(DFAState initial) {
|
||||
this.initial = initial;
|
||||
}
|
||||
/**
|
||||
* Constructs a new DFA with the specified initial state.
|
||||
*
|
||||
* @param initial
|
||||
*/
|
||||
public DFA(DFAState initial) {
|
||||
this.initial = initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles this DFA into a more efficient structure.
|
||||
*
|
||||
* @return the compiled DFA
|
||||
*/
|
||||
public CompiledDFA compile() {
|
||||
ArrayList<DFAState> nodeList = new ArrayList<DFAState>();
|
||||
HashMap<DFAState, Integer> nodes = new HashMap<DFAState, Integer>();
|
||||
nodes.put(initial, 0);
|
||||
nodeList.add(initial);
|
||||
/**
|
||||
* Compiles this DFA into a more efficient structure.
|
||||
*
|
||||
* @return the compiled DFA
|
||||
*/
|
||||
public CompiledDFA compile() {
|
||||
ArrayList<DFAState> nodeList = new ArrayList<DFAState>();
|
||||
HashMap<DFAState, Integer> nodes = new HashMap<DFAState, Integer>();
|
||||
nodes.put(initial, 0);
|
||||
nodeList.add(initial);
|
||||
|
||||
/* Find all reachable nodes in the dfs */
|
||||
int counter = 1;
|
||||
Queue<DFAState> todo = new LinkedList<DFAState>();
|
||||
todo.add(initial);
|
||||
/* Find all reachable nodes in the dfs */
|
||||
int counter = 1;
|
||||
Queue<DFAState> todo = new LinkedList<DFAState>();
|
||||
todo.add(initial);
|
||||
|
||||
while (!todo.isEmpty()) {
|
||||
DFAState current = todo.poll();
|
||||
while (!todo.isEmpty()) {
|
||||
DFAState current = todo.poll();
|
||||
|
||||
IteratorI it = current.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = current.transitions.get(k);
|
||||
if (!nodes.containsKey(next)) {
|
||||
todo.add(next);
|
||||
nodes.put(next, counter++);
|
||||
nodeList.add(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
IteratorI it = current.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = current.transitions.get(k);
|
||||
if (!nodes.containsKey(next)) {
|
||||
todo.add(next);
|
||||
nodes.put(next, counter++);
|
||||
nodeList.add(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Compile */
|
||||
HashMapII[] transitions = new HashMapII[counter];
|
||||
int[] finals2 = new int[counter];
|
||||
/* Compile */
|
||||
HashMapII[] transitions = new HashMapII[counter];
|
||||
int[] finals2 = new int[counter];
|
||||
|
||||
for (DFAState node : nodeList) {
|
||||
int index = nodes.get(node);
|
||||
finals2[index] = node.finalCode;
|
||||
for (DFAState node : nodeList) {
|
||||
int index = nodes.get(node);
|
||||
finals2[index] = node.finalCode;
|
||||
|
||||
transitions[index] = new HashMapII();
|
||||
IteratorI it = node.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = node.transitions.get(k);
|
||||
transitions[index].put(k, nodes.get(next));
|
||||
}
|
||||
}
|
||||
transitions[index] = new HashMapII();
|
||||
IteratorI it = node.transitions.keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
DFAState next = node.transitions.get(k);
|
||||
transitions[index].put(k, nodes.get(next));
|
||||
}
|
||||
}
|
||||
|
||||
return new CompiledDFA(transitions, finals2);
|
||||
}
|
||||
return new CompiledDFA(transitions, finals2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the minimal version of this DFA.
|
||||
*
|
||||
* @return the minimal DFA
|
||||
*/
|
||||
public DFA optimize() {
|
||||
CompiledDFA compiled = compile();
|
||||
HashMapII[] transitions = compiled.transitions;
|
||||
int size = transitions.length;
|
||||
/**
|
||||
* Generates the minimal version of this DFA.
|
||||
*
|
||||
* @return the minimal DFA
|
||||
*/
|
||||
public DFA optimize() {
|
||||
CompiledDFA compiled = compile();
|
||||
HashMapII[] transitions = compiled.transitions;
|
||||
int size = transitions.length;
|
||||
|
||||
/* Collect all edges and determine alphabet */
|
||||
HashSetI alphabet = new HashSetI();
|
||||
for (int i = 0; i < size; i++) {
|
||||
IteratorI it = transitions[i].keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
alphabet.add(k);
|
||||
}
|
||||
}
|
||||
/* Collect all edges and determine alphabet */
|
||||
HashSetI alphabet = new HashSetI();
|
||||
for (int i = 0; i < size; i++) {
|
||||
IteratorI it = transitions[i].keys();
|
||||
while (it.hasNext()) {
|
||||
int k = it.next();
|
||||
alphabet.add(k);
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize distinguishing array */
|
||||
boolean[][] distinguishable = new boolean[size + 1][size + 1];
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
distinguishable[i][j] = compiled.finals[i] != compiled.finals[j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
distinguishable[i][size] = true;
|
||||
distinguishable[size][i] = true;
|
||||
}
|
||||
/* Initialize distinguishing array */
|
||||
boolean[][] distinguishable = new boolean[size + 1][size + 1];
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
distinguishable[i][j] = compiled.finals[i] != compiled.finals[j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
distinguishable[i][size] = true;
|
||||
distinguishable[size][i] = true;
|
||||
}
|
||||
|
||||
/* Minimization algorithm implementation */
|
||||
boolean changed;
|
||||
do {
|
||||
changed = false;
|
||||
IteratorI ita = alphabet.iterator();
|
||||
while (ita.hasNext()) {
|
||||
int x = ita.next();
|
||||
for (int i = 0; i < size; i++) {
|
||||
int ti = transitions[i].get(x, size);
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (distinguishable[i][j]) continue;
|
||||
/* Minimization algorithm implementation */
|
||||
boolean changed;
|
||||
do {
|
||||
changed = false;
|
||||
IteratorI ita = alphabet.iterator();
|
||||
while (ita.hasNext()) {
|
||||
int x = ita.next();
|
||||
for (int i = 0; i < size; i++) {
|
||||
int ti = transitions[i].get(x, size);
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (distinguishable[i][j])
|
||||
continue;
|
||||
|
||||
int tj = transitions[j].get(x, size);
|
||||
if (distinguishable[ti][tj]) {
|
||||
distinguishable[i][j] = true;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
int tj = transitions[j].get(x, size);
|
||||
if (distinguishable[ti][tj]) {
|
||||
distinguishable[i][j] = true;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
|
||||
/* Group nodes */
|
||||
HashMapI<DFAState> nodeMap = new HashMapI<DFAState>();
|
||||
outer: for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (!distinguishable[i][j] && nodeMap.containsKey(j)) {
|
||||
nodeMap.put(i, nodeMap.get(j));
|
||||
if (compiled.finals[i] != NOFINAL) {
|
||||
if (nodeMap.get(j).getFinal() != NOFINAL && nodeMap.get(j).getFinal() != compiled.finals[i]) {
|
||||
throw new RuntimeException("Eh?");
|
||||
}
|
||||
}
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
DFAState node = new DFAState();
|
||||
node.setFinal(compiled.finals[i]);
|
||||
nodeMap.put(i, node);
|
||||
}
|
||||
/* Group nodes */
|
||||
HashMapI<DFAState> nodeMap = new HashMapI<DFAState>();
|
||||
outer: for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (!distinguishable[i][j] && nodeMap.containsKey(j)) {
|
||||
nodeMap.put(i, nodeMap.get(j));
|
||||
if (compiled.finals[i] != NOFINAL) {
|
||||
if (nodeMap.get(j).getFinal() != NOFINAL && nodeMap.get(j).getFinal() != compiled.finals[i]) {
|
||||
throw new RuntimeException("Eh?");
|
||||
}
|
||||
}
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
DFAState node = new DFAState();
|
||||
node.setFinal(compiled.finals[i]);
|
||||
nodeMap.put(i, node);
|
||||
}
|
||||
|
||||
for (int i = 0; i < compiled.transitions.length; i++) {
|
||||
IteratorI iter = transitions[i].keys();
|
||||
while (iter.hasNext()) {
|
||||
int k = iter.next();
|
||||
for (int i = 0; i < compiled.transitions.length; i++) {
|
||||
IteratorI iter = transitions[i].keys();
|
||||
while (iter.hasNext()) {
|
||||
int k = iter.next();
|
||||
|
||||
nodeMap.get(i).addTransition(k, nodeMap.get(transitions[i].get(k)));
|
||||
}
|
||||
}
|
||||
|
||||
DFA result = new DFA(nodeMap.get(0));
|
||||
return result;
|
||||
}
|
||||
nodeMap.get(i).addTransition(k, nodeMap.get(transitions[i].get(k)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
CompiledDFA dfs = compile();
|
||||
for (int i = 0; i < dfs.transitions.length; i++) {
|
||||
HashMapII map = dfs.transitions[i];
|
||||
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dfs.finals.length; i++) {
|
||||
if (dfs.finals[i] != NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(dfs.finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
DFA result = new DFA(nodeMap.get(0));
|
||||
return result;
|
||||
}
|
||||
|
||||
/////////////////////////
|
||||
// Public inner classes
|
||||
/////////////////////////
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
CompiledDFA dfs = compile();
|
||||
for (int i = 0; i < dfs.transitions.length; i++) {
|
||||
HashMapII map = dfs.transitions[i];
|
||||
|
||||
/**
|
||||
* Represents a state in a DFA.
|
||||
*/
|
||||
public static class DFAState {
|
||||
private HashMapI<DFAState> transitions;
|
||||
private int finalCode = NOFINAL;
|
||||
IteratorI it = map.keys();
|
||||
while (it.hasNext()) {
|
||||
int v = it.next();
|
||||
result.append("edge(");
|
||||
result.append(i);
|
||||
result.append(", ");
|
||||
result.append(v);
|
||||
result.append("): ");
|
||||
result.append(map.get(v));
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dfs.finals.length; i++) {
|
||||
if (dfs.finals[i] != NOFINAL) {
|
||||
result.append("final(");
|
||||
result.append(i);
|
||||
result.append("): ");
|
||||
result.append(dfs.finals[i]);
|
||||
result.append("\r\n");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DFA state.
|
||||
*/
|
||||
public DFAState() {
|
||||
transitions = new HashMapI<DFAState>();
|
||||
}
|
||||
// ///////////////////////
|
||||
// Public inner classes
|
||||
// ///////////////////////
|
||||
|
||||
/**
|
||||
* Adds a transition.
|
||||
*
|
||||
* @param label transition edge label
|
||||
* @param next next state
|
||||
*/
|
||||
public void addTransition(int label, DFAState next) {
|
||||
transitions.put(label, next);
|
||||
}
|
||||
/**
|
||||
* Represents a state in a DFA.
|
||||
*/
|
||||
public static class DFAState {
|
||||
private HashMapI<DFAState> transitions;
|
||||
private int finalCode = NOFINAL;
|
||||
|
||||
/**
|
||||
* Sets the final class of this state. Finals can be divided in multiple
|
||||
* class, in which case each class gets its own index. Class NOFINAL is
|
||||
* used to indicate nonfinals.
|
||||
*
|
||||
* @param finalCode final index
|
||||
*/
|
||||
public void setFinal(int finalCode) {
|
||||
this.finalCode = finalCode;
|
||||
}
|
||||
/**
|
||||
* Creates a new DFA state.
|
||||
*/
|
||||
public DFAState() {
|
||||
transitions = new HashMapI<DFAState>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the final class of this state. Equals NOFINAL if this state is
|
||||
* not a final.
|
||||
*
|
||||
* @return final index
|
||||
*/
|
||||
public int getFinal() {
|
||||
return finalCode;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Adds a transition.
|
||||
*
|
||||
* @param label transition edge label
|
||||
* @param next next state
|
||||
*/
|
||||
public void addTransition(int label, DFAState next) {
|
||||
transitions.put(label, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the final class of this state. Finals can be divided in multiple
|
||||
* class, in which case each class gets its own index. Class NOFINAL is
|
||||
* used to indicate nonfinals.
|
||||
*
|
||||
* @param finalCode final index
|
||||
*/
|
||||
public void setFinal(int finalCode) {
|
||||
this.finalCode = finalCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the final class of this state. Equals NOFINAL if this state is
|
||||
* not a final.
|
||||
*
|
||||
* @return final index
|
||||
*/
|
||||
public int getFinal() {
|
||||
return finalCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,206 +11,211 @@ package stanhebben.zenscript.parser;
|
||||
* @param <T>
|
||||
*/
|
||||
public class HashMapI<T> {
|
||||
private int[] keys;
|
||||
private T[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
private int[] keys;
|
||||
private T[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new HashMap for integer keys.
|
||||
*/
|
||||
public HashMapI() {
|
||||
keys = new int[16];
|
||||
values = (T[])new Object[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
/**
|
||||
* Creates a new HashMap for integer keys.
|
||||
*/
|
||||
public HashMapI() {
|
||||
keys = new int[16];
|
||||
values = (T[]) new Object[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, T value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is
|
||||
* replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, T value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
|
||||
int index = key & mask;
|
||||
int index = key & mask;
|
||||
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length) index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length)
|
||||
index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
|
||||
*/
|
||||
public T get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return null;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key
|
||||
*/
|
||||
public T get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return null;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE) result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE)
|
||||
result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
T[] newValues = (T[])new Object[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
T[] newValues = (T[]) new Object[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int key = keys[i];
|
||||
T value = values[i];
|
||||
int index = key & newMask;
|
||||
int key = keys[i];
|
||||
T value = values[i];
|
||||
int index = key & newMask;
|
||||
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,233 +5,240 @@
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* A HashMap implementation which is optimized to associate integers with integers.
|
||||
* A HashMap implementation which is optimized to associate integers with
|
||||
* integers.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class HashMapII {
|
||||
private int[] keys;
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
private int[] keys;
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new HashMap for integer keys and values.
|
||||
*/
|
||||
public HashMapII() {
|
||||
keys = new int[16];
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
/**
|
||||
* Creates a new HashMap for integer keys and values.
|
||||
*/
|
||||
public HashMapII() {
|
||||
keys = new int[16];
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of key/value pairs in the hashmap.
|
||||
*
|
||||
* @return hashmap size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, int value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
/**
|
||||
* Returns the number of key/value pairs in the hashmap.
|
||||
*
|
||||
* @return hashmap size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
int index = key & mask;
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map. If the
|
||||
* map previously contained a mapping for the key, the old value is
|
||||
* replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
*/
|
||||
public void put(int key, int value) {
|
||||
if (size > (keys.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length) index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
int index = key & mask;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
if (keys[index] == Integer.MIN_VALUE) {
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
} else {
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (keys[index] == key) {
|
||||
values[index] = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (keys[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == keys.length)
|
||||
index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
|
||||
size++;
|
||||
}
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or Integer.MIN_VALUE if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @param def the default key value
|
||||
* @return the value to which the specified key is mapped, or the default value if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key, int def) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return def;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @return the value to which the specified key is mapped, or
|
||||
* Integer.MIN_VALUE if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped, or null if this
|
||||
* map contains no mapping for the key.
|
||||
*
|
||||
* @param key the key whose associated value is to be returned
|
||||
* @param def the default key value
|
||||
* @return the value to which the specified key is mapped, or the default
|
||||
* value if this map contains no mapping for the key
|
||||
*/
|
||||
public int get(int key, int def) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return def;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return values[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
/**
|
||||
* Returns true if this map contains a mapping for the specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return true if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(int key) {
|
||||
int index = key & mask;
|
||||
while (keys[index] != key) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE) result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Returns an iterator over the keys in this HashMap. The elements are
|
||||
* returned in no particular order.
|
||||
*
|
||||
* @return an iterator over the keys in this HashMap
|
||||
*/
|
||||
public IteratorI keys() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
/**
|
||||
* Returns an array with all keys in this HashMap. The elements are returned
|
||||
* in no particular order.
|
||||
*
|
||||
* @return an array with all keys in this HashMap
|
||||
*/
|
||||
public int[] keysArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != Integer.MIN_VALUE)
|
||||
result[ix++] = keys[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
int[] newValues = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
/**
|
||||
* Expands the size of this array. (it doubles the size)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[keys.length * 2];
|
||||
int[] newValues = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
int key = keys[i];
|
||||
int value = values[i];
|
||||
int index = key & newMask;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
int key = keys[i];
|
||||
int value = values[i];
|
||||
int index = key & newMask;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,185 +12,187 @@ import java.util.Arrays;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class HashSetI {
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
private int[] values;
|
||||
private int[] next;
|
||||
private int mask;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Creates a new HashSet for integer values.
|
||||
*/
|
||||
public HashSetI() {
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
/**
|
||||
* Creates a new HashSet for integer values.
|
||||
*/
|
||||
public HashSetI() {
|
||||
values = new int[16];
|
||||
next = new int[16];
|
||||
mask = 15;
|
||||
size = 0;
|
||||
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
values[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
values[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
public HashSetI(HashSetI original) {
|
||||
values = Arrays.copyOf(original.values, original.values.length);
|
||||
next = Arrays.copyOf(original.next, original.next.length);
|
||||
mask = original.mask;
|
||||
size = original.size;
|
||||
}
|
||||
public HashSetI(HashSetI original) {
|
||||
values = Arrays.copyOf(original.values, original.values.length);
|
||||
next = Arrays.copyOf(original.next, original.next.length);
|
||||
mask = original.mask;
|
||||
size = original.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified value to this HashSet. If this value is already in this
|
||||
* HashSet, nothing happens.
|
||||
*
|
||||
* @param value value to be added to the HashSet
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size > (values.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
/**
|
||||
* Adds the specified value to this HashSet. If this value is already in
|
||||
* this HashSet, nothing happens.
|
||||
*
|
||||
* @param value value to be added to the HashSet
|
||||
*/
|
||||
public void add(int value) {
|
||||
if (size > (values.length * 3) >> 2) {
|
||||
expand();
|
||||
}
|
||||
|
||||
int index = value & mask;
|
||||
int index = value & mask;
|
||||
|
||||
if (values[index] == Integer.MIN_VALUE) {
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (values[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == values.length) index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
if (values[index] == Integer.MIN_VALUE) {
|
||||
values[index] = value;
|
||||
} else {
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
while (next[index] != 0) {
|
||||
index = next[index] - 1;
|
||||
if (values[index] == value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
int ref = index;
|
||||
while (values[index] != Integer.MIN_VALUE) {
|
||||
index++;
|
||||
if (index == values.length)
|
||||
index = 0;
|
||||
}
|
||||
next[ref] = index + 1;
|
||||
|
||||
values[index] = value;
|
||||
}
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this HashSet contains the specified value.
|
||||
*
|
||||
* @param value integer value to add
|
||||
* @return true if this HashSet contains the specified value
|
||||
*/
|
||||
public boolean contains(int value) {
|
||||
int index = value & mask;
|
||||
while (values[index] != value) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Checks if this HashSet contains the specified value.
|
||||
*
|
||||
* @param value integer value to add
|
||||
* @return true if this HashSet contains the specified value
|
||||
*/
|
||||
public boolean contains(int value) {
|
||||
int index = value & mask;
|
||||
while (values[index] != value) {
|
||||
if (next[index] == 0) {
|
||||
return false;
|
||||
}
|
||||
index = next[index] - 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over this HashSet. The elements are returned in no
|
||||
* particular order.
|
||||
*
|
||||
* @return an iterator over this HashSet
|
||||
*/
|
||||
public IteratorI iterator() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
/**
|
||||
* Returns an iterator over this HashSet. The elements are returned in no
|
||||
* particular order.
|
||||
*
|
||||
* @return an iterator over this HashSet
|
||||
*/
|
||||
public IteratorI iterator() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the contents of this HashSet to an integer array. The order of
|
||||
* the elements is unspecified.
|
||||
*
|
||||
* @return this HashSet as integer array
|
||||
*/
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] != Integer.MIN_VALUE) {
|
||||
result[ix++] = values[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Converts the contents of this HashSet to an integer array. The order of
|
||||
* the elements is unspecified.
|
||||
*
|
||||
* @return this HashSet as integer array
|
||||
*/
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
int ix = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] != Integer.MIN_VALUE) {
|
||||
result[ix++] = values[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner methods
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner methods
|
||||
// ////////////////////////
|
||||
|
||||
/**
|
||||
* Expands the capacity of this HashSet. (double it)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
/**
|
||||
* Expands the capacity of this HashSet. (double it)
|
||||
*/
|
||||
private void expand() {
|
||||
int[] newKeys = new int[values.length * 2];
|
||||
int[] newNext = new int[next.length * 2];
|
||||
int newMask = newKeys.length - 1;
|
||||
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
for (int i = 0; i < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int key = values[i];
|
||||
int index = key & newMask;
|
||||
int key = values[i];
|
||||
int index = key & newMask;
|
||||
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
if (newKeys[index] == Integer.MIN_VALUE) {
|
||||
newKeys[index] = key;
|
||||
} else {
|
||||
while (newNext[index] != 0) {
|
||||
index = newNext[index] - 1;
|
||||
}
|
||||
int ref = index;
|
||||
while (newKeys[index] != Integer.MIN_VALUE) {
|
||||
index = (index + 1) & newMask;
|
||||
}
|
||||
newNext[ref] = index + 1;
|
||||
|
||||
newKeys[index] = key;
|
||||
}
|
||||
}
|
||||
newKeys[index] = key;
|
||||
}
|
||||
}
|
||||
|
||||
values = newKeys;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
values = newKeys;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < values.length;
|
||||
}
|
||||
public boolean hasNext() {
|
||||
return i < values.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = values[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
public int next() {
|
||||
int result = values[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < values.length && values[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
private void skip() {
|
||||
while (i < values.length && values[i] == Integer.MIN_VALUE)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
@@ -11,17 +11,17 @@ package stanhebben.zenscript.parser;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public interface IteratorI {
|
||||
/**
|
||||
* Returns true if the iteration has more elements.
|
||||
*
|
||||
* @return true if the iteration has more elements
|
||||
*/
|
||||
public boolean hasNext();
|
||||
/**
|
||||
* Returns true if the iteration has more elements.
|
||||
*
|
||||
* @return true if the iteration has more elements
|
||||
*/
|
||||
public boolean hasNext();
|
||||
|
||||
/**
|
||||
* Returns the next element in this iteration.
|
||||
*
|
||||
* @return the next element in this iteration
|
||||
*/
|
||||
public int next();
|
||||
/**
|
||||
* Returns the next element in this iteration.
|
||||
*
|
||||
* @return the next element in this iteration
|
||||
*/
|
||||
public int next();
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -15,43 +15,43 @@ public class ParseException extends RuntimeException {
|
||||
private final ZenParsedFile file;
|
||||
private final int line;
|
||||
private final int lineOffset;
|
||||
|
||||
|
||||
private final Token token;
|
||||
private final String message;
|
||||
|
||||
public ParseException(Token token, String error) {
|
||||
super("Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
|
||||
|
||||
public ParseException(Token token, String error) {
|
||||
super("Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
|
||||
this.file = token.getPosition().getFile();
|
||||
this.line = token.getPosition().getLine();
|
||||
this.lineOffset = token.getPosition().getLineOffset();
|
||||
|
||||
this.token = token;
|
||||
this.message = error;
|
||||
}
|
||||
|
||||
|
||||
this.token = token;
|
||||
this.message = error;
|
||||
}
|
||||
|
||||
public ParseException(ZenParsedFile file, int line, int lineOffset, String error) {
|
||||
this.file = file;
|
||||
this.line = line;
|
||||
this.lineOffset = lineOffset;
|
||||
|
||||
|
||||
token = null;
|
||||
message = error;
|
||||
}
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return lineOffset;
|
||||
}
|
||||
|
||||
public String getExplanation() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return lineOffset;
|
||||
}
|
||||
|
||||
public String getExplanation() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -14,46 +14,46 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class Token {
|
||||
private final ZenPosition position;
|
||||
private final String value;
|
||||
private final int type;
|
||||
private final String value;
|
||||
private final int type;
|
||||
|
||||
/**
|
||||
* Constructs a new token.
|
||||
*
|
||||
* @param value token string value
|
||||
* @param type token type
|
||||
/**
|
||||
* Constructs a new token.
|
||||
*
|
||||
* @param value token string value
|
||||
* @param type token type
|
||||
* @param position token position
|
||||
*/
|
||||
public Token(String value, int type, ZenPosition position) {
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
*/
|
||||
public Token(String value, int type, ZenPosition position) {
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string value of this token.
|
||||
*
|
||||
* @return token value
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token type of this token.
|
||||
*
|
||||
* @return token type
|
||||
*/
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
/**
|
||||
* Returns the string value of this token.
|
||||
*
|
||||
* @return token value
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return position.getLine() + ":" + position.getLineOffset() + " (" + type + ") " + value;
|
||||
}
|
||||
/**
|
||||
* Returns the token type of this token.
|
||||
*
|
||||
* @return token type
|
||||
*/
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return position.getLine() + ":" + position.getLineOffset() + " (" + type + ") " + value;
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,10 @@ import stanhebben.zenscript.ZenParsedFile;
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class TokenException extends RuntimeException {
|
||||
public TokenException(ZenParsedFile file, int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + file + ":" + line + " - " + value);
|
||||
|
||||
if (line < 0) throw new IllegalArgumentException("Line cannot be negative");
|
||||
}
|
||||
public TokenException(ZenParsedFile file, int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + file + ":" + line + " - " + value);
|
||||
|
||||
if (line < 0)
|
||||
throw new IllegalArgumentException("Line cannot be negative");
|
||||
}
|
||||
}
|
||||
@@ -24,251 +24,252 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class TokenStream implements Iterator<Token> {
|
||||
private ZenParsedFile file;
|
||||
|
||||
private CountingReader reader;
|
||||
private CompiledDFA dfa;
|
||||
private Token next;
|
||||
private int nextChar;
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
|
||||
private int tokenMemoryOffset;
|
||||
private LinkedList<Token> tokenMemory;
|
||||
private Stack<Integer> marks;
|
||||
private int tokenMemoryCurrent;
|
||||
|
||||
/**
|
||||
* Creates a token stream using the specified reader and DFA.
|
||||
*
|
||||
* @param reader reader to read characters from
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(Reader reader, CompiledDFA dfa) throws IOException {
|
||||
tokenMemoryOffset = 0;
|
||||
tokenMemoryCurrent = 0;
|
||||
tokenMemory = new LinkedList<Token>();
|
||||
marks = new Stack<Integer>();
|
||||
|
||||
this.reader = new CountingReader(reader);
|
||||
this.dfa = dfa;
|
||||
nextChar = this.reader.read();
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
advance();
|
||||
}
|
||||
private CountingReader reader;
|
||||
private CompiledDFA dfa;
|
||||
private Token next;
|
||||
private int nextChar;
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
|
||||
private int tokenMemoryOffset;
|
||||
private LinkedList<Token> tokenMemory;
|
||||
private Stack<Integer> marks;
|
||||
private int tokenMemoryCurrent;
|
||||
|
||||
/**
|
||||
* Creates a token stream using the specified reader and DFA.
|
||||
*
|
||||
* @param reader reader to read characters from
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(Reader reader, CompiledDFA dfa) throws IOException {
|
||||
tokenMemoryOffset = 0;
|
||||
tokenMemoryCurrent = 0;
|
||||
tokenMemory = new LinkedList<Token>();
|
||||
marks = new Stack<Integer>();
|
||||
|
||||
this.reader = new CountingReader(reader);
|
||||
this.dfa = dfa;
|
||||
nextChar = this.reader.read();
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
advance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a token stream which reads data from the specified string.
|
||||
*
|
||||
* @param data data to read
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(String data, CompiledDFA dfa) throws IOException {
|
||||
this(new StringReader(data), dfa);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a token stream which reads data from the specified string.
|
||||
*
|
||||
* @param data data to read
|
||||
* @param dfa DFA to tokenize the stream
|
||||
* @throws IOException
|
||||
*/
|
||||
public TokenStream(String data, CompiledDFA dfa) throws IOException {
|
||||
this(new StringReader(data), dfa);
|
||||
}
|
||||
|
||||
public void setFile(ZenParsedFile file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
|
||||
public int getLineOffset() {
|
||||
return lineOffset;
|
||||
}
|
||||
|
||||
public Token peek() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent) - tokenMemoryOffset);
|
||||
} else {
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNext(int type) {
|
||||
return peek().getType() == type;
|
||||
}
|
||||
public Token peek() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent) - tokenMemoryOffset);
|
||||
} else {
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
public Token optional(int type) {
|
||||
if (peek() != null && peek().getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public boolean isNext(int type) {
|
||||
return peek().getType() == type;
|
||||
}
|
||||
|
||||
public Token required(int type, String error) {
|
||||
public Token optional(int type) {
|
||||
if (peek() != null && peek().getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Token required(int type, String error) {
|
||||
Token t = peek();
|
||||
if (t != null && t.getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
throw new ParseException(file, line, lineOffset, error);
|
||||
}
|
||||
}
|
||||
if (t != null && t.getType() == type) {
|
||||
return next();
|
||||
} else {
|
||||
throw new ParseException(file, line, lineOffset, error);
|
||||
}
|
||||
}
|
||||
|
||||
// LL(*) ability
|
||||
// LL(*) ability
|
||||
|
||||
/**
|
||||
* Pushes a mark on the mark stack.
|
||||
*/
|
||||
public void pushMark() {
|
||||
marks.push(tokenMemoryCurrent);
|
||||
}
|
||||
/**
|
||||
* Pushes a mark on the mark stack.
|
||||
*/
|
||||
public void pushMark() {
|
||||
marks.push(tokenMemoryCurrent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a mark from the mark stack without reset.
|
||||
*/
|
||||
public void popMark() {
|
||||
marks.pop();
|
||||
/**
|
||||
* Pops a mark from the mark stack without reset.
|
||||
*/
|
||||
public void popMark() {
|
||||
marks.pop();
|
||||
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset = tokenMemoryCurrent;
|
||||
tokenMemory.clear();
|
||||
}
|
||||
}
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset = tokenMemoryCurrent;
|
||||
tokenMemory.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops a mark from the mark stack and resets the stream's position to it
|
||||
*/
|
||||
public void reset() {
|
||||
tokenMemoryCurrent = marks.pop();
|
||||
}
|
||||
/**
|
||||
* Pops a mark from the mark stack and resets the stream's position to it
|
||||
*/
|
||||
public void reset() {
|
||||
tokenMemoryCurrent = marks.pop();
|
||||
}
|
||||
|
||||
// //////////////////////////
|
||||
// Iterator implementation
|
||||
// //////////////////////////
|
||||
|
||||
////////////////////////////
|
||||
// Iterator implementation
|
||||
////////////////////////////
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
public Token next() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent++) - tokenMemoryOffset);
|
||||
} else {
|
||||
Token result = next;
|
||||
|
||||
public Token next() {
|
||||
if (tokenMemoryCurrent < tokenMemoryOffset + tokenMemory.size()) {
|
||||
return tokenMemory.get((tokenMemoryCurrent++) - tokenMemoryOffset);
|
||||
} else {
|
||||
Token result = next;
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset++;
|
||||
} else {
|
||||
tokenMemory.add(result);
|
||||
}
|
||||
tokenMemoryCurrent++;
|
||||
|
||||
if (marks.isEmpty()) {
|
||||
tokenMemoryOffset++;
|
||||
} else {
|
||||
tokenMemory.add(result);
|
||||
}
|
||||
tokenMemoryCurrent++;
|
||||
advance();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
advance();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Not supported.");
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Not supported.");
|
||||
}
|
||||
// ////////////////////
|
||||
// Protected methods
|
||||
// ////////////////////
|
||||
|
||||
//////////////////////
|
||||
// Protected methods
|
||||
//////////////////////
|
||||
/**
|
||||
* Can be implemented by subclasses to post-process tokens.
|
||||
*
|
||||
* @param token
|
||||
* @return processed token
|
||||
*/
|
||||
protected Token process(Token token) {
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be implemented by subclasses to post-process tokens.
|
||||
*
|
||||
* @param token
|
||||
* @return processed token
|
||||
*/
|
||||
protected Token process(Token token) {
|
||||
return token;
|
||||
}
|
||||
// //////////////////
|
||||
// Private methods
|
||||
// //////////////////
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
/**
|
||||
* Advances to the next non - whitespace token.
|
||||
*/
|
||||
private void advance() {
|
||||
do {
|
||||
advanceToken();
|
||||
} while (next != null && next.getType() < 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances to the next non - whitespace token.
|
||||
*/
|
||||
private void advance() {
|
||||
do {
|
||||
advanceToken();
|
||||
} while (next != null && next.getType() < 0);
|
||||
}
|
||||
/**
|
||||
* Advances to the next token.
|
||||
*/
|
||||
private void advanceToken() {
|
||||
if (nextChar < 0) {
|
||||
next = null;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int state = 0;
|
||||
StringBuilder value = new StringBuilder();
|
||||
int tLine = line;
|
||||
int tLineOffset = lineOffset;
|
||||
while (dfa.transitions[state].containsKey(nextChar)) {
|
||||
value.append((char) nextChar);
|
||||
state = dfa.transitions[state].get(nextChar);
|
||||
line = reader.line;
|
||||
lineOffset = reader.lineOffset;
|
||||
nextChar = reader.read();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances to the next token.
|
||||
*/
|
||||
private void advanceToken() {
|
||||
if (nextChar < 0) {
|
||||
next = null;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int state = 0;
|
||||
StringBuilder value = new StringBuilder();
|
||||
int tLine = line;
|
||||
int tLineOffset = lineOffset;
|
||||
while (dfa.transitions[state].containsKey(nextChar)) {
|
||||
value.append((char)nextChar);
|
||||
state = dfa.transitions[state].get(nextChar);
|
||||
line = reader.line;
|
||||
lineOffset = reader.lineOffset;
|
||||
nextChar = reader.read();
|
||||
}
|
||||
|
||||
if (line < 0) throw new IllegalStateException("Line cannot be negative");
|
||||
|
||||
if (dfa.finals[state] != CompiledDFA.NOFINAL) {
|
||||
if (state == 0) throw new TokenException(file, line, lineOffset, (char)nextChar);
|
||||
next = process(new Token(value.toString(), dfa.finals[state], new ZenPosition(file, tLine, tLineOffset)));
|
||||
} else {
|
||||
if (line < 0)
|
||||
throw new IllegalStateException("Line cannot be negative");
|
||||
|
||||
if (dfa.finals[state] != CompiledDFA.NOFINAL) {
|
||||
if (state == 0)
|
||||
throw new TokenException(file, line, lineOffset, (char) nextChar);
|
||||
next = process(new Token(value.toString(), dfa.finals[state], new ZenPosition(file, tLine, tLineOffset)));
|
||||
} else {
|
||||
if (nextChar < 0 && value.length() == 0) {
|
||||
return; // happens on comments at the end of files
|
||||
}
|
||||
throw new TokenException(file, line, lineOffset, (char)nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
throw new TokenException(file, line, lineOffset, (char) nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
// ////////////////////////
|
||||
// Private inner classes
|
||||
// ////////////////////////
|
||||
|
||||
/**
|
||||
* Keeps a line and line offset count.
|
||||
*/
|
||||
private class CountingReader {
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
private Reader reader;
|
||||
private boolean eof;
|
||||
/**
|
||||
* Keeps a line and line offset count.
|
||||
*/
|
||||
private class CountingReader {
|
||||
private int line;
|
||||
private int lineOffset;
|
||||
private Reader reader;
|
||||
private boolean eof;
|
||||
|
||||
public CountingReader(Reader reader) {
|
||||
this.reader = reader;
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
}
|
||||
public CountingReader(Reader reader) {
|
||||
this.reader = reader;
|
||||
line = 1;
|
||||
lineOffset = 1;
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
int ch = reader.read();
|
||||
if (ch == -1) {
|
||||
eof = true;
|
||||
return ch;
|
||||
}
|
||||
if (ch == '\n') {
|
||||
line++;
|
||||
lineOffset = 1;
|
||||
} else {
|
||||
lineOffset++;
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
public int read() throws IOException {
|
||||
int ch = reader.read();
|
||||
if (ch == -1) {
|
||||
eof = true;
|
||||
return ch;
|
||||
}
|
||||
if (ch == '\n') {
|
||||
line++;
|
||||
lineOffset = 1;
|
||||
} else {
|
||||
lineOffset++;
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
+58
-57
@@ -39,7 +39,7 @@ public abstract class ParsedExpression {
|
||||
public static ParsedExpression read(ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
return readAssignExpression(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAssignExpression(ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
Token token = parser.peek();
|
||||
if (token == null) {
|
||||
@@ -47,17 +47,17 @@ public abstract class ParsedExpression {
|
||||
environment.error(position, "unexpected end of file; expression expected");
|
||||
return new ParsedExpressionInvalid(position);
|
||||
}
|
||||
|
||||
|
||||
ZenPosition position = token.getPosition();
|
||||
|
||||
|
||||
ParsedExpression left = readConditionalExpression(position, parser, environment);
|
||||
|
||||
|
||||
if (parser.peek() == null) {
|
||||
ZenPosition position2 = new ZenPosition(parser.getFile(), parser.getLine(), parser.getLineOffset());
|
||||
environment.error(position2, "unexpected end of file - ; expected");
|
||||
return new ParsedExpressionInvalid(position2);
|
||||
}
|
||||
|
||||
|
||||
switch (parser.peek().getType()) {
|
||||
case T_ASSIGN:
|
||||
parser.next();
|
||||
@@ -90,76 +90,76 @@ public abstract class ParsedExpression {
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.XOR);
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readConditionalExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readOrOrExpression(position, parser, environment);
|
||||
|
||||
|
||||
if (parser.optional(T_QUEST) != null) {
|
||||
ParsedExpression onIf = readOrOrExpression(parser.peek().getPosition(), parser, environment);
|
||||
parser.required(T_COLON, ": expected");
|
||||
ParsedExpression onElse = readConditionalExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionConditional(position, left, onIf, onElse);
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readOrOrExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAndAndExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_OR2) != null) {
|
||||
ParsedExpression right = readAndAndExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionOrOr(position, left, right);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAndAndExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readOrExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_AND2) != null) {
|
||||
ParsedExpression right = readOrExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionAndAnd(position, left, right);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readOrExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readXorExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_OR) != null) {
|
||||
ParsedExpression right = readXorExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.OR);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readXorExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAndExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_XOR) != null) {
|
||||
ParsedExpression right = readAndExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.XOR);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAndExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readCompareExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (parser.optional(T_AND) != null) {
|
||||
ParsedExpression right = readCompareExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.AND);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readCompareExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAddExpression(position, parser, environment);
|
||||
|
||||
|
||||
switch (parser.peek() == null ? -1 : parser.peek().getType()) {
|
||||
case T_EQ: {
|
||||
parser.next();
|
||||
@@ -197,13 +197,13 @@ public abstract class ParsedExpression {
|
||||
return new ParsedExpressionBinary(position, left, right, OperatorType.CONTAINS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readAddExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readMulExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_PLUS) != null) {
|
||||
ParsedExpression right = readMulExpression(parser.peek().getPosition(), parser, environment);
|
||||
@@ -220,10 +220,10 @@ public abstract class ParsedExpression {
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readMulExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readUnaryExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_MUL) != null) {
|
||||
ParsedExpression right = readUnaryExpression(parser.peek().getPosition(), parser, environment);
|
||||
@@ -238,10 +238,10 @@ public abstract class ParsedExpression {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readUnaryExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_NOT:
|
||||
@@ -260,10 +260,10 @@ public abstract class ParsedExpression {
|
||||
return readPostfixExpression(position, parser, environment);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readPostfixExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression base = readPrimaryExpression(position, parser, environment);
|
||||
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_DOT) != null) {
|
||||
Token indexString = parser.optional(T_ID);
|
||||
@@ -279,8 +279,8 @@ public abstract class ParsedExpression {
|
||||
}
|
||||
}
|
||||
} else if (parser.optional(T_DOT2) != null) {
|
||||
//ParsedExpression to = readAssignExpression(parser, errors);
|
||||
//base = base.range(position, errors, to);
|
||||
// ParsedExpression to = readAssignExpression(parser, errors);
|
||||
// base = base.range(position, errors, to);
|
||||
throw new RuntimeException("not yet supported");
|
||||
} else if (parser.optional(T_SQBROPEN) != null) {
|
||||
ParsedExpression index = readAssignExpression(parser, environment);
|
||||
@@ -303,10 +303,10 @@ public abstract class ParsedExpression {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
|
||||
private static ParsedExpression readPrimaryExpression(ZenPosition position, ZenTokener parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_INTVALUE:
|
||||
@@ -321,20 +321,19 @@ public abstract class ParsedExpression {
|
||||
return new ParsedExpressionValue(
|
||||
position,
|
||||
new ExpressionString(position, unescapeString(parser.next().getValue())));
|
||||
/*case T_DOLLAR: {
|
||||
Expression result = new ExpressionDollar(position);
|
||||
if (parser.isNext(T_STRINGVALUE)) {
|
||||
return new ExpressionIndexString(position, result, unescapeString(parser.next().getValue()));
|
||||
}
|
||||
return result;
|
||||
}*/
|
||||
/*
|
||||
* case T_DOLLAR: { Expression result = new
|
||||
* ExpressionDollar(position); if (parser.isNext(T_STRINGVALUE))
|
||||
* { return new ExpressionIndexString(position, result,
|
||||
* unescapeString(parser.next().getValue())); } return result; }
|
||||
*/
|
||||
case T_ID:
|
||||
return new ParsedExpressionVariable(position, parser.next().getValue());
|
||||
case T_FUNCTION:
|
||||
// function (argname, argname, ...) { ...contents... }
|
||||
parser.next();
|
||||
parser.required(T_BROPEN, "( expected");
|
||||
|
||||
|
||||
List<ParsedFunctionArgument> arguments = new ArrayList<ParsedFunctionArgument>();
|
||||
if (parser.optional(T_BRCLOSE) == null) {
|
||||
do {
|
||||
@@ -344,27 +343,27 @@ public abstract class ParsedExpression {
|
||||
if (parser.optional(T_AS) != null) {
|
||||
type = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
arguments.add(new ParsedFunctionArgument(name, type));
|
||||
} while (parser.optional(T_COMMA) != null);
|
||||
|
||||
|
||||
parser.required(T_BRCLOSE, ") expected");
|
||||
}
|
||||
|
||||
|
||||
ZenType returnType = ZenTypeAny.INSTANCE;
|
||||
if (parser.optional(T_AS) != null) {
|
||||
returnType = ZenType.read(parser, environment);
|
||||
}
|
||||
|
||||
|
||||
parser.required(T_AOPEN, "{ expected");
|
||||
|
||||
|
||||
List<Statement> statements = new ArrayList<Statement>();
|
||||
if (parser.optional(T_ACLOSE) == null) {
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
statements.add(Statement.read(parser, environment, returnType));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ParsedExpressionFunction(position, returnType, arguments, statements);
|
||||
case T_LT: {
|
||||
Token start = parser.next();
|
||||
@@ -380,12 +379,13 @@ public abstract class ParsedExpression {
|
||||
builder.append('<');
|
||||
Token last = null;
|
||||
for (Token token : tokens) {
|
||||
if (last != null) builder.append(' ');
|
||||
if (last != null)
|
||||
builder.append(' ');
|
||||
builder.append(token.getValue());
|
||||
last = token;
|
||||
}
|
||||
builder.append('>');
|
||||
|
||||
|
||||
parser.getEnvironment().getErrorLogger().error(start.getPosition(), "Could not resolve " + builder.toString());
|
||||
return new ParsedExpressionInvalid(start.getPosition());
|
||||
} else {
|
||||
@@ -410,7 +410,7 @@ public abstract class ParsedExpression {
|
||||
}
|
||||
case T_AOPEN: {
|
||||
parser.next();
|
||||
|
||||
|
||||
List<ParsedExpression> keys = new ArrayList<ParsedExpression>();
|
||||
List<ParsedExpression> values = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_ACLOSE) == null) {
|
||||
@@ -418,13 +418,13 @@ public abstract class ParsedExpression {
|
||||
keys.add(readAssignExpression(parser, environment));
|
||||
parser.required(T_COLON, ": expected");
|
||||
values.add(readAssignExpression(parser, environment));
|
||||
|
||||
|
||||
if (parser.optional(T_COMMA) == null) {
|
||||
parser.required(T_ACLOSE, "} or , expected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ParsedExpressionMap(position, keys, values);
|
||||
}
|
||||
}
|
||||
@@ -447,18 +447,19 @@ public abstract class ParsedExpression {
|
||||
throw new ParseException(last, "Invalid expression, last token: " + last.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private final ZenPosition position;
|
||||
|
||||
|
||||
public ParsedExpression(ZenPosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
public abstract IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType);
|
||||
|
||||
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return compile(environment, predictedType).eval(environment);
|
||||
}
|
||||
|
||||
+2
-2
@@ -19,10 +19,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionAndAnd extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
|
||||
|
||||
public ParsedExpressionAndAnd(ZenPosition position, ParsedExpression left, ParsedExpression right) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
+5
-5
@@ -25,10 +25,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ParsedExpressionArray extends ParsedExpression {
|
||||
private final List<ParsedExpression> contents;
|
||||
|
||||
|
||||
public ParsedExpressionArray(ZenPosition position, List<ParsedExpression> contents) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class ParsedExpressionArray extends ParsedExpression {
|
||||
ZenType predictedBaseType = null;
|
||||
ZenTypeArrayBasic arrayType = ZenType.ANYARRAY;
|
||||
ICastingRule castingRule = null;
|
||||
|
||||
|
||||
if (predictedType instanceof ZenTypeArray) {
|
||||
predictedBaseType = ((ZenTypeArray) predictedType).getBaseType();
|
||||
if (predictedType instanceof ZenTypeArrayBasic) {
|
||||
@@ -60,7 +60,7 @@ public class ParsedExpressionArray extends ParsedExpression {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Expression[] cContents = new Expression[contents.size()];
|
||||
for (int i = 0; i < contents.size(); i++) {
|
||||
cContents[i] = contents.get(i).compile(environment, predictedBaseType).eval(environment);
|
||||
@@ -72,7 +72,7 @@ public class ParsedExpressionArray extends ParsedExpression {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
if (contents.size() == 1 && contents.get(0) instanceof ParsedExpressionVariable) {
|
||||
|
||||
+5
-5
@@ -18,19 +18,19 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionAssign extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
|
||||
|
||||
public ParsedExpressionAssign(ZenPosition position, ParsedExpression left, ParsedExpression right) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
IPartialExpression cLeft = left.compile(environment, predictedType);
|
||||
return cLeft
|
||||
.assign(getPosition(), environment,
|
||||
right.compile(environment, cLeft.getType()).eval(environment));
|
||||
.assign(getPosition(), environment,
|
||||
right.compile(environment, cLeft.getType()).eval(environment));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -21,10 +21,10 @@ public class ParsedExpressionBinary extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
private final OperatorType operator;
|
||||
|
||||
|
||||
public ParsedExpressionBinary(ZenPosition position, ParsedExpression left, ParsedExpression right, OperatorType operator) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.operator = operator;
|
||||
|
||||
+2
-2
@@ -18,10 +18,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ParsedExpressionBool extends ParsedExpression {
|
||||
private final boolean value;
|
||||
|
||||
|
||||
public ParsedExpressionBool(ZenPosition position, boolean value) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -20,25 +20,25 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionCall extends ParsedExpression {
|
||||
private final ParsedExpression receiver;
|
||||
private final List<ParsedExpression> arguments;
|
||||
|
||||
|
||||
public ParsedExpressionCall(ZenPosition position, ParsedExpression receiver, List<ParsedExpression> arguments) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.receiver = receiver;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
IPartialExpression cReceiver = receiver.compile(environment, null);
|
||||
ZenType[] predictedTypes = cReceiver.predictCallTypes(arguments.size());
|
||||
|
||||
|
||||
Expression[] cArguments = new Expression[arguments.size()];
|
||||
for (int i = 0; i < cArguments.length; i++) {
|
||||
IPartialExpression cArgument = arguments.get(i).compile(environment, predictedTypes[i]);
|
||||
cArguments[i] = cArgument.eval(environment);
|
||||
}
|
||||
|
||||
|
||||
return cReceiver.call(getPosition(), environment, cArguments);
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -18,10 +18,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionCast extends ParsedExpression {
|
||||
private final ParsedExpression value;
|
||||
private final ZenType type;
|
||||
|
||||
|
||||
public ParsedExpressionCast(ZenPosition position, ParsedExpression value, ZenType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
}
|
||||
@@ -29,8 +29,8 @@ public class ParsedExpressionCast extends ParsedExpression {
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return value
|
||||
.compile(environment, type)
|
||||
.eval(environment)
|
||||
.cast(getPosition(), environment, type);
|
||||
.compile(environment, type)
|
||||
.eval(environment)
|
||||
.cast(getPosition(), environment, type);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -21,14 +21,14 @@ public class ParsedExpressionCompare extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
private final CompareType type;
|
||||
|
||||
|
||||
public ParsedExpressionCompare(
|
||||
ZenPosition position,
|
||||
ParsedExpression left,
|
||||
ParsedExpression right,
|
||||
CompareType type) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.type = type;
|
||||
|
||||
+2
-2
@@ -20,10 +20,10 @@ public class ParsedExpressionConditional extends ParsedExpression {
|
||||
private final ParsedExpression condition;
|
||||
private final ParsedExpression ifThen;
|
||||
private final ParsedExpression ifElse;
|
||||
|
||||
|
||||
public ParsedExpressionConditional(ZenPosition position, ParsedExpression condition, ParsedExpression ifThen, ParsedExpression ifElse) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.condition = condition;
|
||||
this.ifThen = ifThen;
|
||||
this.ifElse = ifElse;
|
||||
|
||||
+4
-4
@@ -28,10 +28,10 @@ public class ParsedExpressionFunction extends ParsedExpression {
|
||||
private final ZenType returnType;
|
||||
private final List<ParsedFunctionArgument> arguments;
|
||||
private final List<Statement> statements;
|
||||
|
||||
|
||||
public ParsedExpressionFunction(ZenPosition position, ZenType returnType, List<ParsedFunctionArgument> arguments, List<Statement> statements) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.returnType = returnType;
|
||||
this.arguments = arguments;
|
||||
this.statements = statements;
|
||||
@@ -41,7 +41,7 @@ public class ParsedExpressionFunction extends ParsedExpression {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
if (predictedType != null && predictedType instanceof ZenTypeNative) {
|
||||
System.out.println("Known predicted function type: " + predictedType);
|
||||
|
||||
|
||||
ZenTypeNative nativeType = (ZenTypeNative) predictedType;
|
||||
Class nativeClass = nativeType.getNativeClass();
|
||||
if (nativeClass.isInterface() && nativeClass.getMethods().length == 1) {
|
||||
@@ -64,7 +64,7 @@ public class ParsedExpressionFunction extends ParsedExpression {
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new ExpressionJavaLambda(
|
||||
getPosition(),
|
||||
nativeClass,
|
||||
|
||||
+2
-2
@@ -20,10 +20,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionIndex extends ParsedExpression {
|
||||
private final ParsedExpression value;
|
||||
private final ParsedExpression index;
|
||||
|
||||
|
||||
public ParsedExpressionIndex(ZenPosition position, ParsedExpression value, ParsedExpression index) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
+4
-4
@@ -21,10 +21,10 @@ public class ParsedExpressionIndexSet extends ParsedExpression {
|
||||
private final ParsedExpression value;
|
||||
private final ParsedExpression index;
|
||||
private final ParsedExpression setValue;
|
||||
|
||||
|
||||
public ParsedExpressionIndexSet(ZenPosition position, ParsedExpression value, ParsedExpression index, ParsedExpression setValue) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.index = index;
|
||||
this.setValue = setValue;
|
||||
@@ -36,6 +36,6 @@ public class ParsedExpressionIndexSet extends ParsedExpression {
|
||||
Expression cValue = value.compile(environment, null).eval(environment);
|
||||
Expression cIndex = index.compile(environment, null).eval(environment);
|
||||
Expression cSetValue = setValue.compile(environment, null).eval(environment);
|
||||
return cValue.getType().trinary(getPosition(), environment, cValue, cIndex, cSetValue,OperatorType.INDEXSET);
|
||||
}
|
||||
return cValue.getType().trinary(getPosition(), environment, cValue, cIndex, cSetValue, OperatorType.INDEXSET);
|
||||
}
|
||||
}
|
||||
+6
-6
@@ -24,13 +24,13 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionMap extends ParsedExpression {
|
||||
private final List<ParsedExpression> keys;
|
||||
private final List<ParsedExpression> values;
|
||||
|
||||
|
||||
public ParsedExpressionMap(
|
||||
ZenPosition position,
|
||||
List<ParsedExpression> keys,
|
||||
List<ParsedExpression> values) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class ParsedExpressionMap extends ParsedExpression {
|
||||
ZenType predictedValueType = null;
|
||||
ICastingRule castingRule = null;
|
||||
ZenTypeAssociative mapType = ZenType.ANYMAP;
|
||||
|
||||
|
||||
if (predictedType instanceof ZenTypeAssociative) {
|
||||
ZenTypeAssociative inputType = (ZenTypeAssociative) predictedType;
|
||||
predictedKeyType = inputType.getKeyType();
|
||||
@@ -60,15 +60,15 @@ public class ParsedExpressionMap extends ParsedExpression {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Expression[] cKeys = new Expression[keys.size()];
|
||||
Expression[] cValues = new Expression[values.size()];
|
||||
|
||||
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
cKeys[i] = keys.get(i).compileKey(environment, predictedKeyType);
|
||||
cValues[i] = values.get(i).compile(environment, predictedValueType).eval(environment);
|
||||
}
|
||||
|
||||
|
||||
Expression result = new ExpressionMap(getPosition(), cKeys, cValues, mapType);
|
||||
if (castingRule != null) {
|
||||
return new ExpressionAs(getPosition(), result, castingRule);
|
||||
|
||||
+2
-2
@@ -18,10 +18,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionMember extends ParsedExpression {
|
||||
private final ParsedExpression value;
|
||||
private final String member;
|
||||
|
||||
|
||||
public ParsedExpressionMember(ZenPosition position, ParsedExpression value, String member) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.member = member;
|
||||
}
|
||||
|
||||
+5
-5
@@ -21,23 +21,23 @@ public class ParsedExpressionOpAssign extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
private final OperatorType operator;
|
||||
|
||||
|
||||
public ParsedExpressionOpAssign(ZenPosition position, ParsedExpression left, ParsedExpression right, OperatorType operator) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
// TODO: validate if the prediction rules are sound
|
||||
Expression cLeft = left.compile(environment, predictedType).eval(environment);
|
||||
Expression cRight = right.compile(environment, cLeft.getType()).eval(environment);
|
||||
|
||||
|
||||
Expression value = cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
|
||||
|
||||
|
||||
return left.compile(environment, predictedType).assign(getPosition(), environment, value);
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -20,10 +20,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionOrOr extends ParsedExpression {
|
||||
private final ParsedExpression left;
|
||||
private final ParsedExpression right;
|
||||
|
||||
|
||||
public ParsedExpressionOrOr(ZenPosition position, ParsedExpression left, ParsedExpression right) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class ParsedExpressionOrOr extends ParsedExpression {
|
||||
public IPartialExpression compile(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
Expression cLeft = left.compile(environment, predictedType).eval(environment);
|
||||
Expression cRight = right.compile(environment, predictedType).eval(environment);
|
||||
|
||||
|
||||
ZenType type;
|
||||
if (cRight.getType().canCastImplicit(cLeft.getType(), environment)) {
|
||||
type = cLeft.getType();
|
||||
@@ -42,7 +42,7 @@ public class ParsedExpressionOrOr extends ParsedExpression {
|
||||
environment.error(getPosition(), "These types could not be unified: " + cLeft.getType() + " and " + cRight.getType());
|
||||
type = ZenType.ANY;
|
||||
}
|
||||
|
||||
|
||||
return new ExpressionOrOr(
|
||||
getPosition(),
|
||||
cLeft.cast(getPosition(), environment, type),
|
||||
|
||||
+2
-2
@@ -20,10 +20,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
public class ParsedExpressionUnary extends ParsedExpression {
|
||||
private final ParsedExpression value;
|
||||
private final OperatorType operator;
|
||||
|
||||
|
||||
public ParsedExpressionUnary(ZenPosition position, ParsedExpression value, OperatorType operator) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.value = value;
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
+5
-4
@@ -17,12 +17,13 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ParsedExpressionValue extends ParsedExpression {
|
||||
private final IPartialExpression value;
|
||||
|
||||
|
||||
public ParsedExpressionValue(ZenPosition position, IPartialExpression value) {
|
||||
super(position);
|
||||
|
||||
if (value == null) throw new IllegalArgumentException("value cannot be null");
|
||||
|
||||
|
||||
if (value == null)
|
||||
throw new IllegalArgumentException("value cannot be null");
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
+6
-5
@@ -20,10 +20,10 @@ import stanhebben.zenscript.util.ZenPosition;
|
||||
*/
|
||||
public class ParsedExpressionVariable extends ParsedExpression {
|
||||
private final String name;
|
||||
|
||||
|
||||
public ParsedExpressionVariable(ZenPosition position, String name) {
|
||||
super(position);
|
||||
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -35,8 +35,9 @@ public class ParsedExpressionVariable extends ParsedExpression {
|
||||
environment.error(getPosition(), "could not find " + name);
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
|
||||
// enable usage of static members of the same type as the predicted type (eg. enum values)
|
||||
|
||||
// enable usage of static members of the same type as the predicted
|
||||
// type (eg. enum values)
|
||||
IPartialExpression member = predictedType.getStaticMember(getPosition(), environment, name);
|
||||
if (member == null || member.getType().getCastingRule(predictedType, environment) == null) {
|
||||
environment.error(getPosition(), "could not find " + name);
|
||||
@@ -48,7 +49,7 @@ public class ParsedExpressionVariable extends ParsedExpression {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment, ZenType predictedType) {
|
||||
return new ExpressionString(getPosition(), name);
|
||||
|
||||
Reference in new issue
Block a user