Skip to content

Define a mini-language in 15 minutes

Audience: anyone who has opened struktJS once and wants to see how a language is made · ~15 min · Prerequisites: Install, a project open (File > Open Project…)

You will not write a language from nothing. You start from the Calculator card of the New Language wizard, which is a small but complete language, use it, break its one check on purpose, and change a rule. By the end you have seen every part a language has: concepts, how they look, how they are checked, and what they do when run. The guided tour teaches the editor and its "Design a language" part builds an empty Shapes language; this page is the shorter route through a finished one.

How this page was verified

The steps follow the Calculator template (plugins/lang-templates/languages/expression-dsl.js) and the Language Editor page. The menu paths are checked by scripts/docs-lint-prose.mjs. The page was not clicked through in a running IDE; if a step does not match what you see, the Language Editor page is the reference and a report is welcome.

1. Create the language (3 min)

  1. In the Explorer right-click Languages and choose New Language….
  2. Type Money as the Language name and pick the Calculator card. Press Create language.
  3. An example model opens at once, written in your new language. It is called Invoice:

    calc Invoice
        let net = 100
        let rate = 19
        let tax = net * rate / 100
        show net + tax
        show net / 0
    

The language is stored in the project folder as Money.lang.js. Nothing else is installed: the model you see is written in a language that exists only in your project.

2. Use it (4 min)

  • The last line is underlined: Division by zero. Hover it to read the message. That is the language's own check, not a built-in one.
  • Run the model with Run > Execute > Run Program. The first show prints 119; the second stops with the same message.
  • Click the 0 after net / and type 4. The underline goes away, and a run now prints 119 and 25.
  • With the caret at the end of the last line press Enter. A new statement opens below; type show, press Space, and complete a variable name with Ctrl+Space.
  • Select the net in show net + tax, press End, and type *. The cell becomes net * _: the operator key of the concept that adds * is declared on the concept, not in a table in the editor. Fill the right-hand side with 2.

You are editing a tree, not text: every cell you can click is a slot of a concept. The text file on disk is ordinary source text (see Projects and models).

3. Look inside (4 min)

Open the language in the Explorer: Languages, Money, Concepts. The Calculator has nine concepts:

Concept Role
calcFile The root: calc <name> and its statements
calcLet, calcShow Statements: give a value a name, print a value
calcNum, calcRef Expressions: a number, the value of a let name
calcAdd, calcSub, calcMul, calcDiv Binary operators, each with an operator key and a precedence

Click calcDiv. Its tabs are the parts of a language, each a view of the same concept (see The tab strip):

  • Structure: two slots, left and right, both expressions. This is what the concept is.
  • Appearance: the cells that draw it: left, /, right.
  • Rules: the division-by-zero check, severity error.
  • Running: what it returns when the model runs: the left value divided by the right, and the error that stops the second show above.

4. Change a rule (4 min)

  1. On calcDiv open the Rules tab and the division-by-zero rule.
  2. Click the value of severity, clear it, and choose warning from the completion (Ctrl+Space).
  3. Go back to the Invoice model and type 0 again after net /. The problem is now a warning: drawn with a different shape from an error, not only a different colour (see Problems and checks).
  4. Press Run Program again. The warning does not stop the run; the evaluator's own Division by zero error from the Running tab still does.

That is the whole loop of language work: change a concept in the Language Editor, switch to a model, see the change at once.

Where to go next