Commit 8ed1c17b authored by mutantoe's avatar mutantoe
Browse files

Fixed some whitespace

parent 337459e4
...@@ -15,12 +15,12 @@ public class Interpreter { ...@@ -15,12 +15,12 @@ public class Interpreter {
private Token m_currentToken; private Token m_currentToken;
public Interpreter(Lexer lexer) { public Interpreter(Lexer lexer) {
m_lexer = lexer; m_lexer = lexer;
try { try {
m_currentToken = m_lexer.getNextToken(); m_currentToken = m_lexer.getNextToken();
} catch(IllegalCharacterException e) { } catch(IllegalCharacterException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
...@@ -30,17 +30,17 @@ public class Interpreter { ...@@ -30,17 +30,17 @@ public class Interpreter {
* @throws IllegalTokenException When the token types do not match * @throws IllegalTokenException When the token types do not match
*/ */
public void eat(TokenType tt) throws IllegalTokenException { public void eat(TokenType tt) throws IllegalTokenException {
if(m_currentToken.getType() == tt) { if(m_currentToken.getType() == tt) {
try { try {
m_currentToken = m_lexer.getNextToken(); m_currentToken = m_lexer.getNextToken();
} catch(IllegalCharacterException e) { } catch(IllegalCharacterException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
throw new IllegalTokenException( throw new IllegalTokenException(
"Expected: " + Lib.tokenTypeToString(tt) + " got: " "Expected: " + Lib.tokenTypeToString(tt) + " got: "
+ Lib.tokenTypeToString(m_currentToken.getType())); + Lib.tokenTypeToString(m_currentToken.getType()));
} }
} }
/** /**
...@@ -50,9 +50,9 @@ public class Interpreter { ...@@ -50,9 +50,9 @@ public class Interpreter {
* @throws IllegalTokenException * @throws IllegalTokenException
*/ */
public int number() throws IllegalTokenException { public int number() throws IllegalTokenException {
Integer value = ((ValueToken) m_currentToken).getValue(); Integer value = ((ValueToken) m_currentToken).getValue();
eat(TokenType.INTEGER); eat(TokenType.INTEGER);
return value; return value;
} }
/** /**
...@@ -62,24 +62,24 @@ public class Interpreter { ...@@ -62,24 +62,24 @@ public class Interpreter {
* @throws IllegalTokenException When an illegal token is enountered. * @throws IllegalTokenException When an illegal token is enountered.
*/ */
public int mulDiv() throws IllegalTokenException { public int mulDiv() throws IllegalTokenException {
int result = number(); int result = number();
while(m_currentToken.getType() == TokenType.MUL while(m_currentToken.getType() == TokenType.MUL
|| m_currentToken.getType() == TokenType.DIV) { || m_currentToken.getType() == TokenType.DIV) {
ValueToken token = (ValueToken) m_currentToken; ValueToken token = (ValueToken) m_currentToken;
switch (token.getType()) { switch (token.getType()) {
case MUL: case MUL:
eat(TokenType.MUL); eat(TokenType.MUL);
result *= number(); result *= number();
break; break;
case DIV: case DIV:
eat(TokenType.DIV); eat(TokenType.DIV);
result /= number(); result /= number();
break; break;
default: default:
break; break;
} }
} }
return result; return result;
} }
/** /**
...@@ -89,24 +89,24 @@ public class Interpreter { ...@@ -89,24 +89,24 @@ public class Interpreter {
* @throws IllegalTokenException When an illegal token is enountered. * @throws IllegalTokenException When an illegal token is enountered.
*/ */
public int addSub() throws IllegalTokenException { public int addSub() throws IllegalTokenException {
int result = number(); int result = number();
while(m_currentToken.getType() == TokenType.ADD while(m_currentToken.getType() == TokenType.ADD
|| m_currentToken.getType() == TokenType.SUB) { || m_currentToken.getType() == TokenType.SUB) {
ValueToken token = (ValueToken) m_currentToken; ValueToken token = (ValueToken) m_currentToken;
switch (token.getType()) { switch (token.getType()) {
case ADD: case ADD:
eat(TokenType.ADD); eat(TokenType.ADD);
result += number(); result += number();
break; break;
case SUB: case SUB:
eat(TokenType.SUB); eat(TokenType.SUB);
result -= number(); result -= number();
break; break;
default: default:
break; break;
} }
} }
return result; return result;
} }
} }
...@@ -9,40 +9,40 @@ public class Lexer { ...@@ -9,40 +9,40 @@ public class Lexer {
private String m_currentChar; private String m_currentChar;
public Lexer(String text) { public Lexer(String text) {
m_text = text; m_text = text;
} }
/** /**
* Advance m_pos and set m_currentChar * Advance m_pos and set m_currentChar
*/ */
private void nextChar() { private void nextChar() {
m_pos += 1; m_pos += 1;
if(m_pos > m_text.length() - 1) { if(m_pos > m_text.length() - 1) {
m_currentChar = null; m_currentChar = null;
} else { } else {
m_currentChar = "" + m_text.charAt(m_pos); m_currentChar = "" + m_text.charAt(m_pos);
} }
} }
/** /**
* Advance until EOF or end of whitespace * Advance until EOF or end of whitespace
*/ */
public void skipWhitespace() { public void skipWhitespace() {
while(m_currentChar != null && Lib.isWhitespace(m_currentChar)) { while(m_currentChar != null && Lib.isWhitespace(m_currentChar)) {
nextChar(); nextChar();
} }
} }
/** /**
* @return A multidigit integer generated from input * @return A multidigit integer generated from input
*/ */
public int integer() { public int integer() {
String ret = ""; String ret = "";
while(m_currentChar != null && Lib.isInt(m_currentChar)) { while(m_currentChar != null && Lib.isInt(m_currentChar)) {
ret += m_currentChar; ret += m_currentChar;
nextChar(); nextChar();
} }
return Integer.parseInt(ret); return Integer.parseInt(ret);
} }
/** /**
...@@ -52,40 +52,40 @@ public class Lexer { ...@@ -52,40 +52,40 @@ public class Lexer {
* @throws IllegalCharacterException When an unknown character is found * @throws IllegalCharacterException When an unknown character is found
*/ */
public Token getNextToken() throws IllegalCharacterException { public Token getNextToken() throws IllegalCharacterException {
while(m_currentChar != null) { while(m_currentChar != null) {
if(Lib.isWhitespace(m_currentChar)) { if(Lib.isWhitespace(m_currentChar)) {
skipWhitespace(); skipWhitespace();
continue; continue;
} }
if(Lib.isInt(m_currentChar)) { if(Lib.isInt(m_currentChar)) {
return new ValueToken(TokenType.INTEGER, integer()); return new ValueToken(TokenType.INTEGER, integer());
} else if(m_currentChar } else if(m_currentChar
.equals(Lib.tokenTypeToString(TokenType.ADD))) { .equals(Lib.tokenTypeToString(TokenType.ADD))) {
nextChar(); nextChar();
return new NamedToken(TokenType.ADD, return new NamedToken(TokenType.ADD,
Lib.tokenTypeToString(TokenType.ADD)); Lib.tokenTypeToString(TokenType.ADD));
} else if(m_currentChar } else if(m_currentChar
.equals(Lib.tokenTypeToString(TokenType.SUB))) { .equals(Lib.tokenTypeToString(TokenType.SUB))) {
nextChar(); nextChar();
return new NamedToken(TokenType.SUB, return new NamedToken(TokenType.SUB,
Lib.tokenTypeToString(TokenType.SUB)); Lib.tokenTypeToString(TokenType.SUB));
} else if(m_currentChar } else if(m_currentChar
.equals(Lib.tokenTypeToString(TokenType.MUL))) { .equals(Lib.tokenTypeToString(TokenType.MUL))) {
nextChar(); nextChar();
return new NamedToken(TokenType.MUL, return new NamedToken(TokenType.MUL,
Lib.tokenTypeToString(TokenType.MUL)); Lib.tokenTypeToString(TokenType.MUL));
} else if(m_currentChar } else if(m_currentChar
.equals(Lib.tokenTypeToString(TokenType.DIV))) { .equals(Lib.tokenTypeToString(TokenType.DIV))) {
nextChar(); nextChar();
return new NamedToken(TokenType.DIV, return new NamedToken(TokenType.DIV,
Lib.tokenTypeToString(TokenType.DIV)); Lib.tokenTypeToString(TokenType.DIV));
} else { } else {
throw new IllegalCharacterException( throw new IllegalCharacterException(
"Unrecognised thing: " + m_currentChar); "Unrecognised thing: " + m_currentChar);
} }
} }
return new Token(TokenType.EOF); return new Token(TokenType.EOF);
} }
} }
...@@ -14,7 +14,7 @@ public abstract class Lib { ...@@ -14,7 +14,7 @@ public abstract class Lib {
* @return True if an integer, false otherwise * @return True if an integer, false otherwise
*/ */
public static boolean isInt(String string) { public static boolean isInt(String string) {
return string.matches("\\d+"); return string.matches("\\d+");
} }
/** /**
...@@ -24,23 +24,23 @@ public abstract class Lib { ...@@ -24,23 +24,23 @@ public abstract class Lib {
* @return True if an integer, false otherwise * @return True if an integer, false otherwise
*/ */
public static boolean isWhitespace(String string) { public static boolean isWhitespace(String string) {
return string.matches("\\s+"); return string.matches("\\s+");
} }
private static final Map<TokenType, String> TOKEN_MAP; private static final Map<TokenType, String> TOKEN_MAP;
static { static {
HashMap<TokenType, String> tempMap = new HashMap<TokenType, String>(); HashMap<TokenType, String> tempMap = new HashMap<TokenType, String>();
tempMap.put(TokenType.EOF, "EOF"); tempMap.put(TokenType.EOF, "EOF");
tempMap.put(TokenType.INTEGER, "INTEGER"); tempMap.put(TokenType.INTEGER, "INTEGER");
tempMap.put(TokenType.SEMICOLON, "SEMICOLON"); tempMap.put(TokenType.SEMICOLON, "SEMICOLON");
tempMap.put(TokenType.DIV, "/"); tempMap.put(TokenType.DIV, "/");
tempMap.put(TokenType.SUB, "-"); tempMap.put(TokenType.SUB, "-");
tempMap.put(TokenType.ADD, "+"); tempMap.put(TokenType.ADD, "+");
tempMap.put(TokenType.MUL, "*"); tempMap.put(TokenType.MUL, "*");
TOKEN_MAP = Collections.unmodifiableMap(tempMap); TOKEN_MAP = Collections.unmodifiableMap(tempMap);
} }
public static String tokenTypeToString(final TokenType tt) { public static String tokenTypeToString(final TokenType tt) {
return TOKEN_MAP.get(tt); return TOKEN_MAP.get(tt);
} }
} }
...@@ -3,9 +3,9 @@ package jrr1g18.boring; ...@@ -3,9 +3,9 @@ package jrr1g18.boring;
public class Main { public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Lexer lexer = new Lexer("1+2"); Lexer lexer = new Lexer("1+2");
Interpreter interpreter = new Interpreter(lexer); Interpreter interpreter = new Interpreter(lexer);
System.out.println(interpreter.addSub()); System.out.println(interpreter.addSub());
} }
} }
...@@ -4,30 +4,30 @@ public class NamedToken extends Token { ...@@ -4,30 +4,30 @@ public class NamedToken extends Token {
private String m_name; private String m_name;
public NamedToken(TokenType type, String value) { public NamedToken(TokenType type, String value) {
super(type); super(type);
m_name = new String(value); m_name = new String(value);
} }
public NamedToken(TokenType type) { public NamedToken(TokenType type) {
super(type); super(type);
m_name = null; m_name = null;
} }
@Override @Override
public String toString() { public String toString() {
return "ValueToken(" + Lib.tokenTypeToString(getType()) + ", " return "ValueToken(" + Lib.tokenTypeToString(getType()) + ", "
+ getName() + ")"; + getName() + ")";
} }
public String getName() { public String getName() {
return m_name; return m_name;
} }
public void setName(final NamedToken value) { public void setName(final NamedToken value) {
m_name = new String(value.getName()); m_name = new String(value.getName());
} }
public void setName(final String value) { public void setName(final String value) {
m_name = new String(value); m_name = new String(value);
} }
} }
...@@ -3,21 +3,21 @@ package jrr1g18.boring; ...@@ -3,21 +3,21 @@ package jrr1g18.boring;
public class Token { public class Token {
public enum TokenType { public enum TokenType {
EOF, INTEGER, SEMICOLON, ADD, SUB, MUL, DIV, EOF, INTEGER, SEMICOLON, ADD, SUB, MUL, DIV,
} }
private final TokenType m_type; private final TokenType m_type;
public Token(TokenType type) { public Token(TokenType type) {
m_type = type; m_type = type;
} }
public TokenType getType() { public TokenType getType() {
return m_type; return m_type;
} }
public String toString() { public String toString() {
return "Token(" + Lib.tokenTypeToString(m_type) + ")"; return "Token(" + Lib.tokenTypeToString(m_type) + ")";
} }
} }
\ No newline at end of file
...@@ -4,35 +4,35 @@ public class ValueToken extends Token { ...@@ -4,35 +4,35 @@ public class ValueToken extends Token {
private Integer m_value; private Integer m_value;
public ValueToken(TokenType type, final Integer name) { public ValueToken(TokenType type, final Integer name) {
super(type); super(type);
m_value = new Integer(name); m_value = new Integer(name);
} }
public ValueToken(TokenType type, final int name) { public ValueToken(TokenType type, final int name) {
super(type); super(type);
m_value = new Integer(name); m_value = new Integer(name);
} }
public ValueToken(TokenType type) { public ValueToken(TokenType type) {
super(type); super(type);
m_value = null; m_value = null;
} }
@Override @Override
public String toString() { public String toString() {
return "ValueToken(" + Lib.tokenTypeToString(getType()) + ", " return "ValueToken(" + Lib.tokenTypeToString(getType()) + ", "
+ getValue() + ")"; + getValue() + ")";
} }
public Integer getValue() { public Integer getValue() {
return m_value; return m_value;
} }
public void setValue(ValueToken value) { public void setValue(ValueToken value) {
m_value = value.getValue(); m_value = value.getValue();
} }
public void setValue(final Integer value) { public void setValue(final Integer value) {
m_value = new Integer(value); m_value = new Integer(value);
} }
} }
...@@ -3,20 +3,20 @@ package jrr1g18.boring.exceptions; ...@@ -3,20 +3,20 @@ package jrr1g18.boring.exceptions;
public class BoringException extends Exception { public class BoringException extends Exception {
public BoringException(String message) { public BoringException(String message) {
super(message); super(message);
} }
public BoringException(Throwable cause) { public BoringException(Throwable cause) {
super(cause); super(cause);
} }
public BoringException(String message, Throwable cause) { public BoringException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public BoringException(String message, Throwable cause, public BoringException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) { boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace); super(message, cause, enableSuppression, writableStackTrace);
} }
} }
...@@ -3,20 +3,20 @@ package jrr1g18.boring.exceptions; ...@@ -3,20 +3,20 @@ package jrr1g18.boring.exceptions;
public class IllegalCharacterException extends BoringException { public class IllegalCharacterException extends BoringException {
public IllegalCharacterException(String message) { public IllegalCharacterException(String message) {
super(message); super(message);
} }
public IllegalCharacterException(Throwable cause) { public IllegalCharacterException(Throwable cause) {
super(cause); super(cause);
} }