diff --git a/README.md b/README.md index 8192aea..1bee829 100644 --- a/README.md +++ b/README.md @@ -10,37 +10,9 @@ A Kotlin library for algebra -## Installation +## Installation and usage -### Maven - -Add the following to your `pom.xml` file; - -```xml - - me.nathanfallet.makth - makth - 1.0.1 - -``` - -### Gradle - -Add the following to your `build.gradle` file: - -```groovy -repositories { - mavenCentral() -} - -dependencies { - implementation 'me.nathanfallet.makth:makth:1.0.1' -} -``` - -## Usage - -Read [documentation](https://docs.makth.org) +[Read documentation](https://docs.makth.org) ## Donate to the developer diff --git a/docs/getstarted/install.md b/docs/getstarted/install.md new file mode 100644 index 0000000..0686879 --- /dev/null +++ b/docs/getstarted/install.md @@ -0,0 +1,27 @@ +# Installation + +## Maven + +Add the following to your `pom.xml` file; + +```xml + + me.nathanfallet.makth + makth + 1.1.0 + +``` + +## Gradle + +Add the following to your `build.gradle` file: + +```groovy +repositories { + mavenCentral() +} + +dependencies { + implementation 'me.nathanfallet.makth:makth:1.1.0' +} +``` diff --git a/docs/index.md b/docs/index.md index 5999fd4..3217db3 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,5 +1,9 @@ # Makth documentation +## Get started + +- [Installation](getstarted/install.md) + ## Numbers - [Instantiate numbers](numbers/instantiate.md) diff --git a/pom.xml b/pom.xml index a988df4..3af51d9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.nathanfallet.makth makth - 1.0.1 + 1.1.0 makth A Kotlin library for algebra diff --git a/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt b/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt index 7f67cc3..749b27e 100644 --- a/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt +++ b/src/main/kotlin/me/nathanfallet/makth/extensions/BooleanExtension.kt @@ -22,8 +22,4 @@ data class BooleanValue(val value: Boolean) : Value { return setOf() } -} - -fun Boolean.getValue(): BooleanValue { - return BooleanValue(this) } \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt b/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt index d5f4562..a6ba51f 100644 --- a/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt +++ b/src/main/kotlin/me/nathanfallet/makth/extensions/StringExtension.kt @@ -4,14 +4,15 @@ import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.resolvables.Context import me.nathanfallet.makth.resolvables.Variable -data class StringValue(val value: String) : Value { +data class StringValue(val value: String, val latex: Boolean = false) : Value { override fun compute(context: Context): Value { return this } override fun toAlgorithmString(): String { - return "\"${value.replace("\"", "\\\"")}\"" + val char = if (latex) "$" else "\"" + return "$char${value.replace("$char", "\\$char")}$char" } override fun toRawString(): String { @@ -19,7 +20,7 @@ data class StringValue(val value: String) : Value { } override fun toLaTeXString(): String { - return "\\text{$value}" + return if (latex) value.replace("\$", "\\\$") else "\\text{${value.replace("{", "\\{").replace("}", "\\}")}}" } override fun extractVariables(): Set { @@ -28,18 +29,6 @@ data class StringValue(val value: String) : Value { } -fun String.getValue(): StringValue { - return StringValue(this) -} - fun String.indentLines(): String { - val builder = StringBuilder() - for (s in split("\n").toTypedArray()) { - if (builder.isNotEmpty()) { - builder.append("\n") - } - builder.append(" ") - builder.append(s) - } - return builder.toString() + return split("\n").joinToString("\n") { " $it" } } \ No newline at end of file diff --git a/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt b/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt index 8d21c39..c664f34 100644 --- a/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt +++ b/src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt @@ -1,6 +1,7 @@ package me.nathanfallet.makth.lexers import me.nathanfallet.makth.extensions.StringValue +import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.numbers.Integer import me.nathanfallet.makth.operations.Operation @@ -36,7 +37,8 @@ class MathLexer(private var content: String) { // Do something with current character when (content[i]) { ' ' -> {} - '"' -> parseString() + '"' -> parseString('"') + '$' -> parseString('$') '(' -> parseBlock() in Constants.NUMBERS -> parseNumber() in Constants.VARIABLES -> parseVariable() @@ -67,12 +69,12 @@ class MathLexer(private var content: String) { // Parse something - private fun parseString() { + private fun parseString(delimiter: Char) { val string = StringBuilder() i++ // Get all the characters - while (i < content.count() && content[i] != '"') { + while (i < content.count() && content[i] != delimiter) { // Check for \ if (content[i] == '\\' && i < content.count() - 1) { // Skip it and get next character @@ -85,7 +87,7 @@ class MathLexer(private var content: String) { } // Insert the value - insertValue(StringValue(string.toString())) + insertValue(StringValue(string.toString(), delimiter == '$')) } private fun parseBlock() { @@ -160,7 +162,11 @@ class MathLexer(private var content: String) { } // Insert into values - insertValue(Variable(name.toString())) + when (val finalName = name.toString()) { + "true" -> insertValue(BooleanValue(true)) + "false" -> insertValue(BooleanValue(false)) + else -> insertValue(Variable(finalName)) + } } // Utils for parsing diff --git a/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt b/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt index 3aac736..b660637 100644 --- a/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt +++ b/src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt @@ -1,6 +1,6 @@ package me.nathanfallet.makth.operations -import me.nathanfallet.makth.extensions.getValue +import me.nathanfallet.makth.extensions.BooleanValue import me.nathanfallet.makth.interfaces.Value import me.nathanfallet.makth.numbers.Real import me.nathanfallet.makth.resolvables.Context @@ -47,14 +47,14 @@ data class Equality( if (left is Real && right is Real) { val leftDouble = left.getDoubleValue() val rightDouble = right.getDoubleValue() - return when (operator) { + return BooleanValue(when (operator) { Operator.Equals -> leftDouble == rightDouble Operator.NotEquals -> leftDouble != rightDouble Operator.LessThan -> leftDouble < rightDouble Operator.GreaterThan -> leftDouble > rightDouble Operator.LessThanOrEquals -> leftDouble <= rightDouble Operator.GreaterThanOrEquals -> leftDouble >= rightDouble - }.getValue() + }) } return Equality(left, right, operator) diff --git a/src/test/kotlin/me/nathanfallet/makth/extensions/BooleanExtensionTest.kt b/src/test/kotlin/me/nathanfallet/makth/extensions/BooleanExtensionTest.kt new file mode 100644 index 0000000..fbe2d5f --- /dev/null +++ b/src/test/kotlin/me/nathanfallet/makth/extensions/BooleanExtensionTest.kt @@ -0,0 +1,38 @@ +package me.nathanfallet.makth.extensions + +import org.junit.Assert.assertEquals +import org.junit.Test + +class BooleanExtensionTest { + + @Test + fun toAlgorithmStringTrue() { + assertEquals("true", BooleanValue(true).toAlgorithmString()) + } + + @Test + fun toRawStringTrue() { + assertEquals("true", BooleanValue(true).toRawString()) + } + + @Test + fun toLaTeXStringTrue() { + assertEquals("\\text{true}", BooleanValue(true).toLaTeXString()) + } + + @Test + fun toAlgorithmStringFalse() { + assertEquals("false", BooleanValue(false).toAlgorithmString()) + } + + @Test + fun toRawStringFalse() { + assertEquals("false", BooleanValue(false).toRawString()) + } + + @Test + fun toLaTeXStringFalse() { + assertEquals("\\text{false}", BooleanValue(false).toLaTeXString()) + } + +} \ No newline at end of file diff --git a/src/test/kotlin/me/nathanfallet/makth/extensions/LongExtensionTest.kt b/src/test/kotlin/me/nathanfallet/makth/extensions/LongExtensionTest.kt new file mode 100644 index 0000000..c57cf97 --- /dev/null +++ b/src/test/kotlin/me/nathanfallet/makth/extensions/LongExtensionTest.kt @@ -0,0 +1,38 @@ +package me.nathanfallet.makth.extensions + +import org.junit.Assert.assertEquals +import org.junit.Test + +class LongExtensionTest { + + @Test + fun gcd() { + assertEquals(2L, 4L.gcd(6L)) + } + + @Test + fun gcdWithZero() { + assertEquals(4L, 4L.gcd(0L)) + } + + @Test + fun gcdWithZero2() { + assertEquals(6L, 0L.gcd(6L)) + } + + @Test + fun power() { + assertEquals(8L, 2L.pow(3L)) + } + + @Test + fun nthRoot() { + assertEquals(2.0, 4L.nthRoot(2L), 0.0) + } + + @Test + fun nthRoot2() { + assertEquals(16.0, 256L.nthRoot(2L), 0.0) + } + +} \ No newline at end of file diff --git a/src/test/kotlin/me/nathanfallet/makth/extensions/StringExtensionTest.kt b/src/test/kotlin/me/nathanfallet/makth/extensions/StringExtensionTest.kt new file mode 100644 index 0000000..38d5427 --- /dev/null +++ b/src/test/kotlin/me/nathanfallet/makth/extensions/StringExtensionTest.kt @@ -0,0 +1,68 @@ +package me.nathanfallet.makth.extensions + +import org.junit.Assert.assertEquals +import org.junit.Test + +class StringExtensionTest { + + @Test + fun toAlgorithmString() { + assertEquals("\"Hello world!\"", StringValue("Hello world!").toAlgorithmString()) + } + + @Test + fun toRawString() { + assertEquals("Hello world!", StringValue("Hello world!").toRawString()) + } + + @Test + fun toLaTeXString() { + assertEquals("\\text{Hello world!}", StringValue("Hello world!").toLaTeXString()) + } + + @Test + fun toAlgorithmStringWithLaTeX() { + assertEquals("\$x ^ 2\$", StringValue("x ^ 2", true).toAlgorithmString()) + } + + @Test + fun toRawStringWithLaTeX() { + assertEquals("x ^ 2", StringValue("x ^ 2", true).toRawString()) + } + + @Test + fun toLaTeXStringWithLaTeX() { + assertEquals("x ^ 2", StringValue("x ^ 2", true).toLaTeXString()) + } + + @Test + fun toAlgorithmStringWithEscape() { + assertEquals("\"Hello \\\"world\\\"!\"", StringValue("Hello \"world\"!").toAlgorithmString()) + } + + @Test + fun toRawStringWithEscape() { + assertEquals("Hello \"world\"!", StringValue("Hello \"world\"!").toRawString()) + } + + @Test + fun toLaTeXStringWithEscape() { + assertEquals("\\text{Hello \"world\"!}", StringValue("Hello \"world\"!").toLaTeXString()) + } + + @Test + fun toAlgorithmStringWithLaTeXAndEscape() { + assertEquals("\$x \\\$ 2\$", StringValue("x \$ 2", true).toAlgorithmString()) + } + + @Test + fun toRawStringWithLaTeXAndEscape() { + assertEquals("x \$ 2", StringValue("x \$ 2", true).toRawString()) + } + + @Test + fun toLaTeXStringWithLaTeXAndEscape() { + assertEquals("x \\\$ 2", StringValue("x \$ 2", true).toLaTeXString()) + } + +} \ No newline at end of file diff --git a/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt b/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt index 41c0471..cee25d0 100644 --- a/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt +++ b/src/test/kotlin/me/nathanfallet/makth/lexers/MathLexerTest.kt @@ -30,6 +30,31 @@ class MathLexerTest { assertEquals(StringValue("Hello world!"), MathLexer("\"Hello world!\"").execute(context)) } + @Test + fun parseStringWithEscape() { + assertEquals(StringValue("Hello \"world\"!"), MathLexer("\"Hello \\\"world\\\"!\"").execute(context)) + } + + @Test + fun parseLaTeXString() { + assertEquals(StringValue("x ^ 2", true), MathLexer("\$x ^ 2\$").execute(context)) + } + + @Test + fun parseLaTeXStringWithEscape() { + assertEquals(StringValue("x \$ 2", true), MathLexer("\$x \\\$ 2\$").execute(context)) + } + + @Test + fun parseBooleanTrue() { + assertEquals(BooleanValue(true), MathLexer("true").execute(context)) + } + + @Test + fun parseBooleanFalse() { + assertEquals(BooleanValue(false), MathLexer("false").execute(context)) + } + @Test fun parseVariable() { assertEquals(Variable("x"), MathLexer("x").execute(context))