Skip to content

Commit

Permalink
Rounding :O
Browse files Browse the repository at this point in the history
  • Loading branch information
camdenorrb committed Aug 26, 2021
1 parent cd3a3e1 commit 0ac335d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/main/kotlin/me/camdenorrb/crescentvm/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ object Main {

val code =
"""
fun main {
println("Me" + "ow")
}
fun main() {
println(" /\")
println(" /__\")
println(" /\ /\")
println(" /__\/__\")
println(" /\ /\")
println(" /__\ /__\")
println(" /\ /\ /\ /\")
println("/__\/__\/__\/__\")
}
"""
/*
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,8 @@ object CrescentParser {

arguments += readExpression(tokenIterator)

if (tokenIterator.peekNext() != CrescentToken.Parenthesis.CLOSE) {
println(arguments)
if (tokenIterator.peekNext().also { println(it) } != CrescentToken.Parenthesis.CLOSE) {
checkEquals(CrescentToken.Operator.COMMA, tokenIterator.next())
}
}
Expand Down
27 changes: 26 additions & 1 deletion src/main/kotlin/me/camdenorrb/crescentvm/vm/CrescentVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import me.camdenorrb.crescentvm.vm.CrescentAST.Node.Primitive
import me.camdenorrb.crescentvm.vm.CrescentAST.Node.Type
import java.util.*
import kotlin.math.pow
import kotlin.math.round
import kotlin.math.sqrt

// TODO: Add a way to add external functions
Expand Down Expand Up @@ -120,6 +121,25 @@ class CrescentVM(val files: List<Node.File>, val mainFile: Node.File) {
}
}

is Node.Statement.For -> {

val forContext = context.copy()

val ranges = node.ranges.map {
(it.start as Primitive.Number).data.toInt()..(it.end as Primitive.Number).data.toInt()
}

node.identifiers.forEachIndexed { index, identifier ->
forContext.variableValues[identifier.name] = Primitive.Number((ranges.getOrNull(index) ?: ranges[0]).first)
}

/*
while ((runNode(node.predicate, context) as Primitive.Boolean).data) {
runBlock(node.block, context)
}
*/
}

is Node.Variable.Basic -> {
context.variableValues[node.name] = runNode(node.value, context)
}
Expand Down Expand Up @@ -395,6 +415,11 @@ class CrescentVM(val files: List<Node.File>, val mainFile: Node.File) {
return Primitive.Number(sqrt((runNode(node.arguments[0], context) as Primitive.Number).data.toDouble()))
}

"round" -> {
checkEquals(1, node.arguments.size)
return Primitive.Number(round((runNode(node.arguments[0], context) as Primitive.Number).data.toDouble()))
}

"print" -> {
checkEquals(1, node.arguments.size)
print(runNode(node.arguments[0], context).asString())
Expand Down Expand Up @@ -501,7 +526,7 @@ class CrescentVM(val files: List<Node.File>, val mainFile: Node.File) {
* @constructor
*/
data class BlockContext(
val holder: Node,
val self: Node,
val parameters: MutableMap<String, Node>,
//val variables: MutableMap<String, Node.Variable> = mutableMapOf(),
val variableValues: MutableMap<String, Node> = mutableMapOf(),
Expand Down
29 changes: 29 additions & 0 deletions src/test/kotlin/me/camdenorrb/crescentvm/manual/Bench.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.camdenorrb.crescentvm.manual
import me.camdenorrb.crescentvm.data.TestCode
import me.camdenorrb.crescentvm.lexers.CrescentLexer
import me.camdenorrb.crescentvm.parsers.CrescentParser
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.file.Path
import kotlin.system.measureNanoTime

Expand All @@ -16,9 +19,33 @@ internal object Bench {

val parserBenchmark = Benchmark("Parser")

val originalSystemOut = System.out

val originalSystemIn = System.`in`


private inline fun collectSystemOut(block: () -> Unit): String {

val byteArrayOutputStream = ByteArrayOutputStream()
val printStream = PrintStream(byteArrayOutputStream)

System.setOut(printStream)
block()
System.setOut(originalSystemOut)

return byteArrayOutputStream.toString()
}

private inline fun fakeUserInput(input: String, block: () -> Unit) {
System.setIn(ByteArrayInputStream(input.toByteArray()))
block()
System.setIn(originalSystemIn)
}


@JvmStatic
fun main(args: Array<String>) {

benchCode("Hello World", TestCode.helloWorlds)
benchCode("If Statement", TestCode.ifStatement)
benchCode("If Input Statement", TestCode.ifInputStatement)
Expand All @@ -30,6 +57,8 @@ internal object Bench {
benchCode("Enum", TestCode.enum)
benchCode("Comments", TestCode.comments)
benchCode("Imports", TestCode.imports)

Benchmark("")
}

fun benchCode(name: String, code: String) {
Expand Down

0 comments on commit 0ac335d

Please sign in to comment.