Last active
December 1, 2016 12:32
-
-
Save be5invis/9044c325617b23e1eec7e8a9d517f4c6 to your computer and use it in GitHub Desktop.
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
module jlos | |
import Base.getindex | |
import Base.colon | |
import Base.(-) | |
import Base.(<<) | |
export endl, @method, Message | |
function <<{T <: IO, X}(io::T, x::X)::T | |
print(io, x) | |
io | |
end | |
const endl = "\n" | |
immutable Message{K, T} | |
value :: T | |
end | |
function Base.colon{T}(x::Symbol, y::T) | |
Message{x, T}(y) | |
end | |
function -(x::Symbol) | |
Message{x, Void}(nothing) | |
end | |
function buildMethod(quantifier, declaration, body) | |
local assignments = [] | |
local interfaces = [] | |
local tempTypes = [] | |
local j = 1 | |
local selfDecl = nothing | |
local pardecl = nothing | |
if(declaration.head == :ref) | |
selfdecl = declaration.args[1] | |
pardecl = @view declaration.args[2:end] | |
elseif(declaration.head == :(::)) | |
selfdecl = Expr(:(::), declaration.args[1], declaration.args[2].args[1]) | |
local q = declaration.args[2] | |
pardecl = @view q.args[2:end] | |
end | |
for term in pardecl | |
if(isa(term, Symbol)) | |
push!(interfaces, term) | |
elseif(term.head == :(:)) | |
local tempParameter = Symbol("__t$j") | |
local tempType = Symbol("__POLYT$j") | |
local key = term.args[1] | |
local arg = term.args[2] | |
local name = nothing; | |
# A message parameter | |
if(isa(arg, Symbol)) | |
push!(interfaces, Expr(:(::), tempParameter, Expr(:curly, :(jlos.Message), key, tempType))) | |
push!(tempTypes, tempType) | |
name = arg | |
elseif(arg.head == :(::)) | |
push!(interfaces, Expr(:(::), tempParameter, Expr(:curly, :(jlos.Message), key, arg.args[2]))) | |
name = arg.args[1] | |
else | |
push!(interfaces, Expr(:(::), tempParameter, Expr(:curly, :(jlos.Message), key, tempType))) | |
push!(tempTypes, tempType) | |
name = arg | |
end | |
push!(assignments, :(local $name = $tempParameter.value)) | |
else | |
push!(interfaces, term) | |
end | |
j += 1 | |
end | |
local fnname = Expr(:curly, cat(1, [:(Base.getindex)], quantifier, tempTypes)...) | |
local decl = Expr(:call, fnname, selfdecl, interfaces...) | |
local defn = Expr(:block, cat(1, assignments, [body])...) | |
local fdef = Expr(:function, decl, defn) | |
return esc(fdef) | |
end | |
macro method(quantifier, declaration, body) | |
return buildMethod(quantifier.args, declaration, body) | |
end | |
macro method(declaration, body) | |
return buildMethod([], declaration, body) | |
end | |
end |
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
include("./jlos.jl") | |
module path | |
using jlos | |
import Base.(/) | |
export Path | |
immutable Path <: AbstractString | |
raw :: String | |
end | |
String(p::Path) = p.raw | |
Base.endof(p::Path) = endof(p.raw) | |
Base.next(p::Path, j::Int) = (p.raw[j], j+1) | |
function /{T <: AbstractString}(a::Path, b::T) | |
return Path(a.raw * (@static is_windows() ? "\\" : "/") * String(b)) | |
end | |
@method self::Path[:basename : x] begin | |
Path(basename(self.raw)) | |
end | |
@method {T <: AbstractString} self::Path[:concat : that::T] begin | |
self / that | |
end | |
end | |
using jlos | |
using path | |
p = Path(pwd()) / "path.jl" | |
@time (STDERR << p[:concat : "aaa"] << endl) | |
@time (STDERR << p / "aaa" << endl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment