mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-17 10:55:04 +00:00
Added projects and basic build scripts. Project for scripting engine, API, Main 1.7 version and one for each mod supported in 1.7.
This commit is contained in:
1 parent
219106f392
commit
38b78ce305
281 files changed
+25090
-49
No files matched your search
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* This class is similar to an ArrayList, but is optimized for integers.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class ArrayListI {
|
||||
private int[] data;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// 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;
|
||||
}
|
||||
|
||||
public int[] toArray() {
|
||||
int[] result = new int[size];
|
||||
System.arraycopy(data, 0, result, 0, size);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* Represents a character stream. Suitable for character by character parsing.
|
||||
*
|
||||
* Used in the regular expression parser.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CharStream {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 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++];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
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.
|
||||
*
|
||||
* A compiled DFA can be converted to a compact integer array. This array can
|
||||
* then be hardcoded in an application.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class CompiledDFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++];
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]);
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* Implements a DFA.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class DFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
|
||||
private DFAState 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);
|
||||
|
||||
/* 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();
|
||||
|
||||
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];
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
/* 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();
|
||||
|
||||
nodeMap.get(i).addTransition(k, nodeMap.get(transitions[i].get(k)));
|
||||
}
|
||||
}
|
||||
|
||||
DFA result = new DFA(nodeMap.get(0));
|
||||
return result;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
/////////////////////////
|
||||
// Public inner classes
|
||||
/////////////////////////
|
||||
|
||||
/**
|
||||
* Represents a state in a DFA.
|
||||
*/
|
||||
public static class DFAState {
|
||||
private HashMapI<DFAState> transitions;
|
||||
private int finalCode = NOFINAL;
|
||||
|
||||
/**
|
||||
* Creates a new DFA state.
|
||||
*/
|
||||
public DFAState() {
|
||||
transitions = new HashMapI<DFAState>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* A HashMap implementation which is optimized for integer keys.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
* @param <T>
|
||||
*/
|
||||
public class HashMapI<T> {
|
||||
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;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
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 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 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
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* 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 < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
int index = key & mask;
|
||||
|
||||
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;
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
}
|
||||
|
||||
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 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 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 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 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
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* 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 < newKeys.length; i++) {
|
||||
newKeys[i] = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] == Integer.MIN_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int key = keys[i];
|
||||
int 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;
|
||||
|
||||
newKeys[index] = key;
|
||||
newValues[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
keys = newKeys;
|
||||
values = newValues;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return i < keys.length;
|
||||
}
|
||||
|
||||
public int next() {
|
||||
int result = keys[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < keys.length && keys[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* HashSet implementation which is optimized for integer values.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class HashSetI {
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
newKeys[index] = key;
|
||||
}
|
||||
}
|
||||
|
||||
values = newKeys;
|
||||
next = newNext;
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// Private inner classes
|
||||
//////////////////////////
|
||||
|
||||
private class KeyIterator implements IteratorI {
|
||||
private int i;
|
||||
|
||||
public KeyIterator() {
|
||||
i = 0;
|
||||
skip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < values.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int next() {
|
||||
int result = values[i++];
|
||||
skip();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void skip() {
|
||||
while (i < values.length && values[i] == Integer.MIN_VALUE) i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* This interface represents an iterator over integer values.
|
||||
*
|
||||
* @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 the next element in this iteration.
|
||||
*
|
||||
* @return the next element in this iteration
|
||||
*/
|
||||
public int next();
|
||||
}
|
||||
@@ -0,0 +1,574 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class NFA {
|
||||
public static final int NOFINAL = Integer.MIN_VALUE;
|
||||
public static final int EPSILON = Integer.MIN_VALUE + 1;
|
||||
|
||||
private final NFAState initial;
|
||||
|
||||
private HashMap<NodeSet, DFA.DFAState> converted;
|
||||
|
||||
/**
|
||||
* Creates a new NFA with the specified initial state.
|
||||
*
|
||||
* @param initial initial state
|
||||
*/
|
||||
public NFA(NFAState initial) {
|
||||
this.initial = initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new NFA from the specified regular expression.
|
||||
*
|
||||
* Note: the regular expression implementation is not complete. Shorthand
|
||||
* character classes (\s, \w, \d), hexadecimal and unicode escape sequences and
|
||||
* unicode properties are not implemented.
|
||||
*
|
||||
* Anchors, lazy plus operators, lookbehind and lookforward are not implemented
|
||||
* since they cannot be implemented in an NFA.
|
||||
*
|
||||
* @param regexp regular expression
|
||||
*/
|
||||
public NFA(String regexp) {
|
||||
Partial main = processRegExp(new CharStream(regexp));
|
||||
initial = new NFAState();
|
||||
initial.addTransition(main.tailLabel, main.tail);
|
||||
main.head.setFinal(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of regular expressions to an NFA. Each regular expression
|
||||
* can have its own final class. The length of both arrays must match.
|
||||
*
|
||||
* @param regexp regular expression array
|
||||
* @param finals final classes
|
||||
*/
|
||||
public NFA(String[] regexp, int[] finals) {
|
||||
initial = new NFAState();
|
||||
|
||||
Partial[] partials = new Partial[regexp.length];
|
||||
for (int i = 0; i < regexp.length; i++) {
|
||||
//System.out.println("Processing regexp " + i + ": " + regexp[i]);
|
||||
partials[i] = processRegExp(new CharStream(regexp[i]));
|
||||
partials[i].head.setFinal(finals[i]);
|
||||
initial.addTransition(partials[i].tailLabel, partials[i].tail);
|
||||
}
|
||||
|
||||
//System.out.println("NFA generated");
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this NFA to a DFA. The resulting DFA is not optimal. If the
|
||||
* NFA is ambiguous, the token with lowest value will receive preference.
|
||||
*
|
||||
* @return this NFA as DFA
|
||||
*/
|
||||
public DFA toDFA() {
|
||||
converted = new HashMap<NodeSet, DFA.DFAState>();
|
||||
HashSet<NFAState> closure = new HashSet<NFAState>();
|
||||
this.initial.closure(closure);
|
||||
DFA.DFAState init = convert(new NodeSet(closure));
|
||||
|
||||
DFA dfs = new DFA(init);
|
||||
return dfs;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
|
||||
/* Converts a set of possible states to a DFA state. */
|
||||
private DFA.DFAState convert(NodeSet nodes) {
|
||||
if (!converted.containsKey(nodes)) {
|
||||
DFA.DFAState node = new DFA.DFAState();
|
||||
converted.put(nodes, node);
|
||||
|
||||
HashSet<Integer> edgeSet = new HashSet<Integer>();
|
||||
for (NFAState n : nodes.nodes) {
|
||||
n.alphabet(edgeSet);
|
||||
}
|
||||
for (int i : edgeSet) {
|
||||
HashSet<NFAState> edge = new HashSet<NFAState>();
|
||||
for (NFAState n : nodes.nodes) {
|
||||
n.edge(i, edge);
|
||||
}
|
||||
NodeSet set = new NodeSet(edge);
|
||||
node.addTransition(i, convert(set));
|
||||
}
|
||||
int finalCode = DFA.NOFINAL;
|
||||
for (NFAState n : nodes.nodes) {
|
||||
if (n.finalCode != NOFINAL) {
|
||||
if (finalCode != DFA.NOFINAL) {
|
||||
finalCode = Math.min(n.finalCode, finalCode);
|
||||
} else {
|
||||
finalCode = n.finalCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
node.setFinal(finalCode);
|
||||
|
||||
return node;
|
||||
}
|
||||
return converted.get(nodes);
|
||||
}
|
||||
|
||||
/* Processes a regular expression */
|
||||
private Partial processRegExp(CharStream stream) {
|
||||
Partial partial = processRegExp0(stream);
|
||||
if (stream.optional('|')) {
|
||||
ArrayList<Partial> partials = new ArrayList<Partial>();
|
||||
partials.add(partial);
|
||||
partials.add(processRegExp0(stream));
|
||||
while (stream.optional('|')) {
|
||||
partials.add(processRegExp0(stream));
|
||||
}
|
||||
NFAState head = new NFAState();
|
||||
NFAState tail = new NFAState();
|
||||
for (Partial p : partials) {
|
||||
tail.addTransition(p.tailLabel, p.tail);
|
||||
p.head.addTransition(EPSILON, head);
|
||||
}
|
||||
return new Partial(EPSILON, tail, head);
|
||||
} else {
|
||||
return partial;
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes an element of an alternation clause */
|
||||
private Partial processRegExp0(CharStream stream) {
|
||||
Partial partial = processRegExp1(stream);
|
||||
while (!stream.peek(')') && !stream.peek('|') && stream.hasMore()) {
|
||||
Partial partial2 = processRegExp1(stream);
|
||||
partial.head.addTransition(partial2.tailLabel, partial2.tail);
|
||||
partial = new Partial(partial.tailLabel, partial.tail, partial2.head);
|
||||
}
|
||||
return partial;
|
||||
}
|
||||
|
||||
/* Processes a partial with optional repetition */
|
||||
private Partial processRegExp1(CharStream stream) {
|
||||
Partial partial = processPartial(stream);
|
||||
if (stream.optional('*')) {
|
||||
NFAState node = new NFAState();
|
||||
partial.head.addTransition(EPSILON, node);
|
||||
node.addTransition(partial.tailLabel, partial.tail);
|
||||
return new Partial(EPSILON, node, node);
|
||||
} else if (stream.optional('+')) {
|
||||
NFAState node = new NFAState();
|
||||
partial.head.addTransition(EPSILON, node);
|
||||
node.addTransition(partial.tailLabel, partial.tail);
|
||||
return new Partial(partial.tailLabel, partial.tail, node);
|
||||
} else if (stream.optional('?')) {
|
||||
NFAState node = new NFAState();
|
||||
node.addTransition(EPSILON, partial.head);
|
||||
node.addTransition(partial.tailLabel, partial.tail);
|
||||
return new Partial(EPSILON, node, partial.head);
|
||||
} else if (stream.optional('{')) {
|
||||
int amount = processInt(stream);
|
||||
if (amount < 0) throw new IllegalArgumentException("Repitition count expected");
|
||||
if (stream.optional(',')) {
|
||||
int amount2 = processInt(stream);
|
||||
stream.required('}');
|
||||
if (amount2 < 0) {
|
||||
// unbounded
|
||||
Partial[] duplicates = new Partial[amount];
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i] = partial.duplicate();
|
||||
}
|
||||
duplicates[amount - 1] = partial;
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i].head.addTransition(duplicates[i + 1].tailLabel, duplicates[i + 1].tail);
|
||||
}
|
||||
duplicates[amount - 1].head.addTransition(duplicates[amount - 1].tailLabel, duplicates[amount - 1].tail);
|
||||
return new Partial(duplicates[0].tailLabel, duplicates[0].tail, duplicates[amount - 1].head);
|
||||
} else {
|
||||
Partial[] duplicates = new Partial[amount2];
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i] = partial.duplicate();
|
||||
}
|
||||
duplicates[amount2 - 1] = partial;
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i].head.addTransition(duplicates[i + 1].tailLabel, duplicates[i + 1].tail);
|
||||
}
|
||||
for (int i = amount; i < amount2; i++) {
|
||||
if (i == 0) {
|
||||
/* insert additional node before the chain because minimal repeat is 0 */
|
||||
NFAState additional = new NFAState();
|
||||
additional.addTransition(duplicates[0].tailLabel, duplicates[0].tail);
|
||||
additional.addTransition(EPSILON, duplicates[amount2 - 1].head);
|
||||
duplicates[0].tailLabel = EPSILON;
|
||||
duplicates[0].tail = additional;
|
||||
} else {
|
||||
duplicates[i - 1].head.addTransition(EPSILON, duplicates[amount2 - 1].head);
|
||||
}
|
||||
}
|
||||
return new Partial(duplicates[0].tailLabel, duplicates[0].tail, duplicates[amount2 - 1].head);
|
||||
}
|
||||
} else {
|
||||
stream.required('}');
|
||||
|
||||
Partial[] duplicates = new Partial[amount];
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i] = partial.duplicate();
|
||||
}
|
||||
duplicates[amount - 1] = partial;
|
||||
for (int i = 0; i < duplicates.length - 1; i++) {
|
||||
duplicates[i].head.addTransition(duplicates[i + 1].tailLabel, duplicates[i + 1].tail);
|
||||
}
|
||||
return new Partial(duplicates[0].tailLabel, duplicates[0].tail, duplicates[amount - 1].head);
|
||||
}
|
||||
} else {
|
||||
return partial;
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a part of a regular expression, which can be a character,
|
||||
* expression between brackets, a dot or a character list */
|
||||
private Partial processPartial(CharStream stream) {
|
||||
if (stream.optional('(')) {
|
||||
Partial result = processRegExp(stream);
|
||||
stream.required(')');
|
||||
return result;
|
||||
} else if (stream.optional('[')) {
|
||||
NFAState head = new NFAState();
|
||||
NFAState tail = new NFAState();
|
||||
|
||||
IteratorI iter = processCharList(stream).iterator();
|
||||
while (iter.hasNext()) {
|
||||
tail.addTransition(iter.next(), head);
|
||||
}
|
||||
stream.required(']');
|
||||
return new Partial(EPSILON, tail, head);
|
||||
} else if (stream.optional('.')) {
|
||||
NFAState head = new NFAState();
|
||||
NFAState tail = new NFAState();
|
||||
|
||||
for (int i = 0; i <= 256; i++) {
|
||||
tail.addTransition(i, head);
|
||||
}
|
||||
return new Partial(EPSILON, tail, head);
|
||||
} else {
|
||||
NFAState node = new NFAState();
|
||||
return new Partial(processChar(stream), node, node);
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a character list */
|
||||
private HashSetI processCharList(CharStream stream) {
|
||||
boolean invert = stream.optional('^');
|
||||
HashSetI base = new HashSetI();
|
||||
do {
|
||||
processCharPartial(base, stream);
|
||||
} while (!stream.peek(']'));
|
||||
if (invert) {
|
||||
HashSetI result = new HashSetI();
|
||||
for (int i = 0; i <= 256; i++) {
|
||||
if (!base.contains(i)) result.add(i);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
return base;
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a character partial, which can be a single character or a range
|
||||
* of characters. */
|
||||
private void processCharPartial(HashSetI out, CharStream stream) {
|
||||
if (stream.optional('.')) {
|
||||
for (int i = 0; i <= 256; i++) {
|
||||
out.add(i);
|
||||
}
|
||||
} else {
|
||||
char from = processChar(stream);
|
||||
if (stream.optional('-')) {
|
||||
char to = processChar(stream);
|
||||
for (int i = from; i <= to; i++) {
|
||||
out.add(i);
|
||||
}
|
||||
} else {
|
||||
out.add(from);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes a single character */
|
||||
private char processChar(CharStream stream) {
|
||||
if (stream.optional('\\')) {
|
||||
if (stream.optional('r')) return '\r';
|
||||
if (stream.optional('n')) return '\n';
|
||||
if (stream.optional('t')) return '\t';
|
||||
if (stream.optional('[')) return '[';
|
||||
if (stream.optional(']')) return ']';
|
||||
if (stream.optional('(')) return '(';
|
||||
if (stream.optional(')')) return ')';
|
||||
if (stream.optional('.')) return '.';
|
||||
if (stream.optional('+')) return '+';
|
||||
if (stream.optional('-')) return '-';
|
||||
if (stream.optional('\\')) return '\\';
|
||||
if (stream.optional('{')) return '{';
|
||||
if (stream.optional('}')) return '}';
|
||||
if (stream.optional('?')) return '?';
|
||||
if (stream.optional('*')) return '*';
|
||||
if (stream.optional('~')) return '~';
|
||||
if (stream.optional('|')) return '|';
|
||||
if (stream.optional('^')) return '^';
|
||||
throw new IllegalArgumentException("Invalid character: " + stream.next());
|
||||
} else {
|
||||
if (stream.peek('[')
|
||||
|| stream.peek(']')
|
||||
|| stream.peek('(')
|
||||
|| stream.peek(')')
|
||||
|| stream.peek('{')
|
||||
|| stream.peek('}')
|
||||
|| stream.peek('.')
|
||||
|| stream.peek('-')
|
||||
|| stream.peek('+')
|
||||
|| stream.peek('?')
|
||||
|| stream.peek('*')) {
|
||||
throw new IllegalArgumentException("Invalid character: " + stream.next());
|
||||
} else {
|
||||
return stream.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Processes an optional integer, returns -1 if the next character do not
|
||||
* represent an integer */
|
||||
private int processInt(CharStream stream) {
|
||||
int data = stream.optional('0', '9') - '0';
|
||||
if (data < 0) return -1;
|
||||
char ch = stream.optional('0', '9');
|
||||
while (ch != 0) {
|
||||
data = data * 10 + (ch - '0');
|
||||
ch = stream.optional('0', '9');
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/////////////////////////
|
||||
// Public inner classes
|
||||
/////////////////////////
|
||||
|
||||
/**
|
||||
* Represents an NFA state.
|
||||
*/
|
||||
public static class NFAState {
|
||||
private static int counter = 1;
|
||||
|
||||
private ArrayList<Transition> transitions;
|
||||
private ArrayList<NFAState> closure;
|
||||
private int index;
|
||||
private int finalCode = NOFINAL;
|
||||
|
||||
/**
|
||||
* Creates a new state.
|
||||
*/
|
||||
public NFAState() {
|
||||
transitions = new ArrayList<Transition>();
|
||||
index = counter++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a transition.
|
||||
*
|
||||
* @param label transition label
|
||||
* @param next next state
|
||||
*/
|
||||
public void addTransition(int label, NFAState next) {
|
||||
transitions.add(new Transition(label, next));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the final state of this state.
|
||||
*
|
||||
* @param finalCode
|
||||
*/
|
||||
public void setFinal(int finalCode) {
|
||||
if (this.finalCode == finalCode) return;
|
||||
this.finalCode = finalCode;
|
||||
|
||||
for (Transition t : transitions) {
|
||||
if (t.label == EPSILON) {
|
||||
t.next.setFinal(finalCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* Determines the (partial) closure of this state.
|
||||
*
|
||||
* @param output output to store the closure in
|
||||
*/
|
||||
private void closure(HashSet<NFAState> output) {
|
||||
for (NFAState node : closure()) output.add(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the (full) closure of this state.
|
||||
*
|
||||
* @return this state's closure
|
||||
*/
|
||||
private ArrayList<NFAState> closure() {
|
||||
if (closure == null) {
|
||||
HashSet<NFAState> tmp = new HashSet<NFAState>();
|
||||
tmp.add(this);
|
||||
for (Transition transition : transitions) {
|
||||
if (transition.label == EPSILON) {
|
||||
if (!tmp.contains(transition.next)) {
|
||||
tmp.add(transition.next);
|
||||
transition.next.closure(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
closure = new ArrayList<NFAState>();
|
||||
for (NFAState node : tmp) closure.add(node);
|
||||
}
|
||||
return closure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the possible set of states after a transition with the
|
||||
* specified value
|
||||
*
|
||||
* @param label transition label
|
||||
* @param output possible set of states (out)
|
||||
*/
|
||||
private void edge(int label, HashSet<NFAState> output) {
|
||||
for (Transition transition : transitions) {
|
||||
if (transition.label == label) {
|
||||
if (!output.contains(transition.next)) {
|
||||
output.add(transition.next);
|
||||
transition.next.closure(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the alphabet of this state.
|
||||
*
|
||||
* @param output alphabet (out)
|
||||
*/
|
||||
private void alphabet(HashSet<Integer> output) {
|
||||
for (NFAState node : closure()) {
|
||||
for (Transition t : node.transitions) {
|
||||
if (t.label != EPSILON) {
|
||||
output.add(t.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////
|
||||
// Inner class
|
||||
////////////////
|
||||
|
||||
/* Represents a transition */
|
||||
private class Transition {
|
||||
private int label;
|
||||
private NFAState next;
|
||||
|
||||
public Transition(int input, NFAState next) {
|
||||
this.label = input;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Hashable set of nodes */
|
||||
private class NodeSet {
|
||||
private NFAState[] nodes;
|
||||
|
||||
public NodeSet(HashSet<NFAState> nodes) {
|
||||
this.nodes = new NFAState[nodes.size()];
|
||||
int i = 0;
|
||||
for (NFAState node : nodes) this.nodes[i++] = node;
|
||||
Arrays.sort(this.nodes, new Comparator<NFAState>() {
|
||||
public int compare(NFAState o1, NFAState o2) {
|
||||
return o1.index - o2.index;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(nodes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other.getClass() != NodeSet.class) return false;
|
||||
return Arrays.equals(nodes, ((NodeSet)other).nodes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains a partially parsed regular expression. Has a head and a tail.
|
||||
*
|
||||
* The tail is a transition and thus has a label and a next node. The head
|
||||
* is the 'final' state of the partial.
|
||||
*/
|
||||
private static class Partial {
|
||||
private int tailLabel;
|
||||
private NFAState tail;
|
||||
private NFAState head;
|
||||
|
||||
/**
|
||||
* Creates a new partial
|
||||
* @param tailLabel tail label
|
||||
* @param tail tail node
|
||||
* @param head head node
|
||||
*/
|
||||
public Partial(int tailLabel, NFAState tail, NFAState head) {
|
||||
this.tailLabel = tailLabel;
|
||||
this.tail = tail;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicates the NFA of this partial.
|
||||
*
|
||||
* @return a duplicate of this partial
|
||||
*/
|
||||
public Partial duplicate() {
|
||||
HashMap<NFAState, NFAState> nodes = new HashMap<NFAState, NFAState>();
|
||||
|
||||
Queue<NFAState> todo = new LinkedList<NFAState>();
|
||||
todo.add(tail);
|
||||
nodes.put(tail, new NFAState());
|
||||
while (!todo.isEmpty()) {
|
||||
NFAState node = todo.poll();
|
||||
for (NFAState.Transition t : node.transitions) {
|
||||
if (!nodes.containsKey(t.next)) {
|
||||
nodes.put(t.next, new NFAState());
|
||||
todo.add(t.next);
|
||||
}
|
||||
nodes.get(node).addTransition(t.label, nodes.get(t.next));
|
||||
}
|
||||
}
|
||||
|
||||
return new Partial(tailLabel, nodes.get(tail), nodes.get(head));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import stanhebben.zenscript.ZenParsedFile;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public class ParseException extends RuntimeException {
|
||||
private final Token token;
|
||||
private final String message;
|
||||
|
||||
public ParseException(Token token, String error) {
|
||||
super(token == null ? "Error at end of file - " + error : "Error parsing line " + token.getPosition().getLine() + ":" + token.getPosition().getLineOffset() + " - " + error + " (last token: " + token.getValue() + ")");
|
||||
|
||||
this.token = token;
|
||||
this.message = error;
|
||||
}
|
||||
|
||||
public ZenParsedFile getFile() {
|
||||
return token == null ? null : token.getPosition().getFile();
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return token == null ? -1 : token.getPosition().getLine();
|
||||
}
|
||||
|
||||
public int getLineOffset() {
|
||||
return token == null ? -1 : token.getPosition().getLineOffset();
|
||||
}
|
||||
|
||||
public String getExplanation() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
* Represents a token in a token stream.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class Token {
|
||||
private final ZenPosition position;
|
||||
private final String value;
|
||||
private final int 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;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public ZenPosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string value of this token.
|
||||
*
|
||||
* @return token value
|
||||
*/
|
||||
public String getValue() {
|
||||
return 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
/**
|
||||
* Thrown when an exception occurs while tokening a character stream.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
public class TokenException extends RuntimeException {
|
||||
public TokenException(int line, int lineOffset, char value) {
|
||||
super("Invalid character at " + line + ":" + lineOffset + " - " + value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* This file is subject to the license.txt file in the main folder
|
||||
* of this project.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Stack;
|
||||
import stanhebben.zenscript.ZenParsedFile;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
* Represents a token stream. A token stream reads characters from a reader and
|
||||
* presents it as a series of tokens.
|
||||
*
|
||||
* Token classes with a negative class are considered to be whitespace.
|
||||
*
|
||||
* @author Stan Hebben
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 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(t, error);
|
||||
}
|
||||
}
|
||||
|
||||
// LL(*) ability
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////
|
||||
// Iterator implementation
|
||||
////////////////////////////
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
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++;
|
||||
|
||||
advance();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Not supported.");
|
||||
}
|
||||
|
||||
//////////////////////
|
||||
// Protected methods
|
||||
//////////////////////
|
||||
|
||||
/**
|
||||
* Can be implemented by subclasses to post-process tokens.
|
||||
*
|
||||
* @param token
|
||||
* @return processed token
|
||||
*/
|
||||
protected Token process(Token token) {
|
||||
return token;
|
||||
}
|
||||
|
||||
////////////////////
|
||||
// Private methods
|
||||
////////////////////
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
if (dfa.finals[state] != CompiledDFA.NOFINAL) {
|
||||
if (state == 0) throw new TokenException(line, lineOffset, (char)nextChar);
|
||||
next = process(new Token(value.toString(), dfa.finals[state], new ZenPosition(file, tLine, tLineOffset)));
|
||||
} else {
|
||||
throw new TokenException(line, lineOffset, (char)nextChar);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
// 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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,440 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.ZenParser;
|
||||
import static stanhebben.zenscript.ZenParser.*;
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentGlobal;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.definitions.ParsedFunctionArgument;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionFloat;
|
||||
import stanhebben.zenscript.expression.ExpressionFunction;
|
||||
import stanhebben.zenscript.expression.ExpressionInt;
|
||||
import stanhebben.zenscript.expression.ExpressionString;
|
||||
import stanhebben.zenscript.parser.ParseException;
|
||||
import stanhebben.zenscript.parser.Token;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.statements.Statement;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.type.ZenTypeAny;
|
||||
import stanhebben.zenscript.type.ZenTypeDouble;
|
||||
import stanhebben.zenscript.type.ZenTypeInt;
|
||||
import static stanhebben.zenscript.util.StringUtil.unescapeString;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public abstract class ParsedExpression {
|
||||
public static ParsedExpression read(ZenParser parser, IEnvironmentGlobal environment) {
|
||||
return readAssignExpression(parser, environment);
|
||||
}
|
||||
|
||||
private static ParsedExpression readAssignExpression(ZenParser parser, IEnvironmentGlobal environment) {
|
||||
ZenPosition position = parser.peek().getPosition();
|
||||
|
||||
ParsedExpression left = readConditionalExpression(position, parser, environment);
|
||||
switch (parser.peek() == null ? -1 : parser.peek().getType()) {
|
||||
case T_ASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionAssign(position, left, readAssignExpression(parser, environment));
|
||||
case T_PLUSASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.ADD);
|
||||
case T_MINUSASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.SUB);
|
||||
case T_TILDEASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.CAT);
|
||||
case T_MULASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.MUL);
|
||||
case T_DIVASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.DIV);
|
||||
case T_MODASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.MOD);
|
||||
case T_ORASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.OR);
|
||||
case T_ANDASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.AND);
|
||||
case T_XORASSIGN:
|
||||
parser.next();
|
||||
return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, environment), OperatorType.XOR);
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readConditionalExpression(ZenPosition position, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser 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, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression left = readAddExpression(position, parser, environment);
|
||||
|
||||
switch (parser.peek() == null ? -1 : parser.peek().getType()) {
|
||||
case T_EQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.EQ);
|
||||
}
|
||||
case T_NOTEQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.NE);
|
||||
}
|
||||
case T_LT: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.LT);
|
||||
}
|
||||
case T_LTEQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.LE);
|
||||
}
|
||||
case T_GT: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.GT);
|
||||
}
|
||||
case T_GTEQ: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionCompare(position, left, right, CompareType.GE);
|
||||
}
|
||||
case T_IN: {
|
||||
parser.next();
|
||||
ParsedExpression right = readAddExpression(parser.peek().getPosition(), parser, environment);
|
||||
return new ParsedExpressionBinary(position, left, right, OperatorType.CONTAINS);
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readAddExpression(ZenPosition position, ZenParser 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);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.ADD);
|
||||
} else if (parser.optional(T_MINUS) != null) {
|
||||
ParsedExpression right = readMulExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.SUB);
|
||||
} else if (parser.optional(T_TILDE) != null) {
|
||||
ParsedExpression right = readMulExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.CAT);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readMulExpression(ZenPosition position, ZenParser 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);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.MUL);
|
||||
} else if (parser.optional(T_DIV) != null) {
|
||||
ParsedExpression right = readUnaryExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.DIV);
|
||||
} else if (parser.optional(T_MOD) != null) {
|
||||
ParsedExpression right = readUnaryExpression(parser.peek().getPosition(), parser, environment);
|
||||
left = new ParsedExpressionBinary(position, left, right, OperatorType.MOD);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
private static ParsedExpression readUnaryExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_NOT:
|
||||
parser.next();
|
||||
return new ParsedExpressionUnary(
|
||||
position,
|
||||
readUnaryExpression(parser.peek().getPosition(), parser, environment),
|
||||
OperatorType.NOT);
|
||||
case T_MINUS:
|
||||
parser.next();
|
||||
return new ParsedExpressionUnary(
|
||||
position,
|
||||
readUnaryExpression(parser.peek().getPosition(), parser, environment),
|
||||
OperatorType.NEG);
|
||||
default:
|
||||
return readPostfixExpression(position, parser, environment);
|
||||
}
|
||||
}
|
||||
|
||||
private static ParsedExpression readPostfixExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
ParsedExpression base = readPrimaryExpression(position, parser, environment);
|
||||
|
||||
while (true) {
|
||||
if (parser.optional(T_DOT) != null) {
|
||||
Token indexString = parser.optional(T_ID);
|
||||
if (indexString != null) {
|
||||
base = new ParsedExpressionMember(position, base, indexString.getValue());
|
||||
} else {
|
||||
Token indexString2 = parser.optional(T_STRING);
|
||||
if (indexString2 != null) {
|
||||
base = new ParsedExpressionMember(position, base, unescapeString(indexString2.getValue()));
|
||||
} else {
|
||||
Token last = parser.next();
|
||||
throw new ParseException(last, "Invalid expression, last token: " + last.getValue());
|
||||
}
|
||||
}
|
||||
} else if (parser.optional(T_DOT2) != null) {
|
||||
//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);
|
||||
base = new ParsedExpressionIndex(position, base, index);
|
||||
parser.required(T_SQBRCLOSE, "] expected");
|
||||
} else if (parser.optional(T_BROPEN) != null) {
|
||||
ArrayList<ParsedExpression> arguments = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_BRCLOSE) == null) {
|
||||
arguments.add(readAssignExpression(parser, environment));
|
||||
while (parser.optional(T_COMMA) != null) {
|
||||
arguments.add(readAssignExpression(parser, environment));
|
||||
}
|
||||
parser.required(T_BRCLOSE, ") expected");
|
||||
}
|
||||
base = new ParsedExpressionCall(position, base, arguments);
|
||||
} else if (parser.optional(T_AS) != null) {
|
||||
ZenType type = ZenType.read(parser, environment);
|
||||
base = new ParsedExpressionCast(position, base, type);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
private static ParsedExpression readPrimaryExpression(ZenPosition position, ZenParser parser, IEnvironmentGlobal environment) {
|
||||
switch (parser.peek().getType()) {
|
||||
case T_INTVALUE:
|
||||
return new ParsedExpressionValue(
|
||||
position,
|
||||
new ExpressionInt(position, Long.parseLong(parser.next().getValue()), ZenTypeInt.INSTANCE));
|
||||
case T_FLOATVALUE:
|
||||
return new ParsedExpressionValue(
|
||||
position,
|
||||
new ExpressionFloat(position, Double.parseDouble(parser.next().getValue()), ZenTypeDouble.INSTANCE));
|
||||
case T_STRINGVALUE:
|
||||
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_ID:
|
||||
return new ParsedExpressionVariable(position, parser.next().getValue());
|
||||
// TODO: implement function values
|
||||
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 {
|
||||
String name = parser.required(T_ID, "identifier expected").getValue();
|
||||
ZenType type = ZenTypeAny.INSTANCE;
|
||||
|
||||
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");
|
||||
|
||||
Statement[] statements;
|
||||
if (parser.optional(T_ACLOSE) != null) {
|
||||
statements = new Statement[0];
|
||||
} else {
|
||||
ArrayList<Statement> statementsAL = new ArrayList<Statement>();
|
||||
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
statementsAL.add(Statement.read(parser, environment));
|
||||
}
|
||||
statements = statementsAL.toArray(new Statement[statementsAL.size()]);
|
||||
}
|
||||
return new ParsedExpressionValue(position, new ExpressionFunction(position, arguments, returnType, statements));
|
||||
case T_LT: {
|
||||
Token start = parser.next();
|
||||
List<Token> tokens = new ArrayList<Token>();
|
||||
Token next = parser.next();
|
||||
while (next.getType() != ZenParser.T_GT) {
|
||||
tokens.add(next);
|
||||
next = parser.next();
|
||||
}
|
||||
return new ParsedExpressionValue(
|
||||
start.getPosition(),
|
||||
parser.getEnvironment().getBracketed(tokens).instance(start.getPosition()));
|
||||
}
|
||||
case T_SQBROPEN: {
|
||||
parser.next();
|
||||
List<ParsedExpression> contents = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_SQBRCLOSE) == null) {
|
||||
while (parser.optional(T_SQBRCLOSE) == null) {
|
||||
contents.add(readAssignExpression(parser, environment));
|
||||
if (parser.optional(T_COMMA) == null) {
|
||||
parser.required(T_SQBRCLOSE, "] or , expected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ParsedExpressionArray(position, contents);
|
||||
}
|
||||
case T_AOPEN: {
|
||||
parser.next();
|
||||
|
||||
List<ParsedExpression> keys = new ArrayList<ParsedExpression>();
|
||||
List<ParsedExpression> values = new ArrayList<ParsedExpression>();
|
||||
if (parser.optional(T_ACLOSE) == null) {
|
||||
while (parser.optional(T_ACLOSE) == null) {
|
||||
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 ParsedExpressionTable(position, keys, values);
|
||||
}
|
||||
}
|
||||
case T_TRUE:
|
||||
parser.next();
|
||||
return new ParsedExpressionBool(position, true);
|
||||
case T_FALSE:
|
||||
parser.next();
|
||||
return new ParsedExpressionBool(position, false);
|
||||
case T_NULL:
|
||||
parser.next();
|
||||
return new ParsedExpressionNull(position);
|
||||
case T_BROPEN:
|
||||
parser.next();
|
||||
ParsedExpression result = readAssignExpression(parser, environment);
|
||||
parser.required(T_BRCLOSE, ") expected");
|
||||
return result;
|
||||
default:
|
||||
Token last = parser.next();
|
||||
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);
|
||||
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
return compile(environment).eval(environment);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionOrOr;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionOrOr(
|
||||
getPosition(),
|
||||
left.compile(environment).eval(environment),
|
||||
right.compile(environment).eval(environment));
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionArray;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionArray extends ParsedExpression {
|
||||
private final List<ParsedExpression> contents;
|
||||
|
||||
public ParsedExpressionArray(ZenPosition position, List<ParsedExpression> contents) {
|
||||
super(position);
|
||||
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression[] cContents = new Expression[contents.size()];
|
||||
for (int i = 0; i < contents.size(); i++) {
|
||||
cContents[i] = contents.get(i).compile(environment).eval(environment);
|
||||
}
|
||||
return new ExpressionArray(getPosition(), cContents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
if (contents.size() == 1 && contents.get(0) instanceof ParsedExpressionVariable) {
|
||||
return contents.get(0).compile(environment).eval(environment);
|
||||
} else {
|
||||
return compile(environment).eval(environment);
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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) {
|
||||
IPartialExpression cLeft = left.compile(environment);
|
||||
return cLeft
|
||||
.assign(getPosition(), environment,
|
||||
right.compile(environment).eval(environment));
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cLeft = left.compile(environment).eval(environment);
|
||||
Expression cRight = right.compile(environment).eval(environment);
|
||||
return cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionBool;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionBool extends ParsedExpression {
|
||||
private final boolean value;
|
||||
|
||||
public ParsedExpressionBool(ZenPosition position, boolean value) {
|
||||
super(position);
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionBool(getPosition(), value);
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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) {
|
||||
IPartialExpression cReceiver = receiver.compile(environment);
|
||||
Expression[] cArguments = new Expression[arguments.size()];
|
||||
for (int i = 0; i < cArguments.length; i++) {
|
||||
IPartialExpression cArgument = arguments.get(i).compile(environment);
|
||||
cArguments[i] = cArgument.eval(environment);
|
||||
}
|
||||
|
||||
return cReceiver.call(getPosition(), environment, cArguments);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.type.ZenType;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return value
|
||||
.compile(environment)
|
||||
.eval(environment)
|
||||
.cast(getPosition(), environment, type);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.CompareType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cLeft = left.compile(environment).eval(environment);
|
||||
Expression cRight = right.compile(environment).eval(environment);
|
||||
return cLeft.getType().compare(getPosition(), environment, cLeft, cRight, type);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionConditional;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionConditional(
|
||||
getPosition(),
|
||||
condition.compile(environment).eval(environment),
|
||||
ifThen.compile(environment).eval(environment),
|
||||
ifElse.compile(environment).eval(environment));
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cValue = value.compile(environment).eval(environment);
|
||||
Expression cIndex = index.compile(environment).eval(environment);
|
||||
return cValue.getType().binary(getPosition(), environment, cValue, cIndex, OperatorType.INDEXGET);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cValue = value.compile(environment).eval(environment);
|
||||
Expression cIndex = index.compile(environment).eval(environment);
|
||||
Expression cSetValue = setValue.compile(environment).eval(environment);
|
||||
return cValue.getType().trinary(getPosition(), environment, cValue, cIndex, cSetValue,OperatorType.INDEXSET);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionInvalid extends ParsedExpression {
|
||||
public ParsedExpressionInvalid(ZenPosition position) {
|
||||
super(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionInvalid(getPosition());
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return value.compile(environment).getMember(getPosition(), environment, member);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionNull;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionNull extends ParsedExpression {
|
||||
public ParsedExpressionNull(ZenPosition position) {
|
||||
super(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionNull(getPosition());
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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) {
|
||||
Expression cLeft = left.compile(environment).eval(environment);
|
||||
Expression cRight = right.compile(environment).eval(environment);
|
||||
|
||||
Expression value = cLeft.getType().binary(getPosition(), environment, cLeft, cRight, operator);
|
||||
|
||||
return left.compile(environment).assign(getPosition(), environment, value);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.ExpressionOrOr;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return new ExpressionOrOr(
|
||||
getPosition(),
|
||||
left.compile(environment).eval(environment),
|
||||
right.compile(environment).eval(environment));
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import java.util.List;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionTable;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionTable extends ParsedExpression {
|
||||
private final List<ParsedExpression> keys;
|
||||
private final List<ParsedExpression> values;
|
||||
|
||||
public ParsedExpressionTable(
|
||||
ZenPosition position,
|
||||
List<ParsedExpression> keys,
|
||||
List<ParsedExpression> values) {
|
||||
super(position);
|
||||
|
||||
this.keys = keys;
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
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);
|
||||
cValues[i] = values.get(i).compile(environment).eval(environment);
|
||||
}
|
||||
|
||||
return new ExpressionTable(getPosition(), cKeys, cValues);
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
Expression cValue = value.compile(environment).eval(environment);
|
||||
return cValue.getType().unary(getPosition(), environment, cValue, operator);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionValue extends ParsedExpression {
|
||||
private final IPartialExpression value;
|
||||
|
||||
public ParsedExpressionValue(ZenPosition position, IPartialExpression value) {
|
||||
super(position);
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package stanhebben.zenscript.parser.expression;
|
||||
|
||||
import stanhebben.zenscript.compiler.IEnvironmentMethod;
|
||||
import stanhebben.zenscript.expression.Expression;
|
||||
import stanhebben.zenscript.expression.ExpressionInvalid;
|
||||
import stanhebben.zenscript.expression.ExpressionString;
|
||||
import stanhebben.zenscript.expression.partial.IPartialExpression;
|
||||
import stanhebben.zenscript.util.ZenPosition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stanneke
|
||||
*/
|
||||
public class ParsedExpressionVariable extends ParsedExpression {
|
||||
private final String name;
|
||||
|
||||
public ParsedExpressionVariable(ZenPosition position, String name) {
|
||||
super(position);
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartialExpression compile(IEnvironmentMethod environment) {
|
||||
IPartialExpression result = environment.getValue(name, getPosition());
|
||||
if (result == null) {
|
||||
environment.error(getPosition(), "could not find " + name);
|
||||
return new ExpressionInvalid(getPosition());
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression compileKey(IEnvironmentMethod environment) {
|
||||
return new ExpressionString(getPosition(), name);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user