jsoftware / jsource

J engine source mirror
Other
657 stars 90 forks source link

Expanded Multiple Assignment (strawman) #52

Open tangentstorm opened 3 years ago

tangentstorm commented 3 years ago

Let multiple assignment handle more cases: default values for missing ones; assignments at multiple levels of a boxed RH side; options for opening the box or not. Examples:

   'a [b c d]' =. 1;2 3 4
   a,b,c,d
1 2 3 4
   '<a> [b c d(99)]' =. 1;2 3
   a
+-+
|1|
+-=
   b,c,d
2 3 99

BNF description of the assignand:

 qname ::= any valid J name
           any valid J name?

 sname ::= .
           :
           qname
           qname.
           qname:

 name ::= sname
          sname ( default )

 default ::= any valid one-line J sentence

 item ::= < itemlistv >
          [ itemlistv ]

 items ::= name items
           item
           items itemlist

 itemlist ::= name
              items

 itemlistv ::= itemlist
               itemlist *

Multiple assignment is a sentence of the form

'itemlistv' =: value NB. or =.

If the rule for 'items' is matched, the words that match the rule are assigned from the corresponding items of the value. Any item that is enclosed in <> is assigned from the corresponding item; any other item is opened before it is assigned.

If the rule for 'items' is not matched, there must be a single name, and it is assigned with the entire value.

Multiple levels of assignment are called for by [] and <> . Each of these selects a single item of the current value and uses that item for assignments to the names within the [] or <> . Except when <> is used, one level of boxing is removed from the value before the assignment is performed.

If the rule for 'items' is matched, the number of items in the value must match the number of items specified by the pattern, except that

If the name ends with ?, the assignment is not performed if the name is already assigned.

If the name ends with . or : (possibly preceded by ?), the ending character overrides the namespace used for the assignment: . indicates private assignment, : indicates public assignment. If the name does not end with . or : the namespace is given by the type of copula used, =. or =: .

A name consisting of the single character . or : matches an item of the value but performs no assignment.

@HenryHRich -- 01:26, 2 July 2017 (UTC)