Skip to content

Commit

Permalink
Raise an error when invalid condition type is used
Browse files Browse the repository at this point in the history
  • Loading branch information
WingedSeal committed Nov 1, 2022
1 parent c7b1e71 commit 2118aea
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 64 deletions.
9 changes: 8 additions & 1 deletion src/jmc/command/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Union

from ..tokenizer import TokenType, Tokenizer, Token
from ..exception import JMCSyntaxException
from ..exception import JMCSyntaxException, JMCValueError
from ..datapack import DataPack
from .utils import find_scoreboard_player_type, PlayerType
from .jmc_function import JMCFunction, FuncType
Expand Down Expand Up @@ -169,6 +169,13 @@ def custom_condition(
*matched_function(tokens[1], datapack, tokenizer).call_bool())
# End
conditions: list[str] = []
if tokens[0].string not in {"block", "blocks",
"data", "entity", "predicate", "score"}:
raise JMCValueError(
f"Unrecoginized condition '{tokens[0].string}'",
tokens[0],
tokenizer,
suggestion="Consider using 'block' or 'blocks' or 'data' or 'entity' or 'predicate' or 'score'.")
for token in tokens:
if token.token_type == TokenType.PAREN_SQUARE:
if not conditions:
Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import jmc

VERSION = 'v1.2.5-alpha'
VERSION = 'v1.2.5-alpha.1'

CWD = Path(os.getcwd())
LOG_PATH = CWD / 'log'
Expand Down
26 changes: 13 additions & 13 deletions src/tests/integration/test_flow_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestIfElse(unittest.TestCase):

def test_if(self):
pack = JMCPack().set_jmc_file("""
if (condition) {
if (entity condition) {
say "Hello World";
}
""").build()
Expand All @@ -27,13 +27,13 @@ def test_if(self):
> VIRTUAL/data/TEST/functions/__load__.mcfunction
scoreboard objectives add __variable__ dummy
scoreboard objectives add __int__ dummy
execute if condition run say Hello World
execute if entity condition run say Hello World
""")
)

def test_if_else(self):
pack = JMCPack().set_jmc_file("""
if (condition) {
if (entity condition) {
say "TRUE";
} else {
say "FALSE";
Expand All @@ -53,7 +53,7 @@ def test_if_else(self):
scoreboard objectives add __variable__ dummy
scoreboard objectives add __int__ dummy
scoreboard players set __if_else__ __variable__ 0
execute if condition run function TEST:__private__/if_else/0
execute if entity condition run function TEST:__private__/if_else/0
execute if score __if_else__ __variable__ matches 0 run function TEST:__private__/if_else/1
> VIRTUAL/data/TEST/functions/__private__/if_else/0.mcfunction
say TRUE
Expand All @@ -65,9 +65,9 @@ def test_if_else(self):

def test_if_elif(self):
pack = JMCPack().set_jmc_file("""
if (condition) {
if (entity condition) {
say "CONDITION1";
} else if (condition2) {
} else if (entity condition2) {
say "CONDITION2";
}
""").build()
Expand All @@ -85,13 +85,13 @@ def test_if_elif(self):
scoreboard objectives add __variable__ dummy
scoreboard objectives add __int__ dummy
scoreboard players set __if_else__ __variable__ 0
execute if condition run function TEST:__private__/if_else/0
execute if entity condition run function TEST:__private__/if_else/0
execute if score __if_else__ __variable__ matches 0 run function TEST:__private__/if_else/1
> VIRTUAL/data/TEST/functions/__private__/if_else/0.mcfunction
say CONDITION1
scoreboard players set __if_else__ __variable__ 1
> VIRTUAL/data/TEST/functions/__private__/if_else/1.mcfunction
execute if condition2 run function TEST:__private__/if_else/2
execute if entity condition2 run function TEST:__private__/if_else/2
> VIRTUAL/data/TEST/functions/__private__/if_else/2.mcfunction
say CONDITION2
scoreboard players set __if_else__ __variable__ 1
Expand All @@ -103,7 +103,7 @@ class TestDoWhile(unittest.TestCase):

def test_while(self):
pack = JMCPack().set_jmc_file("""
while (condition) {
while (entity condition) {
say "Hello World";
}
""").build()
Expand All @@ -120,18 +120,18 @@ def test_while(self):
> VIRTUAL/data/TEST/functions/__load__.mcfunction
scoreboard objectives add __variable__ dummy
scoreboard objectives add __int__ dummy
execute if condition run function TEST:__private__/while_loop/0
execute if entity condition run function TEST:__private__/while_loop/0
> VIRTUAL/data/TEST/functions/__private__/while_loop/0.mcfunction
say Hello World
execute if condition run function TEST:__private__/while_loop/0
execute if entity condition run function TEST:__private__/while_loop/0
""")
)

def test_do_while(self):
pack = JMCPack().set_jmc_file("""
do {
say "Hello World";
} while (condition);
} while (entity condition);
""").build()

self.assertDictEqual(
Expand All @@ -149,7 +149,7 @@ def test_do_while(self):
function TEST:__private__/while_loop/0
> VIRTUAL/data/TEST/functions/__private__/while_loop/0.mcfunction
say Hello World
execute if condition run function TEST:__private__/while_loop/0
execute if entity condition run function TEST:__private__/while_loop/0
""")
)

Expand Down
95 changes: 46 additions & 49 deletions src/tests/integration/test_jmc_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,55 +66,52 @@ def test_MathSqrt(self):
""").build()

def test_MathRandom(self):
# pack = JMCPack().set_jmc_file("""
# $x = Math.random();
# $y = Math.random(min=5, max=10);
# $z = Math.random(max=10);
# """).build()

# self.assertDictEqual(
# pack.built,
# string_to_tree_dict("""
# > VIRTUAL/data/minecraft/tags/functions/load.json
# {
# "values": [
# "TEST:__load__"
# ]
# }
# > VIRTUAL/data/TEST/functions/__load__.mcfunction
# scoreboard objectives add __variable__ dummy
# scoreboard objectives add __int__ dummy
# scoreboard players set 10 __int__ 10
# scoreboard players set 6 __int__ 6
# scoreboard players set -1 __int__ -1
# scoreboard players set 2147483647 __int__ 2147483647
# execute unless score __math__.seed __variable__ matches -2147483648..2147483647 run function TEST:__private__/math_random/setup
# function TEST:__private__/math_random/main
# scoreboard players operation $x __variable__ = __math__.seed __variable__
# scoreboard players operation $x __variable__ %= 2147483647 __int__
# scoreboard players add $x __variable__ 1
# function TEST:__private__/math_random/main
# scoreboard players operation $y __variable__ = __math__.seed __variable__
# scoreboard players operation $y __variable__ %= 6 __int__
# scoreboard players add $y __variable__ 5
# function TEST:__private__/math_random/main
# scoreboard players operation $z __variable__ = __math__.seed __variable__
# scoreboard players operation $z __variable__ %= 10 __int__
# scoreboard players add $z __variable__ 1
# > VIRTUAL/data/TEST/functions/__private__/math_random/setup.mcfunction
# summon minecraft:area_effect_cloud ~ ~ ~ {Tags:["__private__.math_random"]}
# execute store result score __math__.seed __variable__ run data get entity @e[limit=1,type=area_effect_cloud,tag=__private__.math_random] UUID[0] 1
# execute store result score __math__.rng.a __variable__ run data get entity @e[limit=1,type=area_effect_cloud,tag=__private__.math_random] UUID[1] 1
# execute if score __math__.rng.a __variable__ matches ..0 run scoreboard players operation __math__.rng.a __variable__ *= -1 __int__
# execute store result score __math__.rng.c __variable__ run data get entity @e[limit=1,type=area_effect_cloud,tag=__private__.math_random] UUID[2] 1
# execute if score __math__.rng.c __variable__ matches ..0 run scoreboard players operation __math__.rng.c __variable__ *= -1 __int__
# kill @e[type=area_effect_cloud,tag=__private__.math_random]
# > VIRTUAL/data/TEST/functions/__private__/math_random/main.mcfunction
# execute if score __math__.seed __variable__ matches ..0 run scoreboard players add __math__.seed __variable__ 2147483647
# scoreboard players operation __math__.seed __variable__ *= __math__.rng.a __variable__
# scoreboard players operation __math__.seed __variable__ += __math__.rng.c __variable__
# """)
# )
pack = JMCPack().set_jmc_file("""
$x = Math.random();
$y = Math.random(min=5, max=10);
$z = Math.random(max=10);
""").build()

self.assertDictEqual(
pack.built,
string_to_tree_dict("""
> VIRTUAL/data/minecraft/tags/functions/load.json
{
"values": [
"TEST:__load__"
]
}
> VIRTUAL/data/TEST/functions/__load__.mcfunction
scoreboard objectives add __variable__ dummy
scoreboard objectives add __int__ dummy
scoreboard players set 10 __int__ 10
scoreboard players set 6 __int__ 6
scoreboard players set 2147483647 __int__ 2147483647
execute unless score __math__.seed __variable__ matches -2147483648..2147483647 run function TEST:__private__/math_random/setup
function TEST:__private__/math_random/main
scoreboard players operation $x __variable__ = __math__.seed __variable__
scoreboard players operation $x __variable__ %= 2147483647 __int__
scoreboard players add $x __variable__ 1
function TEST:__private__/math_random/main
scoreboard players operation $y __variable__ = __math__.seed __variable__
scoreboard players operation $y __variable__ %= 6 __int__
scoreboard players add $y __variable__ 5
function TEST:__private__/math_random/main
scoreboard players operation $z __variable__ = __math__.seed __variable__
scoreboard players operation $z __variable__ %= 10 __int__
scoreboard players add $z __variable__ 1
> VIRTUAL/data/TEST/functions/__private__/math_random/setup.mcfunction
summon minecraft:area_effect_cloud ~ ~ ~ {Tags:["__private__.math_random"]}
execute store result score __math__.seed __variable__ run data get entity @e[limit=1,type=area_effect_cloud,tag=__private__.math_random] UUID[0] 1
kill @e[type=area_effect_cloud,tag=__private__.math_random]
scoreboard players set __math__.rng.a __variable__ 656891
scoreboard players set __math__.rng.c __variable__ 875773
> VIRTUAL/data/TEST/functions/__private__/math_random/main.mcfunction
execute if score __math__.seed __variable__ matches ..0 run scoreboard players add __math__.seed __variable__ 2147483647
scoreboard players operation __math__.seed __variable__ *= __math__.rng.a __variable__
scoreboard players operation __math__.seed __variable__ += __math__.rng.c __variable__
""")
)

with self.assertRaises(JMCValueError):
JMCPack().set_jmc_file("""
Expand Down

0 comments on commit 2118aea

Please sign in to comment.