diff options
Diffstat (limited to 'src/main/java/bjc/dicelang/scl/tokens')
13 files changed, 690 insertions, 0 deletions
diff --git a/src/main/java/bjc/dicelang/scl/tokens/ArraySCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/ArraySCLToken.java new file mode 100644 index 0000000..a06a0fd --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/ArraySCLToken.java @@ -0,0 +1,27 @@ +package bjc.dicelang.scl.tokens; + +import bjc.utils.funcdata.IList; + +/** + * Represents an array token. + * + * @author student + * + */ +public class ArraySCLToken extends WordListSCLToken { + + /** + * Create a new array token. + * + * @param tokens + * The tokens in the array. + */ + public ArraySCLToken(IList<SCLToken> tokens) { + super(true, tokens); + } + + @Override + public String toString() { + return "ArraySCLToken [tokenVals=" + tokenVals + "]"; + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/BooleanSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/BooleanSCLToken.java new file mode 100644 index 0000000..bccffe0 --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/BooleanSCLToken.java @@ -0,0 +1,53 @@ +package bjc.dicelang.scl.tokens; + +/** + * Represents a boolean token. + * + * @author student + * + */ +public class BooleanSCLToken extends SCLToken { + /** + * The value of the token. + */ + public boolean boolVal; + + /** + * Create a new token. + * + * @param val + * The value of the token. + */ + public BooleanSCLToken(boolean val) { + super(TokenType.BLIT); + + boolVal = val; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + (boolVal ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + BooleanSCLToken other = (BooleanSCLToken) obj; + if (boolVal != other.boolVal) + return false; + return true; + } + + @Override + public String toString() { + return "BooleanSCLToken [boolVal=" + boolVal + "]"; + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/FloatSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/FloatSCLToken.java new file mode 100644 index 0000000..82e44e2 --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/FloatSCLToken.java @@ -0,0 +1,55 @@ +package bjc.dicelang.scl.tokens; + +/** + * Represents a floating-point token. + * + * @author student + * + */ +public class FloatSCLToken extends SCLToken { + /** + * The value of the token. + */ + public double floatVal; + + /** + * Create a new floating-point token. + * + * @param val + * The value of the token. + */ + public FloatSCLToken(double val) { + super(TokenType.FLIT); + + floatVal = val; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + long temp; + temp = Double.doubleToLongBits(floatVal); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + FloatSCLToken other = (FloatSCLToken) obj; + if (Double.doubleToLongBits(floatVal) != Double.doubleToLongBits(other.floatVal)) + return false; + return true; + } + + @Override + public String toString() { + return "FloatSCLToken [floatVal=" + floatVal + "]"; + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/IntSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/IntSCLToken.java new file mode 100644 index 0000000..3c77eee --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/IntSCLToken.java @@ -0,0 +1,26 @@ +package bjc.dicelang.scl.tokens; + +/** + * Represents an integer token. + * + * @author student + * + */ +public class IntSCLToken extends SCLToken { + /** + * The integer value of the token. + */ + public long intVal; + + /** + * Create a new integer token. + * + * @param iVal + * The value of the token. + */ + public IntSCLToken(final long iVal) { + super(TokenType.ILIT); + + intVal = iVal; + } +}
\ No newline at end of file diff --git a/src/main/java/bjc/dicelang/scl/tokens/SCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/SCLToken.java new file mode 100644 index 0000000..4d4987f --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/SCLToken.java @@ -0,0 +1,98 @@ +package bjc.dicelang.scl.tokens; + +import java.util.HashMap; +import java.util.Map; + +import bjc.dicelang.scl.Errors; +import bjc.utils.parserutils.TokenUtils; + +import static bjc.dicelang.scl.Errors.ErrorKey.*; +import static bjc.dicelang.scl.tokens.TokenType.*; + +/** + * Base class for SCL tokens. + * + * @author student + * + */ +public class SCLToken { + /** + * The type of the token. + */ + public TokenType type; + + /** + * Convert a string into a token. + * + * @param token + * The string to convert into a token. + * @return The token. + */ + public static SCLToken tokenizeString(final String token) { + if (litTokens.containsKey(token)) { + return new SCLToken(litTokens.get(token)); + } else if (token.startsWith("\\")) { + return new SymbolSCLToken(token.substring(1)); + } else if (WordSCLToken.isBuiltinWord(token)) { + return new WordSCLToken(token); + } else if (token.equals("true")) { + return new BooleanSCLToken(true); + } else if (token.equals("false")) { + return new BooleanSCLToken(false); + } else if (TokenUtils.isInt(token)) { + return new IntSCLToken(Long.parseLong(token)); + } else if (TokenUtils.isDouble(token)) { + return new FloatSCLToken(Double.parseDouble(token)); + } else { + Errors.inst.printError(EK_SCL_INVTOKEN, token); + return null; + } + } + + protected static final Map<String, TokenType> litTokens; + + protected SCLToken() { + + } + + protected SCLToken(TokenType typ) { + type = typ; + } + + static { + /* Init literal tokens. */ + litTokens = new HashMap<>(); + + litTokens.put("'", SQUOTE); + litTokens.put("\"", DQUOTE); + litTokens.put("[", OBRACKET); + litTokens.put("{", OBRACE); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((type == null) ? 0 : type.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SCLToken other = (SCLToken) obj; + if (type != other.type) + return false; + return true; + } + + @Override + public String toString() { + return "SCLToken [type=" + type + "]"; + } +}
\ No newline at end of file diff --git a/src/main/java/bjc/dicelang/scl/tokens/StringLitSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/StringLitSCLToken.java new file mode 100644 index 0000000..d2a10f9 --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/StringLitSCLToken.java @@ -0,0 +1,25 @@ +package bjc.dicelang.scl.tokens; + +/** + * Represents a literal string token. + * + * @author student + * + */ +public class StringLitSCLToken extends StringSCLToken { + + /** + * Create a new literal string token. + * + * @param val + * The string value of the token. + */ + public StringLitSCLToken(String val) { + super(false, val); + } + + @Override + public String toString() { + return "StringLitSCLToken [stringVal=" + stringVal + "]"; + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/StringSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/StringSCLToken.java new file mode 100644 index 0000000..40e5c27 --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/StringSCLToken.java @@ -0,0 +1,54 @@ +package bjc.dicelang.scl.tokens; + +/** + * Base class for tokens containing strings. + * + * @author student + * + */ +public abstract class StringSCLToken extends SCLToken { + /** + * String value of the token. + */ + public String stringVal; + + protected StringSCLToken(boolean isSymbol, String val) { + if (isSymbol) { + type = TokenType.SYMBOL; + } else { + type = TokenType.SLIT; + } + + stringVal = val; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((stringVal == null) ? 0 : stringVal.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + StringSCLToken other = (StringSCLToken) obj; + if (stringVal == null) { + if (other.stringVal != null) + return false; + } else if (!stringVal.equals(other.stringVal)) + return false; + return true; + } + + @Override + public String toString() { + return "StringSCLToken [stringVal=" + stringVal + "]"; + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/SymbolSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/SymbolSCLToken.java new file mode 100644 index 0000000..d67be78 --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/SymbolSCLToken.java @@ -0,0 +1,25 @@ +package bjc.dicelang.scl.tokens; + +/** + * Represents a symbol literal. + * + * @author student + * + */ +public class SymbolSCLToken extends StringSCLToken { + + /** + * Create a symbol literal + * + * @param val + * The value of the symbol. + */ + public SymbolSCLToken(String val) { + super(true, val); + } + + @Override + public String toString() { + return "SymbolSCLToken [stringVal=" + stringVal + "]"; + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/TokenType.java b/src/main/java/bjc/dicelang/scl/tokens/TokenType.java new file mode 100644 index 0000000..519331c --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/TokenType.java @@ -0,0 +1,61 @@ +package bjc.dicelang.scl.tokens; + +/** + * Represent all the types of a token. + * + * @author student + * + */ +public enum TokenType { + /* Natural tokens. These come directly from strings */ + /** + * Integer literal. + */ + ILIT, + /** + * Floating-point literal. + */ + FLIT, + /** + * Boolean literal. + */ + BLIT, + /** + * Single-quote. + */ + SQUOTE, + /** + * Double-quote. + */ + DQUOTE, + /** + * Open-bracket. + */ + OBRACKET, + /** + * Open-brace. + */ + OBRACE, + /** + * Symbol. + */ + SYMBOL, + /** + * Word. + */ + WORD, + + /* Synthetic tokens. These are produced from special tokens. */ + /** + * String literal. + */ + SLIT, + /** + * List of words. + */ + WORDS, + /** + * List of data. + */ + ARRAY, +}
\ No newline at end of file diff --git a/src/main/java/bjc/dicelang/scl/tokens/WordListSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/WordListSCLToken.java new file mode 100644 index 0000000..1d870db --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/WordListSCLToken.java @@ -0,0 +1,56 @@ +package bjc.dicelang.scl.tokens; + +import bjc.utils.funcdata.IList; + +/** + * Represents a list of words. + * + * @author student + * + */ +public abstract class WordListSCLToken extends SCLToken { + /** + * The list of words. + */ + public IList<SCLToken> tokenVals; + + protected WordListSCLToken(boolean isArray, IList<SCLToken> tokens) { + if (isArray) { + type = TokenType.ARRAY; + } else { + type = TokenType.WORDS; + } + + tokenVals = tokens; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((tokenVals == null) ? 0 : tokenVals.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + WordListSCLToken other = (WordListSCLToken) obj; + if (tokenVals == null) { + if (other.tokenVals != null) + return false; + } else if (!tokenVals.equals(other.tokenVals)) + return false; + return true; + } + + @Override + public String toString() { + return "WordsSCLToken [tokenVals=" + tokenVals + "]"; + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java new file mode 100644 index 0000000..6fd444d --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java @@ -0,0 +1,106 @@ +package bjc.dicelang.scl.tokens; + +import static bjc.dicelang.scl.tokens.WordType.*; + +import java.util.HashMap; +import java.util.Map; + +/** + * Represents a single word. + * + * @author student + * + */ +public class WordSCLToken extends SCLToken { + /** + * The value of the word. + */ + public WordType wordVal; + + /** + * Create a new word token. + * + * @param wrd + * The value of the word. + */ + public WordSCLToken(String wrd) { + this(builtinWords.get(wrd)); + } + + /** + * Create a new word token. + * + * @param wrd + * The value of the word. + */ + public WordSCLToken(WordType wrd) { + super(TokenType.WORD); + + wordVal = wrd; + } + + @Override + public String toString() { + return "WordSCLToken [wordVal=" + wordVal + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((wordVal == null) ? 0 : wordVal.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + WordSCLToken other = (WordSCLToken) obj; + if (wordVal != other.wordVal) + return false; + return true; + } + + /** + * Check if a word is built-in. + * + * @param wrd + * The word to check. + * + * @return Whether or not the word is builtin. + */ + public static boolean isBuiltinWord(String wrd) { + return builtinWords.containsKey(wrd); + } + + private static final Map<String, WordType> builtinWords; + + static { + /* Init builtin words. */ + builtinWords = new HashMap<>(); + + builtinWords.put("makearray", MAKEARRAY); + builtinWords.put("cvx", MAKEEXEC); + builtinWords.put("cvux", MAKEUNEXEC); + + builtinWords.put("+stream", NEWSTREAM); + builtinWords.put(">stream", LEFTSTREAM); + builtinWords.put("<stream", RIGHTSTREAM); + builtinWords.put("-stream", DELETESTREAM); + builtinWords.put("<-stream", MERGESTREAM); + + builtinWords.put("#", STACKCOUNT); + builtinWords.put("empty?", STACKEMPTY); + builtinWords.put("drop", DROP); + builtinWords.put("ndrop", NDROP); + builtinWords.put("nip", NIP); + builtinWords.put("nnip", NNIP); + + builtinWords.put("def", DEFINE); + } +} diff --git a/src/main/java/bjc/dicelang/scl/tokens/WordType.java b/src/main/java/bjc/dicelang/scl/tokens/WordType.java new file mode 100644 index 0000000..5a8eb85 --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/WordType.java @@ -0,0 +1,77 @@ +package bjc.dicelang.scl.tokens; + +/** + * Represents the word type. + * + * @author student + * + */ +public enum WordType { + /* Array manipulation */ + /** + * Create an array + */ + MAKEARRAY, + /** + * Make a token executable. + */ + MAKEEXEC, + /** + * Make a token unexecutable. + */ + MAKEUNEXEC, + + /* Stream manipulation */ + /** + * Create a new stream. + */ + NEWSTREAM, + /** + * Swap to the left stream. + */ + LEFTSTREAM, + /** + * Swap to the right stream. + */ + RIGHTSTREAM, + /** + * Delete the current stream. + */ + DELETESTREAM, + /** + * Merge the streams. + */ + MERGESTREAM, + + /* Stack manipulation */ + /** + * Get the count of items on the stack. + */ + STACKCOUNT, + /** + * Check if the stack is empty. + */ + STACKEMPTY, + /** + * Drop an item from the top of the stack. + */ + DROP, + /** + * Drop a number of items from the top of the stack. + */ + NDROP, + /** + * Drop an item, leaving the top of the stack alone. + */ + NIP, + /** + * Drop a number of items, leaving the top of the stack alone. + */ + NNIP, + + /* Definition manipulation. */ + /** + * Define a word. + */ + DEFINE, +}
\ No newline at end of file diff --git a/src/main/java/bjc/dicelang/scl/tokens/WordsSCLToken.java b/src/main/java/bjc/dicelang/scl/tokens/WordsSCLToken.java new file mode 100644 index 0000000..40c4cd4 --- /dev/null +++ b/src/main/java/bjc/dicelang/scl/tokens/WordsSCLToken.java @@ -0,0 +1,27 @@ +package bjc.dicelang.scl.tokens; + +import bjc.utils.funcdata.IList; + +/** + * A token representing an executable bunch of words. + * + * @author student + * + */ +public class WordsSCLToken extends WordListSCLToken { + + /** + * Create a new executable words token. + * + * @param tokens + * The tokens to use. + */ + public WordsSCLToken(IList<SCLToken> tokens) { + super(false, tokens); + } + + @Override + public String toString() { + return "WordsSCLToken [tokenVals=" + tokenVals + "]"; + } +} |
