From 25311370e215cd12018dde8bd162583dbe36d473 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Mon, 15 Nov 2021 22:16:46 -0500 Subject: Compress expression section in syntax summary, moving details to expression page --- docs/doc/expression.html | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'docs/doc/expression.html') diff --git a/docs/doc/expression.html b/docs/doc/expression.html index 9e386079..8f9cc5cc 100644 --- a/docs/doc/expression.html +++ b/docs/doc/expression.html @@ -62,6 +62,13 @@

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.

+

Below, the function {𝕎𝕩} treats its left argument 𝕎 as a function and its right argument 𝕩 as a subject. With a list of functions, we can make a table of the square and square root of a few numbers:

+↗️
    ט, {𝕎𝕩} 149
+┌─         
+╵ 1 16 81  
+  1  2  3  
+          ┘
+

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.

@@ -180,3 +187,26 @@

A function with an asterisk indicates that a subject can also be used. Since the role doesn't exist after parsing, 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.

+

Assignment

+

Another element that can be included in expressions is assignment, which is written with to define (also called "declare" in many other languages) a variable and to change its definition. A variable can only be defined once within a scope, and can only be changed if it has already been defined. However, it can be shadowed, meaning that it is defined again in an inner scope even though it has a definition in an outer scope already.

+↗️
    x1  {x2  x3  x}
+3
+    x
+1
+
+

Assignment can be used inline in an expression, and its result is always the value being assigned. The role of the identifier used must match the value being assigned.

+↗️
    2×a(Neg-)3
+¯6
+    a
+¯3
+
+

Exports

+

The double arrow is used to export variables from a block or program, causing the result to be a namespace. There are two ways to export variables. First, in the variable definition can be replaced with to export the variable as it's defined. Second, an export statement consisting of an assignment target followed by with nothing to the right exports the variables in the assignment target and does nothing else. Export statements can be placed anywhere in the relevant program or body, including before declaration or on the last line, and a given variable can be exported any number of times.

+
aliasa, b, c0c1c, b2b{
+  bc   # Non-definition exports can go anywhere
+  a2    # Define and export
+  b1+a
+  cb"str"
+}
+
+

Fields of the resulting namespace can be accessed either directly using namespace.field syntax, or with a destructuring assignment as shown above. This assignment's target is a list where each element specifies one of the names exported by the block and what it should be assigned to. The element can be either a single name (such as b above), which gives both, or a combination of the assignment target, then , then a name. If is never used, the names can be given as a strand with . To use for aliases, bracket syntax ⟨⟩ is needed. Imported names can be repeated and can be spelled with any role (the role is ignored).

-- cgit v1.2.3