Skip to content

Commit

Permalink
Exporting usecases for lexers
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet committed Dec 18, 2023
1 parent 798aec0 commit 8b59197
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 11 deletions.
8 changes: 6 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "me.nathanfallet.makth"
version = "1.2.3"
version = "1.2.4"

repositories {
mavenCentral()
Expand Down Expand Up @@ -61,7 +61,11 @@ kotlin {
optIn("kotlin.js.ExperimentalJsExport")
}
}
val commonMain by getting
val commonMain by getting {
dependencies {
api("me.nathanfallet.usecases:usecases:1.5.5")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
Expand Down
5 changes: 3 additions & 2 deletions docs/getstarted/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
Add the following to your `pom.xml` file;

```xml

<dependency>
<groupId>me.nathanfallet.makth</groupId>
<artifactId>makth-jvm</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
</dependency>
```

Expand All @@ -22,7 +23,7 @@ repositories {
}
dependencies {
implementation 'me.nathanfallet.makth:makth:1.2.3'
implementation 'me.nathanfallet.makth:makth:1.2.4'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.reflect.KClass
* @param content Content of the algorithm
*/
@JsExport
class AlgorithmLexer(private var content: String) {
class AlgorithmLexer(private val content: String) {

// Errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.js.JsExport
* @param content Content to parse
*/
@JsExport
class MathLexer(private var content: String) {
class MathLexer(private val content: String) {

// Errors

Expand All @@ -28,14 +28,14 @@ class MathLexer(private var content: String) {
* @param operator Unknown operator
*/
open class UnknownOperatorException(val operator: String) :
SyntaxException("Unknown operator: $operator")
SyntaxException("Unknown operator: $operator")

// Constants

object Constants {
const val NUMBERS = "0123456789"
const val VARIABLES =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΛλΜμΝνΞξΟοΠπΣσςϹϲΤτΥυΦφΧχΨψΩω_" + NUMBERS
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΛλΜμΝνΞξΟοΠπΣσςϹϲΤτΥυΦφΧχΨψΩω_" + NUMBERS
}

// Parsing vars
Expand Down Expand Up @@ -173,7 +173,7 @@ class MathLexer(private var content: String) {

private fun parseVariable() {
// Check name
var name = StringBuilder()
val name = StringBuilder()
name.append(content[i])
while (i < content.length - 1 && Constants.VARIABLES.contains(content[i + 1])) {
// Add character to function name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import me.nathanfallet.makth.exceptions.ExecutionException
import me.nathanfallet.makth.interfaces.Action
import me.nathanfallet.makth.interfaces.Output
import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.usecases.context.IContext
import kotlin.js.JsExport
import kotlin.js.JsName

Expand All @@ -15,8 +16,8 @@ import kotlin.js.JsName
@JsExport
data class Context(
val data: Map<String, Value> = mapOf(),
val outputs: List<Output> = listOf()
) {
val outputs: List<Output> = listOf(),
) : IContext {

/**
* Execute an action in this context
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.nathanfallet.makth.usecases

import me.nathanfallet.makth.interfaces.Action
import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.usecases.base.IPairUseCase
import kotlin.js.JsExport
import kotlin.js.JsName

@JsExport
interface IParseAlgorithmUseCase : IPairUseCase<String, Map<String, (List<Value>) -> Action>, List<Action>> {

@JsName("invokeDefault")
operator fun invoke(input: String): List<Action> {
return invoke(input, emptyMap())
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.nathanfallet.makth.usecases

import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.resolvables.Context
import me.nathanfallet.usecases.base.IPairUseCase
import kotlin.js.JsExport

@JsExport
interface IParseMathUseCase : IPairUseCase<String, Context, Value>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.nathanfallet.makth.usecases

import me.nathanfallet.makth.interfaces.Action
import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.lexers.AlgorithmLexer
import kotlin.js.JsExport

@JsExport
class ParseAlgorithmUseCase : IParseAlgorithmUseCase {

override fun invoke(input1: String, input2: Map<String, (List<Value>) -> Action>): List<Action> {
return AlgorithmLexer(input1).registerKeywords(input2).execute()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.nathanfallet.makth.usecases

import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.lexers.MathLexer
import me.nathanfallet.makth.resolvables.Context
import kotlin.js.JsExport

@JsExport
class ParseMathUseCase : IParseMathUseCase {

override fun invoke(input1: String, input2: Context): Value {
return MathLexer(input1).execute(input2)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.nathanfallet.makth.usecases

import me.nathanfallet.makth.actions.SetAction
import me.nathanfallet.makth.lexers.AlgorithmLexerTest
import me.nathanfallet.makth.numbers.integers.IntegerFactory
import kotlin.test.Test
import kotlin.test.assertEquals

class ParseAlgorithmUseCaseTest {

@Test
fun testInvoke() {
val useCase = ParseAlgorithmUseCase()
assertEquals(
listOf(
SetAction("x", IntegerFactory.instantiate(2))
),
useCase.invoke("set(x, 2)")
)
}

@Test
fun testInvokeWithCustomKeywords() {
val useCase = ParseAlgorithmUseCase()
assertEquals(
listOf(
AlgorithmLexerTest.CustomAction(IntegerFactory.instantiate(2))
),
useCase.invoke("custom(2)", mapOf("custom" to AlgorithmLexerTest.CustomAction::handler))
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.nathanfallet.makth.usecases

import me.nathanfallet.makth.numbers.integers.IntegerFactory
import me.nathanfallet.makth.resolvables.Context
import kotlin.test.Test
import kotlin.test.assertEquals

class ParseMathUseCaseTest {

private val contextWithX = Context(
mapOf(
"x" to IntegerFactory.instantiate(2)
)
)

@Test
fun testInvoke() {
val useCase = ParseMathUseCase()
assertEquals(
IntegerFactory.instantiate(4),
useCase("2 * x", contextWithX)
)
}

}

0 comments on commit 8b59197

Please sign in to comment.