Skip to content
Snippets Groups Projects
Commit 28253044 authored by xo30xoqa's avatar xo30xoqa
Browse files

Implemented the @species and @phase macros.

Macros are hard! But they seem to work now. And should make adding
species much more comfortable :-) (Still need to be integrated into
nature.jl, though.)
parent ab355e0d
Branches
Tags
No related merge requests found
......@@ -18,6 +18,7 @@ struct Species
update!::Function # takes two arguments: animal, model
traits::Dict{String,Any} # a collection of species-specific traits
end
#TODO convert species into dicts
"""
Animal
......@@ -35,6 +36,7 @@ end
# This dict stores the definitions for all species that can be simulated.
# (The definitions are created in separate files and registered here.)
##TODO this needs to be rewritten to deal with the new @species macro
let specieslist = Dict{String, Species}()
"""
registerspecies(species)
......
### Persephone - a socio-economic-ecological model of European agricultural landscapes.
###
### This file implements the macros needed for the species DSL.
###
#TODO configure species traits via a separate TOML file
##TODO replace the current registerspecies()
function registerspecies(speciesdict)
speciesdict["testphase"]()
end
macro species(name, body)
speciesdict = gensym()
quote
$speciesdict = Dict{String, Any}("name"=>string($(QuoteNode(name))))
for line in $(body.args)
(typeof(line) == LineNumberNode) && continue
if line.head == :macrocall
line = macroexpand($(__module__), line)
line.args[2] = Core.eval($(__module__), line.args[2])
end
if line.head == :(=)
$speciesdict[string(line.args[1])] = line.args[2]
end
end
registerspecies($speciesdict)
end
end
macro phase(name, body)
#:($(esc(name)) = function() $body end) #for testing
:($(esc(name)) = function(animal::Animal, model::AgentBasedModel) $body end)
end
### Persephone - a socio-economic-ecological model of European agricultural landscapes.
###
### This file holds the code for the Eurasian Skylark (Alauda arvensis).
###
##XXX At the moment, this is just a skeleton to show what I want to be able to interpret
## with the @species and @phase macros
@species Skylark begin
lifeexpectancy = 365*5
phase = "egg"
@phase egg begin
if !parentsalive(animal)
killanimal(animal)
end
if animal.age == 14
changephase("nestling")
end
end
@phase nestling begin
#TODO
end
#TODO
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment