-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from d409f19/interpreter
Interpreter
- Loading branch information
Showing
11 changed files
with
983 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.