Skip to content

Commit

Permalink
refactor: Move side-effect commands to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
korenmiklos committed Jul 2, 2024
1 parent 4579c62 commit d09d84b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/Kezdi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include("macros.jl")
include("parse.jl")
include("codegen.jl")
include("commands.jl")
include("side_effects.jl")

include("With.jl")
@reexport using .With: @with, @with!
Expand Down
51 changes: 0 additions & 51 deletions src/commands.jl
Original file line number Diff line number Diff line change
@@ -1,48 +1,6 @@
# use multiple dispatch to generate code
rewrite(command::Command) = rewrite(Val(command.command), command)

function rewrite(::Val{:tabulate}, command::Command)
gc = generate_command(command; options=[:variables, :ifable, :nofunction])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
columns = [x[1] for x in extract_variable_references.(command.arguments)]
quote
$setup
Kezdi.tabulate($target_df, $columns) |> $teardown
end |> esc
end

function rewrite(::Val{:summarize}, command::Command)
gc = generate_command(command; options=[:variables, :ifable, :replace_variables, :single_argument, :nofunction])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
column = extract_variable_references(command.arguments[1])
quote
$setup
Kezdi.summarize($target_df, $column[1]) |> $teardown
end |> esc
end

function rewrite(::Val{:regress}, command::Command)
gc = generate_command(command; options=[:variables, :ifable], allowed=[:robust, :cluster])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
if :robust in get_top_symbol.(options)
vcov = :(Vcov.robust())
elseif :cluster in get_top_symbol.(options)
vars = get_option(command, :cluster)
vars = replace_variable_references.(vars)
vcov = :(Vcov.cluster($(vars...)))
else
vcov = :(Vcov.simple())
end
quote
$setup
if length($(arguments[2:end])) == 1
reg($target_df, @formula($(arguments[1]) ~ $(arguments[2])), $vcov) |> $teardown
else
reg($target_df, @formula($(arguments[1]) ~ $(Expr(:call, :+, arguments[2:end]...))), $vcov) |> $teardown
end
end |> esc
end

function rewrite(::Val{:generate}, command::Command)
gc = generate_command(command; options=[:single_argument, :variables, :ifable, :replace_variables, :vectorize, :assignment], allowed=[:by])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
Expand Down Expand Up @@ -136,15 +94,6 @@ function rewrite(::Val{:egen}, command::Command)
end |> esc
end

function rewrite(::Val{:count}, command::Command)
gc = generate_command(command; options=[:ifable, :nofunction], allowed=[:by])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
quote
$setup
Kezdi.counter($target_df) |> $teardown
end |> esc
end

function rewrite(::Val{:sort}, command::Command)
gc = generate_command(command; options=[:variables, :nofunction], allowed=[:desc])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
Expand Down
51 changes: 51 additions & 0 deletions src/side_effects.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function rewrite(::Val{:tabulate}, command::Command)
gc = generate_command(command; options=[:variables, :ifable, :nofunction])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
columns = [x[1] for x in extract_variable_references.(command.arguments)]
quote
$setup
Kezdi.tabulate($target_df, $columns) |> $teardown
end |> esc
end

function rewrite(::Val{:summarize}, command::Command)
gc = generate_command(command; options=[:variables, :ifable, :replace_variables, :single_argument, :nofunction])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
column = extract_variable_references(command.arguments[1])
quote
$setup
Kezdi.summarize($target_df, $column[1]) |> $teardown
end |> esc
end

function rewrite(::Val{:regress}, command::Command)
gc = generate_command(command; options=[:variables, :ifable], allowed=[:robust, :cluster])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
if :robust in get_top_symbol.(options)
vcov = :(Vcov.robust())
elseif :cluster in get_top_symbol.(options)
vars = get_option(command, :cluster)
vars = replace_variable_references.(vars)
vcov = :(Vcov.cluster($(vars...)))
else
vcov = :(Vcov.simple())
end
quote
$setup
if length($(arguments[2:end])) == 1
reg($target_df, @formula($(arguments[1]) ~ $(arguments[2])), $vcov) |> $teardown
else
reg($target_df, @formula($(arguments[1]) ~ $(Expr(:call, :+, arguments[2:end]...))), $vcov) |> $teardown
end
end |> esc
end

function rewrite(::Val{:count}, command::Command)
gc = generate_command(command; options=[:ifable, :nofunction], allowed=[:by])
(; df, local_copy, target_df, setup, teardown, arguments, options) = gc
quote
$setup
Kezdi.counter($target_df) |> $teardown
end |> esc
end

0 comments on commit d09d84b

Please sign in to comment.