Only allow a single interpreter to be built
Renamed Environment to RuntimeEnvironment
This commit is contained in:
parent
4719e14d7f
commit
913fd031c6
|
@ -2,11 +2,11 @@ package environment;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class Environment {
|
public class RuntimeEnvironment {
|
||||||
|
|
||||||
private static Environment uniqueInstance = new Environment();
|
private static RuntimeEnvironment uniqueInstance = new RuntimeEnvironment();
|
||||||
|
|
||||||
public static Environment getInstance() {
|
public static RuntimeEnvironment getInstance() {
|
||||||
return uniqueInstance;
|
return uniqueInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ public class Environment {
|
||||||
private Runnable terminationFunction;
|
private Runnable terminationFunction;
|
||||||
private Runnable errorTerminationFunction;
|
private Runnable errorTerminationFunction;
|
||||||
|
|
||||||
private Environment() {}
|
private RuntimeEnvironment() {}
|
||||||
|
|
||||||
public void setInput(InputStream input) {
|
public void setInput(InputStream input) {
|
||||||
this.input = input;
|
this.input = input;
|
|
@ -2,7 +2,7 @@ package error;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints error messages and potentially terminates the application.
|
* Prints error messages and potentially terminates the application.
|
||||||
|
@ -15,10 +15,10 @@ public class ErrorManager {
|
||||||
private static final String ANSI_RED = "\u001B[31m";
|
private static final String ANSI_RED = "\u001B[31m";
|
||||||
private static final String ANSI_PURPLE = "\u001B[35m";
|
private static final String ANSI_PURPLE = "\u001B[35m";
|
||||||
|
|
||||||
private Environment environment;
|
private RuntimeEnvironment environment;
|
||||||
|
|
||||||
public ErrorManager() {
|
public ErrorManager() {
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateError(LispException lispException) {
|
public void generateError(LispException lispException) {
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
package function.builtin;
|
package function.builtin;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.*;
|
import function.*;
|
||||||
import sexpression.*;
|
import sexpression.*;
|
||||||
|
|
||||||
public class EXIT extends LispFunction {
|
public class EXIT extends LispFunction {
|
||||||
|
|
||||||
private ArgumentValidator argumentValidator;
|
private ArgumentValidator argumentValidator;
|
||||||
private Environment environment;
|
private RuntimeEnvironment environment;
|
||||||
|
|
||||||
public EXIT() {
|
public EXIT() {
|
||||||
this.argumentValidator = new ArgumentValidator("EXIT");
|
this.argumentValidator = new ArgumentValidator("EXIT");
|
||||||
this.argumentValidator.setMaximumNumberOfArguments(0);
|
this.argumentValidator.setMaximumNumberOfArguments(0);
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SExpression call(Cons argumentList) {
|
public SExpression call(Cons argumentList) {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package function.builtin;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.*;
|
import function.*;
|
||||||
import parser.LispParser;
|
import parser.LispParser;
|
||||||
import sexpression.*;
|
import sexpression.*;
|
||||||
|
@ -11,13 +11,13 @@ import sexpression.*;
|
||||||
public class LOAD extends LispFunction {
|
public class LOAD extends LispFunction {
|
||||||
|
|
||||||
private ArgumentValidator argumentValidator;
|
private ArgumentValidator argumentValidator;
|
||||||
private Environment environment;
|
private RuntimeEnvironment environment;
|
||||||
|
|
||||||
public LOAD() {
|
public LOAD() {
|
||||||
this.argumentValidator = new ArgumentValidator("LOAD");
|
this.argumentValidator = new ArgumentValidator("LOAD");
|
||||||
this.argumentValidator.setExactNumberOfArguments(1);
|
this.argumentValidator.setExactNumberOfArguments(1);
|
||||||
this.argumentValidator.setEveryArgumentExpectedType(LispString.class);
|
this.argumentValidator.setEveryArgumentExpectedType(LispString.class);
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SExpression call(Cons argumentList) {
|
public SExpression call(Cons argumentList) {
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
package function.builtin;
|
package function.builtin;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.*;
|
import function.*;
|
||||||
import sexpression.*;
|
import sexpression.*;
|
||||||
|
|
||||||
public class PRINT extends LispFunction {
|
public class PRINT extends LispFunction {
|
||||||
|
|
||||||
private ArgumentValidator argumentValidator;
|
private ArgumentValidator argumentValidator;
|
||||||
private Environment environment;
|
private RuntimeEnvironment environment;
|
||||||
|
|
||||||
public PRINT() {
|
public PRINT() {
|
||||||
this.argumentValidator = new ArgumentValidator("PRINT");
|
this.argumentValidator = new ArgumentValidator("PRINT");
|
||||||
this.argumentValidator.setExactNumberOfArguments(1);
|
this.argumentValidator.setExactNumberOfArguments(1);
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SExpression call(Cons argumentList) {
|
public SExpression call(Cons argumentList) {
|
||||||
|
|
|
@ -2,7 +2,7 @@ package function.builtin.special;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.*;
|
import function.*;
|
||||||
import function.builtin.cons.LIST;
|
import function.builtin.cons.LIST;
|
||||||
import sexpression.*;
|
import sexpression.*;
|
||||||
|
@ -13,7 +13,7 @@ public class DEFUN extends LispFunction {
|
||||||
private ArgumentValidator argumentValidator;
|
private ArgumentValidator argumentValidator;
|
||||||
private ArgumentValidator lambdaListIsListValidator;
|
private ArgumentValidator lambdaListIsListValidator;
|
||||||
private ArgumentValidator lambdaListValidator;
|
private ArgumentValidator lambdaListValidator;
|
||||||
private Environment environment;
|
private RuntimeEnvironment environment;
|
||||||
|
|
||||||
public DEFUN() {
|
public DEFUN() {
|
||||||
this.argumentValidator = new ArgumentValidator("DEFUN");
|
this.argumentValidator = new ArgumentValidator("DEFUN");
|
||||||
|
@ -26,7 +26,7 @@ public class DEFUN extends LispFunction {
|
||||||
this.lambdaListValidator = new ArgumentValidator("DEFUN|parameter|");
|
this.lambdaListValidator = new ArgumentValidator("DEFUN|parameter|");
|
||||||
this.lambdaListValidator.setEveryArgumentExpectedType(Symbol.class);
|
this.lambdaListValidator.setEveryArgumentExpectedType(Symbol.class);
|
||||||
|
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SExpression call(Cons argumentList) {
|
public SExpression call(Cons argumentList) {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package interpreter;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import error.*;
|
import error.*;
|
||||||
import function.builtin.EVAL;
|
import function.builtin.EVAL;
|
||||||
import parser.LispParser;
|
import parser.LispParser;
|
||||||
|
@ -16,16 +16,16 @@ public class LispInterpreter {
|
||||||
|
|
||||||
private LispParser parser;
|
private LispParser parser;
|
||||||
private ErrorManager errorManager;
|
private ErrorManager errorManager;
|
||||||
protected Environment environment;
|
protected RuntimeEnvironment environment;
|
||||||
|
|
||||||
public LispInterpreter() {
|
public LispInterpreter() {
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
this.errorManager = new ErrorManager();
|
this.errorManager = new ErrorManager();
|
||||||
this.parser = new LispParser(this.environment.getInput(), this.environment.getInputName());
|
this.parser = new LispParser(this.environment.getInput(), this.environment.getInputName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public LispInterpreter(String fileName) {
|
public LispInterpreter(String fileName) {
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
this.errorManager = new ErrorManager();
|
this.errorManager = new ErrorManager();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -2,47 +2,60 @@ package interpreter;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
|
import error.*;
|
||||||
|
|
||||||
public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
||||||
|
|
||||||
|
private static LispInterpreterBuilder uniqueInstance = new LispInterpreterBuilderImpl();
|
||||||
|
|
||||||
|
public static LispInterpreterBuilder getInstance() {
|
||||||
|
return uniqueInstance;
|
||||||
|
}
|
||||||
|
|
||||||
private Environment environment;
|
private InputStream inputStream;
|
||||||
|
private PrintStream outputStream;
|
||||||
|
private PrintStream errorOutputStream;
|
||||||
|
private Runnable terminationFunction;
|
||||||
|
private Runnable errorTerminationFunction;
|
||||||
|
private RuntimeEnvironment environment;
|
||||||
private String fileName;
|
private String fileName;
|
||||||
private boolean isInteractive;
|
private boolean isInteractive;
|
||||||
|
private boolean isBuilt;
|
||||||
|
|
||||||
public LispInterpreterBuilderImpl() {
|
private LispInterpreterBuilderImpl() {
|
||||||
this.environment = Environment.getInstance();
|
this.environment = RuntimeEnvironment.getInstance();
|
||||||
this.fileName = "";
|
this.fileName = "";
|
||||||
this.isInteractive = true;
|
this.isInteractive = true;
|
||||||
|
this.isBuilt = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setInput(InputStream inputStream) {
|
public void setInput(InputStream inputStream) {
|
||||||
this.environment.setInput(inputStream);
|
this.inputStream = inputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setOutput(PrintStream outputStream) {
|
public void setOutput(PrintStream outputStream) {
|
||||||
this.environment.setOutput(outputStream);
|
this.outputStream = outputStream;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setErrorOutput(PrintStream errorOutputStream) {
|
public void setErrorOutput(PrintStream errorOutputStream) {
|
||||||
this.environment.setErrorOutput(errorOutputStream);
|
this.errorOutputStream = errorOutputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setTerminationFunction(Runnable terminationFunction) {
|
public void setTerminationFunction(Runnable terminationFunction) {
|
||||||
this.environment.setTerminationFunction(terminationFunction);
|
this.terminationFunction = terminationFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setErrorTerminationFunction(Runnable errorTerminationFunction) {
|
public void setErrorTerminationFunction(Runnable errorTerminationFunction) {
|
||||||
this.environment.setErrorTerminationFunction(errorTerminationFunction);
|
this.errorTerminationFunction = errorTerminationFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void useFile(String fileName) {
|
public void useFile(String fileName) {
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
|
@ -51,10 +64,40 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LispInterpreter build() {
|
public LispInterpreter build() {
|
||||||
if (isInteractive)
|
if (!isBuilt)
|
||||||
return new InteractiveLispInterpreter();
|
return buildInterpreter();
|
||||||
else
|
else
|
||||||
return new LispInterpreter(fileName);
|
throw new InterpreterAlreadyBuiltException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private LispInterpreter buildInterpreter() {
|
||||||
|
environment.setInput(inputStream);
|
||||||
|
environment.setOutput(outputStream);
|
||||||
|
environment.setErrorOutput(errorOutputStream);
|
||||||
|
environment.setTerminationFunction(terminationFunction);
|
||||||
|
environment.setErrorTerminationFunction(errorTerminationFunction);
|
||||||
|
|
||||||
|
LispInterpreter lispInterpreter = isInteractive ? new InteractiveLispInterpreter()
|
||||||
|
: new LispInterpreter(fileName);
|
||||||
|
|
||||||
|
isBuilt = true;
|
||||||
|
|
||||||
|
return lispInterpreter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InterpreterAlreadyBuiltException extends LispException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSeverity() {
|
||||||
|
return ErrorManager.CRITICAL_LEVEL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return "Refusing to build more than one interpreter.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ public class LispMain {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static LispInterpreter buildInterpreter(String[] args) {
|
private static LispInterpreter buildInterpreter(String[] args) {
|
||||||
LispInterpreterBuilder builder = new LispInterpreterBuilderImpl();
|
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
||||||
builder.setInput(System.in);
|
builder.setInput(System.in);
|
||||||
builder.setOutput(System.out);
|
builder.setOutput(System.out);
|
||||||
builder.setErrorOutput(System.err);
|
builder.setErrorOutput(System.err);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.util.*;
|
||||||
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
|
|
||||||
public class ErrorManagerTester {
|
public class ErrorManagerTester {
|
||||||
|
|
||||||
|
@ -18,8 +18,8 @@ public class ErrorManagerTester {
|
||||||
private ByteArrayOutputStream outputStream;
|
private ByteArrayOutputStream outputStream;
|
||||||
|
|
||||||
private ErrorManager createErrorManagerWithIndicators() {
|
private ErrorManager createErrorManagerWithIndicators() {
|
||||||
Environment.getInstance().setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED));
|
RuntimeEnvironment.getInstance().setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED));
|
||||||
Environment.getInstance().setErrorOutput(new PrintStream(outputStream));
|
RuntimeEnvironment.getInstance().setErrorOutput(new PrintStream(outputStream));
|
||||||
|
|
||||||
return new ErrorManager();
|
return new ErrorManager();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.util.*;
|
||||||
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.ArgumentValidator.TooManyArgumentsException;
|
import function.ArgumentValidator.TooManyArgumentsException;
|
||||||
|
|
||||||
public class EXITTester {
|
public class EXITTester {
|
||||||
|
@ -26,7 +26,7 @@ public class EXITTester {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
indicatorSet = new HashSet<>();
|
indicatorSet = new HashSet<>();
|
||||||
Environment.getInstance().setTerminationFunction(() -> indicatorSet.add(TERMINATED));
|
RuntimeEnvironment.getInstance().setTerminationFunction(() -> indicatorSet.add(TERMINATED));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.io.*;
|
||||||
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.ArgumentValidator.*;
|
import function.ArgumentValidator.*;
|
||||||
import sexpression.*;
|
import sexpression.*;
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ public class LOADTester {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
this.outputStream = new ByteArrayOutputStream();
|
this.outputStream = new ByteArrayOutputStream();
|
||||||
Environment.getInstance().setErrorOutput(new PrintStream(outputStream));
|
RuntimeEnvironment.getInstance().setErrorOutput(new PrintStream(outputStream));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.text.MessageFormat;
|
||||||
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.ArgumentValidator.*;
|
import function.ArgumentValidator.*;
|
||||||
|
|
||||||
public class PRINTTester {
|
public class PRINTTester {
|
||||||
|
@ -22,7 +22,7 @@ public class PRINTTester {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
this.outputStream = new ByteArrayOutputStream();
|
this.outputStream = new ByteArrayOutputStream();
|
||||||
Environment.getInstance().setOutput(new PrintStream(outputStream));
|
RuntimeEnvironment.getInstance().setOutput(new PrintStream(outputStream));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.io.*;
|
||||||
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
import environment.Environment;
|
import environment.RuntimeEnvironment;
|
||||||
import function.ArgumentValidator.*;
|
import function.ArgumentValidator.*;
|
||||||
import table.FunctionTable;
|
import table.FunctionTable;
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ public class DEFUNTester {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
this.outputStream = new ByteArrayOutputStream();
|
this.outputStream = new ByteArrayOutputStream();
|
||||||
Environment.getInstance().setOutput(new PrintStream(outputStream));
|
RuntimeEnvironment.getInstance().setOutput(new PrintStream(outputStream));
|
||||||
FunctionTable.reset();
|
FunctionTable.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue