Completed Expression evaluation
parent
35974645a4
commit
78c65bbf39
@ -0,0 +1,46 @@
|
|||||||
|
package fr.celticinfo.lox
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.Assertions.*
|
||||||
|
|
||||||
|
class InterpreterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `validate interpreter`() {
|
||||||
|
val code = """
|
||||||
|
(1 + 2 * 3 - 5) / 2
|
||||||
|
""".trimIndent()
|
||||||
|
val scanner = Scanner(code)
|
||||||
|
val tokens = scanner.scanTokens()
|
||||||
|
val parser = Parser(tokens)
|
||||||
|
val expr = parser.parse()
|
||||||
|
val value = Interpreter().interpret(expr!!)
|
||||||
|
assertEquals(1.0, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Division by zero should raise error`() {
|
||||||
|
val code = """
|
||||||
|
1 / 0
|
||||||
|
""".trimIndent()
|
||||||
|
val scanner = Scanner(code)
|
||||||
|
val tokens = scanner.scanTokens()
|
||||||
|
val parser = Parser(tokens)
|
||||||
|
val expr = parser.parse()
|
||||||
|
val value = Interpreter().interpret(expr!!)
|
||||||
|
assertNull(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Invalid type raise error`() {
|
||||||
|
val code = """
|
||||||
|
1 + false
|
||||||
|
""".trimIndent()
|
||||||
|
val scanner = Scanner(code)
|
||||||
|
val tokens = scanner.scanTokens()
|
||||||
|
val parser = Parser(tokens)
|
||||||
|
val expr = parser.parse()
|
||||||
|
val value = Interpreter().interpret(expr!!)
|
||||||
|
assertNull(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue