Skip to content

Commit

Permalink
Pushing v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet committed Nov 11, 2022
1 parent 5691f87 commit fe6a9ae
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 59 deletions.
32 changes: 2 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,9 @@

A Kotlin library for algebra

## Installation
## Installation and usage

### Maven

Add the following to your `pom.xml` file;

```xml
<dependency>
<groupId>me.nathanfallet.makth</groupId>
<artifactId>makth</artifactId>
<version>1.0.1</version>
</dependency>
```

### 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

Expand Down
27 changes: 27 additions & 0 deletions docs/getstarted/install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Installation

## Maven

Add the following to your `pom.xml` file;

```xml
<dependency>
<groupId>me.nathanfallet.makth</groupId>
<artifactId>makth</artifactId>
<version>1.1.0</version>
</dependency>
```

## Gradle

Add the following to your `build.gradle` file:

```groovy
repositories {
mavenCentral()
}
dependencies {
implementation 'me.nathanfallet.makth:makth:1.1.0'
}
```
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Makth documentation

## Get started

- [Installation](getstarted/install.md)

## Numbers

- [Instantiate numbers](numbers/instantiate.md)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.nathanfallet.makth</groupId>
<artifactId>makth</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>

<name>makth</name>
<description>A Kotlin library for algebra</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,4 @@ data class BooleanValue(val value: Boolean) : Value {
return setOf()
}

}

fun Boolean.getValue(): BooleanValue {
return BooleanValue(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ 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 {
return value
}

override fun toLaTeXString(): String {
return "\\text{$value}"
return if (latex) value.replace("\$", "\\\$") else "\\text{${value.replace("{", "\\{").replace("}", "\\}")}}"
}

override fun extractVariables(): Set<Variable> {
Expand All @@ -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" }
}
16 changes: 11 additions & 5 deletions src/main/kotlin/me/nathanfallet/makth/lexers/MathLexer.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/me/nathanfallet/makth/operations/Equality.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}

}
Original file line number Diff line number Diff line change
@@ -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)
}

}
Original file line number Diff line number Diff line change
@@ -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())
}

}
Loading

0 comments on commit fe6a9ae

Please sign in to comment.