-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
major refactoring to fable inputs and user methods
- Loading branch information
Showing
4 changed files
with
120 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,29 @@ | ||
export FableInput, fi, @fi, set!, combine, to_args, find_fi_index, value | ||
|
||
struct FableInput | ||
s::Union{Symbol, String} | ||
x::Ref{Union{Number, Tuple, Vector}} | ||
#-------------fable_input------------------------------------------------------# | ||
# | ||
# Purpose: This file is meant to create fable inputs for Fable | ||
# Fable Inputs are any values meant to be modified by the user | ||
# within a Fable executable to avoid recompilation. | ||
# | ||
# Notes: Maybe save as pointers to expressions for usability? | ||
# | ||
#------------------------------------------------------------------------------# | ||
export FableInput, @fi | ||
|
||
struct FableInput{E} | ||
expr::E | ||
end | ||
|
||
fi(args...) = FableInput(args...) | ||
|
||
function set!(fi::FableInput, val) | ||
fi.x.x = val | ||
end | ||
""" | ||
x = @fi 7 | ||
function combine(nt::Tuple, fis::Tuple) | ||
return Tuple(combine(nt[i], fis[i]) for i = 1:length(fis)) | ||
end | ||
or | ||
combine(fis::Vector{FableInput}, nt::NamedTuple) = combine(nt, fis) | ||
x = @fi 7*y+5 | ||
function combine(nt::NamedTuple, fis::Vector{FableInput}) | ||
if length(fis) == 0 | ||
return nt | ||
end | ||
|
||
fi_vals = (remove_vectors(fis[i].x.x) for i = 1:length(fis)) | ||
fi_keys = (Symbol(fis[i].s) for i = 1:length(fis)) | ||
|
||
return NamedTuple{(keys(nt)..., fi_keys...)}((values(nt)..., fi_vals...)) | ||
Marks the variable `x` as a FableInput -- a dynamic variable passed in to the | ||
final executable at runtime. | ||
This avoids recompilation of code for variables that are dynamically changing. | ||
""" | ||
macro fi(ex) | ||
esc(FableInput(ex)) | ||
end | ||
|
||
function to_args(nt::Tuple, fis::Tuple) | ||
return Tuple(combine(fis[i], nt[i]) for i = 1:length(fis)) | ||
end | ||
|
||
function to_args(nt::NamedTuple, fis::Vector{FableInput}) | ||
if length(fis) == 0 | ||
return values(nt) | ||
end | ||
fi_vals = (remove_vectors(fis[i].x.x) for i = 1:length(fis)) | ||
return (values(nt)..., fi_vals...) | ||
end | ||
|
||
function find_fi_index(s, fis::FT) where FT <: Union{Vector{FableInput}, | ||
Tuple} | ||
for i = 1:length(fis) | ||
if Symbol(fis[i].s) == Symbol(s) | ||
return i | ||
end | ||
end | ||
end | ||
|
||
value(fi::FableInput) = fi.x.x | ||
value(a) = a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters