Skip to content

Commit

Permalink
preparing for ccall improvements
Browse files Browse the repository at this point in the history
- make ccall a reserved word
- make &x parse as (& x)
adjusting code to allow for these syntax changes
  • Loading branch information
JeffBezanson committed Feb 11, 2012
1 parent 94275fa commit 3fe7446
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion j/char.j
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ rem(x::Char, y::Char) = rem(int(x), int(y))
mod(x::Char, y::Char) = rem(int(x), int(y))

~(x::Char) = ~uint32(x)
&(x::Char, y::Char) = uint32(x) & uint32(y)
(&)(x::Char, y::Char) = uint32(x) & uint32(y)

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Feb 12, 2012

Member

this is kind of unfortunate.

|(x::Char, y::Char) = uint32(x) | uint32(y)
($)(x::Char, y::Char) = uint32(x) $ uint32(y)

Expand Down
5 changes: 3 additions & 2 deletions j/inference.j
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ t_func[fpiseq32] = (2, 2, cmp_tfunc)
t_func[fpiseq64] = (2, 2, cmp_tfunc)
t_func[fpislt32] = (2, 2, cmp_tfunc)
t_func[fpislt64] = (2, 2, cmp_tfunc)
t_func[ccall] =
t_func[eval(Base,:ccall)] =
(3, Inf, (fptr, rt, at, a...)->(is(rt,Type{Void}) ? Nothing :
isType(rt) ? rt.parameters[1] : Any))
t_func[is] = (2, 2, cmp_tfunc)
Expand Down Expand Up @@ -1394,7 +1394,8 @@ function inlining_pass(e::Expr, vars)
end
arg1 = e.args[1]
if is(e.head,:call) && (is(arg1, :ccall) ||
(isa(arg1,SymbolNode) && is(arg1.name, :ccall)))
(isa(arg1,SymbolNode) && is(arg1.name, :ccall)) ||
(isa(arg1,TopNode) && is(arg1.name, :ccall)))
if length(e.args)>1
e.args[2] = remove_call1(e.args[2])
end
Expand Down
18 changes: 9 additions & 9 deletions j/int.j
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,15 @@ mod{T<:Integer }(x::T, y::T) = rem(y+rem(x,y),y)
~(x::Uint32) = boxui32(not_int(unbox32(x)))
~(x::Uint64) = boxui64(not_int(unbox64(x)))

&(x::Int8 , y::Int8 ) = boxsi8 (and_int(unbox8 (x), unbox8 (y)))
&(x::Int16, y::Int16) = boxsi16(and_int(unbox16(x), unbox16(y)))
&(x::Int32, y::Int32) = boxsi32(and_int(unbox32(x), unbox32(y)))
&(x::Int64, y::Int64) = boxsi64(and_int(unbox64(x), unbox64(y)))

&(x::Uint8 , y::Uint8 ) = boxui8 (and_int(unbox8 (x), unbox8 (y)))
&(x::Uint16, y::Uint16) = boxui16(and_int(unbox16(x), unbox16(y)))
&(x::Uint32, y::Uint32) = boxui32(and_int(unbox32(x), unbox32(y)))
&(x::Uint64, y::Uint64) = boxui64(and_int(unbox64(x), unbox64(y)))
(&)(x::Int8 , y::Int8 ) = boxsi8 (and_int(unbox8 (x), unbox8 (y)))
(&)(x::Int16, y::Int16) = boxsi16(and_int(unbox16(x), unbox16(y)))
(&)(x::Int32, y::Int32) = boxsi32(and_int(unbox32(x), unbox32(y)))
(&)(x::Int64, y::Int64) = boxsi64(and_int(unbox64(x), unbox64(y)))

(&)(x::Uint8 , y::Uint8 ) = boxui8 (and_int(unbox8 (x), unbox8 (y)))
(&)(x::Uint16, y::Uint16) = boxui16(and_int(unbox16(x), unbox16(y)))
(&)(x::Uint32, y::Uint32) = boxui32(and_int(unbox32(x), unbox32(y)))
(&)(x::Uint64, y::Uint64) = boxui64(and_int(unbox64(x), unbox64(y)))

|(x::Int8 , y::Int8 ) = boxsi8 (or_int(unbox8 (x), unbox8 (y)))
|(x::Int16, y::Int16) = boxsi16(or_int(unbox16(x), unbox16(y)))
Expand Down
6 changes: 3 additions & 3 deletions j/intset.j
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ end

~(s::IntSet) = not!(IntSet(s))
|(s1::IntSet, s2::IntSet) = (s1.limit >= s2.limit ? or!(IntSet(s1), s2) : or!(IntSet(s2), s1))
&(s1::IntSet, s2::IntSet) = (s1.limit >= s2.limit ? and!(IntSet(s1), s2) : and!(IntSet(s2), s1))
(&)(s1::IntSet, s2::IntSet) = (s1.limit >= s2.limit ? and!(IntSet(s1), s2) : and!(IntSet(s2), s1))
($)(s1::IntSet, s2::IntSet) = (s1.limit >= s2.limit ? xor!(IntSet(s1), s2) : xor!(IntSet(s2), s1))

union!(s1::IntSet, s2::IntSet) = or!(s1, s2)
union(s1::IntSet, s2::IntSet) = |(s1, s2)
union(s1::IntSet, s2::IntSet) = s1 | s2
intersection!(s1::IntSet, s2::IntSet) = and!(s1, s2)
intersection(s1::IntSet, s2::IntSet) = &(s1, s2)
intersection(s1::IntSet, s2::IntSet) = s1 & s2
complement!(s1::IntSet) = not!(s1)
complement(s1::IntSet) = ~s1

Expand Down
8 changes: 4 additions & 4 deletions j/operators.j
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ min(x,y) = x < y ? x : y

+() = 0
*() = 1
&() = error("zero-argument & is ambiguous")
|() = error("zero-argument | is ambiguous")
(&)() = error("zero-argument & is ambiguous")
(|)() = error("zero-argument | is ambiguous")
($)() = error("zero-argument \$ is ambiguous")

+(x::Number) = x
*(x::Number) = x
&(x::Integer) = x
|(x::Integer) = x
(&)(x::Integer) = x
(|)(x::Integer) = x
($)(x::Integer) = x

for op = (:+, :*, :&, :|, :$, :min, :max)
Expand Down
8 changes: 4 additions & 4 deletions j/promotion.j
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ end
/(x::Number, y::Number) = /(promote(x,y)...)
^(x::Number, y::Number) = ^(promote(x,y)...)

&(x::Integer, y::Integer) = &(promote(x,y)...)
|(x::Integer, y::Integer) = |(promote(x,y)...)
(&)(x::Integer, y::Integer) = (&)(promote(x,y)...)
(|)(x::Integer, y::Integer) = (|)(promote(x,y)...)
($)(x::Integer, y::Integer) = ($)(promote(x,y)...)

==(x::Number, y::Number) = (==)(promote(x,y)...)
Expand All @@ -68,8 +68,8 @@ no_op_err(name, T) = error(name," not defined for ",T)
/{T<:Number}(x::T, y::T) = no_op_err("/", T)
^{T<:Number}(x::T, y::T) = no_op_err("^", T)
&{T<:Integer}(x::T, y::T) = no_op_err("&", T)
|{T<:Integer}(x::T, y::T) = no_op_err("|", T)
(&){T<:Integer}(x::T, y::T) = no_op_err("&", T)
(|){T<:Integer}(x::T, y::T) = no_op_err("|", T)
($){T<:Integer}(x::T, y::T) = no_op_err("\$", T)
=={T<:Number}(x::T, y::T) = no_op_err("==", T)
Expand Down
6 changes: 4 additions & 2 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ static int is_intrinsic(jl_sym_t *s)

static int has_intrinsics(jl_expr_t *e)
{
if (e->head == call_sym && jl_is_symbol(jl_exprarg(e,0)) &&
is_intrinsic((jl_sym_t*)jl_exprarg(e,0)))
jl_value_t *e0 = jl_exprarg(e,0);
if (e->head == call_sym &&
((jl_is_symbol(e0) && is_intrinsic((jl_sym_t*)e0)) ||
(jl_is_topnode(e0) && is_intrinsic((jl_sym_t*)jl_fieldref(e0,0)))))
return 1;
int i;
for(i=0; i < e->args->length; i++) {
Expand Down
13 changes: 9 additions & 4 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@

(define assignment-ops (prec-ops 0))

(define unary-ops '(+ - ! ~ $ |<:| |>:|))
(define unary-ops '(+ - ! ~ $ & |<:| |>:|))

; operators that are both unary and binary
(define unary-and-binary-ops '(+ - $))
(define unary-and-binary-ops '(+ - $ &))

; operators that are special forms, not function names
(define syntactic-operators
'(= := += -= *= /= //= .//= .*= ./= |\\=| |.\\=| ^= .^= %= |\|=| &= $= =>
<<= >>= >>>= -> --> |\|\|| && : |::| |.|))
(define syntactic-unary-operators '($))
(define syntactic-unary-operators '($ &))

(define reserved-words '(begin while if for try return break continue
function macro quote let local global const
abstract typealias type bitstype
module import export))
module import export ccall))

(define (syntactic-op? op) (memq op syntactic-operators))
(define (syntactic-unary-op? op) (memq op syntactic-unary-operators))
Expand Down Expand Up @@ -728,6 +728,11 @@
(error (string "invalid module name " name)))
(begin0 (list word name (parse-block s))
(expect-end s))))
((ccall)
(if (not (eqv? (peek-token s) #\())
(error "expected ( after ccall"))
(take-token s)
(cons 'ccall (parse-arglist s #\) )))
(else (error "unhandled reserved word")))))

; parse comma-separated assignments, like "i=1:n,j=1:m,..."
Expand Down
5 changes: 5 additions & 0 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,11 @@
(pattern-lambda (call (-/ *) (|.'| a) (|.'| b))
`(call aTbT ,a ,b))

(pattern-lambda (ccall name RT (tuple . argtypes) . args)
`(call (top ccall) ,name ,RT ,(cadddr __)
,@args)
)

)) ; patterns

; patterns that verify all syntactic sugar was well-formed
Expand Down

2 comments on commit 3fe7446

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that unary & is going to be a general operator? Or will it be specific to ccall syntax?

@JeffBezanson
Copy link
Member Author

Choose a reason for hiding this comment

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

It's just like $ actually; it's syntactic (not a function call) when used as a unary operator, and for now it will only do anything inside ccall, just like $x only does something inside quote.

Please sign in to comment.