Skip to content

Commit 8abd102

Browse files
committed
RFC: Allow macrocall as function sig
@KristofferC requested that `function @main(args) end` should work. This is currently a parse error. This PR makes it work as expected by allowing macrocall as a valid signature in function (needs to exapand to a call expr). Note that this is only the flisp changes. If this PR is accepted, an equivalent change would need to be made in JuliaSyntax.
1 parent ed987f2 commit 8abd102

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

base/client.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ The `@main` macro may be used standalone or as part of the function definition,
580580
case, parentheses are required. In particular, the following are equivalent:
581581
582582
```
583-
function (@main)(args)
583+
function @main(args)
584584
println("Hello World")
585585
end
586586
```
@@ -599,7 +599,7 @@ imported into `Main`, it will be treated as an entrypoint in `Main`:
599599
```
600600
module MyApp
601601
export main
602-
(@main)(args) = println("Hello World")
602+
@main(args) = println("Hello World")
603603
end
604604
using .MyApp
605605
# `julia` Will execute MyApp.main at the conclusion of script execution
@@ -609,7 +609,7 @@ Note that in particular, the semantics do not attach to the method
609609
or the name:
610610
```
611611
module MyApp
612-
(@main)(args) = println("Hello World")
612+
@main(args) = println("Hello World")
613613
end
614614
const main = MyApp.main
615615
# `julia` Will *NOT* execute MyApp.main unless there is a separate `@main` annotation in `Main`
@@ -619,9 +619,6 @@ const main = MyApp.main
619619
```
620620
"""
621621
macro main(args...)
622-
if !isempty(args)
623-
error("USAGE: `@main` is expected to be used as `(@main)` without macro arguments.")
624-
end
625622
if isdefined(__module__, :main)
626623
if Base.binding_module(__module__, :main) !== __module__
627624
error("USAGE: Symbol `main` is already a resolved import in module $(__module__). `@main` must be used in the defining module.")
@@ -632,5 +629,9 @@ macro main(args...)
632629
global main
633630
global var"#__main_is_entrypoint__#"::Bool = true
634631
end)
635-
esc(:main)
632+
if !isempty(args)
633+
Expr(:call, esc(:main), map(esc, args)...)
634+
else
635+
esc(:main)
636+
end
636637
end

src/julia-parser.scm

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,18 +1154,20 @@
11541154
(parse-atom s))))
11551155

11561156
(define (parse-def s is-func anon)
1157-
(let* ((ex (parse-unary-prefix s))
1158-
(sig (if (or (and is-func (reserved-word? ex)) (initial-reserved-word? ex))
1159-
(error (string "invalid name \"" ex "\""))
1160-
(parse-call-chain s ex #f)))
1161-
(decl-sig
1162-
(if (and is-func (eq? (peek-token s) '|::|))
1163-
(begin (take-token s)
1164-
`(|::| ,sig ,(parse-call s)))
1165-
sig)))
1166-
(if (eq? (peek-token s) 'where)
1167-
(parse-where-chain s decl-sig)
1168-
decl-sig)))
1157+
(let ((ex (parse-unary-prefix s)))
1158+
(if (and (pair? ex) (eq? (car ex) 'macrocall))
1159+
ex
1160+
(let* ((sig (if (or (and is-func (reserved-word? ex)) (initial-reserved-word? ex))
1161+
(error (string "invalid name \"" ex "\""))
1162+
(parse-call-chain s ex #f)))
1163+
(decl-sig
1164+
(if (and is-func (eq? (peek-token s) '|::|))
1165+
(begin (take-token s)
1166+
`(|::| ,sig ,(parse-call s)))
1167+
sig)))
1168+
(if (eq? (peek-token s) 'where)
1169+
(parse-where-chain s decl-sig)
1170+
decl-sig)))))
11691171

11701172
(define (disallowed-space-error lno ex t)
11711173
(error (string "space before \"" t "\" not allowed in \""
@@ -1329,7 +1331,8 @@
13291331

13301332
(define (valid-func-sig? paren sig)
13311333
(and (pair? sig)
1332-
(or (eq? (car sig) 'call)
1334+
(or (eq? (car sig) 'macrocall)
1335+
(eq? (car sig) 'call)
13331336
(eq? (car sig) 'tuple)
13341337
(and paren (eq? (car sig) 'block))
13351338
(and paren (eq? (car sig) '...))

0 commit comments

Comments
 (0)