diff --git a/src/main/kotlin/me/camdenorrb/crescentvm/Main.kt b/src/main/kotlin/me/camdenorrb/crescentvm/Main.kt index 6ac9653..9eb4110 100644 --- a/src/main/kotlin/me/camdenorrb/crescentvm/Main.kt +++ b/src/main/kotlin/me/camdenorrb/crescentvm/Main.kt @@ -21,7 +21,7 @@ object Main { val code = """ fun main() { - println(round(2.5)) + println(round(-2.5)) } """ /* diff --git a/src/main/kotlin/me/camdenorrb/crescentvm/lexers/CrescentLexer.kt b/src/main/kotlin/me/camdenorrb/crescentvm/lexers/CrescentLexer.kt index bdfc83f..a42ddcf 100644 --- a/src/main/kotlin/me/camdenorrb/crescentvm/lexers/CrescentLexer.kt +++ b/src/main/kotlin/me/camdenorrb/crescentvm/lexers/CrescentLexer.kt @@ -1,6 +1,7 @@ package me.camdenorrb.crescentvm.lexers import me.camdenorrb.crescentvm.iterator.PeekingCharIterator +import me.camdenorrb.crescentvm.iterator.PeekingTokenIterator import me.camdenorrb.crescentvm.project.checkEquals import me.camdenorrb.crescentvm.vm.CrescentToken @@ -34,7 +35,11 @@ object CrescentLexer { val next = charIterator.next() val peek = charIterator.peekNext() - if (peek == '=' || next == '-' && peek == '>') { + if (next == '-' && (peek.isDigit() || (peek == '.' && charIterator.peekNext(2).isDigit()))) { + isANumber = true + "-${readNumber(charIterator)}" + } + else if (peek == '=' || next == '-' && peek == '>') { "$next${charIterator.next()}" } else { @@ -56,14 +61,7 @@ object CrescentLexer { isANumber = true // Select number, stop if rangeTo (..) is found - charIterator.nextUntil { - if (it == '.' && charIterator.peekNext(1) != '.') { - false - } - else { - !it.isDigit() - } - } + readNumber(charIterator) } peekNext.isLetter() -> { @@ -225,4 +223,15 @@ object CrescentLexer { return tokens } + fun readNumber(charIterator: PeekingCharIterator): String { + return charIterator.nextUntil { + if (it == '.' && charIterator.peekNext(1) != '.') { + false + } + else { + !it.isDigit() + } + } + } + } \ No newline at end of file diff --git a/src/test/kotlin/me/camdenorrb/crescentvm/manual/Bench.kt b/src/test/kotlin/me/camdenorrb/crescentvm/manual/Bench.kt index 447c6b4..d388b6d 100644 --- a/src/test/kotlin/me/camdenorrb/crescentvm/manual/Bench.kt +++ b/src/test/kotlin/me/camdenorrb/crescentvm/manual/Bench.kt @@ -3,15 +3,18 @@ package me.camdenorrb.crescentvm.manual import me.camdenorrb.crescentvm.data.TestCode import me.camdenorrb.crescentvm.lexers.CrescentLexer import me.camdenorrb.crescentvm.parsers.CrescentParser +import me.camdenorrb.crescentvm.vm.CrescentToPTIR +import me.camdenorrb.crescentvm.vm.CrescentVM import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.PrintStream import java.nio.file.Path +import kotlin.io.path.Path import kotlin.system.measureNanoTime internal object Bench { - const val DEFAULT_CYCLES = 5_000_000 + const val DEFAULT_CYCLES = 1_000_000 val filePath = Path.of("example.crescent") @@ -19,6 +22,8 @@ internal object Bench { val parserBenchmark = Benchmark("Parser") + val vmBenchmark = Benchmark("VM") + val originalSystemOut = System.out val originalSystemIn = System.`in` @@ -46,7 +51,8 @@ internal object Bench { @JvmStatic fun main(args: Array) { - benchCode("Hello World", TestCode.helloWorlds) + /* + benchCode("Hello Worlds", TestCode.helloWorlds) benchCode("If Statement", TestCode.ifStatement) benchCode("If Input Statement", TestCode.ifInputStatement) benchCode("Calculator", TestCode.calculator) @@ -57,8 +63,11 @@ internal object Bench { benchCode("Enum", TestCode.enum) benchCode("Comments", TestCode.comments) benchCode("Imports", TestCode.imports) + */ + + //benchVM("Hello Worlds", TestCode.helloWorlds) + benchVM("Triangles", TestCode.triangleRecursion) - Benchmark("") } fun benchCode(name: String, code: String) { @@ -80,6 +89,25 @@ internal object Bench { println() } + fun benchVM(name: String, code: String) { + + val tokens = CrescentLexer.invoke(code) + val parsed = CrescentParser.invoke(Path("example.moo"), tokens) + val result = CrescentToPTIR.craft(parsed) + + vmBenchmark.bench("Moo:$name") { + collectSystemOut { + CrescentToPTIR.execute("static.main", result) + } + } + + vmBenchmark.bench("Kat:$name") { + collectSystemOut { + CrescentVM(listOf(parsed), parsed) + } + } + } + class Benchmark(val name: String) {