Skip to content

Commit

Permalink
Add to strings in the vm! :D
Browse files Browse the repository at this point in the history
  • Loading branch information
camdenorrb committed Aug 26, 2021
1 parent abc813a commit 5bbc84e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 1 addition & 7 deletions src/main/kotlin/me/camdenorrb/crescentvm/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ object Main {
val code =
"""
fun main {
var thing = 1
while (thing <= 10) {
println(thing)
thing = thing + 1
}
println("Me" + "ow")
}
"""
/*
Expand Down
14 changes: 11 additions & 3 deletions src/main/kotlin/me/camdenorrb/crescentvm/vm/CrescentVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,18 @@ class CrescentVM(val files: List<Node.File>, val mainFile: Node.File) {
// TODO: Override operators for these in Primitive.Number
CrescentToken.Operator.ADD -> {

val pop1 = (runNode(stack.pop(), context) as Primitive.Number).data.toDouble()
val pop2 = (runNode(stack.pop(), context) as Primitive.Number).data.toDouble()

stack.push(Primitive.Number(pop2 + pop1))
val pop1 = runNode(stack.pop(), context)
val pop2 = runNode(stack.pop(), context)

stack.push(
if (pop2 is Primitive.String || pop1 is Primitive.String) {
Primitive.String(pop2.asString() + pop1.asString())
}
else {
Primitive.Number((pop2 as Primitive.Number).data.toDouble() + (pop1 as Primitive.Number).data.toDouble())
}
)
}
CrescentToken.Operator.SUB -> {

Expand Down

0 comments on commit 5bbc84e

Please sign in to comment.