Administrator approval is now required for registering new accounts. If you are registering a new account, and are external to the University, please ask the repository owner to contact ServiceLine to request your account be approved. Repository owners must include the newly registered email address, and specific repository in the request for approval.

Commit 7c93e23f authored by mutantoe's avatar mutantoe
Browse files

Updated interpreter

parent df296cfc
package jrr1g18.bb;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Interpreter {
import jrr1g18.bb.Token.TokenType;
private String m_code;
private int m_idx;
public class Interpreter {
public static boolean isInt(String character) {
return character.matches("\\d");
}
private String m_code;
private int m_idx;
private Map<String, Integer> m_variables;
public Map<String, Integer> getVariables(String var) {
return m_variables;
}
public Interpreter(String code) {
m_code = new String(code);
m_idx = 0;
}
public static boolean isInt(String character) {
return character.matches("\\d");
}
public Token getNextToken() {
if(m_idx > m_code.length()) {
return new Token(TokenType.EOF);
public Interpreter(String code) {
m_code = new String(code);
m_idx = 0;
}
String nextChar = "" + m_code.charAt(m_idx);
Token token;
if(nextChar.equals(";")) {
// Semicolon
token = new Token(TokenType.SEMICOLON);
} else if(isInt(nextChar)) {
// Integer
Matcher m = Pattern.compile("\\d+").matcher(m_code);
m.find(m_idx);
token = new ValueToken(TokenType.INTEGER, "" + nextChar);
} else {
// Variable or instruction
Matcher m = Pattern.compile("[A-Za-z][A-Za-z\\d]+").matcher(m_code);
m.find(m_idx);
String name = m.group();
switch (name) {
case "incr":
token = new Token(TokenType.INCR);
break;
case "decr":
token = new Token(TokenType.DECR);
break;
case "clear":
token = new Token(TokenType.CLEAR);
break;
default:
token = new ValueToken(TokenType.IDENTIFIER, name);
break;
}
public Token getNextToken() {
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);
}
String nextChar = "" + m_code.charAt(m_idx);
Token token;
if(nextChar.equals(";")) {
// Semicolon
token = new Token(TokenType.SEMICOLON);
++m_idx;
} else if(isInt(nextChar)) {
// Integer
Matcher m = Pattern.compile("\\d+").matcher(m_code);
m.find(m_idx);
token = new ValueToken(TokenType.INTEGER, "" + nextChar);
m_idx+=m.group().length();
} else {
// Variable or instruction
Matcher m = Pattern.compile("[A-Za-z][A-Za-z\\d]*").matcher(m_code);
m.find(m_idx);
String name = m.group();
switch (name) {
case "incr":
token = new Token(TokenType.INCR);
break;
case "decr":
token = new Token(TokenType.DECR);
break;
case "clear":
token = new Token(TokenType.CLEAR);
break;
default:
token = new ValueToken(TokenType.IDENTIFIER, name);
break;
}
m_idx+=m.group().length();
}
return token;
}
return token;
}
}
package jrr1g18.bb;
import jrr1g18.bb.Token.TokenType;
public class Main {
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;
public class Token {
final TokenType m_type;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public Token(TokenType type) {
m_type = type;
}
public class Token {
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) {
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
}
......@@ -2,26 +2,32 @@ package jrr1g18.bb;
public class ValueToken extends Token {
String m_value;
String m_value;
public ValueToken(TokenType type, String value) {
super(type);
m_value = value;
}
public ValueToken(TokenType type, String value) {
super(type);
m_value = value;
}
public ValueToken(TokenType type) {
super(type);
}
public ValueToken(TokenType type) {
super(type);
m_value="NULL";
}
@Override
public String toString() {
return "ValueToken("+tokenTypeToString(getType())+", " + getValue()+")";
}
public String getValue() {
return m_value;
}
public String getValue() {
return m_value;
}
public void setValue(ValueToken value) {
m_value = value.getValue();
}
public void setValue(ValueToken value) {
m_value = value.getValue();
}
public void setValue(String value) {
m_value = value;
}
public void setValue(String value) {
m_value = value;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment