Skip to content

Commit

Permalink
Merge pull request #162 from d409f19/interpreter
Browse files Browse the repository at this point in the history
Interpreter
  • Loading branch information
NicEastvillage authored May 16, 2019
2 parents 64cbfc3 + ca0e7fc commit 0b3d759
Show file tree
Hide file tree
Showing 11 changed files with 983 additions and 19 deletions.
6 changes: 3 additions & 3 deletions compiler/src/main/kotlin/ast/ast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ class StateDecl(
ctx: SourceContext,
var ident: String,
val multiStateCount: Int,
var red: Short,
var blue: Short,
var green: Short,
var red: Int,
var blue: Int,
var green: Int,
val body: CodeBlock
) : Decl(ctx)

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/main/kotlin/ast/reduce.kt
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ private fun reduceDecl(node: ParseTree): Decl {
/**
* Parses an Integer_literalContext to a Short between 0 and 256 from a string. Used for state-declaration's colors
*/
fun parseColor(intCtx: CellmataParser.Integer_literalContext): Short {
val value = intCtx.text.toShortOrNull()
fun parseColor(intCtx: CellmataParser.Integer_literalContext): Int {
val value = intCtx.text.toIntOrNull()
if (value == null || value < 0 || 255 < value) {
ErrorLogger += CompileError(SourceContext(intCtx), "'${intCtx.text}' is not a valid colour. It must be an integer between 0 and 255.")
}
Expand Down
59 changes: 59 additions & 0 deletions compiler/src/main/kotlin/interpreter/builtins.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dk.aau.cs.d409f19.cellumata.interpreter

import dk.aau.cs.d409f19.cellumata.ast.*
import kotlin.random.Random

/**
* Calls a builtin function by dispatching to the equivalent kotlin function.
*/
fun callBuiltinFunction(funcDecl: BuiltinFunc, arguments: List<Any>): Any {
@Suppress("UNCHECKED_CAST")
return when (funcDecl) {
BuiltinFuncCount -> builtInCount(arguments[0] as StateValue, arguments[1] as List<StateValue>)
BuiltinFuncRandi -> builtInRandi(arguments[0] as Int, arguments[1] as Int)
BuiltinFuncRandf -> builtInRandf(arguments[0] as Float, arguments[1] as Float)
BuiltinFuncAbsi -> builtInAbsi(arguments[0] as Int)
BuiltinFuncAbsf -> builtInAbsf(arguments[0] as Float)
BuiltinFuncFloor -> builtInFloor(arguments[0] as Float)
BuiltinFuncCeil -> builtInCeil(arguments[0] as Float)
BuiltinFuncRoot -> builtInRoot(arguments[0] as Float, arguments[1] as Float)
BuiltinFuncPow -> builtInPow(arguments[0] as Float, arguments[1] as Float)
else -> throw InterpretRuntimeException("No built-in implementation of '${funcDecl.javaClass}'.")
}
}

fun builtInCount(state: StateValue, nei: List<StateValue>): Int {
return nei.count { it == state }
}

fun builtInRandi(min: Int, max: Int): Int {
return Random.nextInt(min, max)
}

fun builtInRandf(min: Float, max: Float): Float {
return Random.nextFloat() * (max - min) + min
}

fun builtInAbsi(value: Int): Int {
return Math.abs(value)
}

fun builtInAbsf(value: Float): Float {
return Math.abs(value)
}

fun builtInFloor(value: Float): Int {
return Math.floor(value.toDouble()).toInt()
}

fun builtInCeil(value: Float): Int {
return Math.ceil(value.toDouble()).toInt()
}

fun builtInRoot(value: Float, root: Float): Float {
return Math.pow(value.toDouble(), 1.0 / root).toFloat()
}

fun builtInPow(value: Float, exp: Float): Float {
return Math.pow(value.toDouble(), exp.toDouble()).toFloat()
}
Loading

0 comments on commit 0b3d759

Please sign in to comment.