2017-09-23 17:13:56 -07:00
|
|
|
package org.nwapw.abacus.plugin.standard.operator
|
2017-09-23 17:11:05 -07:00
|
|
|
|
2017-11-23 02:21:06 -08:00
|
|
|
import org.nwapw.abacus.context.PluginEvaluationContext
|
2017-09-23 17:11:05 -07:00
|
|
|
import org.nwapw.abacus.function.OperatorAssociativity
|
|
|
|
import org.nwapw.abacus.function.OperatorType
|
2017-09-23 23:43:08 -07:00
|
|
|
import org.nwapw.abacus.function.interfaces.TreeValueOperator
|
2017-09-23 17:11:05 -07:00
|
|
|
import org.nwapw.abacus.number.NumberInterface
|
2017-09-23 23:43:08 -07:00
|
|
|
import org.nwapw.abacus.tree.nodes.TreeNode
|
|
|
|
import org.nwapw.abacus.tree.nodes.VariableNode
|
2017-09-23 17:11:05 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The definition operator.
|
|
|
|
*
|
|
|
|
* This is a standard operator that creates a definition - something that doesn't capture variable values
|
|
|
|
* when it's created, but rather the variables themselves, and changes when the variables it uses change.
|
|
|
|
*/
|
|
|
|
class OperatorDefine: TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) {
|
|
|
|
|
2017-11-23 02:21:06 -08:00
|
|
|
override fun matchesParams(context: PluginEvaluationContext, params: Array<out TreeNode>) =
|
2017-09-23 17:11:05 -07:00
|
|
|
params.size == 2 && params[0] is VariableNode
|
|
|
|
|
2017-11-23 02:21:06 -08:00
|
|
|
override fun applyInternal(context: PluginEvaluationContext, params: Array<out TreeNode>): NumberInterface {
|
2017-09-23 17:11:05 -07:00
|
|
|
val assignTo = (params[0] as VariableNode).variable
|
|
|
|
context.setDefinition(assignTo, params[1])
|
2017-11-14 23:03:12 -08:00
|
|
|
return params[1].reduce(context)
|
2017-09-23 17:11:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|