Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhirkevich Alexander Y authored and Zhirkevich Alexander Y committed Sep 5, 2024
1 parent 98990b2 commit ef7f406
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.alexzhirkevich.skriptie.ecmascript

import io.github.alexzhirkevich.skriptie.Expression
import io.github.alexzhirkevich.skriptie.InterpretationContext

public open class EcmascriptInterpretationContext(
public val namedArgumentsEnabled : Boolean = false
) : InterpretationContext {
override fun interpret(callable: String?, args: List<Expression>?): Expression? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import io.github.alexzhirkevich.skriptie.isAssignable
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

internal val EXPR_DEBUG_PRINT_ENABLED = true
internal val EXPR_DEBUG_PRINT_ENABLED = false

internal enum class LogicalContext {
And, Or, Compare
Expand Down Expand Up @@ -150,21 +150,54 @@ internal class EcmascriptInterpreterImpl(
}
}

private fun parseAssignment(context: Expression, blockContext: List<BlockContext>): Expression {
private fun parseAssignment(
context: Expression,
blockContext: List<BlockContext>,
unaryOnly : Boolean = false
): Expression {
var x = parseExpressionOp(context, blockContext = blockContext)
if (EXPR_DEBUG_PRINT_ENABLED) {
println("Parsing assignment for $x")
}

val checkAssignment = {
if (unaryOnly)
throw SyntaxError("Invalid left-hand side in assignment")
}

while (true) {
prepareNextChar()
x = when {
eatSequence("+=") -> parseAssignmentValue(x, langContext::sum)
eatSequence("-=") -> parseAssignmentValue(x, langContext::sub)
eatSequence("*=") -> parseAssignmentValue(x, langContext::mul)
eatSequence("/=") -> parseAssignmentValue(x, langContext::div)
eatSequence("%=") -> parseAssignmentValue(x, langContext::mod)
eatSequence("+=") -> {
checkAssignment()
parseAssignmentValue(x, langContext::sum)
}

eatSequence("-=") -> {
checkAssignment()
parseAssignmentValue(x, langContext::sub)
}

eatSequence("*=") -> {
checkAssignment()
parseAssignmentValue(x, langContext::mul)
}

eatSequence("/=") -> {
checkAssignment()
parseAssignmentValue(x, langContext::div)
}

eatSequence("%=") -> {
checkAssignment()
parseAssignmentValue(x, langContext::mod)
}

eatSequence("=>") -> OpConstant(parseArrowFunction(listOf(x), blockContext))
eat('=') -> parseAssignmentValue(x, null)
eat('=') -> {
checkAssignment()
parseAssignmentValue(x, null)
}
eatSequence("++") -> {
check(x.isAssignable()) {
"Not assignable"
Expand All @@ -187,6 +220,29 @@ internal class EcmascriptInterpreterImpl(
)
}

eat('?') -> {
if (EXPR_DEBUG_PRINT_ENABLED){
println("making ternary operator: onTrue...")
}

val onTrue = parseAssignment(globalContext, blockContext)

if (!eat(':')){
throw SyntaxError("Unexpected end of input")
}
if (EXPR_DEBUG_PRINT_ENABLED) {
println("making ternary operator: onFalse...")
}
val onFalse = parseAssignment(globalContext, blockContext)

OpIfCondition(
condition = x,
onTrue = onTrue,
onFalse = onFalse,
expressible = true
)
}

else -> return x
}
}
Expand Down Expand Up @@ -384,7 +440,7 @@ internal class EcmascriptInterpreterImpl(
return@buildList
}
do {
add(parseExpressionOp(context, blockContext = blockContext))
add(parseAssignment(context, blockContext = blockContext))
} while (eat(','))

check(eat(')')) {
Expand Down Expand Up @@ -491,8 +547,8 @@ internal class EcmascriptInterpreterImpl(
println("making index... ")
}
OpIndex(
context,
parseExpressionOp(globalContext, blockContext = blockContext)
variable = context,
index = parseExpressionOp(globalContext, blockContext = blockContext)
).also {
require(eat(']')) {
"Bad expression: Missing ']'"
Expand Down Expand Up @@ -632,6 +688,23 @@ internal class EcmascriptInterpreterImpl(
}
}

"typeof" -> {
val expr = parseAssignment(
context = globalContext,
blockContext = emptyList(),
unaryOnly = true
)
Expression {
when (val v = expr(it)) {
null -> "object"
Unit -> "undefined"
true, false -> "boolean"

is ESAny -> v.type
else -> v::class.simpleName
}
}
}

"null" -> OpConstant(null)
"true" -> OpConstant(true)
Expand Down Expand Up @@ -856,15 +929,12 @@ internal class EcmascriptInterpreterImpl(
if (assign != null) {
check(eat(';'))
}
println(assign)
val comparison = if (eat(';')) null else parseAssignment(globalContext, emptyList())
if (comparison != null) {
check(eat(';'))
}
println(comparison)
val increment = if (eat(')')) null else parseAssignment(globalContext, emptyList())

println(increment)
if (increment != null) {
check(eat(')'))
}
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit ef7f406

Please sign in to comment.