Chuyển đến nội dung

Bài 26 · Macros and Languages

Languages with #lang

See how Racket's #lang line selects a language, how a module language is built from provide and macros, and run a JavaScript stand-in for a tiny reader plus evaluator.

The first line of most Racket files is #lang racket (or #lang racket/base). That line is not a comment or a boilerplate import: it chooses the language the rest of the file is written in. Racket is often called "a language for making languages" because anyone can write a new #lang, with new syntax, new meaning, or both, and use it side by side with others.

This lesson explains what happens behind #lang: a reader that turns text into syntax, and a module language that gives the syntax meaning. You will see how a small language is defined, then build a stand-in with the same two pieces. You should know macros and modules (next chapter).

Racket itself is not available in our editor, so real #lang examples are read-only. The runnable JavaScript block is a stand-in that models a reader and a small evaluator.

What #lang does

When Racket sees #lang foo, it looks for the module foo/lang/reader, uses it to read the rest of the file, and wraps the result in a module whose language is foo. Two ingredients make a language:

  1. A reader: turns the characters of the file into a syntax object. For languages that use parentheses, the default reader is enough. For new surface syntax (infix math, Markdown-like text, Python-style indentation), you write your own.
  2. A module language: a module whose exports define what names and forms are available. It usually supplies a #%module-begin form that wraps the whole module and can transform it.
RACKET
#lang racket
(define (greet name) (string-append "Hello, " name "!"))
(greet "Ada")

Here the reader is Racket's standard one and the module language is racket. Replacing the first line with #lang typed/racket keeps the syntax but changes the meaning: functions can have type annotations and the checker runs before the program does. Other #langs ship with Racket: racket/base (minimal), scribble/manual (documentation), slideshow (presentations), and datalog.

A minimal language: reuse and restrict

The easiest language to build is a restricted Racket. A module language is just a module that re-exports what you want and defines #%module-begin:

RACKET
;; file: tiny/main.rkt
#lang racket/base
(provide (rename-out [tiny-module-begin #%module-begin])
         #%app #%datum #%top
         + - * / display)

(define-syntax-rule (tiny-module-begin body ...)
  (#%module-begin
   (displayln "tiny language v1")
   body ...))

Then #lang s-exp "tiny/main.rkt" (or a proper tiny/lang/reader.rkt for #lang tiny) gives a file where only these names exist: you cannot call define or lambda, because the language does not export them. This is how educational languages (such as Racket's own "student" languages) hide features from beginners.

The #%module-begin form is the hook for the whole file: it can add output, collect all top-level expressions, run checks or reorder things. The names #%app, #%datum and #%top control function application, literal values and unbound identifiers; redefining them gives a language different semantics (for example, one that prints every top-level value in color).

A custom reader

For different surface syntax you write a reader function. Suppose a language where each line is item: price and the program computes a total:

RACKET
#lang receipt
coffee: 3.50
bagel: 2.25
tip: 1.00

The reader parses the lines, producing a syntax object like (module receipt-mod receipt-lang (items ...)), and the module language defines how items is evaluated (sum and print). Racket supplies helpers in syntax/module-reader so you do not write the module wrapper by hand.

flowchart LR
  F["file text with #lang receipt"] --> R["reader: text to syntax"]
  R --> M["module using receipt module language"]
  M --> X["macro expander"]
  X --> P["compiled program"]

The #lang line chooses the reader and the module language that give the file its meaning.

Stand-in: a reader and a language

The JavaScript below keeps the same two-part structure. read turns the text of a "receipt" file into data; the language object supplies meaning by evaluating that data. Swap the language and the same text means something else.

JS Stand-in: reader plus module language

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The same three lines of data give a receipt under one language and a single answer under another. That is the whole idea behind #lang.

Languages in practice

Racket programmers use #lang for many things: domain-specific languages for build systems, document formats (Scribble), typed dialects, teaching languages, and even embedded languages for games and logic puzzles. A good rule: build a language when the problem has its own vocabulary and users who should not need to write general-purpose Racket. Start with a macro-based embedded DSL in a normal module; move to a full #lang with its own reader only when the syntax truly needs to look different.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does the line #lang racket do?

    The #lang line tells Racket which reader and module language to use for the rest of the file.
  2. 2. What are the two parts of a Racket language?

    The reader turns text into syntax; the module language provides the meaning through exported names and forms such as #%module-begin.
  3. 3. How can a language hide features from beginners?

    Only what the module language provides is in scope, so omitted forms like define or lambda simply do not exist.
  4. 4. What does #%module-begin control?

    It wraps every form in the module, so a language can add output, collect expressions, or transform the whole program.

Summary

  • #lang picks the reader and the module language for a file.
  • A restricted language is a module that re-exports a subset of Racket and defines #%module-begin.
  • A new surface syntax needs a custom reader that produces syntax objects.
  • Macros and modules are the tools, so everything in the previous lessons applies.
  • Start with an embedded DSL; build a full #lang only when the syntax needs it.

Next: Modules and provide

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.