From d9c8e65f8bf6219c169eddbfb9320a045f70d636 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 6 Aug 2021 22:10:51 -0400 Subject: Start expression syntax document --- docs/doc/expression.html | 172 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 docs/doc/expression.html (limited to 'docs/doc/expression.html') diff --git a/docs/doc/expression.html b/docs/doc/expression.html new file mode 100644 index 00000000..7597fe4c --- /dev/null +++ b/docs/doc/expression.html @@ -0,0 +1,172 @@ + + + + BQN: Expression syntax + + +

Expression syntax

+

BQN expressions are the part of syntax that describes computations to perform. Programs are mainly made up of expressions with a little organizing material like blocks and namespaces around them. This page explains how functions, modifiers, and assignment combine with their inputs. It doesn't describe constant and array literals, which each form a single subject for grammatical purposes.

+

The first tutorial also covers how to build and read BQN expressions.

+

Overview

+

BQN expressions consist of subjects, functions, and modifiers arranged in sequence, with parentheses to group parts into subexpressions. Assignment arrows and can also be present and mostly behave similar to functions. Functions can be applied to subjects or grouped into trains, while modifiers can be applied to subjects or functions. The most important kinds of application are:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
leftmainrightoutputnamebinding
w?FxSubjectFunctionRtL, looser
F?GHFunctionTrain
F_mFunction1-ModifierLtR, tighter
F_c_GFunction2-Modifier
+

The four roles (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's type doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed.

+

If you're comfortable reading BNF and want to understand things in more detail than described below, you might check the grammar specification as well.

+

Syntactic role

+

This issue is approached from a different angle in Context free grammar.

+

In APL, the way one part of an expression interacts with others is determined by its value. That means that to parse an expression, in general you would have to evaluate that part, get a value, check its type, and then figure out how it fits in with the rest of the expression. This is a lot of work. BQN changes things so that you can determine how to parse an expression just by looking at its source code. But because it still needs to support expressions that can evaluate to more than one possible type, BQN has to introduce a new and independent concept, called syntactic role, in order to support APL-like expressions.

+

Syntactic role is a property of an expression, not its value. To describe it in terms of English grammar, you might say "I like BQN", using "BQN" as an object, or "BQN scares me", using it as a subject. BQN itself isn't a subject or object, it's a programming language. Similarly you might write F g, placing f in a function role to apply it to g, or G f to use f as an argument. Maybe even in the same program, although it's unlikely.

+

Role spellings

+

The four roles are subject, function, 1-modifier, and 2-modifier, as shown in the table below. Each type has an associated role (with non-operation types all corresponding to subjects), and the value of an expression will often have a matching type, but it doesn't have to.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BQNNamesPrimitives
SubjectlowerCaseLiterals
FunctionUpperCase+-×÷⋆√⌊⌈|¬∧∨
1-modifier_leading˙˜˘¨⌜⁼´˝`
2-modifier_both_∘○⊸⟜⌾⊘◶⎉⚇⍟⎊
+

Primitive tokens, since they have a fixed value, always have a role that matches their type. They are functions, unless they fall into one of the two modifier patterns. 1-modifiers have superscript glyphs, and 2-modifiers have glyphs with an unbroken circle—that is, one without a line through it, excluding functions and .

+

Variable names can be written in any case and with underscores added, and these changes don't affect what identifier the name refers to. ab, aB, AB, and _a_B_ are all the same variable. However, the spelling—specifically the first and last characters—determine the variable's role. A lowercase first letter indicates a subject, and an uppercase first letter makes it a function. A leading underscore (regardless of the following character) indicates a 1-modifier, and both leading and trailing underscores makes a 2-modifier.

+

Besides these, character, string, and list literals always have a subject role, and the role of a block is determined by its type, which depends either on the header it has or which special variables it uses.

+

The role of a compound expression, formed by applying an operation to some inputs, depends on the operation applied. This system is discussed in the remaining sections below.

+

Kinds of application

+

Here is a table of the modifier and function application rules:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
leftmainrightoutputname
FxSubjectMonadic function
wFxSubjectDyadic function
FGFunction2-train
F*GHFunction3-train
F*_mFunction1-Modifier
F*_c_G*Function2-Modifier
_c_G*1-ModifierPartial application
F*_c_1-ModifierPartial application
+

A function with an asterisk indicates that a subject can also be used. Since the role doesn't exist after parting function and subject spellings are indistinguishable in these positions. Modifier applications bind more tightly than functions, and associate left-to-right while functions associate right-to-left.

-- cgit v1.2.3