Convert Print to kotlin
This commit is contained in:
parent
c603c443ec
commit
b2867042fe
|
@ -14,6 +14,7 @@ import sexpression.LambdaExpression
|
|||
import sexpression.Nil
|
||||
import sexpression.SExpression
|
||||
import sexpression.Symbol
|
||||
import sexpression.Symbol.Companion.T
|
||||
import table.ExecutionContext
|
||||
import table.FunctionTable
|
||||
|
||||
|
@ -120,7 +121,7 @@ class Eval(name: String) : LispFunction() {
|
|||
@JvmStatic
|
||||
fun lookupSymbol(symbolName: String) = when {
|
||||
symbolName == "NIL" -> Nil
|
||||
symbolName == "T" -> Symbol.T
|
||||
symbolName == "T" -> T
|
||||
symbolName.startsWith(":") -> Symbol(symbolName)
|
||||
else -> ExecutionContext.lookupSymbolValue(symbolName)
|
||||
}
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
package function.builtin;
|
||||
|
||||
import environment.RuntimeEnvironment;
|
||||
import function.ArgumentValidator;
|
||||
import function.FunctionNames;
|
||||
import function.LispFunction;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
|
||||
@FunctionNames({ "PRINT" })
|
||||
public class PRINT extends LispFunction {
|
||||
|
||||
private ArgumentValidator argumentValidator;
|
||||
private RuntimeEnvironment environment;
|
||||
|
||||
public PRINT(String name) {
|
||||
this.argumentValidator = new ArgumentValidator(name);
|
||||
this.argumentValidator.setExactNumberOfArguments(1);
|
||||
this.environment = RuntimeEnvironment.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SExpression call(Cons argumentList) {
|
||||
argumentValidator.validate(argumentList);
|
||||
SExpression argument = argumentList.getFirst();
|
||||
environment.getOutput().println(argument);
|
||||
|
||||
return argument;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package function.builtin
|
||||
|
||||
import environment.RuntimeEnvironment
|
||||
import function.ArgumentValidator
|
||||
import function.FunctionNames
|
||||
import function.LispFunction
|
||||
import sexpression.Cons
|
||||
import sexpression.SExpression
|
||||
|
||||
@FunctionNames("PRINT")
|
||||
class Print(name: String) : LispFunction() {
|
||||
|
||||
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
|
||||
setExactNumberOfArguments(1)
|
||||
}
|
||||
|
||||
override fun call(argumentList: Cons): SExpression {
|
||||
argumentValidator.validate(argumentList)
|
||||
|
||||
return argumentList.first.also {
|
||||
RuntimeEnvironment.output!!.println(it)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package function.builtin;
|
||||
|
||||
import environment.RuntimeEnvironment;
|
||||
import function.ArgumentValidator.TooFewArgumentsException;
|
||||
import function.ArgumentValidator.TooManyArgumentsException;
|
||||
import org.junit.Test;
|
||||
import testutil.SymbolAndFunctionCleaner;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static java.text.MessageFormat.format;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static testutil.TestUtilities.evaluateString;
|
||||
|
||||
public class PRINTTest extends SymbolAndFunctionCleaner {
|
||||
|
||||
private RuntimeEnvironment environment;
|
||||
private ByteArrayOutputStream outputStream;
|
||||
|
||||
public PRINTTest() {
|
||||
this.environment = RuntimeEnvironment.INSTANCE;
|
||||
}
|
||||
|
||||
private void assertPrinted(String expected) {
|
||||
assertEquals(expected, outputStream.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void additionalSetUp() {
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
environment.reset();
|
||||
environment.setOutput(new PrintStream(outputStream));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void additionalTearDown() {
|
||||
environment.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printStringWorks() {
|
||||
String output = "\"Hello, world!\"";
|
||||
|
||||
evaluateString(format("(print {0})", output));
|
||||
assertPrinted(format("{0}\n", output));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void printSymbolWorks() {
|
||||
String output = "A";
|
||||
|
||||
evaluateString(format("(print ''{0})", output));
|
||||
assertPrinted(format("{0}\n", output));
|
||||
}
|
||||
|
||||
@Test(expected = TooManyArgumentsException.class)
|
||||
public void printWithTooManyArguments() {
|
||||
evaluateString("(print '1 '2)");
|
||||
}
|
||||
|
||||
@Test(expected = TooFewArgumentsException.class)
|
||||
public void printWithTooFewArguments() {
|
||||
evaluateString("(print)");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package function.builtin
|
||||
|
||||
import environment.RuntimeEnvironment
|
||||
import function.ArgumentValidator.TooFewArgumentsException
|
||||
import function.ArgumentValidator.TooManyArgumentsException
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
import testutil.LispTestInstance
|
||||
import testutil.SymbolAndFunctionCleaner
|
||||
import testutil.TestUtilities.evaluateString
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import java.text.MessageFormat.format
|
||||
|
||||
@LispTestInstance
|
||||
class PrintTest : SymbolAndFunctionCleaner() {
|
||||
|
||||
private var outputStream: ByteArrayOutputStream? = null
|
||||
|
||||
private fun assertPrinted(expected: String) {
|
||||
assertThat(outputStream!!.toString()).isEqualTo(expected)
|
||||
}
|
||||
|
||||
override fun additionalSetUp() {
|
||||
outputStream = ByteArrayOutputStream()
|
||||
RuntimeEnvironment.reset()
|
||||
RuntimeEnvironment.output = PrintStream(outputStream!!)
|
||||
}
|
||||
|
||||
override fun additionalTearDown() {
|
||||
RuntimeEnvironment.reset()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun printStringWorks() {
|
||||
val output = "\"Hello, world!\""
|
||||
|
||||
evaluateString(format("(print {0})", output))
|
||||
assertPrinted(format("{0}\n", output))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun printSymbolWorks() {
|
||||
val output = "A"
|
||||
|
||||
evaluateString(format("(print ''{0})", output))
|
||||
assertPrinted(format("{0}\n", output))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun printWithTooManyArguments() {
|
||||
assertThrows(TooManyArgumentsException::class.java) {
|
||||
evaluateString("(print '1 '2)")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun printWithTooFewArguments() {
|
||||
assertThrows(TooFewArgumentsException::class.java) {
|
||||
evaluateString("(print)")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue