-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhirkevich Alexander Y
authored and
Zhirkevich Alexander Y
committed
Jul 31, 2024
1 parent
0e52188
commit 87c4f9a
Showing
49 changed files
with
2,362 additions
and
1,655 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
skriptie/src/commonMain/kotlin/io/github/alexzhirkevich/skriptie/ExpressionContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.github.alexzhirkevich.skriptie | ||
|
||
import io.github.alexzhirkevich.skriptie.javascript.JSInterpretationContext | ||
import kotlin.math.min | ||
|
||
public class ExpressionContext : JSInterpretationContext() { | ||
|
||
override fun sum(a: Any?, b: Any?): Any? { | ||
return when { | ||
a is List<*> && b is List<*> -> { | ||
a as List<Number> | ||
b as List<Number> | ||
|
||
List(min(a.size, b.size)) { | ||
a[it].toDouble() + b[it].toDouble() | ||
} | ||
} | ||
|
||
a is List<*> && b is Number -> { | ||
if (a is MutableList<*>) { | ||
a as MutableList<Number> | ||
a[0] = a[0].toDouble() + b.toDouble() | ||
a | ||
} else { | ||
listOf((a as List<Number>).first().toDouble() + b.toDouble()) + a.drop(1) | ||
} | ||
} | ||
|
||
a is Number && b is List<*> -> { | ||
if (b is MutableList<*>) { | ||
b as MutableList<Number> | ||
b[0] = b[0].toDouble() + a.toDouble() | ||
b | ||
} else { | ||
listOf(a.toDouble() + (b as List<Number>).first().toDouble()) + b.drop(1) | ||
} | ||
} | ||
|
||
else -> super.sum(a, b) | ||
} | ||
} | ||
|
||
override fun sub(a: Any?, b: Any?): Any? { | ||
return when { | ||
a is List<*> && b is List<*> -> { | ||
a as List<Number> | ||
b as List<Number> | ||
List(min(a.size, b.size)) { | ||
a[it].toDouble() - b[it].toDouble() | ||
} | ||
} | ||
else -> super.sub(a, b) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
skriptie/src/commonMain/kotlin/io/github/alexzhirkevich/skriptie/Script.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package io.github.alexzhirkevich.skriptie | ||
|
||
public fun interface Script<C : ScriptContext> { | ||
public operator fun invoke(engine: ScriptEngine<C>): Any? | ||
public operator fun invoke(context: C): Any? | ||
} | ||
|
||
public fun <C : ScriptContext> Expression<C>.asScript(): Script<C> = Script { invoke(it.context) } | ||
public fun <C : ScriptContext> Expression<C>.asScript(): Script<C> = Script { invoke(it) } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 13 additions & 5 deletions
18
skriptie/src/commonMain/kotlin/io/github/alexzhirkevich/skriptie/ScriptEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
package io.github.alexzhirkevich.skriptie | ||
|
||
public interface ScriptEngine<C : ScriptContext> { | ||
public interface ScriptEngine<C : ScriptContext> : ScriptInterpreter<C> { | ||
|
||
public val context : C | ||
|
||
public fun compile(script : String) : Script<C> | ||
|
||
public fun reset() | ||
public fun reset() { | ||
context.reset() | ||
} | ||
} | ||
|
||
public fun <C : ScriptContext> ScriptEngine<C>.invoke(script: String) : Any? { | ||
return compile(script).invoke(this) | ||
return interpret(script).invoke(context) | ||
} | ||
|
||
public fun <C : ScriptContext> ScriptEngine( | ||
context: C, | ||
interpreter: ScriptInterpreter<C> | ||
): ScriptEngine<C> = object : ScriptEngine<C>, ScriptInterpreter<C> by interpreter { | ||
override val context: C | ||
get() = context | ||
} |
5 changes: 3 additions & 2 deletions
5
skriptie/src/commonMain/kotlin/io/github/alexzhirkevich/skriptie/ScriptInterpreter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package io.github.alexzhirkevich.skriptie | ||
|
||
internal interface ScriptInterpreter<C : ScriptContext> { | ||
fun interpret() : Script<C> | ||
public interface ScriptInterpreter<C : ScriptContext> { | ||
|
||
public fun interpret(script : String) : Script<C> | ||
} |
14 changes: 14 additions & 0 deletions
14
skriptie/src/commonMain/kotlin/io/github/alexzhirkevich/skriptie/common/Erorrs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.alexzhirkevich.skriptie.common | ||
|
||
public sealed class SkriptieError(message : String?, cause : Throwable?) : Exception(message, cause) | ||
|
||
public class SyntaxError(message : String? = null, cause : Throwable? = null) : SkriptieError(message, cause) | ||
|
||
public class TypeError(message : String? = null, cause : Throwable? = null) : SkriptieError(message, cause) | ||
|
||
public class ReferenceError(message : String? = null, cause : Throwable? = null) : SkriptieError(message, cause) | ||
|
||
internal fun unresolvedReference(ref : String, obj : String? = null) : Nothing = | ||
if (obj != null) | ||
throw ReferenceError("Unresolved reference '$ref' for $obj") | ||
else throw ReferenceError("$ref is not defined") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.