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.

Verified Commit df296cfc authored by Emily Rowlands's avatar Emily Rowlands
Browse files

Started on the interpreter

parent ad832dde
package jrr1g18.bb;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Interpreter {
private String m_code;
private int m_idx;
public static boolean isInt(String character) {
return character.matches("\\d");
}
public Interpreter(String code) {
m_code = new String(code);
m_idx = 0;
}
public Token getNextToken() {
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);
} 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;
}
}
return token;
}
}
package jrr1g18.bb;
public class Main {
public static void main(String[] args) {
}
}
package jrr1g18.bb;
public class Token {
final TokenType m_type;
public Token(TokenType type) {
m_type = type;
}
}
\ No newline at end of file
package jrr1g18.bb;
public enum TokenType {
EOF, INTEGER, SEMICOLON, IDENTIFIER, INCR, DECR, CLEAR
}
package jrr1g18.bb;
public class ValueToken extends Token {
String m_value;
public ValueToken(TokenType type, String value) {
super(type);
m_value = value;
}
public ValueToken(TokenType type) {
super(type);
}
public String getValue() {
return m_value;
}
public void setValue(ValueToken value) {
m_value = value.getValue();
}
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