Make choice type available in dish, make tostring idempotent

This commit is contained in:
Midgard 2020-01-26 02:09:53 +01:00
parent 22aa5a0fb0
commit 996444e1b0
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 15 additions and 7 deletions

View file

@ -12,6 +12,11 @@ def _format_tags(tags):
) )
def _format_type_and_choice(type_and_choice):
type_, choice = type_and_choice
return "{} {}".format(type_, choice)
class Option: class Option:
def __init__(self, id_, *, name, description, price, tags): def __init__(self, id_, *, name, description, price, tags):
self.id = id_ self.id = id_
@ -53,15 +58,15 @@ class Dish:
self.price = price self.price = price
self.tags = tags self.tags = tags
self.choices: List[Choice] = choices self.choices: List[(str, Choice)] = choices
def __str__(self): def __str__(self):
return "{0.id}: {0.name}{1}{2}{3}\n\t{4}".format( return "base {0.id}: {0.name}{1}{2}{3}\n\t{4}".format(
self, self,
" -- {}".format(self.description) if self.description else "", " -- {}".format(self.description) if self.description else "",
_format_tags(self.tags), _format_tags(self.tags),
" {}".format(self.price) if self.price else "", " {}".format(self.price) if self.price else "",
"\n\t".join(map(str, self.choices)) "\n\t".join(map(_format_type_and_choice, self.choices))
) )

View file

@ -23,9 +23,8 @@ class HldsSemanticActions:
dishes = filter_instance(Dish, ast["items_"]) dishes = filter_instance(Dish, ast["items_"])
for dish in dishes: for dish in dishes:
for i, choice in enumerate(dish.choices): for i, choice in enumerate(dish.choices):
if not isinstance(choice, Choice): if not isinstance(choice[1], Choice):
assert "id" in choice dish.choices[i] = (dish.choices[i][0], choices[choice[1]])
dish.choices[i] = choices[choice["id"]]
return Location( return Location(
ast["id"], ast["id"],
@ -53,7 +52,11 @@ class HldsSemanticActions:
) )
def indent_choice_block(self, ast): def indent_choice_block(self, ast):
return self.choice_block(ast) if ast["kind"] == "declaration" else ast return (
(ast["type"], self.choice_block(ast))
if ast["kind"] == "declaration" else
(ast["type"], ast["id"])
)
def indent_choice_entry(self, ast): def indent_choice_entry(self, ast):
return Option( return Option(