fixed comment before else in if;

dynamic now support indexing access
This commit is contained in:
sergeych committed 2025-10-04 11:25:19 +04:00
1 parent e8b630715a
commit c854b06683
7 files changed
+90 -8

No files matched your search

@@ -888,6 +888,7 @@ class Compiler(
}
}
@Suppress("SameParameterValue")
private fun parseNumber(isPlus: Boolean): Obj {
return parseNumberOrNull(isPlus) ?: throw ScriptError(cc.currentPos(), "Expecting number")
}
@@ -1681,7 +1682,7 @@ class Compiler(
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
// could be else block:
val t2 = cc.next()
val t2 = cc.nextNonWhitespace()
// we generate different statements: optimization
return if (t2.type == Token.Type.ID && t2.value == "else") {
@@ -28,7 +28,9 @@ data class Pos(val source: Source, val line: Int, val column: Int) {
else if( line > 0) Pos(source, line-1, source.lines[line-1].length - 1)
else throw IllegalStateException("can't go back from line 0, column 0")
val currentLine: String get() = if( end ) "EOF" else source.lines[line]
val currentLine: String get() =
if( end ) "EOF"
else if( line >= 0 ) source.lines[line] else "<no line information>"
val end: Boolean get() = line >= source.lines.size
@@ -26,7 +26,7 @@ open class ScriptError(val pos: Pos, val errorMessage: String, cause: Throwable?
$pos: Error: $errorMessage
${pos.currentLine}
${"-".repeat(pos.column)}^
${if( pos.column >= 0 ) "-".repeat(pos.column) + "^" else ""}
""".trimIndent(),
cause
)
@@ -67,6 +67,16 @@ class ObjDynamic : Obj() {
?: super.writeField(scope, name, newValue)
}
override suspend fun getAt(scope: Scope, index: Obj): Obj {
return readCallback?.execute( scope.copy(Arguments(index)))
?: super.getAt(scope, index)
}
override suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
writeCallback?.execute(scope.copy(Arguments(index, newValue)))
?: super.putAt(scope, index, newValue)
}
companion object {
suspend fun create(scope: Scope, builder: Statement): ObjDynamic {
@@ -76,7 +86,9 @@ class ObjDynamic : Obj() {
return delegate
}
val type = object : ObjClass("Delegate") {}
val type = object : ObjClass("Delegate") {}.apply {
// addClassConst("IndexGetName", operatorGetName)
}
}
}
+20
View File
@@ -107,6 +107,26 @@ class OOTest {
""".trimIndent())
}
@Test
fun testDynamicIndexAccess() = runTest {
eval("""
val store = Map()
val accessor = dynamic {
get { name ->
store[name]
}
set { name, value ->
store[name] = value
}
}
assertEquals(null, accessor["foo"])
assertEquals(null, accessor.foo)
accessor["foo"] = "bar"
assertEquals("bar", accessor["foo"])
assertEquals("bar", accessor.foo)
""".trimIndent())
}
@Test
fun testMultilineConstructor() = runTest {
eval("""