big refactoring: all tests passed
This commit is contained in:
parent
eba29aedb7
commit
361c1d5b13
@ -283,12 +283,30 @@ class Compiler {
|
|||||||
operand = Accessor {
|
operand = Accessor {
|
||||||
statement.execute(it)
|
statement.execute(it)
|
||||||
}
|
}
|
||||||
|
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
|
||||||
cc.skipTokenOfType(Token.Type.RPAREN, "missing ')'")
|
cc.skipTokenOfType(Token.Type.RPAREN, "missing ')'")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Token.Type.ID -> {
|
Token.Type.ID -> {
|
||||||
operand?.let { left ->
|
// there could be terminal operators or keywords:// variable to read or like
|
||||||
|
when (t.value) {
|
||||||
|
"else" -> {
|
||||||
|
cc.previous()
|
||||||
|
return operand?.let { op -> statement(startPos) { op.getter(it) } }
|
||||||
|
}
|
||||||
|
"if", "when", "do", "while", "return" -> {
|
||||||
|
if( operand != null ) throw ScriptError(t.pos, "unexpected keyword")
|
||||||
|
cc.previous()
|
||||||
|
val s = parseStatement(cc) ?: throw ScriptError(t.pos, "Expecting valid statement")
|
||||||
|
operand = Accessor { s.execute(it) }
|
||||||
|
}
|
||||||
|
"break", "continue" -> {
|
||||||
|
cc.previous()
|
||||||
|
return operand?.let { op -> statement(startPos) { op.getter(it) } }
|
||||||
|
|
||||||
|
}
|
||||||
|
else -> operand?.let { left ->
|
||||||
// selector: <lvalue>, '.' , <id>
|
// selector: <lvalue>, '.' , <id>
|
||||||
// we replace operand with selector code, that
|
// we replace operand with selector code, that
|
||||||
// is RW:
|
// is RW:
|
||||||
@ -305,6 +323,10 @@ class Compiler {
|
|||||||
operand = parseAccessor(cc)
|
operand = parseAccessor(cc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// selector: <lvalue>, '.' , <id>
|
||||||
|
// we replace operand with selector code, that
|
||||||
|
// is RW:
|
||||||
|
}
|
||||||
|
|
||||||
Token.Type.PLUS2 -> {
|
Token.Type.PLUS2 -> {
|
||||||
// note: post-increment result is not assignable (truly lvalue)
|
// note: post-increment result is not assignable (truly lvalue)
|
||||||
|
@ -443,6 +443,7 @@ class ScriptTest {
|
|||||||
"""
|
"""
|
||||||
val count = 3
|
val count = 3
|
||||||
val res = if( count > 10 ) "too much" else "just " + count
|
val res = if( count > 10 ) "too much" else "just " + count
|
||||||
|
println(count)
|
||||||
println(res)
|
println(res)
|
||||||
res
|
res
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
@ -549,4 +550,18 @@ class ScriptTest {
|
|||||||
// assertEquals( "4", c.eval("x+0").toString())
|
// assertEquals( "4", c.eval("x+0").toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun bookTest2() = runTest {
|
||||||
|
val src = """
|
||||||
|
fn check(amount, prefix = "answer: ") {
|
||||||
|
prefix + if( amount > 100 )
|
||||||
|
"enough"
|
||||||
|
else
|
||||||
|
"more"
|
||||||
|
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
eval(src)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user