Skip to content

Commit

Permalink
Negative numbers!
Browse files Browse the repository at this point in the history
  • Loading branch information
camdenorrb committed Aug 26, 2021
1 parent a2e3128 commit 39f4c97
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/me/camdenorrb/crescentvm/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Main {
val code =
"""
fun main() {
println(round(2.5))
println(round(-2.5))
}
"""
/*
Expand Down
27 changes: 18 additions & 9 deletions src/main/kotlin/me/camdenorrb/crescentvm/lexers/CrescentLexer.kt
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 {
Expand All @@ -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() -> {
Expand Down Expand Up @@ -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()
}
}
}

}
34 changes: 31 additions & 3 deletions src/test/kotlin/me/camdenorrb/crescentvm/manual/Bench.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ 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")

val lexerBenchmark = Benchmark("Lexer")

val parserBenchmark = Benchmark("Parser")

val vmBenchmark = Benchmark("VM")

val originalSystemOut = System.out

val originalSystemIn = System.`in`
Expand Down Expand Up @@ -46,7 +51,8 @@ internal object Bench {
@JvmStatic
fun main(args: Array<String>) {

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)
Expand All @@ -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) {
Expand All @@ -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) {

Expand Down

0 comments on commit 39f4c97

Please sign in to comment.