Skip to content

Commit

Permalink
ast compressing for function body: #47, #45
Browse files Browse the repository at this point in the history
  • Loading branch information
thautwarm committed May 26, 2020
1 parent 15f185d commit bb0b9b7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CanonicalTraits = "a603d957-0e48-4f86-8fbd-0b7bc66df689"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
JuliaVariables = "b14d175d-62b4-44ba-8fb7-3064adc8c3ec"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[compat]
CanonicalTraits = "^0.1, ^0.2"
Expand Down
4 changes: 2 additions & 2 deletions src/closure_conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ function closure_conv(top::Any, ex::Any)
conv(ex.args[2])
end

function _get_body(::RuntimeFn{Args, Kwargs, Body}) where {Args, Kwargs, Body}
Body
function _get_body(::RuntimeFn{Args, Kwargs, Body})::Any where {Args, Kwargs, Body}
bytes_to_type(Body)
end

function _get_body(ex)
Expand Down
3 changes: 2 additions & 1 deletion src/ngg/ngg.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module NGG
export to_type, from_type, show_repr, TypeLevel, TVal, TApp, TCons, TNil
export RuntimeFn, Unset, Argument, mkngg, rmlines
export bytes_to_type, type_to_bytes
using MLStyle
using CanonicalTraits
using DataStructures
Expand Down Expand Up @@ -51,7 +52,7 @@ function mkngg(

kwarglist = vectolist(kwargs)
Kwargs = to_type(kwarglist)
Ex = to_type(ex)
Ex = type_to_bytes(to_type(ex))
RuntimeFn{Args, Kwargs, Ex, name}()
end

Expand Down
19 changes: 16 additions & 3 deletions src/ngg/runtime_fns.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
# A generalized generated function implementation, but not that generalized.

import Serialization

struct RuntimeFn{Args, Kwargs, Body, Name} end
struct Unset end

function type_to_bytes(@nospecialize(x))::(NTuple{N, UInt8} where N)
io = IOBuffer()
Serialization.serialize(io, x)
Tuple(take!(io))
end

function bytes_to_type(x::NTuple{N, UInt8})::Any where N
io = IOBuffer(UInt8[x...])
Serialization.deserialize(io)
end

@implement Typeable{RuntimeFn{Args, Kwargs, Body, Name}} where {Args, Kwargs, Body, Name}

Base.show(io::IO, rtfn::RuntimeFn{Args, Kwargs, Body, Name}) where {Args, Kwargs, Body, Name} = begin
args = interpret(Args)
kwargs = interpret(Kwargs)
args = join(map(string, args), ", ")
kwargs = join(map(string, kwargs), ", ")
body = interpret(Body) |> rmlines
body = interpret(bytes_to_type(Body)) |> rmlines
repr = "$Name = ($args;$kwargs) -> $body"
print(io, repr)
end
Expand Down Expand Up @@ -65,7 +78,7 @@ end
args = interpret(Args)
ninput = length(pargs)
assign_block = Expr[]
body = interpret(Body)
body = interpret(bytes_to_type(Body))
_ass_positional_args!(assign_block, args, ninput, :pargs)
quote
let $(assign_block...)
Expand All @@ -79,7 +92,7 @@ end
kwargs = interpret(Kwargs)
ninput = length(pargs)
assign_block = Expr[]
body = interpret(Body)
body = interpret(bytes_to_type(Body))
if isempty(kwargs)
_ass_positional_args!(assign_block, args, ninput, :pargs)
else
Expand Down

0 comments on commit bb0b9b7

Please sign in to comment.