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 a5b295a4 authored by mutantoe's avatar mutantoe
Browse files

Added code for incr

parent 73c6fc9b
package jrr1g18.bb;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jrr1g18.bb.Token.TokenType;
/**
* Interpreter for BareBones
* @author jrr1g18
*
*/
public class Interpreter {
// The code to be interpreted
private String m_code;
// The index of the current position in the code
private int m_idx;
// Variable dictionary
private Map<String, Integer> m_variables;
/**
* Get the value of a variable
* @param var The variable to get
* @return The value of the variable
*/
public Map<String, Integer> getVariables(String var) {
return m_variables;
}
/**
* Checks if a character is an integer
* @param character
* @return
*/
public static boolean isInt(String character) {
return character.matches("\\d");
return character.matches("\\d+");
}
public Interpreter(String code) {
m_code = new String(code);
m_idx = 0;
m_variables = new HashMap<String, Integer>();
}
public String variablesToString() {
return m_variables.toString();
}
public Token getNextToken() {
......@@ -70,35 +92,56 @@ public class Interpreter {
return token;
}
public boolean validateStatement(final ArrayList<Token> tokens, int stmt_begin, int stmt_end){
public void exec() {
public void exec() throws Exception {
ArrayList<Token> tokenHistory = new ArrayList<Token>();
Token lookahead;
int stmt_begin = 0;
int stmt_end = 0;
while(true){
// Find next statement
stmt_begin = stmt_end+1;
do{
++stmt_end;
lookahead = getNextToken();
if(lookahead.getType()==TokenType.EOF) {
return;
}
tokenHistory.add(lookahead);
} while(lookahead.getType()!=TokenType.SEMICOLON);
// Run statement
int stmt_len = stmt_end-stmt_begin;
Token currentToken = tokenHistory.get(stmt_idx);
Token currentToken = tokenHistory.get(stmt_begin);
switch (currentToken.getType()) {
case CLEAR:
if(tokenHistory.get(stmt_begin+1).getType()==TokenType.IDENTIFIER){
ValueToken token = tokenHistory.get(stmt_begin+1);
m_variables.get(token.getName()) = 0;
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(), 0);
} else {
m_variables.put(token.getValue(), 0);
}
} else {
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()));
}
stmt_begin = stmt_end;
}
}
}
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);
public static void main(String[] args) throws Exception {
Interpreter interpreter = new Interpreter("clear x;\nincr x;");
interpreter.exec();
System.out.println(interpreter.variablesToString());
}
}
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