Skip to content

Commit fab400a

Browse files
committed
Lab7: Added intermediate speciec macro.
1 parent d455cd5 commit fab400a

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

docs/src/lecture_07/Ecosystem.jl/src/Ecosystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using EcosystemCore
66
include("./ecosystem_macros.jl")
77
include("./ecosystem_agents.jl")
88

9-
export Grass, Sheep, Wolf, World, Mushroom
9+
export World
1010
export agent_step!, agent_count, world_step!, simulate!, every_nth
1111

1212
function simulate!(world::World, iters::Int; cb=()->())

docs/src/lecture_07/Ecosystem.jl/src/ecosystem_agents.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ abstract type Wolf <: AnimalSpecies end
99
abstract type Mushroom <: PlantSpecies end
1010
abstract type Grass <: PlantSpecies end
1111
12+
export Grass, Sheep, Wolf, Mushroom
13+
1214
Base.show(io::IO, ::Type{Sheep}) = print(io,"🐑")
1315
Base.show(io::IO, ::Type{Wolf}) = print(io,"🐺")
1416

docs/src/lecture_07/Ecosystem.jl/src/ecosystem_macros.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
### species definition
2-
macro plant(name, icon)
3-
return esc(_species(name, icon; animal=false))
4-
end
5-
6-
macro animal(name, icon)
7-
return esc(_species(name, icon; animal=true))
2+
macro species(typ, name, icon)
3+
esc(_species(typ, name, icon))
84
end
95

10-
function _species(name, icon; animal=false)
6+
function _species(typ, name, icon)
117
quote
12-
abstract type $name <: $(animal ? AnimalSpecies : PlantSpecies) end
8+
abstract type $name <: $(typ == :Animal ? AnimalSpecies : PlantSpecies) end
139
Base.show(io::IO, ::Type{$name}) = print(io, $(QuoteNode(icon)))
1410
export $name
1511
end
1612
end
1713

14+
macro plant(name, icon)
15+
return :(@species Plant $name $icon)
16+
end
1817

19-
### eating
18+
macro animal(name, icon)
19+
return :(@species Animal $name $icon)
20+
end
21+
22+
### eating behavior
2023
macro eats(species::Symbol, foodlist::Expr)
2124
return esc(_eats(species, foodlist))
2225
end
2326

24-
_parse_eats(ex) = Dict(arg.args[2] => arg.args[3] for arg in ex.args if arg.head == :call && arg.args[1] == :(=>))
27+
2528
function _generate_eat(eater::Type{<:AnimalSpecies}, food::Type{<:PlantSpecies}, multiplier)
2629
quote
2730
EcosystemCore.eats(::Animal{$(eater)}, ::Plant{$(food)}) = true
@@ -44,6 +47,8 @@ function _generate_eat(eater::Type{<:AnimalSpecies}, food::Type{<:AnimalSpecies}
4447
end
4548
end
4649

50+
_parse_eats(ex) = Dict(arg.args[2] => arg.args[3] for arg in ex.args if arg.head == :call && arg.args[1] == :(=>))
51+
4752
function _eats(species, foodlist)
4853
cfg = _parse_eats(foodlist)
4954
code = Expr(:block)

0 commit comments

Comments
 (0)