Skip to content

Commit

Permalink
add types for return, assign, gotoifnot
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 26, 2016
1 parent ea92526 commit 486286a
Show file tree
Hide file tree
Showing 14 changed files with 520 additions and 343 deletions.
19 changes: 18 additions & 1 deletion base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@
# name::Symbol
#end

#immutable ReturnNode
# expr
#end

#immutable AssignNode
# lhs
# rhs
#end

#immutable GotoIfNotNode
# cond
# label::Int
#end

# type Task
# parent::Task
# storage::Any
Expand Down Expand Up @@ -140,7 +154,7 @@ export
StackOverflowError, SegmentationFault, UndefRefError, UndefVarError, TypeError,
# AST representation
Expr, GotoNode, LabelNode, LineNumberNode, QuoteNode, TopNode,
GlobalRef, NewvarNode, GenSym, Slot,
GlobalRef, NewvarNode, GenSym, Slot, ReturnNode, AssignNode, GotoIfNotNode,
# object model functions
fieldtype, getfield, setfield!, nfields, throw, tuple, is, ===, isdefined, eval,
# arrayref, arrayset, arraysize,
Expand Down Expand Up @@ -308,6 +322,9 @@ eval(:((::Type{LineNumberNode})(f::Symbol, l::Int) = $(Expr(:new, :LineNumberNod
eval(:((::Type{GlobalRef})(m::Module, s::Symbol) = $(Expr(:new, :GlobalRef, :m, :s))))
eval(:((::Type{Slot})(n::Int) = $(Expr(:new, :Slot, :n, Any))))
eval(:((::Type{Slot})(n::Int, t::ANY) = $(Expr(:new, :Slot, :n, :t))))
eval(:((::Type{ReturnNode})(ex::ANY) = $(Expr(:new, :ReturnNode, :ex))))
eval(:((::Type{AssignNode})(l::ANY, r::ANY) = $(Expr(:new, :AssignNode, :l, :r))))
eval(:((::Type{GotoIfNotNode})(cond::ANY, label::Int) = $(Expr(:new, :GotoIfNotNode, :cond, :label))))

Module(name::Symbol=:anonymous, std_imports::Bool=true) = ccall(:jl_f_new_module, Any, (Any, Bool), name, std_imports)::Module

Expand Down
8 changes: 7 additions & 1 deletion base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ copy(e::Expr) = (n = Expr(e.head);
n.typ = e.typ;
n)
copy(s::Slot) = Slot(s.id, s.typ)
copy(x::AssignNode) = AssignNode(astcopy(x.lhs), astcopy(x.rhs))
copy(x::ReturnNode) = ReturnNode(astcopy(x.expr))
copy(x::GotoIfNotNode) = GotoIfNotNode(astcopy(x.cond), x.label)

# copy parts of an AST that the compiler mutates
astcopy(x::Union{Slot,Expr}) = copy(x)
astcopy(x::Union{Slot,Expr,AssignNode,ReturnNode,GotoIfNotNode}) = copy(x)
astcopy(x::Array{Any,1}) = Any[astcopy(a) for a in x]
astcopy(x) = x

==(x::Expr, y::Expr) = x.head === y.head && x.args == y.args
==(x::QuoteNode, y::QuoteNode) = x.value == y.value
==(x::Slot, y::Slot) = x.id === y.id && x.typ === y.typ
==(x::AssignNode, y::AssignNode) = x.lhs==y.lhs && x.rhs==y.rhs
==(x::ReturnNode, y::ReturnNode) = x.expr==y.expr
==(x::GotoIfNotNode, y::GotoIfNotNode) = x.cond==y.cond && x.label==y.label

expand(x) = ccall(:jl_expand, Any, (Any,), x)
macroexpand(x) = ccall(:jl_macroexpand, Any, (Any,), x)
Expand Down
3 changes: 3 additions & 0 deletions base/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ end

hash(x::QuoteNode, h::UInt) = hash(x.value, hash(QuoteNode, h))
hash(x::Slot, h::UInt) = hash(x.id, hash(x.typ, hash(Slot, h)))
hash(x::ReturnNode, h::UInt) = hash(x.expr, hash(ReturnNode, h))
hash(x::AssignNode, h::UInt) = hash(x.lhs, hash(x.rhs, hash(AssignNode, h)))
hash(x::GotoIfNotNode, h::UInt) = hash(x.cond, hash(x.label, hash(GotoIfNotNode, h)))

# hashing ranges by component at worst leads to collisions for very similar ranges
const hashr_seed = UInt === UInt64 ? 0x80707b6821b70087 : 0x21b70087
Expand Down
Loading

0 comments on commit 486286a

Please sign in to comment.