Skip to content

Commit

Permalink
feat: make Data.cast lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWii committed Aug 10, 2024
1 parent ae424a1 commit 4d4e739
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
7 changes: 4 additions & 3 deletions bolt_expressions/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .sources import (
DataSource,
ScoreSource,
resolve,
)


Expand Down Expand Up @@ -162,7 +163,7 @@ def cast(self, value: Any, nbt_type: NbtType | str):
if isinstance(nbt_type, str):
nbt_type = literal_types[nbt_type]

source = self.dummy(nbt_type)
source.__rebind__(value)
result = self.dummy(nbt_type)
resolve(self.expr, value, result, nbt_type, lazy=True)

return source
return result
4 changes: 1 addition & 3 deletions bolt_expressions/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def resolve(
cast: NbtType | None = None,
lazy: bool = False,
):
value_node = (
value if isinstance(value, ExpressionNode) else Literal(value=value, ctx=expr)
)
value_node = convert_node(value, expr)

result_type = None
in_place = False
Expand Down
8 changes: 6 additions & 2 deletions examples/operation_nbt_type/src/main.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ temp.value = Data.cast(obj["$a"], "byte")

temp.pos[0] = Data.cast(obj["$x"] / 100, "double")

n = Data.cast(obj["$n"], "int")
n = Data.cast(obj["$n"], "int").evaluate()

temp.n = n
temp.m = Data.cast(n, "float")
Expand All @@ -19,7 +19,7 @@ temp.m = Data.cast(n, "float")
temp.x = Data.cast((temp.x*100 + 1) / 100, "double")
temp.num = Data.cast(temp.num*100 + 1, "double") / 100

s = Data.cast(obj["$val"], "short")
s = Data.cast(obj["$val"], "short").evaluate()
temp.a = s * 100
temp.a = s / 10

Expand Down Expand Up @@ -80,3 +80,7 @@ x[Any] = temp.arbitrary

say casts to a double
x = temp.arbitrary


temp.elements[0] = Data.cast(obj["$value"] / 10, Double)
temp.elements[0][Double] = obj["$value"] / 10
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ scoreboard players remove @s abc.main 20
scoreboard players operation @s abc.main *= $30 bolt.expr.const
scoreboard players operation @s abc.main /= $40 bolt.expr.const
scoreboard players operation @s abc.main %= $50 bolt.expr.const
scoreboard players operation $a abc.main = $a abc.main
scoreboard players operation $a abc.main *= $-1 bolt.expr.const
scoreboard players operation $b abc.main = $b abc.main
scoreboard players operation $b abc.main /= $-1 bolt.expr.const
scoreboard players operation $c abc.main /= $2 bolt.expr.const
scoreboard players operation $d abc.main /= $10 bolt.expr.const
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
execute store result storage bolt.expr:temp 2384k242hd495_0 byte 1 run scoreboard players get $a obj
data modify storage demo value set from storage bolt.expr:temp 2384k242hd495_0
execute store result storage bolt.expr:temp 2384k242hd495_2 double 0.01 run scoreboard players get $x obj
data modify storage demo pos[0] set from storage bolt.expr:temp 2384k242hd495_2
execute store result storage bolt.expr:temp 2384k242hd495_4 int 1 run scoreboard players get $n obj
data modify storage demo n set from storage bolt.expr:temp 2384k242hd495_4
execute store result storage bolt.expr:temp 2384k242hd495_5 float 1 run data get storage bolt.expr:temp 2384k242hd495_4 1
data modify storage demo m set from storage bolt.expr:temp 2384k242hd495_5
execute store result storage demo value byte 1 run scoreboard players get $a obj
execute store result storage demo pos[0] double 0.01 run scoreboard players get $x obj
execute store result storage bolt.expr:temp 2384k242hd495_5 int 1 run scoreboard players get $n obj
data modify storage demo n set from storage bolt.expr:temp 2384k242hd495_5
execute store result storage demo m float 1 run data get storage bolt.expr:temp 2384k242hd495_5 1
execute store result score $i0 bolt.expr.temp run data get storage demo x 100
execute store result storage bolt.expr:temp 2384k242hd495_12 double 0.01 run scoreboard players add $i0 bolt.expr.temp 1
data modify storage demo x set from storage bolt.expr:temp 2384k242hd495_12
execute store result storage bolt.expr:temp i0 double 0.01 run scoreboard players add $i0 bolt.expr.temp 1
data modify storage demo x set from storage bolt.expr:temp i0
execute store result score $i0 bolt.expr.temp run data get storage demo num 100
execute store result storage bolt.expr:temp 2384k242hd495_19 double 1 run scoreboard players add $i0 bolt.expr.temp 1
execute store result storage demo num double 0.01 run data get storage bolt.expr:temp 2384k242hd495_19 1
execute store result storage bolt.expr:temp 2384k242hd495_24 short 1 run scoreboard players get $val obj
execute store result storage demo a int 100 run data get storage bolt.expr:temp 2384k242hd495_24 1
execute store result storage demo a double 0.1 run data get storage bolt.expr:temp 2384k242hd495_24 1
execute store result storage bolt.expr:temp 2384k242hd495_32 short 1 run scoreboard players get $val obj
execute store result storage demo a int 100 run data get storage bolt.expr:temp 2384k242hd495_32 1
execute store result storage demo a double 0.1 run data get storage bolt.expr:temp 2384k242hd495_32 1
scoreboard players operation $i0 bolt.expr.temp = $a obj
execute store result storage demo a int 1 run scoreboard players add $i0 bolt.expr.temp 1
scoreboard players operation $i0 bolt.expr.temp = $b obj
execute store result storage bolt.expr:temp 2384k242hd495_32 double 1 run scoreboard players add $i0 bolt.expr.temp 2
execute store result storage demo a double 0.1 run data get storage bolt.expr:temp 2384k242hd495_32 1
say nicer
execute store result storage bolt.expr:temp 2384k242hd495_37 double 0.01 run scoreboard players get $foo obj
data modify storage demo foo set from storage bolt.expr:temp 2384k242hd495_37
execute store result storage demo foo double 0.01 run scoreboard players get $foo obj
data modify storage demo bar set from storage demo foo
execute store result storage bolt.expr:temp 2384k242hd495_39 double 1 run data get storage demo foo 1
data modify storage demo bar set from storage bolt.expr:temp 2384k242hd495_39
execute store result storage demo bar double 1 run data get storage demo foo 1
say temp.c will be scaled and converted to a double (preserve precision)
execute store result storage demo bar double 0.1 run data get storage demo c 1
say force temp.c to be truncated
execute store result storage bolt.expr:temp 2384k242hd495_43 int 0.1 run data get storage demo c 1
data modify storage demo bar set from storage bolt.expr:temp 2384k242hd495_43
execute store result storage demo bar int 0.1 run data get storage demo c 1
say score is casted to int
execute store result storage demo value int 1 run scoreboard players get @s obj
say score is casted to double
Expand All @@ -43,8 +33,8 @@ say casting to byte before appending to list
execute store result storage demo value byte 1 run scoreboard players get $value obj
data modify storage demo list append from storage demo value
say or use Data.cast
execute store result storage bolt.expr:temp 2384k242hd495_60 byte 1 run scoreboard players get $value obj
data modify storage demo list append from storage bolt.expr:temp 2384k242hd495_60
execute store result storage bolt.expr:temp i0 byte 1 run scoreboard players get $value obj
data modify storage demo list append from storage bolt.expr:temp i0
say casts y to double
execute store result storage demo x double 1 run data get storage demo y 1
say only copies the value
Expand All @@ -57,3 +47,5 @@ say no cast (type of x is temporarely overwritten)
data modify storage demo x set from storage demo arbitrary
say casts to a double
execute store result storage demo x double 1 run data get storage demo arbitrary 1
execute store result storage demo elements[0] double 0.1 run scoreboard players get $value obj
execute store result storage demo elements[0] double 0.1 run scoreboard players get $value obj

0 comments on commit 4d4e739

Please sign in to comment.