Commit 638df9d7 authored by mutantoe's avatar mutantoe
Browse files

Updated interpreter

parent a5b295a4
......@@ -20,7 +20,7 @@ public class Interpreter {
private int m_idx;
// Variable dictionary
private Map<String, Integer> m_variables;
/**
* Get the value of a variable
* @param var The variable to get
......@@ -29,7 +29,7 @@ public class Interpreter {
public Map<String, Integer> getVariables(String var) {
return m_variables;
}
/**
* Checks if a character is an integer
* @param character
......@@ -44,7 +44,7 @@ public class Interpreter {
m_idx = 0;
m_variables = new HashMap<String, Integer>();
}
public String variablesToString() {
return m_variables.toString();
}
......@@ -107,14 +107,29 @@ public class Interpreter {
}
tokenHistory.add(lookahead);
} while(lookahead.getType()!=TokenType.SEMICOLON);
// Run statement
int stmt_len = stmt_end-stmt_begin;
Token currentToken = tokenHistory.get(stmt_begin);
int setVarTo;
switch (currentToken.getType()) {
case CLEAR:
case INCR:
case DECR:
if(stmt_len==3 && tokenHistory.get(stmt_begin+1).getType()==TokenType.IDENTIFIER){
ValueToken token = (ValueToken) tokenHistory.get(stmt_begin+1);
switch (currentToken.getType()) {
case CLEAR:
setVarTo=0;
break;
case INCR:
setVarTo=m_variables.get(token.getValue())+1;
break;
case DECR:
setVarTo=m_variables.get(token.getValue())-1;
default:
throw new Exception("How did you even get here?");
}
if(m_variables.containsKey(token.getValue())) {
m_variables.replace(token.getValue(), 0);
} else {
......@@ -124,19 +139,6 @@ public class Interpreter {
throw new Exception("Nonsense statement.");
}
break;
case INCR:
if(stmt_len==3 && tokenHistory.get(stmt_begin+1).getType()==TokenType.IDENTIFIER){
ValueToken token = (ValueToken) tokenHistory.get(stmt_begin+1);
if(m_variables.containsKey(token.getValue())) {
m_variables.replace(token.getValue(),
m_variables.get(token.getValue())+1);
} else {
throw new Exception("Undefined variable.");
}
} else {
throw new Exception("Nonsense statement.");
}
break;
default:
throw new IllegalStateException("Illegal token type: "
+ Token.tokenTypeToString(currentToken.getType()));
......
Supports Markdown
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