-
Notifications
You must be signed in to change notification settings - Fork 1
/
handcalc_marco.jl
196 lines (172 loc) · 4.94 KB
/
handcalc_marco.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
@handcalc expression
Create `LaTeXString` representing `expression`. The expression being a vaiable followed by
an equals sign and an algebraic equation.
Any side effects of the expression, like assignments, are evaluated as well.
The RHS can be formatted or otherwise transformed by supplying a function as kwarg `post`.
# Examples
```julia-repl
julia> a = 2
2
julia> b = 5
5
julia> @handcalc c = a + b
L"\$c = a + b = 2 + 5 = 7\$"
julia> c
7
```
"""
macro handcalc(expr, kwargs...)
expr = unblock(expr)
expr = rmlines(expr)
if @capture(expr, x_ = f_(fields__) | f_(fields__)) # Check if function call
if f ∉ math_syms && check_not_funcs(f, kwargs)
kwargs = kwargs..., :(is_recursive = true)
return esc(:(@handfunc $(expr) $(kwargs...)))
end
end
expr_post = expr.head == :(=) ? expr.args[2:end] : expr
expr_numeric = _walk_expr(expr_post, math_syms)
params = _extractparam.(kwargs)
post = :identity
for param in params
if param === :post
post = :post
break
end
if param isa Expr && param.args[1] === :post
post = param.args[2]
break
end
end
return _handcalc(expr, expr_numeric, post, kwargs)
end
# Handcalcs - Symbolic and Numeric return
# ***************************************************
function _handcalc(expr, expr_numeric, post, kwargs)
esc(
Expr(
:call,
:latexify,
Expr(:parameters, _extractparam.(kwargs)...),
Expr(:call, :Expr,
QuoteNode(:(=)), Meta.quot(expr), # symbolic portion
Expr(:call, :Expr,
QuoteNode(:(=)), Meta.quot(expr_numeric), # numeric portion
Expr(:call, post, _executable(expr)))), # defines variable
),
)
end
# ***************************************************
# Latexify Functions
# ***************************************************
function _executable(expr)
return postwalk(expr) do ex
if Meta.isexpr(ex, :$)
return ex.args[1]
end
return ex
end
end
_extractparam(arg::Symbol) = arg
_extractparam(arg::Expr) = Expr(:kw, arg.args[1], arg.args[2])
# ***************************************************
# These Functions parse the original expression to
# create an expression that interplotes the variables
# ***************************************************
# ***************************************************
function numeric_sub(x)
Expr(:($), x)
end
function _walk_expr(expr::Vector, math_syms)
count = 0
return prewalk(expr...) do ex
if count > 0 # skip sections based on prewalk
count -= 1
return ex
end
if Meta.isexpr(ex, :.) # interpolates field args
count = _det_branch_size(ex; count=3)
return Expr(:$, ex)
end
if Meta.isexpr(ex, :kw) # interpolates field args
count = 1
return ex
end
if (ex isa Symbol) & (ex ∉ math_syms)
count = 1
return numeric_sub(ex)
end
return ex
end
end
function _walk_expr(expr::Expr, math_syms)
count = 0
return prewalk(expr) do ex
if count > 0 # skip sections based on prewalk
count -= 1
return ex
end
if Meta.isexpr(ex, :.) # interpolates field args
count = length(ex.args) + 1
return Expr(:$, ex)
end
if Meta.isexpr(ex, :kw) # interpolates field args
count = 1
return ex
end
if (ex isa Symbol) & (ex ∉ math_syms)
count = 1
return numeric_sub(ex)
end
return ex
end
end
function _det_branch_size(expr; count=3) # determines field arg depth
arg1 = expr.args[1]
if Meta.isexpr(arg1, :.)
count += 1
return _det_branch_size(arg1; count=count)
end
return count
end
# ***************************************************
# ***************************************************
function check_not_funcs(f, kwargs)
not_funcs = find_not_funcs(kwargs)
not_funcs = typeof(not_funcs) == Symbol ? [not_funcs] : not_funcs
return f ∉ not_funcs
end
function find_not_funcs(kwargs)
not_funcs = []
for kwarg in kwargs
split_kwarg = _split_kwarg(kwarg)
if split_kwarg == :not_funcs
not_funcs = parse_not_funcs(kwarg.args[2])
end
end
return not_funcs
end
function parse_not_funcs(value::QuoteNode)
[value.value]
end
function parse_not_funcs(value::Symbol)
[value]
end
function parse_not_funcs(expr::Expr)
not_funcs = []
if expr.head in [:vcat :hcat :vect]
for arg in expr.args
push!(not_funcs, parse_not_func(arg))
end
elseif expr.head == :.
return [expr]
end
return not_funcs
end
function parse_not_func(value::QuoteNode)
value.value
end
function parse_not_func(value)
value
end