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)¶
- In the Explorer right-click Languages and choose New Language….
- Type
Moneyas the Language name and pick the Calculator card. Press Create language. -
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
showprints119; the second stops with the same message. - Click the
0afternet /and type4. The underline goes away, and a run now prints119and25. - 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
netinshow net + tax, press End, and type*. The cell becomesnet * _: 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 with2.
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,
leftandright, both expressions. This is what the concept is. - Appearance: the cells that draw it:
left,/,right. - Rules: the
division-by-zerocheck, severityerror. - Running: what it returns when the model runs: the left value divided by the right, and the error that stops the second
showabove.
4. Change a rule (4 min)¶
- On
calcDivopen the Rules tab and thedivision-by-zerorule. - Click the value of
severity, clear it, and choosewarningfrom the completion (Ctrl+Space). - Go back to the
Invoicemodel and type0again afternet /. The problem is now a warning: drawn with a different shape from an error, not only a different colour (see Problems and checks). - Press Run Program again. The warning does not stop the run; the evaluator's own
Division by zeroerror 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¶
- Start empty instead: the Blank card, or follow the nine steps of Help > Design a Language (Tutorial) in the guided tour.
- Add a concept of your own: right-click the Concepts folder and choose Add new concept (Make a language).
- Say how a concept prints and reads in files: the Text syntax tab. Turn it into another language: Generators and Open Preview.
- Constraints, scopes and types: Constraints and placement rules, Type checking.
- Ship it to others: Import and export languages.