From abb777120a79efd29daf546807bd932c11e71dae Mon Sep 17 00:00:00 2001 From: Paul Bone Date: Sun, 26 Jan 2025 21:32:49 +1100 Subject: [PATCH] [compiler/parse] Accept programs that combine entrypoint and export --- src/parse.m | 13 ++++++++++--- tests/modules/BUILD.plz | 11 +++++++++++ tests/modules/entrypoint_2.exp | 1 + tests/modules/entrypoint_2a.p | 15 +++++++++++++++ tests/modules/entrypoint_2b.p | 14 ++++++++++++++ tests/modules/entrypoint_3.exp | 1 + tests/modules/entrypoint_3a.p | 16 ++++++++++++++++ tests/modules/entrypoint_3b.p | 14 ++++++++++++++ 8 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 tests/modules/BUILD.plz create mode 100644 tests/modules/entrypoint_2.exp create mode 100644 tests/modules/entrypoint_2a.p create mode 100644 tests/modules/entrypoint_2b.p create mode 100644 tests/modules/entrypoint_3.exp create mode 100644 tests/modules/entrypoint_3a.p create mode 100644 tests/modules/entrypoint_3b.p diff --git a/src/parse.m b/src/parse.m index 9a3d3455..badf5096 100644 --- a/src/parse.m +++ b/src/parse.m @@ -1598,13 +1598,20 @@ tokens::in, tokens::out) is det. maybe_parse_func_export(Sharing, IsEntrypoint, !Tokens) :- + parse_export(Sharing0, !Tokens), optional(match_token(entrypoint), ok(MaybeEntrypoint), !Tokens), ( MaybeEntrypoint = yes(_), - Sharing = s_private, + ( Sharing0 = s_private, + % the export keyword might have come after entrypoint, so check + % again. + parse_export(Sharing, !Tokens) + ; Sharing0 = s_public, + Sharing = s_public + ), IsEntrypoint = is_entrypoint ; MaybeEntrypoint = no, - parse_export(Sharing, !Tokens), - IsEntrypoint = not_entrypoint + IsEntrypoint = not_entrypoint, + Sharing = Sharing0 ). :- pred parse_export_opaque(sharing_opaque::out, diff --git a/tests/modules/BUILD.plz b/tests/modules/BUILD.plz new file mode 100644 index 00000000..dba5b903 --- /dev/null +++ b/tests/modules/BUILD.plz @@ -0,0 +1,11 @@ +# This is free and unencumbered software released into the public domain. +# See ../LICENSE.unlicense + +[entrypoint_2] +type = program +modules = [Entrypoint2a, Entrypoint2b] + +[entrypoint_3] +type = program +modules = [Entrypoint3a, Entrypoint3b] + diff --git a/tests/modules/entrypoint_2.exp b/tests/modules/entrypoint_2.exp new file mode 100644 index 00000000..110c58e6 --- /dev/null +++ b/tests/modules/entrypoint_2.exp @@ -0,0 +1 @@ +Test 2 diff --git a/tests/modules/entrypoint_2a.p b/tests/modules/entrypoint_2a.p new file mode 100644 index 00000000..d45a739b --- /dev/null +++ b/tests/modules/entrypoint_2a.p @@ -0,0 +1,15 @@ +/* + * vim: ft=plasma + * This is free and unencumbered software released into the public domain. + * See ../LICENSE.unlicense + */ + +module Entrypoint2a + +export entrypoint +func test2a() uses IO -> Int { + print!("Test 2\n") + + return 0 +} + diff --git a/tests/modules/entrypoint_2b.p b/tests/modules/entrypoint_2b.p new file mode 100644 index 00000000..2dd66cb9 --- /dev/null +++ b/tests/modules/entrypoint_2b.p @@ -0,0 +1,14 @@ +/* + * vim: ft=plasma + * This is free and unencumbered software released into the public domain. + * See ../LICENSE.unlicense + */ + +module Entrypoint2b + +import Entrypoint2a as T2a + +func test2b() uses IO -> Int { + return T2a.test2a!() +} + diff --git a/tests/modules/entrypoint_3.exp b/tests/modules/entrypoint_3.exp new file mode 100644 index 00000000..0256ffd2 --- /dev/null +++ b/tests/modules/entrypoint_3.exp @@ -0,0 +1 @@ +Test 3 diff --git a/tests/modules/entrypoint_3a.p b/tests/modules/entrypoint_3a.p new file mode 100644 index 00000000..68df8118 --- /dev/null +++ b/tests/modules/entrypoint_3a.p @@ -0,0 +1,16 @@ +/* + * vim: ft=plasma + * This is free and unencumbered software released into the public domain. + * See ../LICENSE.unlicense + */ + +module Entrypoint3a + +// entrypoint2 has the keywords in the opposite order. +entrypoint export +func test3a() uses IO -> Int { + print!("Test 3\n") + + return 0 +} + diff --git a/tests/modules/entrypoint_3b.p b/tests/modules/entrypoint_3b.p new file mode 100644 index 00000000..80368d73 --- /dev/null +++ b/tests/modules/entrypoint_3b.p @@ -0,0 +1,14 @@ +/* + * vim: ft=plasma + * This is free and unencumbered software released into the public domain. + * See ../LICENSE.unlicense + */ + +module Entrypoint3b + +import Entrypoint3a as T3a + +func test3b() uses IO -> Int { + return T3a.test3a!() +} +