Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Almost done
- Loading branch information
Stephen
committed
Mar 13, 2018
1 parent
30143f5
commit aa42bad
Showing
2 changed files
with
76 additions
and
16 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,14 +1,33 @@ | ||
macro create_ast(fns, tms) | ||
return esc(Expr(:call, fns[1], tms[1], tms[2])) | ||
function create_full_tree(depth, fs, ts) | ||
""" | ||
Creates a single AST with full depth | ||
Inputs | ||
depth Current depth of tree. Initially called from main() with max depth | ||
fs Function Set - Array of allowed functions | ||
ts Terminal Set - Array of allowed terminal values | ||
Output | ||
Full AST of typeof()==Expr | ||
""" | ||
|
||
# If we are at the bottom | ||
if depth == 1 | ||
# End of tree, return function with two terminal nodes | ||
return Expr(:call, fs[rand(1:length(fs))], ts[rand(1:length(ts))], ts[rand(1:length(ts))]) | ||
else | ||
# Not end of expression, recurively go back through and create functions for each new node | ||
return Expr(:call, fs[rand(1:length(fs))], create_full_tree(depth-1, fs, ts), create_full_tree(depth-1, fs, ts)) | ||
end | ||
end | ||
|
||
function main() | ||
fns = [:+, :-] | ||
tms = [:x, :y] | ||
tst = @create_ast(fns, tms) | ||
println(tst) | ||
x = 1 | ||
y = 2 | ||
eval(tst) | ||
fs = [:+, :-, :DIV, :GT] | ||
ts = [:x,:v,-1] | ||
|
||
expr = create_full_tree(6, fs, ts) | ||
|
||
println(expr.args[2].args) | ||
end | ||
|
||
main() | ||
|
||
|