From 7ac470c22e9e179daf0a10579a9f9e347cf6f94f Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Mon, 28 May 2018 13:42:11 -0300 Subject: Move SCL into new project SCL is now independant of dicelang, and thus deserving of its own repo. --- .../java/bjc/dicelang/scl/tokens/WordSCLToken.java | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java (limited to 'src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java') 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 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("