Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alias and prelude shorthand tags #258

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ which demonstrates a number of language features.
Run as the main script or enter it into the Lissp REPL.
Requires [Bottle.](https://bottlepy.org/docs/dev/)
```Racket
(hissp.._macro_.prelude)
hissp..prelude#:

(define enjoin en#X#(.join "" (map str X)))

Expand Down
20 changes: 11 additions & 9 deletions docs/macro_tutorial.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. Copyright 2020, 2021, 2022, 2023 Matthew Egan Odendahl
.. Copyright 2020, 2021, 2022, 2023, 2024 Matthew Egan Odendahl
SPDX-License-Identifier: CC-BY-SA-4.0

.. All Source Code Examples in this file are licensed "Apache-2.0 OR CC-BY-SA-4.0"
Expand Down Expand Up @@ -218,18 +218,18 @@ Fire up the Lissp REPL in a terminal,
or in your editor if it does that,
in the same directory as your Lissp file.

Add the prelude to the top of the file:
Add the `prelude<hissp.prelude>` shorthand to the top of the file:

.. code-block:: Lissp

(hissp.._macro_.prelude)
hissp..prelude#:

And push it to the REPL as well:

.. code-block:: REPL

#> (hissp.._macro_.prelude)
>>> # hissp.._macro_.prelude
#> hissp..prelude#:
>>> # hissp.macros.._macro_.prelude
... __import__('builtins').exec(
... ('from functools import partial,reduce\n'
... 'from itertools import *;from operator import *\n'
Expand All @@ -253,7 +253,9 @@ And push it to the REPL as well:

.. caution::

The `prelude` macro overwrites your ``_macro_`` namespace with a copy of the bundled one.
The ``:`` directs it to dump into the module's global namespace.
The `prelude<hissp.macros._macro_.prelude>`
macro overwrites your ``_macro_`` namespace (if any) with a copy of the bundled one.
Any macros you've defined in there are lost.
In Lissp files, the prelude is meant to be used before any definitions,
when it is used at all.
Expand Down Expand Up @@ -1787,7 +1789,7 @@ Let's review. The code you need to make the version we have so far is

.. code-block:: Lissp

(hissp.._macro_.prelude)
hissp..prelude#:

(defmacro L (: :* expr)
`(lambda ,(map (lambda (i)
Expand Down Expand Up @@ -1838,11 +1840,11 @@ rather than pasting them all in again.

To use your macros from other Lissp modules,
use their fully-qualified names,
abbreviate the qualifier with `alias`,
abbreviate the qualifier with `alias<hissp.macros._macro_.alias>`,
or (if you must) `attach` them to your current module's ``_macro_`` object.
That last one would require that your macros also be available at run time,
although there are ways to avoid that if you need to.
See the `prelude` expansion for a hint.
See the `prelude<hissp.macros._macro_.alias>` expansion for a hint.

You can use the resulting macro as a shorter lambda for higher-order functions:

Expand Down
47 changes: 39 additions & 8 deletions src/hissp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020, 2022 Matthew Egan Odendahl
# Copyright 2020, 2022, 2024 Matthew Egan Odendahl
# SPDX-License-Identifier: Apache-2.0

# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -46,13 +46,44 @@
from hissp.reader import transpile
from hissp.repl import interact

from contextlib import suppress

# Hissp must be importable to compile macros.lissp in the first place.
with suppress(ImportError):
# noinspection PyUnresolvedReferences
try: # Hissp must be importable to compile macros.lissp the first time.
from hissp.macros import _macro_
del suppress

except ImportError: # Print warning, but continue.
print("Unable to import hissp macros.", file=__import__("sys").stderr)

VERSION = "0.5.dev"


def prelude(ns):
"""Lissp prelude shorthand tag.

Usage: ``hissp..prelude#ns``, which expands to

.. code-block:: Lissp

(hissp.macros.._macro_.prelude ns)

``hissp..prelude#:`` is short for
``hissp..prelude#(builtins..globals)``.

See `hissp.macros._macro_.prelude`.
"""
return "hissp.macros.._macro_.prelude", *([] if ns == ":" else [ns])


def alias(abbreviation, qualifier="hissp.macros.._macro_"):
"""Lissp alias shorthand tag.

Usage: ``hissp..alias## abbreviation qualifier``,
which expands to

.. code-block:: Lissp

(hissp.macros.._macro_.alias abbreviation qualifier)

The single-argument form
``hissp..alias#abbreviation`` aliases the bundled macro qualifier.

See `hissp.macros._macro_.alias`.
"""
return "hissp.macros.._macro_.alias", abbreviation, qualifier
1 change: 1 addition & 0 deletions src/hissp/macros.lissp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,7 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
;; or similarly constrained environments (e.g. embedded, readerless).
;; There, the first form should be ``(hissp.._macro_.prelude)``,
;; which is also implied in ``$ lissp -c`` commands.
;; (See the `hissp.prelude` shorthand for Lissp.)
;;
;; Larger projects with access to functional and macro libraries need not
;; use this prelude at all.
Expand Down
4 changes: 2 additions & 2 deletions tests/argv.lissp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;;; Copyright 2020 Matthew Egan Odendahl
;;; Copyright 2020, 2024 Matthew Egan Odendahl
;;; SPDX-License-Identifier: Apache-2.0
(hissp.._macro_.prelude)
hissp..prelude#:

(print sys..argv)
(print .#"f'{__name__=} {__package__=}'")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_macros.lissp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! Transpiler should ignore the shebang line!
;;; Copyright 2019, 2020 Matthew Egan Odendahl
;;; Copyright 2019, 2020, 2024 Matthew Egan Odendahl
;;; SPDX-License-Identifier: Apache-2.0

(hissp.._macro_.alias * hissp.._macro_)
hissp..alias#*

(*#define enlist
(lambda (: :* a) (list a)))
Expand Down
Loading