@@ -5,17 +5,22 @@ struct DesugaringContext{GraphType} <: AbstractLoweringContext
55 bindings:: Bindings
66 scope_layers:: Vector{ScopeLayer}
77 mod:: Module
8+ expr_compat_mode:: Bool
89end
910
10- function DesugaringContext (ctx)
11+ function DesugaringContext (ctx, expr_compat_mode :: Bool )
1112 graph = ensure_attributes (syntax_graph (ctx),
1213 kind= Kind, syntax_flags= UInt16,
1314 source= SourceAttrType,
1415 value= Any, name_val= String,
1516 scope_type= Symbol, # :hard or :soft
1617 var_id= IdTag,
1718 is_toplevel_thunk= Bool)
18- DesugaringContext (graph, ctx. bindings, ctx. scope_layers, first (ctx. scope_layers). mod)
19+ DesugaringContext (graph,
20+ ctx. bindings,
21+ ctx. scope_layers,
22+ first (ctx. scope_layers). mod,
23+ expr_compat_mode)
1924end
2025
2126# -------------------------------------------------------------------------------
@@ -3246,20 +3251,39 @@ function expand_macro_def(ctx, ex)
32463251 name = sig[1 ]
32473252 args = remove_empty_parameters (children (sig))
32483253 @chk kind (args[end ]) != K " parameters" (args[end ], " macros cannot accept keyword arguments" )
3249- ret = @ast ctx ex [K " function"
3250- [K " call" (sig)
3251- _make_macro_name (ctx, name)
3252- [K " ::"
3253- adopt_scope (@ast (ctx, sig, " __context__" :: K"Identifier" ),
3254- kind (name) == K " ." ? name[1 ] : name)
3255- MacroContext:: K"Value"
3254+ scope_ref = kind (name) == K " ." ? name[1 ] : name
3255+ if ctx. expr_compat_mode
3256+ @ast ctx ex [K " function"
3257+ [K " call" (sig)
3258+ _make_macro_name (ctx, name)
3259+ [K " ::"
3260+ # TODO : should we be adopting the scope of the K"macro" expression itself?
3261+ adopt_scope (@ast (ctx, sig, " __source__" :: K"Identifier" ), scope_ref)
3262+ LineNumberNode:: K"Value"
3263+ ]
3264+ [K " ::"
3265+ adopt_scope (@ast (ctx, sig, " __module__" :: K"Identifier" ), scope_ref)
3266+ Module:: K"Value"
3267+ ]
3268+ map (e-> _apply_nospecialize (ctx, e), args[2 : end ])...
32563269 ]
3257- # flisp: We don't mark these @nospecialize because all arguments to
3258- # new macros will be of type SyntaxTree
3259- args[2 : end ]. ..
3270+ ex[2 ]
32603271 ]
3261- ex[2 ]
3262- ]
3272+ else
3273+ @ast ctx ex [K " function"
3274+ [K " call" (sig)
3275+ _make_macro_name (ctx, name)
3276+ [K " ::"
3277+ adopt_scope (@ast (ctx, sig, " __context__" :: K"Identifier" ), scope_ref)
3278+ MacroContext:: K"Value"
3279+ ]
3280+ # flisp: We don't mark these @nospecialize because all arguments to
3281+ # new macros will be of type SyntaxTree
3282+ args[2 : end ]. ..
3283+ ]
3284+ ex[2 ]
3285+ ]
3286+ end
32633287end
32643288
32653289# -------------------------------------------------------------------------------
@@ -4284,9 +4308,10 @@ function expand_module(ctx, ex::SyntaxTree)
42844308 [K " inert" ex]
42854309 ]
42864310 [K " call"
4287- eval_module :: K"Value"
4288- ctx. mod :: K"Value"
4289- modname :: K"String"
4311+ eval_module :: K"Value"
4312+ ctx. mod :: K"Value"
4313+ modname :: K"String"
4314+ ctx. expr_compat_mode :: K"Bool"
42904315 [K " inert" (body)
42914316 [K " toplevel"
42924317 std_defs
@@ -4477,14 +4502,21 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
44774502 elseif k == K " toplevel"
44784503 # The toplevel form can't be lowered here - it needs to just be quoted
44794504 # and passed through to a call to eval.
4480- @ast ctx ex [K " block"
4505+ ex2 = @ast ctx ex [K " block"
44814506 [K " assert" " toplevel_only" :: K"Symbol" [K " inert" ex]]
44824507 [K " call"
4483- eval :: K"Value"
4484- ctx. mod :: K"Value"
4508+ eval :: K"Value"
4509+ ctx. mod :: K"Value"
44854510 [K " inert" ex]
4511+ [K " parameters"
4512+ [K " ="
4513+ " expr_compat_mode" :: K"Identifier"
4514+ ctx. expr_compat_mode:: K"Bool"
4515+ ]
4516+ ]
44864517 ]
44874518 ]
4519+ expand_forms_2 (ctx, ex2)
44884520 elseif k == K " vect"
44894521 check_no_parameters (ex, " unexpected semicolon in array expression" )
44904522 expand_array (ctx, ex, " vect" )
@@ -4546,7 +4578,7 @@ function expand_forms_2(ctx::StatementListCtx, args...)
45464578end
45474579
45484580function expand_forms_2 (ctx:: MacroExpansionContext , ex:: SyntaxTree )
4549- ctx1 = DesugaringContext (ctx)
4581+ ctx1 = DesugaringContext (ctx, ctx . expr_compat_mode )
45504582 ex1 = expand_forms_2 (ctx1, reparent (ctx1, ex))
45514583 ctx1, ex1
45524584end
0 commit comments