Skip to content

Commit

Permalink
Refactoring to compile for JS
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfallet committed Nov 4, 2023
1 parent 7f53ca7 commit 26f9719
Show file tree
Hide file tree
Showing 86 changed files with 2,669 additions and 2,150 deletions.
7 changes: 6 additions & 1 deletion 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.1"
version = "1.2.2"

repositories {
mavenCentral()
Expand Down Expand Up @@ -41,6 +41,11 @@ kotlin {


sourceSets {
all {
languageSettings.apply {
optIn("kotlin.js.ExperimentalJsExport")
}
}
val commonMain by getting
val commonTest by getting {
dependencies {
Expand Down
2 changes: 1 addition & 1 deletion docs/actions/action_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SetAction("name", value)
Or

```kotlin
SetAction(Variable.instantiate("name"), value)
SetAction(VariableFactory.instantiate("name"), value)
```

## Makth lexer syntax
Expand Down
13 changes: 7 additions & 6 deletions docs/actions/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ You can execute actions to play with a context:

```kotlin
val result = Context().execute(listOf(
SetAction("x", Integer.instantiate(2)),
WhileAction(Equality(Variable.instantiate("x"), Integer.instantiate(10), Equality.Operator.LessThan), listOf(
SetAction("x", Sum(Variable.instantiate("x"), Integer.instantiate(1)))
SetAction("x", IntegerFactory.instantiate(2)),
WhileAction(
Equality(VariableFactory.instantiate("x"), IntegerFactory.instantiate(10), Equality.Operator.LessThan), listOf(
SetAction("x", Sum(VariableFactory.instantiate("x"), IntegerFactory.instantiate(1)))
)),
PrintAction(listOf(StringValue("x = "), Variable.instantiate("x")))
PrintAction(listOf(StringValue("x = "), VariableFactory.instantiate("x")))
))
```

Expand All @@ -17,10 +18,10 @@ val result = Context().execute(listOf(
```kotlin
Context(
mapOf(
"x" to Integer.instantiate(10) // The value of x
"x" to IntegerFactory.instantiate(10) // The value of x
),
listOf(
StringValue("x = "), Integer.instantiate(10), StringValue("\n") // What we printed
StringValue("x = "), IntegerFactory.instantiate(10), StringValue("\n") // What we printed
)
)
```
20 changes: 18 additions & 2 deletions docs/getstarted/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add the following to your `pom.xml` file;
<dependency>
<groupId>me.nathanfallet.makth</groupId>
<artifactId>makth-jvm</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</dependency>
```

Expand All @@ -22,6 +22,22 @@ repositories {
}
dependencies {
implementation 'me.nathanfallet.makth:makth:1.2.1'
implementation 'me.nathanfallet.makth:makth:1.2.2'
}
```

## npm

Install the package using npm:

```bash
npm install makth
```

## yarn

Install the package using yarn:

```bash
yarn add makth
```
8 changes: 4 additions & 4 deletions docs/numbers/instantiate.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Instantiate numbers

```kotlin
val integer = Integer.instantiate(2) // 2
val rational = Rational.instantiate(3, 2) // 3/2
val real = Real.instantiate(1.23456789) // 1.23456789
val integer = IntegerFactory.instantiate(2) // 2
val rational = RationalFactory.instantiate(3, 2) // 3/2
val real = RealFactory.instantiate(1.23456789) // 1.23456789
```

## Predefined constants

```kotlin
val pi = Real.pi
val pi = RealFactory.pi
```
2 changes: 1 addition & 1 deletion docs/numbers/lexer.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Thanks to the context, you can pass variables:

```kotlin
val context = Context(mapOf(
"x" to Integer.instantiate(2)
"x" to IntegerFactory.instantiate(2)
))
val result = MathLexer("x + 3").execute(context) // 5
```
15 changes: 11 additions & 4 deletions src/commonMain/kotlin/me/nathanfallet/makth/actions/ForAction.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.nathanfallet.makth.actions

import me.nathanfallet.makth.exceptions.ExecutionException
import me.nathanfallet.makth.exceptions.NotIterableException
import me.nathanfallet.makth.exceptions.UnknownVariablesException
import me.nathanfallet.makth.extensions.StringValue
import me.nathanfallet.makth.extensions.indentedLines
import me.nathanfallet.makth.interfaces.Action
Expand All @@ -8,14 +11,17 @@ import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException
import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentTypeException
import me.nathanfallet.makth.resolvables.Context
import me.nathanfallet.makth.resolvables.Variable
import me.nathanfallet.makth.resolvables.variables.Variable
import kotlin.js.JsExport
import kotlin.jvm.JvmStatic

/**
* Action that executes a list of actions for a given set or interval.
* @param identifier Identifier of the current iterated value
* @param iterable Iterable to iterate during the loop
* @param actions Actions to execute while condition is true
*/
@JsExport
data class ForAction(val identifier: String, val iterable: Value, val actions: List<Action>) : Action {

companion object {
Expand All @@ -25,6 +31,7 @@ data class ForAction(val identifier: String, val iterable: Value, val actions: L
* @param args List of arguments
* @return Action created from arguments
*/
@JvmStatic
fun handler(args: List<Value>): Action {
if (args.count() != 2) {
throw IncorrectArgumentCountException("for", args.count(), 2)
Expand All @@ -38,19 +45,19 @@ data class ForAction(val identifier: String, val iterable: Value, val actions: L
}
}

@Throws(Action.ExecutionException::class)
@Throws(ExecutionException::class)
override fun execute(context: Context): Context {
// Eval iterator
val evaluatedIterable = iterable.compute(context)

// Check if there are missing variables
evaluatedIterable.variables.takeIf { it.isNotEmpty() }?.let {
throw Action.UnknownVariablesException(this, context, it)
throw UnknownVariablesException(this, context, it)
}

// Check if condition is a boolean
if (evaluatedIterable !is Iterable) {
throw Action.NotIterableException(this, context, evaluatedIterable)
throw NotIterableException(this, context, evaluatedIterable)
}

// Iterate
Expand Down
16 changes: 12 additions & 4 deletions src/commonMain/kotlin/me/nathanfallet/makth/actions/IfAction.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package me.nathanfallet.makth.actions

import me.nathanfallet.makth.exceptions.ExecutionException
import me.nathanfallet.makth.exceptions.NotABooleanException
import me.nathanfallet.makth.exceptions.UnknownVariablesException
import me.nathanfallet.makth.extensions.BooleanValue
import me.nathanfallet.makth.extensions.indentedLines
import me.nathanfallet.makth.interfaces.Action
import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException
import me.nathanfallet.makth.resolvables.Context
import kotlin.js.JsExport
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic

/**
* Action that executes a list of actions if a condition is true,
Expand All @@ -14,7 +20,8 @@ import me.nathanfallet.makth.resolvables.Context
* @param actions Actions to execute if condition is true
* @param elseActions Actions to execute if condition is false
*/
data class IfAction(
@JsExport
data class IfAction @JvmOverloads constructor(
val condition: Value,
val actions: List<Action>,
val elseActions: List<Action> = listOf()
Expand All @@ -27,6 +34,7 @@ data class IfAction(
* @param args List of arguments
* @return Action created from arguments
*/
@JvmStatic
fun handler(args: List<Value>): Action {
if (args.count() != 1) {
throw IncorrectArgumentCountException("if", args.count(), 1)
Expand All @@ -35,19 +43,19 @@ data class IfAction(
}
}

@Throws(Action.ExecutionException::class)
@Throws(ExecutionException::class)
override fun execute(context: Context): Context {
// Eval condition
val evaluatedCondition = condition.compute(context)

// Check if there are missing variables
evaluatedCondition.variables.takeIf { it.isNotEmpty() }?.let {
throw Action.UnknownVariablesException(this, context, it)
throw UnknownVariablesException(this, context, it)
}

// Check if condition is a boolean
if (evaluatedCondition !is BooleanValue) {
throw Action.NotABooleanException(this, context, evaluatedCondition)
throw NotABooleanException(this, context, evaluatedCondition)
}

// Execute if it is true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package me.nathanfallet.makth.actions

import me.nathanfallet.makth.exceptions.ExecutionException
import me.nathanfallet.makth.exceptions.UnknownVariablesException
import me.nathanfallet.makth.extensions.StringValue
import me.nathanfallet.makth.interfaces.Action
import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.resolvables.Context
import kotlin.js.JsExport
import kotlin.jvm.JvmStatic

/**
* Action that prints values
* @param values Values to print
*/
@JsExport
data class PrintAction(val values: List<Value>) : Action {

companion object {
Expand All @@ -18,12 +23,13 @@ data class PrintAction(val values: List<Value>) : Action {
* @param args Arguments of the action
* @return Action created from the arguments
*/
@JvmStatic
fun handler(args: List<Value>): Action {
return PrintAction(args.toList())
}
}

@Throws(Action.ExecutionException::class)
@Throws(ExecutionException::class)
override fun execute(context: Context): Context {
// Generate output
val output =
Expand All @@ -33,7 +39,7 @@ data class PrintAction(val values: List<Value>) : Action {

// Check for missing variables
computed.variables.takeIf { it.isNotEmpty() }?.let {
throw Action.UnknownVariablesException(this, context, it)
throw UnknownVariablesException(this, context, it)
}

// Return
Expand Down
12 changes: 9 additions & 3 deletions src/commonMain/kotlin/me/nathanfallet/makth/actions/SetAction.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package me.nathanfallet.makth.actions

import me.nathanfallet.makth.exceptions.ExecutionException
import me.nathanfallet.makth.exceptions.UnknownVariablesException
import me.nathanfallet.makth.extensions.StringValue
import me.nathanfallet.makth.interfaces.Action
import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException
import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentTypeException
import me.nathanfallet.makth.resolvables.Context
import me.nathanfallet.makth.resolvables.Variable
import me.nathanfallet.makth.resolvables.variables.Variable
import kotlin.js.JsExport
import kotlin.jvm.JvmStatic

/**
* Action that sets a variable
* @param identifier Identifier of the variable to set
* @param value Value to set
*/
@JsExport
data class SetAction(val identifier: String, val value: Value) : Action {

companion object {
Expand All @@ -22,6 +27,7 @@ data class SetAction(val identifier: String, val value: Value) : Action {
* @param args Arguments of the action
* @return Action created from the arguments
*/
@JvmStatic
fun handler(args: List<Value>): Action {
if (args.count() != 2) {
throw IncorrectArgumentCountException("set", args.count(), 2)
Expand All @@ -35,14 +41,14 @@ data class SetAction(val identifier: String, val value: Value) : Action {
}
}

@Throws(Action.ExecutionException::class)
@Throws(ExecutionException::class)
override fun execute(context: Context): Context {
// First, compute the value with the given context
val valueToSet = value.compute(context)

// Check if there are missing variables
valueToSet.variables.takeIf { it.isNotEmpty() }?.let {
throw Action.UnknownVariablesException(this, context, it)
throw UnknownVariablesException(this, context, it)
}

// Return the new context
Expand Down
13 changes: 10 additions & 3 deletions src/commonMain/kotlin/me/nathanfallet/makth/actions/WhileAction.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package me.nathanfallet.makth.actions

import me.nathanfallet.makth.exceptions.ExecutionException
import me.nathanfallet.makth.exceptions.NotABooleanException
import me.nathanfallet.makth.exceptions.UnknownVariablesException
import me.nathanfallet.makth.extensions.BooleanValue
import me.nathanfallet.makth.extensions.indentedLines
import me.nathanfallet.makth.interfaces.Action
import me.nathanfallet.makth.interfaces.Value
import me.nathanfallet.makth.lexers.AlgorithmLexer.IncorrectArgumentCountException
import me.nathanfallet.makth.resolvables.Context
import kotlin.js.JsExport
import kotlin.jvm.JvmStatic

/**
* Action that executes a list of actions while a condition is true.
* @param condition Condition to check
* @param actions Actions to execute while condition is true
*/
@JsExport
data class WhileAction(val condition: Value, val actions: List<Action>) : Action {

companion object {
Expand All @@ -21,6 +27,7 @@ data class WhileAction(val condition: Value, val actions: List<Action>) : Action
* @param args List of arguments
* @return Action created from arguments
*/
@JvmStatic
fun handler(args: List<Value>): Action {
if (args.count() != 1) {
throw IncorrectArgumentCountException("while", args.count(), 1)
Expand All @@ -29,19 +36,19 @@ data class WhileAction(val condition: Value, val actions: List<Action>) : Action
}
}

@Throws(Action.ExecutionException::class)
@Throws(ExecutionException::class)
override fun execute(context: Context): Context {
// Eval condition
val evaluatedCondition = condition.compute(context)

// Check if there are missing variables
evaluatedCondition.variables.takeIf { it.isNotEmpty() }?.let {
throw Action.UnknownVariablesException(this, context, it)
throw UnknownVariablesException(this, context, it)
}

// Check if condition is a boolean
if (evaluatedCondition !is BooleanValue) {
throw Action.NotABooleanException(this, context, evaluatedCondition)
throw NotABooleanException(this, context, evaluatedCondition)
}

// Execute if it is true
Expand Down
Loading

0 comments on commit 26f9719

Please sign in to comment.