Skip to content

Commit

Permalink
Simplify UCI option parsing
Browse files Browse the repository at this point in the history
Replace repeated code with a dictionary.
  • Loading branch information
MarkZH committed Sep 13, 2023
1 parent 9eccfb9 commit 4cb2b9a
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions chess/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ def __repr__(self) -> str:

UCI_OPTION_REGEX = re.compile(r"\s*(name|type|default|min|max|var)\s*")


class UciProtocol(Protocol):
"""
An implementation of the
Expand Down Expand Up @@ -1316,46 +1317,31 @@ def line_received(self, engine: UciProtocol, line: str) -> None:
self._id(engine, remaining)

def _option(self, engine: UciProtocol, arg: str) -> None:
name = ""
type = ""
default = ""
min = None
max = None
current_parameter = None
option_parts: dict[str, str] = {k: "" for k in ["name", "type", "default", "min", "max"]}
var = []

def parse_min_max_value(token: str, which: Literal["min", "max"]) -> Optional[int]:
for token in UCI_OPTION_REGEX.split(arg):
if token == "var" or (token in option_parts and not option_parts[token]):
current_parameter = token
elif current_parameter == "var":
var.append(token)
elif current_parameter:
option_parts[current_parameter] = token

def parse_min_max_value(option_parts: dict[str, str], which: Literal["min", "max"]) -> Optional[int]:
try:
return int(token)
number = option_parts[which]
return int(number) if number else None
except ValueError:
LOGGER.exception(f"Exception parsing option {which}")
return None

current_parameter = None
for token in UCI_OPTION_REGEX.split(arg):
if token == "name" and not name:
current_parameter = "name"
elif token == "type" and not type:
current_parameter = "type"
elif token == "default" and not default:
current_parameter = "default"
elif token == "min" and min is None:
current_parameter = "min"
elif token == "max" and max is None:
current_parameter = "max"
elif token == "var":
current_parameter = "var"
elif current_parameter == "name":
name = token
elif current_parameter == "type":
type = token
elif current_parameter == "default":
default = token
elif current_parameter == "var":
var.append(token)
elif current_parameter == "min":
min = parse_min_max_value(token, "min")
elif current_parameter == "max":
max = parse_min_max_value(token, "max")
name = option_parts["name"]
type = option_parts["type"]
default = option_parts["default"]
min = parse_min_max_value(option_parts, "min")
max = parse_min_max_value(option_parts, "max")

without_default = Option(name, type, None, min, max, var)
option = Option(without_default.name, without_default.type, without_default.parse(default), min, max, var)
Expand Down

0 comments on commit 4cb2b9a

Please sign in to comment.