Skip to content

Commit

Permalink
Implement doomeer#26: set_aside multiple items in a list
Browse files Browse the repository at this point in the history
* state.aside is now a list
* set_aside pushes the current item as the head
* with_aside fails if the list is empty; uses head otherwise
* awaken and recombine consume the head of the list
* beastcraft_split pushes the split item as the head of the list
* swap replaces the current item with the head of the list
  * if there is no current item: removes the head of the list, if not empty
  * if the list is empty: sets the current item to None
  • Loading branch information
haggys22 committed Jun 19, 2022
1 parent 3088650 commit a0ae7ea
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type state =
echo: string -> unit;
debug: string -> unit;
item: Item.t option;
aside: Item.t option;
aside: Item.t list;
imprint: Item.t option;
paid: Amount.t;
gained: Amount.t;
Expand All @@ -79,7 +79,7 @@ let start ~echo ~debug program =
echo;
debug;
item = None;
aside = None;
aside = [];
imprint = None;
paid = Amount.zero;
gained = Amount.zero;
Expand All @@ -96,9 +96,9 @@ let with_item state f =

let with_aside state f =
match state.aside with
| None ->
| [] ->
fail "no item set aside"
| Some item ->
| item :: _ ->
f item

module Q:
Expand Down Expand Up @@ -554,13 +554,33 @@ let apply_currency state (currency: AST.currency) =
|> Item.add_influence (SEC aside_influence)
|> Item.spawn_additional_random_mods
in
{ (return item) with aside = None }
{
(return item) with
aside = match state.aside with
| [] -> [] (* cannot happen *)
| _ :: t -> t
}
| Armour_recombinator ->
{ (return @@ recombine state `armour) with aside = None }
{
(return @@ recombine state `armour) with
aside = match state.aside with
| [] -> [] (* cannot happen *)
| _ :: t -> t
}
| Weapon_recombinator ->
{ (return @@ recombine state `weapon) with aside = None }
{
(return @@ recombine state `weapon) with
aside = match state.aside with
| [] -> [] (* cannot happen *)
| _ :: t -> t
}
| Jewellery_recombinator ->
{ (return @@ recombine state `jewellery) with aside = None }
{
(return @@ recombine state `jewellery) with
aside = match state.aside with
| [] -> [] (* cannot happen *)
| _ :: t -> t
}
| Ember tier ->
with_item state @@ fun item ->
return @@ Item.apply_eldritch_ember (AST.eldritch_tier_of_currency tier) item
Expand Down Expand Up @@ -655,7 +675,7 @@ let apply_currency state (currency: AST.currency) =
);
let item1, item2 = Item.split item in
let state = return item1 in
{ state with aside = Some item2 }
{ state with aside = item2 :: state.aside }
| Beastcraft_imprint ->
with_item state @@ fun item ->
item_must_be_normal_or_magic item;
Expand Down Expand Up @@ -771,10 +791,24 @@ let run_simple_instruction state (instruction: AST.simple_instruction) =
| Set_aside ->
state.debug "set aside";
with_item state @@ fun item ->
goto_next { state with aside = Some item; item = None }
goto_next {
state with
aside = item :: state.aside;
item = None
}
| Swap ->
state.debug "swap";
goto_next { state with aside = state.item; item = state.aside }
let aside = match state.aside, state.item with
| [], None -> []
| [], Some item -> [item]
| h :: t, None -> t
| h :: t, Some item -> item :: t
in
let item = match state.aside with
| [] -> None
| h :: t -> Some h
in
goto_next { state with aside; item }
| Use_imprint ->
state.debug "use_imprint";
let imprint =
Expand Down

0 comments on commit a0ae7ea

Please sign in to comment.