Racket – creare linguaggi – 1

l9

Si parte con un nuovo capitolo, copiano da qui: [doc]/guide/languages.html.

The macro facilities defined in the preceding chapter let a programmer define syntactic extensions to a language, but a macro is limited in two ways:

  • a macro cannot restrict the syntax available in its context or change the meaning of surrounding forms; and
  • a macro can extend the syntax of a language only within the parameters of the language’s lexical conventions, such as using parentheses to group the macro name with its subforms and using the core syntax of identifiers, keywords, and literals.

Note: The distinction between the reader and expander layer is introduced in Lists and Racket Syntax [[doc]/guide/Pairs__Lists__and_Racket_Syntax.html].

That is, a macro can only extend a language, and it can do so only at the expander layer. Racket offers additional facilities for defining a starting point of the expander layer, for extending the reader layer, for defining the starting point of the reader layer, and for packaging a reader and expander starting point into a conveniently named language.

Uhmmm… il Lisp era più semplice 😉
In ogni caso si parte qui: [doc]/guide/module-languages.html.

Linguaggi modulo

When using the longhand module form for writing modules, the module path that is specified after the new module’s name provides the initial imports for the module. Since the initial-import module determines even the most basic bindings that are available in a module’s body, such as require, the initial import can be called a module language.

The most common module languages are racket or racket/base, but you can define your own module language by defining a suitable module. For example, using provide subforms like all-from-out, except-out, and rename-out, you can add, remove, or rename bindings from racket to produce a module language that is a variant of racket:

Note: The module Form introduces the longhand module form.

c0

Collegamenti di form implicito

If you try to remove too much from racket in defining your own module language, then the resulting module will no longer work right as a module language:

c1

The #%module-begin form is an implicit form that wraps the body of a module. It must be provided by a module that is to be used as module language:

c2

The other implicit forms provided by racket/base are #%app for function calls, #%datum for literals, and #%top for identifiers that have no binding:

c3

Implicit forms such as #%app can be used explicitly in a module, but they exist mainly to allow a module language to restrict or change the meaning of implicit uses. For example, a lambda-calculus module language might restrict functions to a single argument, restrict function calls to supply a single argument, restrict the module body to a single expression, disallow literals, and treat unbound identifiers as uninterpreted symbols:

c4

Module languages rarely redefine #%app, #%datum, and #%top, but redefining #%module-begin is more frequently useful. For example, when using modules to construct descriptions of HTML pages where a description is exported from the module as page, an alternate #%module-begin can help eliminate provide and quasiquoting boilerplate, as in “html.rkt“:

c5

Using the “html.rkt” module language, a simple web page can be described without having to explicitly define or export page and starting in quasiquoted mode instead of expression mode:

c6

Uso di #lang s-exp

Implementing a language at the level of #lang is more complex than declaring a single module, because #lang lets programmers control several different facets of a language. The s-exp language, however, acts as a kind of meta-language for using a module language with the #lang shorthand:

#lang s-exp module-name
form ...

is the same as

(module name module-name
  form ...)

where name is derived from the source file containing the #lang program. The name s-exp is short for “S-expression,” which is a traditional name for Racket’s reader-level lexical conventions: parentheses, identifiers, numbers, double-quoted strings with certain backslash escapes, and so on.

Using #lang s-exp, the lady-with-the-spinning-head example from before can be written more compactly as:

c7

Later in this guide, Defining new #lang Languages [in [doc]/guide/hash-languages.html] explains how to define your own #lang language, but first we explain how you can write reader-level extensions to Racket.

:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

  • […] Oggi qui [doc]/guide/languages.html, in particolare qui [doc]/guide/module-languages.html dove continuo a copiare da qui. […]

  • […] a language that implements its own nested protocol. We have already seen one example (in Using [Uso di] #lang s-exp): the s-exp language allows a programmer to specify a module language using the general module path […]

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: