From b2867042fe17f201c8869b3e55fed11afb4b4561 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 21 Oct 2018 17:05:28 -0400 Subject: [PATCH] Convert Print to kotlin --- src/main/kotlin/function/builtin/Eval.kt | 3 +- src/main/kotlin/function/builtin/PRINT.java | 30 --------- src/main/kotlin/function/builtin/Print.kt | 24 +++++++ .../kotlin/function/builtin/PRINTTest.java | 66 ------------------- src/test/kotlin/function/builtin/PrintTest.kt | 64 ++++++++++++++++++ 5 files changed, 90 insertions(+), 97 deletions(-) delete mode 100644 src/main/kotlin/function/builtin/PRINT.java create mode 100644 src/main/kotlin/function/builtin/Print.kt delete mode 100644 src/test/kotlin/function/builtin/PRINTTest.java create mode 100644 src/test/kotlin/function/builtin/PrintTest.kt diff --git a/src/main/kotlin/function/builtin/Eval.kt b/src/main/kotlin/function/builtin/Eval.kt index fe5506d..c6b1a53 100644 --- a/src/main/kotlin/function/builtin/Eval.kt +++ b/src/main/kotlin/function/builtin/Eval.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/PRINT.java b/src/main/kotlin/function/builtin/PRINT.java deleted file mode 100644 index f8e69dc..0000000 --- a/src/main/kotlin/function/builtin/PRINT.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/kotlin/function/builtin/Print.kt b/src/main/kotlin/function/builtin/Print.kt new file mode 100644 index 0000000..459bd92 --- /dev/null +++ b/src/main/kotlin/function/builtin/Print.kt @@ -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) + } + } +} diff --git a/src/test/kotlin/function/builtin/PRINTTest.java b/src/test/kotlin/function/builtin/PRINTTest.java deleted file mode 100644 index 32b2c3f..0000000 --- a/src/test/kotlin/function/builtin/PRINTTest.java +++ /dev/null @@ -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)"); - } -} diff --git a/src/test/kotlin/function/builtin/PrintTest.kt b/src/test/kotlin/function/builtin/PrintTest.kt new file mode 100644 index 0000000..1478322 --- /dev/null +++ b/src/test/kotlin/function/builtin/PrintTest.kt @@ -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)") + } + } +}