Skip to content

Commit

Permalink
Merge pull request #28 from thautwarm/0.2
Browse files Browse the repository at this point in the history
0.2: refactoring
  • Loading branch information
cscherrer authored Nov 19, 2019
2 parents 9815ca0 + 393ccdf commit 83cf2f4
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 237 deletions.
8 changes: 5 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
name = "GeneralizedGenerated"
uuid = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb"
authors = ["thautwarm"]
version = "0.1.4"
version = "0.2.0"

[deps]
CanonicalTraits = "a603d957-0e48-4f86-8fbd-0b7bc66df689"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
JuliaVariables = "b14d175d-62b4-44ba-8fb7-3064adc8c3ec"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
NameResolution = "71a1bf82-56d0-4bbc-8a3c-48b961074391"

[compat]
CanonicalTraits = "^0.1"
DataStructures = "^0.17"
JuliaVariables = "^0.2"
MLStyle = "^0.3.1"
julia = "1"
JuliaVariables = "0.1.3"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand Down
60 changes: 43 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,54 @@ end

f(1)(2) # => 3

@gg function h(x, c)
quote
d = x + 10
function g(x, y=c)
x + y + d
end
end
end

h(1, 2)(1) # => 14
```
P.S: We can figure out a pure Julia way to resolve symbols, thus free variables and
other stuffs can be resolved automatically.
Note there're some restrictions to the generalized generated functions yet:
Note there're some restrictions to the closures of generated functions yet:
- Multiple dispatch is not allowed, and `f(x) = ...` is equivalent to `f = x -> ...`. This will never gets supported for it needs a thorough implementation of multuple dispatch in GG.
- Comprehensions for generated functions are not implemented yet. It won't cost a long time for being supported.
- Multiple dispatch is not allowed, and `f(x) = ...` is equivalent to `f = x -> ...`.
- Comprehensions for generated functions are not implemented yet.
- Default arguments doesn't work unless they're constants. You can just splice variables into the AST to achieve the same functionlity. The following code works.
The evaluation module can be specified in this way:
```julia
@gg function f(x)
k = 10
quote
d = k + 10
function g(x, y=$k)
x + y + d
end
end
end
```
```julia
julia> module S
run(y) = y + 1
end
Main.S

julia> @gg m function g(m::Module, y) :(run(y)) end
# the global variable `run` is from the module `m`
g (generic function with 1 method)

julia> g(S, 1)
2
```
Of course you can use structures to imitate modules:
```julia
julia> struct S
run :: Function
end
Main.S

julia> @gg m function g(m::S, y) :(run(y)) end
# the global variable `run` is from the datum `m`
g (generic function with 1 method)

julia> g(S(x -> x + 1), 1)
2
```
## No `eval`/`invokelatest`!
Expand Down
21 changes: 9 additions & 12 deletions src/GeneralizedGenerated.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
module GeneralizedGenerated
using MLStyle
using JuliaVariables
using NameResolution
using CanonicalTraits
using DataStructures
List = LinkedList

export gg, @gg, top_level_closure_conv, expr2typelevel, interpret
export RuntimeFn, mk_function
export NGG
export gg, @gg, closure_conv, interpret
export RuntimeFn, mk_function, mkngg
export to_type, to_typelist, types_to_typelist, from_type, runtime_eval

include("utils.jl")
include("typeable.jl")
include("runtime_funcs.jl")
include("closure.jl")
include("closure_conv.jl")


Expand All @@ -23,9 +18,11 @@ end

function mk_function(mod::Module, ex)
ex = macroexpand(mod, ex)
fn = top_level_closure_conv(mod, solve(ex))
ex = simplify_ex(ex)
ex = solve(ex)
fn = closure_conv(mod, ex)
if !(fn isa RuntimeFn)
error("Expect a function expression")
error("Expect an unnamed function expression. ")
end
fn
end
Expand All @@ -35,7 +32,7 @@ function mk_function(mod::Module, args, kwargs, body)
end

function mk_function(args, kwargs, body)
mk_function(@__MODULE__, args, kwargs, body)
mk_function(Main, args, kwargs, body)
end

function runtime_eval(mod::Module, ex)
Expand All @@ -44,7 +41,7 @@ function runtime_eval(mod::Module, ex)
end

function runtime_eval(ex)
runtime_eval(@__MODULE__, ex)
runtime_eval(Main, ex)
end

end # module
Loading

2 comments on commit 83cf2f4

@thautwarm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/5624

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 83cf2f4f8be0353a12191c4506bc4872eaae0834
git push origin v0.2.0

Please sign in to comment.