Skip to content
Snippets Groups Projects
Commit 7c93e23f authored by mutantoe's avatar mutantoe
Browse files

Updated interpreter

parent df296cfc
No related branches found
No related tags found
No related merge requests found
package jrr1g18.bb; package jrr1g18.bb;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import jrr1g18.bb.Token.TokenType;
public class Interpreter { public class Interpreter {
private String m_code; private String m_code;
private int m_idx; private int m_idx;
private Map<String, Integer> m_variables;
public Map<String, Integer> getVariables(String var) {
return m_variables;
}
public static boolean isInt(String character) { public static boolean isInt(String character) {
return character.matches("\\d"); return character.matches("\\d");
} }
...@@ -18,7 +27,10 @@ public class Interpreter { ...@@ -18,7 +27,10 @@ public class Interpreter {
} }
public Token getNextToken() { public Token getNextToken() {
if(m_idx > m_code.length()) { Matcher whitespaceMatcher = Pattern.compile("\\s*").matcher(m_code);
whitespaceMatcher.find(m_idx);
m_idx+=whitespaceMatcher.group().length();
if(m_idx >= m_code.length()) {
return new Token(TokenType.EOF); return new Token(TokenType.EOF);
} }
String nextChar = "" + m_code.charAt(m_idx); String nextChar = "" + m_code.charAt(m_idx);
...@@ -26,14 +38,16 @@ public class Interpreter { ...@@ -26,14 +38,16 @@ public class Interpreter {
if(nextChar.equals(";")) { if(nextChar.equals(";")) {
// Semicolon // Semicolon
token = new Token(TokenType.SEMICOLON); token = new Token(TokenType.SEMICOLON);
++m_idx;
} else if(isInt(nextChar)) { } else if(isInt(nextChar)) {
// Integer // Integer
Matcher m = Pattern.compile("\\d+").matcher(m_code); Matcher m = Pattern.compile("\\d+").matcher(m_code);
m.find(m_idx); m.find(m_idx);
token = new ValueToken(TokenType.INTEGER, "" + nextChar); token = new ValueToken(TokenType.INTEGER, "" + nextChar);
m_idx+=m.group().length();
} else { } else {
// Variable or instruction // Variable or instruction
Matcher m = Pattern.compile("[A-Za-z][A-Za-z\\d]+").matcher(m_code); Matcher m = Pattern.compile("[A-Za-z][A-Za-z\\d]*").matcher(m_code);
m.find(m_idx); m.find(m_idx);
String name = m.group(); String name = m.group();
switch (name) { switch (name) {
...@@ -50,7 +64,10 @@ public class Interpreter { ...@@ -50,7 +64,10 @@ public class Interpreter {
token = new ValueToken(TokenType.IDENTIFIER, name); token = new ValueToken(TokenType.IDENTIFIER, name);
break; break;
} }
m_idx+=m.group().length();
} }
return token; return token;
} }
} }
package jrr1g18.bb; package jrr1g18.bb;
import jrr1g18.bb.Token.TokenType;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Interpreter interpreter = new Interpreter("clear x;");
Token token;
do{
token = interpreter.getNextToken();
System.out.println(token.toString());
} while(token.getType()!=TokenType.EOF);
} }
} }
package jrr1g18.bb; package jrr1g18.bb;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Token { public class Token {
final TokenType m_type;
public enum TokenType {
EOF, INTEGER, SEMICOLON, IDENTIFIER, INCR, DECR, CLEAR,
}
private static final Map<TokenType, String> TOKEN_MAP;
static {
HashMap<TokenType, String> tempMap = new HashMap<TokenType, String>();
tempMap.put(TokenType.EOF, "EOF");
tempMap.put(TokenType.INTEGER, "INTEGER");
tempMap.put(TokenType.SEMICOLON, "SEMICOLON");
tempMap.put(TokenType.IDENTIFIER, "IDENTIFIER");
tempMap.put(TokenType.INCR, "INCR");
tempMap.put(TokenType.DECR, "DECR");
tempMap.put(TokenType.CLEAR, "CLEAR");
TOKEN_MAP = Collections.unmodifiableMap(tempMap);
}
public static String tokenTypeToString(final TokenType tt) {
return TOKEN_MAP.get(tt);
}
private final TokenType m_type;
public Token(TokenType type) { public Token(TokenType type) {
m_type = type; m_type = type;
} }
public TokenType getType() {
return m_type;
}
public String toString() {
return "Token(" + tokenTypeToString(m_type)+")";
}
} }
\ No newline at end of file
package jrr1g18.bb;
public enum TokenType {
EOF, INTEGER, SEMICOLON, IDENTIFIER, INCR, DECR, CLEAR
}
...@@ -11,6 +11,12 @@ public class ValueToken extends Token { ...@@ -11,6 +11,12 @@ public class ValueToken extends Token {
public ValueToken(TokenType type) { public ValueToken(TokenType type) {
super(type); super(type);
m_value="NULL";
}
@Override
public String toString() {
return "ValueToken("+tokenTypeToString(getType())+", " + getValue()+")";
} }
public String getValue() { public String getValue() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment