Skip to content

Commit

Permalink
wip expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhirkevich committed Jul 1, 2024
1 parent d5cbbeb commit 2f29526
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ internal object LottieOp {
TODO()
}

fun clamp(v : Any, from : Any, to : Any) : Any{
return (v as Number).toFloat().coerceIn(
(from as Number).toFloat(),
(to as Number).toFloat(),
)
fun clamp(v : Any, from : Any, to : Any) : Any {
require(v is Number && from is Number && to is Number) {
"Cant clamp ($v, $from, $to) : not a number"
}

return v.toFloat().coerceIn(from.toFloat(), to.toFloat(),)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,35 @@ internal class OperationParser(private val expr : String) {
var x = parseFactorOp()
while (true) {
x = when {
eat('*') -> {
LottieOp.op(x, parseFactorOp(), LottieOp::mul)
}

eat('/') -> {
LottieOp.op(x, parseFactorOp(), LottieOp::div)
}
eat('*') -> LottieOp.op(x, parseFactorOp(), LottieOp::mul)
eat('/') -> LottieOp.op(x, parseFactorOp(), LottieOp::div)

else -> return x
}
}
}

private fun parseFactorOp(): Operation {
if (eat('+')) {
val factor = parseFactorOp()
return Operation { v, vars, s ->
+(factor(v, vars, s) as Number).toFloat()
return when {
eat('+') -> { // unary +
val factor = parseFactorOp()
Operation { v, vars, s ->
+(factor(v, vars, s) as Number).toFloat()
}
}
}
if (eat('-')) {
val factor = parseFactorOp()
return Operation { v, vars, s ->
-(factor(v, vars, s) as Number).toFloat()
eat('-') -> { // unary -
val factor = parseFactorOp()
Operation { v, vars, s ->
-(factor(v, vars, s) as Number).toFloat()
}
}
}

return when {
eat('(') -> {
val exp = parseExpressionOp()
require(eat(')')) {
"Lottie expression: Missing ')'"
parseExpressionOp().also {
require(eat(')')) {
"Bad expression: Missing ')'"
}
}
exp
}

ch.isDigit() || ch == '.' -> {
Expand All @@ -108,7 +103,7 @@ internal class OperationParser(private val expr : String) {
add(parseExpressionOp())
} while (eat(','))
require(eat(']')) {
"Missing ] in list creation"
"Bad expression: missing ]"
}
}
makeList(arrayArgs)
Expand All @@ -132,7 +127,7 @@ internal class OperationParser(private val expr : String) {
} while (eat(','))

require(eat(')')) {
("Missing ')' after argument to $func")
"Bad expression:Missing ')' after argument to $func"
}
}
// else {
Expand Down Expand Up @@ -220,7 +215,7 @@ internal class OperationParser(private val expr : String) {
}
}

else -> error("Unsupported Lottie expression")
else -> error("Unsupported Lottie expression: $expr")
}
}

Expand Down

0 comments on commit 2f29526

Please sign in to comment.