123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- package me.hammerle.snuviscript.token;
- import java.util.LinkedList;
- import me.hammerle.snuviscript.code.Instruction;
- import me.hammerle.snuviscript.exceptions.PreScriptException;
- public class Tokenizer
- {
- private final char[] code;
- private int line;
-
- private final LinkedList<Token> data;
-
- public Tokenizer(String code)
- {
- this.code = code.toCharArray();
- this.data = new LinkedList<>();
- }
-
- private void addToken(TokenType t)
- {
- data.add(new Token(t, line + 1));
- }
-
- private void addToken(TokenType t, Object o)
- {
- data.add(new Token(t, line + 1, o));
- }
-
- public void tokenize()
- {
- line = 0;
- for(int index = 0; index < code.length; index++)
- {
- if(Character.isLetter(code[index]))
- {
- int old = index;
- index++;
- while(index < code.length && (Character.isLetterOrDigit(code[index]) || code[index] == '.' || code[index] == '_'))
- {
- index++;
- }
- String s = new String(code, old, index - old);
- switch(s)
- {
- case "if": addToken(TokenType.IF); break;
- case "elseif": addToken(TokenType.ELSE_IF); break;
- case "else": addToken(TokenType.ELSE); break;
- case "for": addToken(TokenType.FOR); break;
- case "while": addToken(TokenType.WHILE); break;
- case "function": addToken(TokenType.FUNCTION); break;
- case "break": addToken(TokenType.BREAK); break;
- case "continue": addToken(TokenType.CONTINUE); break;
- case "return": addToken(TokenType.RETURN); break;
- case "try": addToken(TokenType.TRY); break;
- case "catch": addToken(TokenType.CATCH); break;
- default:
- addToken(TokenType.VAR, s);
- }
- index--;
- }
- else if(Character.isDigit(code[index]))
- {
- int old = index;
- index++;
- while(index < code.length)
- {
- switch(code[index])
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- {
- index++;
- continue;
- }
- case '.':
- {
- index++;
- while(index < code.length && Character.isDigit(code[index]))
- {
- index++;
- }
- break;
- }
- }
- break;
- }
- addToken(TokenType.DOUBLE, Double.parseDouble(new String(code, old, index - old)));
- index--;
- }
- else
- {
- int startLine = line;
- try
- {
- switch(code[index])
- {
- case '\n':
- {
- line++;
- break;
- }
- case '@':
- {
- int old = index;
- index++;
- while(index < code.length && (Character.isLetterOrDigit(code[index]) || code[index] == '.' || code[index] == '_'))
- {
- index++;
- }
- addToken(TokenType.LABEL, new String(code, old, index - old));
- index--;
- break;
- }
- case '"':
- {
- int old = index + 1;
- index++;
- while(index < code.length && code[index] != '"')
- {
- index++;
- }
- addToken(TokenType.TEXT, new String(code, old, index - old));
- break;
- }
- case '+':
- {
- switch(code[index + 1])
- {
- case '+':
- addToken(TokenType.INC);
- index++;
- break;
- case '=':
- addToken(TokenType.ADD_SET);
- index++;
- break;
- default:
- addToken(TokenType.ADD);
- }
- break;
- }
- case '-':
- {
- switch(code[index + 1])
- {
- case '-':
- addToken(TokenType.DEC);
- index++;
- break;
- case '=':
- addToken(TokenType.SUB_SET);
- index++;
- break;
- default:
- addToken(TokenType.SUB);
- }
- break;
- }
- case '*':
- {
- if(code[index + 1] == '=')
- {
- addToken(TokenType.MUL_SET);
- index++;
- }
- else
- {
- addToken(TokenType.MUL);
- }
- break;
- }
- case '/':
- {
- switch(code[index + 1])
- {
- case '/':
- index += 2;
- while(code[index] != '\n')
- {
- index++;
- }
- index--;
- break;
- case '*':
- index += 2;
- while(code[index] != '*' || code[index + 1] != '/')
- {
- if(code[index] == '\n')
- {
- line++;
- }
- index++;
- }
- index++;
- break;
- case '=':
- addToken(TokenType.DIV_SET);
- index++;
- break;
- default:
- addToken(TokenType.DIV);
- }
- break;
- }
- case '!':
- {
- if(code[index + 1] == '=')
- {
- addToken(TokenType.NOT_EQUAL);
- index++;
- break;
- }
- else
- {
- addToken(TokenType.INVERT);
- }
- break;
- }
- case '~':
- {
- addToken(TokenType.BIT_INVERT);
- break;
- }
- case '%':
- {
- if(code[index + 1] == '=')
- {
- addToken(TokenType.MOD_SET);
- index++;
- }
- else
- {
- addToken(TokenType.MOD);
- }
- break;
- }
- case '<':
- {
- switch(code[index + 1])
- {
- case '<':
- if(code[index + 2] == '=')
- {
- addToken(TokenType.LEFT_SHIFT_SET);
- index += 2;
- }
- else
- {
- addToken(TokenType.LEFT_SHIFT);
- index++;
- }
- break;
- case '=':
- addToken(TokenType.LESS_EQUAL);
- index++;
- break;
- default:
- addToken(TokenType.LESS);
- }
- break;
- }
- case '>':
- {
- switch(code[index + 1])
- {
- case '>':
- if(code[index + 2] == '=')
- {
- addToken(TokenType.RIGHT_SHIFT_SET);
- index += 2;
- }
- else
- {
- addToken(TokenType.RIGHT_SHIFT);
- index++;
- }
- break;
- case '=':
- addToken(TokenType.GREATER_EQUAL);
- index++;
- break;
- default:
- addToken(TokenType.GREATER);
- }
- break;
- }
- case '=':
- {
- if(code[index + 1] == '=')
- {
- addToken(TokenType.EQUAL);
- index++;
- break;
- }
- else
- {
- addToken(TokenType.SET);
- }
- break;
- }
- case '&':
- {
- switch(code[index + 1])
- {
- case '&':
- addToken(TokenType.AND);
- index++;
- break;
- case '=':
- addToken(TokenType.BIT_AND_SET);
- index++;
- break;
- default:
- addToken(TokenType.BIT_AND);
- }
- break;
- }
- case '^':
- {
- if(code[index + 1] == '=')
- {
- addToken(TokenType.BIT_XOR_SET);
- index++;
- break;
- }
- else
- {
- addToken(TokenType.BIT_XOR);
- }
- break;
- }
- case '|':
- {
- switch(code[index + 1])
- {
- case '|':
- addToken(TokenType.OR);
- index++;
- break;
- case '=':
- addToken(TokenType.BIT_OR_SET);
- index++;
- break;
- default:
- addToken(TokenType.BIT_OR);
- }
- break;
- }
- case ',':
- addToken(TokenType.COMMA);
- break;
- case '(':
- addToken(TokenType.OPEN_BRACKET);
- break;
- case ')':
- addToken(TokenType.CLOSE_BRACKET);
- break;
- case '[':
- addToken(TokenType.OPEN_SQUARE_BRACKET);
- break;
- case ']':
- addToken(TokenType.CLOSE_SQUARE_BRACKET);
- break;
- case '{':
- addToken(TokenType.OPEN_CURVED_BRACKET);
- break;
- case '}':
- addToken(TokenType.CLOSE_CURVED_BRACKET);
- break;
- case ';':
- addToken(TokenType.SEMICOLON);
- break;
- }
- }
- catch(ArrayIndexOutOfBoundsException ex)
- {
- throw new PreScriptException("unexpected code end", startLine, line);
- }
- }
- }
- addToken(TokenType.END_OF_FILE);
- //data.forEach(e -> System.out.println(e));
-
- Parser p = new Parser(data);
- for(Instruction in : p.parseTokens())
- {
- System.out.println(in);
- }
- }
- }
|