Skip to content

Commit

Permalink
Simplified the syntax of defdataclass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Dec 10, 2023
1 parent fb994f0 commit f5829ea
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
10 changes: 5 additions & 5 deletions simalq/commands.hy
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
(defdataclass Walk [Action]
"Try to walk one step in the given direction, or attack something
that's in the way with your sword."
[direction]
:fields [direction]
:frozen T)
(defdataclass Shoot [Action]
"Fire an arrow in the given direction."
[direction]
:fields [direction]
:frozen T)
(defdataclass UseItem [Action]
"Apply an item from your inventory."
[item-ix target-x target-y]
:fields [item-ix target-x target-y]
:frozen T)

(defdataclass Help [Command]
Expand All @@ -60,11 +60,11 @@
(defdataclass ShiftHistory [Command]
"{details}"
; Undo or redo.
[steps]
:fields [steps]
:frozen T)
(defdataclass SaveGame [Command]
"Save the game, {details}."
[kind]
:fields [kind]
:frozen T)
(defdataclass LoadGame [Command]
"Load a saved game.")
Expand Down
2 changes: 1 addition & 1 deletion simalq/display.hy
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(defdataclass ColorChar []
"A character with a foreground and background color. Colors can
be `None` or a symbol (a key of `color.by-name`)."
[char fg bg])
:fields [char fg bg])

(defn colorstr [s [fg None] [bg None]]
"Convert a string to a colorstr, i.e., a list of `ColorChar`s."
Expand Down
6 changes: 3 additions & 3 deletions simalq/geometry.hy
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(defdataclass Map []
"A level layout."

[wrap-x wrap-y data width height]
:fields [wrap-x wrap-y data width height]
; `wrap-x` and `wrap-y` are Booleans.
; `data` is a tuple of tuples representing the squares of the map.
; Each tile is itself a list representing a stack of tiles on
Expand All @@ -33,7 +33,7 @@


(defdataclass Direction []
[name x y]
:fields [name x y]
:frozen T)
((fn []
; Define the direction constants (`Direction.N`, `.NE`, etc.)
Expand Down Expand Up @@ -72,7 +72,7 @@
(defdataclass Pos []
"A position; a point on a map."

[map x y]
:fields [map x y]
:frozen T

(defmeth __init__ [map x y]
Expand Down
24 changes: 12 additions & 12 deletions simalq/macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@


(defmacro defdataclass [class-name superclasses #* rest]
(setv rest (list rest) docstring [] fields [] field-defaults [] kwargs [])
(setv rest (list rest) docstring [])
(when (and rest (isinstance (get rest 0) hy.models.String))
(.append docstring (.pop rest 0)))
(when rest (cond
(= (get rest 0) ':field-defaults)
(do
(.pop rest 0)
(setv field-defaults (.pop rest 0)))
(isinstance (get rest 0) hy.models.List)
(setv fields (.pop rest 0))
True
(raise ValueError)))
(setv kwargs [] fields [] field-defaults [])
(while (and rest (isinstance (get rest 0) hy.models.Keyword))
(.append kwargs (.pop rest 0))
(.append kwargs (.pop rest 0)))
(setv [k v #* rest] rest)
(cond
(= k ':fields)
(setv fields v)
(= k ':field-defaults)
(setv field-defaults v)
True
(.extend kwargs [k v])))
(assert (not (and fields field-defaults)))
; `field-defaults` defines `fields` implicitly.
(unless (or fields field-defaults)
(.extend kwargs '[:frozen True]))
`(defclass
Expand Down
5 changes: 3 additions & 2 deletions simalq/quest.hy
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

(defdataclass Quest []
"A scenario or campaign; a sequence of levels to play."
[name title authors starting-hp levels]
:fields [name title authors starting-hp levels]
:frozen T)

(defn start-quest [quest [rules None]]
Expand All @@ -32,7 +32,8 @@

(defdataclass Level []
"A map and associated data for playing it."
[n title player-start next-level
:fields [
n title player-start next-level
poison-intensity time-limit exit-speed moving-exit-start
map]
; Poison intensity is a fraction.Fraction, the amount of poison to
Expand Down

0 comments on commit f5829ea

Please sign in to comment.