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 25047382 authored by Emily Rowlands's avatar Emily Rowlands
Browse files

Moved the interpreter to a parser class

parent 2c8e79de
......@@ -9,8 +9,8 @@ public class Main {
Lexer lexer = new Lexer("3*(2+1)");
System.out.println(lexer.getText());
Interpreter interpreter = new Interpreter(lexer);
System.out.println(interpreter.expr());
Parser parser = new Parser(lexer);
System.out.println(parser.expr());
}
public static void inputLoop() {
......@@ -21,9 +21,9 @@ public class Main {
return;
}
Lexer lexer = new Lexer(input);
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
System.out.println(interpreter.expr());
System.out.println(parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
}
......
......@@ -7,16 +7,16 @@ import jrr1g18.boring.tokens.Token.TokenType;
import jrr1g18.boring.tokens.ValueToken;
/**
* Interpreter for Boring
* Parser for Boring
*
* @author jrr1g18
*
*/
public class Interpreter {
public class Parser {
private Lexer m_lexer;
private Token m_currentToken;
public Interpreter(Lexer lexer) {
public Parser(Lexer lexer) {
m_lexer = lexer;
try {
m_currentToken = m_lexer.getNextToken();
......
......@@ -8,17 +8,17 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import jrr1g18.boring.Interpreter;
import jrr1g18.boring.Parser;
import jrr1g18.boring.Lexer;
import jrr1g18.boring.exceptions.IllegalTokenException;
class InterpreterTest {
class ParserTest {
@Test
@DisplayName("Empty Test")
void emptyTest() {
Lexer lexer = new Lexer("");
Interpreter interpreter = new Interpreter(lexer);
Executable closure = () -> interpreter.expr();
Parser parser = new Parser(lexer);
Executable closure = () -> parser.expr();
assertThrows(IllegalTokenException.class, closure);
}
......@@ -26,8 +26,8 @@ class InterpreterTest {
@DisplayName("Just whitespace Test")
void justWhitespaceTest() {
Lexer lexer = new Lexer(" ");
Interpreter interpreter = new Interpreter(lexer);
Executable closure = () -> interpreter.expr();
Parser parser = new Parser(lexer);
Executable closure = () -> parser.expr();
assertThrows(IllegalTokenException.class, closure);
}
......@@ -35,9 +35,9 @@ class InterpreterTest {
@DisplayName("Basic Integer Test")
void basicIntTest() {
Lexer lexer = new Lexer("2");
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
assertEquals(2, interpreter.expr());
assertEquals(2, parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
fail("Exception thrown");
......@@ -48,9 +48,9 @@ class InterpreterTest {
@DisplayName("Basic Add Test")
void basicAddTest() {
Lexer lexer = new Lexer("2+3");
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
assertEquals(5, interpreter.expr());
assertEquals(5, parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
fail("Exception thrown");
......@@ -61,9 +61,9 @@ class InterpreterTest {
@DisplayName("Basic Subtraction Test")
void basicSubTest() {
Lexer lexer = new Lexer("3-2");
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
assertEquals(1, interpreter.expr());
assertEquals(1, parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
fail("Exception thrown");
......@@ -74,9 +74,9 @@ class InterpreterTest {
@DisplayName("Basic Multiplication Test")
void basicMulTest() {
Lexer lexer = new Lexer("2*3");
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
assertEquals(6, interpreter.expr());
assertEquals(6, parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
fail("Exception thrown");
......@@ -87,9 +87,9 @@ class InterpreterTest {
@DisplayName("Basic Division Test")
void basicDivTest() {
Lexer lexer = new Lexer("10/2");
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
assertEquals(5, interpreter.expr());
assertEquals(5, parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
fail("Exception thrown");
......@@ -100,9 +100,9 @@ class InterpreterTest {
@DisplayName("Parentheses Test")
void parenTest() {
Lexer lexer = new Lexer("(2+3)");
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
assertEquals(5, interpreter.expr());
assertEquals(5, parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
fail("Exception thrown");
......@@ -114,9 +114,9 @@ class InterpreterTest {
void complexParenTest() {
Lexer lexer = new Lexer(
"7 + 3 * (10 / (12 / (3 + 1) - 1)) / (2 + 3) - 5 - 3 + (8)");
Interpreter interpreter = new Interpreter(lexer);
Parser parser = new Parser(lexer);
try {
assertEquals(10, interpreter.expr());
assertEquals(10, parser.expr());
} catch(IllegalTokenException e) {
e.printStackTrace();
fail("Exception thrown");
......
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