diff --git a/src/main/java/dev/latvian/apps/ichor/lang/js/InKeywordJS.java b/src/main/java/dev/latvian/apps/ichor/lang/js/InKeywordJS.java deleted file mode 100644 index db23014..0000000 --- a/src/main/java/dev/latvian/apps/ichor/lang/js/InKeywordJS.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.latvian.apps.ichor.lang.js; - -import dev.latvian.apps.ichor.ast.expression.binary.AstBinary; -import dev.latvian.apps.ichor.token.KeywordToken; -import dev.latvian.apps.ichor.token.PositionedToken; - -public class InKeywordJS extends KeywordToken { - public InKeywordJS() { - super("in"); - identifier(); - } - - @Override - public AstBinary createBinaryAst(PositionedToken pos) { - return new AstIn(); - } -} diff --git a/src/main/java/dev/latvian/apps/ichor/lang/js/InstanceofKeywordJS.java b/src/main/java/dev/latvian/apps/ichor/lang/js/InstanceofKeywordJS.java deleted file mode 100644 index 91f5d07..0000000 --- a/src/main/java/dev/latvian/apps/ichor/lang/js/InstanceofKeywordJS.java +++ /dev/null @@ -1,16 +0,0 @@ -package dev.latvian.apps.ichor.lang.js; - -import dev.latvian.apps.ichor.ast.expression.binary.AstBinary; -import dev.latvian.apps.ichor.token.KeywordToken; -import dev.latvian.apps.ichor.token.PositionedToken; - -public class InstanceofKeywordJS extends KeywordToken { - public InstanceofKeywordJS() { - super("instanceof"); - } - - @Override - public AstBinary createBinaryAst(PositionedToken pos) { - return new AstInstanceOf(); - } -} diff --git a/src/main/java/dev/latvian/apps/ichor/lang/js/KeywordTokenJS.java b/src/main/java/dev/latvian/apps/ichor/lang/js/KeywordTokenJS.java deleted file mode 100644 index f2c8113..0000000 --- a/src/main/java/dev/latvian/apps/ichor/lang/js/KeywordTokenJS.java +++ /dev/null @@ -1,84 +0,0 @@ -package dev.latvian.apps.ichor.lang.js; - -import dev.latvian.apps.ichor.Special; -import dev.latvian.apps.ichor.slot.Slot; -import dev.latvian.apps.ichor.token.DeclaringToken; -import dev.latvian.apps.ichor.token.KeywordToken; -import dev.latvian.apps.ichor.token.Token; - -import java.util.HashMap; -import java.util.Map; - -public interface KeywordTokenJS { - Map CACHE = new HashMap<>(); - - private static Object cache(String name, Object token) { - CACHE.put(name, token); - return token; - } - - private static KeywordToken cache(KeywordToken token) { - return (KeywordToken) cache(token.name, token); - } - - private static KeywordToken cache(String name) { - var token = new KeywordToken(name); - cache(name, token); - return token; - } - - Object NULL = cache("null", Special.NULL); - Object UNDEFINED = cache("undefined", Special.UNDEFINED); - Object TRUE = cache("true", Boolean.TRUE); - Object FALSE = cache("false", Boolean.FALSE); - Token IN = cache(new InKeywordJS()); - Token INSTANCEOF = cache(new InstanceofKeywordJS()); - - DeclaringToken LET = (DeclaringToken) cache(new DeclaringToken("let", Slot.DEFAULT)); - DeclaringToken CONST = (DeclaringToken) cache(new DeclaringToken("const", Slot.IMMUTABLE)); - DeclaringToken VAR = (DeclaringToken) cache(new DeclaringToken("var", Slot.DEFAULT)); - - Token ARGUMENTS = cache("arguments").identifier(); - Token AS = cache("as").literalPre(); // TODO - Token ASYNC = cache("async"); - Token AWAIT = cache("await").literalPre(); - Token BREAK = cache("break").literalPre().insertToken(SymbolTokenJS.SEMI); - Token CASE = cache("case").literalPre(); - Token CATCH = cache("catch"); - Token CLASS = cache("class").identifier(); - Token CONTINUE = cache("continue").literalPre().insertToken(SymbolTokenJS.SEMI); - Token DEBUGGER = cache("debugger").identifier(); - Token DEFAULT = cache("default").identifier(); - Token DELETE = cache("delete").literalPre(); - Token DO = cache("do"); - Token ELSE = cache("else"); - Token ENUM = cache("enum").identifier(); // TODO - Token EXPORT = cache("export").identifier(); // TODO - Token EXTENDS = cache("extends"); - Token FINALLY = cache("finally"); - Token FOR = cache("for"); - Token FROM = cache("from").literalPre().identifier();// TODO - Token FUNCTION = cache("function"); - Token GET = cache("get").identifier(); // TODO - Token IF = cache("if").literalPre(); - Token IMPORT = cache("import").identifier(); // TODO - Token INTERFACE = cache("interface").identifier(); - Token NEW = cache("new").literalPre(); - Token OF = cache("of").literalPre().identifier(); - Token PACKAGE = cache("package").identifier(); // TODO - Token PRIVATE = cache("private").identifier(); // TODO - Token PROTECTED = cache("protected").identifier(); // TODO - Token PUBLIC = cache("public").identifier(); // TODO - Token RETURN = cache("return").literalPre().insertToken(SymbolTokenJS.SEMI); - Token SET = cache("set").identifier(); // TODO - Token STATIC = cache("static").identifier(); // TODO - Token SUPER = cache("super").identifier(); - Token SWITCH = cache("switch"); - Token THIS = cache("this").identifier(); - Token THROW = cache("throw").literalPre(); - Token TRY = cache("try"); - Token TYPEOF = cache("typeof").literalPre(); - Token VOID = cache("void").literalPre().identifier(); // TODO - Token WHILE = cache("while"); - Token YIELD = cache("yield").literalPre().identifier(); // TODO -} diff --git a/src/main/java/dev/latvian/apps/ichor/token/InKeywordToken.java b/src/main/java/dev/latvian/apps/ichor/token/InKeywordToken.java new file mode 100644 index 0000000..1d91a00 --- /dev/null +++ b/src/main/java/dev/latvian/apps/ichor/token/InKeywordToken.java @@ -0,0 +1,16 @@ +package dev.latvian.apps.ichor.token; + +import dev.latvian.apps.ichor.ast.expression.binary.AstBinary; +import dev.latvian.apps.ichor.ast.expression.binary.AstIn; + +public class InKeywordToken extends KeywordToken { + public InKeywordToken() { + super("in"); + identifier(); + } + + @Override + public AstBinary createBinaryAst(PositionedToken pos) { + return new AstIn(); + } +} diff --git a/src/main/java/dev/latvian/apps/ichor/token/InstanceofKeywordToken.java b/src/main/java/dev/latvian/apps/ichor/token/InstanceofKeywordToken.java new file mode 100644 index 0000000..8883f96 --- /dev/null +++ b/src/main/java/dev/latvian/apps/ichor/token/InstanceofKeywordToken.java @@ -0,0 +1,15 @@ +package dev.latvian.apps.ichor.token; + +import dev.latvian.apps.ichor.ast.expression.binary.AstBinary; +import dev.latvian.apps.ichor.ast.expression.binary.AstInstanceOf; + +public class InstanceofKeywordToken extends KeywordToken { + public InstanceofKeywordToken() { + super("instanceof"); + } + + @Override + public AstBinary createBinaryAst(PositionedToken pos) { + return new AstInstanceOf(); + } +} diff --git a/src/main/java/dev/latvian/apps/ichor/token/Keyword.java b/src/main/java/dev/latvian/apps/ichor/token/Keyword.java new file mode 100644 index 0000000..f55a870 --- /dev/null +++ b/src/main/java/dev/latvian/apps/ichor/token/Keyword.java @@ -0,0 +1,119 @@ +package dev.latvian.apps.ichor.token; + +import dev.latvian.apps.ichor.Special; +import dev.latvian.apps.ichor.slot.Slot; +import org.jetbrains.annotations.Nullable; + +public interface Keyword { + private static KeywordToken create(String name) { + return new KeywordToken(name); + } + + Token ARGUMENTS = create("arguments").identifier(); + Token AS = create("as").literalPre(); // TODO + Token ASYNC = create("async"); + Token AWAIT = create("await").literalPre(); + Token BREAK = create("break").literalPre().insertToken(Symbol.SEMI); + Token CASE = create("case").literalPre(); + Token CATCH = create("catch"); + Token CLASS = create("class").identifier(); + DeclaringToken CONST = new DeclaringToken("const", Slot.IMMUTABLE); + Token CONTINUE = create("continue").literalPre().insertToken(Symbol.SEMI); + Token DEBUGGER = create("debugger").identifier(); + Token DEFAULT = create("default").identifier(); + Token DELETE = create("delete").literalPre(); + Token DO = create("do"); + Token ELSE = create("else"); + Token ENUM = create("enum").identifier(); // TODO + Token EXPORT = create("export").identifier(); // TODO + Token EXTENDS = create("extends"); + Token FINALLY = create("finally"); + Token FOR = create("for"); + Token FROM = create("from").literalPre().identifier();// TODO + Token FUNCTION = create("function"); + Token GET = create("get").identifier(); // TODO + Token IF = create("if").literalPre(); + Token IMPORT = create("import").identifier(); // TODO + Token IN = new InKeywordToken(); + Token INSTANCEOF = new InstanceofKeywordToken(); + Token INTERFACE = create("interface").identifier(); + DeclaringToken LET = new DeclaringToken("let", Slot.DEFAULT); + Token NEW = create("new").literalPre(); + Token OF = create("of").literalPre().identifier(); + Token PACKAGE = create("package").identifier(); // TODO + Token PRIVATE = create("private").identifier(); // TODO + Token PROTECTED = create("protected").identifier(); // TODO + Token PUBLIC = create("public").identifier(); // TODO + Token RETURN = create("return").literalPre().insertToken(Symbol.SEMI); + Token SET = create("set").identifier(); // TODO + Token STATIC = create("static").identifier(); // TODO + Token SUPER = create("super").identifier(); + Token SWITCH = create("switch"); + Token THIS = create("this").identifier(); + Token THROW = create("throw").literalPre(); + Token TRY = create("try"); + Token TYPEOF = create("typeof").literalPre(); + DeclaringToken VAR = new DeclaringToken("var", Slot.DEFAULT); + Token VOID = create("void").literalPre().identifier(); // TODO + Token WHILE = create("while"); + Token YIELD = create("yield").literalPre().identifier(); // TODO + + @Nullable + static Object get(String name) { + return switch (name) { + case "null" -> Special.NULL; + case "undefined" -> Special.UNDEFINED; + case "true" -> Boolean.TRUE; + case "false" -> Boolean.FALSE; + case "arguments" -> ARGUMENTS; + case "as" -> AS; + case "async" -> ASYNC; + case "await" -> AWAIT; + case "break" -> BREAK; + case "case" -> CASE; + case "catch" -> CATCH; + case "class" -> CLASS; + case "const" -> CONST; + case "continue" -> CONTINUE; + case "debugger" -> DEBUGGER; + case "default" -> DEFAULT; + case "delete" -> DELETE; + case "do" -> DO; + case "else" -> ELSE; + case "enum" -> ENUM; + case "export" -> EXPORT; + case "extends" -> EXTENDS; + case "finally" -> FINALLY; + case "for" -> FOR; + case "from" -> FROM; + case "function" -> FUNCTION; + case "get" -> GET; + case "if" -> IF; + case "import" -> IMPORT; + case "in" -> IN; + case "instanceof" -> INSTANCEOF; + case "interface" -> INTERFACE; + case "let" -> LET; + case "new" -> NEW; + case "of" -> OF; + case "package" -> PACKAGE; + case "private" -> PRIVATE; + case "protected" -> PROTECTED; + case "public" -> PUBLIC; + case "return" -> RETURN; + case "set" -> SET; + case "static" -> STATIC; + case "super" -> SUPER; + case "switch" -> SWITCH; + case "this" -> THIS; + case "throw" -> THROW; + case "try" -> TRY; + case "typeof" -> TYPEOF; + case "var" -> VAR; + case "void" -> VOID; + case "while" -> WHILE; + case "yield" -> YIELD; + default -> null; + }; + } +}