NB. aatrace
cocurrent 'jtrace'
Notes=: 0 : 0
Developmental version as of: 3 November 2010
Invitation for:
- Comments on structural details Comments on format options Tests of a variety of statements Reports of problematic depictions Offers of alternative actions Code for specific changes
Study:
- Other orientations Other connection schemes--sides, "diagonal", ... Other functional categorizations
Intended work:
- Modularize code Regularize variable names
Hopes:
- Installation in distributed trace.ijs Adoption in 5!:n verb series Display in grid for editing and re-constitution Devising mnemonic graphic icons for action symbols
)
help=: Note 0 Programmers new to J soon find that liberal use of parentheses is good insurance against misunderstandings of the order of execution, but then find that managing large numbers of parentheses is also challenging. Programmers exploring advanced features of J can also experience unexpected results or error messages from symbol sequences which seem plausibly appropriate.
The trace workspace offers tools to assist with understanding, at three different levels of complexity, the behavior of a puzzling expression in J:
-> pyr y elevates parenthesized sub-expressions in a pyramidal format,
- and reports on imbalance.
Examples:
- pyr 'fn((+/)i.4)' (+/)
( i.4)
fn _
- pyr '(3 4$i.12)+/ .**:i.4'
(3 4$i.12)
+/ .**:i.4
- pyr '(a+b)*c)-d)'
(a+b)
_ *c)-d)
*Unbalanced
-> paren y fully parenthesizes an expression to show clearly which
- symbols are bracketed together syntactically. (Requires balanced parentheses.)
Examples:
- paren '+/i.4'
((+/)(i.4))
- paren '(3 4$i.12)+/ .**:i.4'
((3 4$(i.12))((+/) .*)(*:(i.4)))
-> parenpyr y fully parenthesizes an expression and elevates inner
- sub-expressions in an expanded pyramidal format.
Examples:
- parenpyr '+/i.4'
1 (+ /) (i. 4) 0 ( _ )
- parenpyr '(3 4$i.12)+/ .**:i.4'
2 (i. 12) (+ /) (i. 4) 1 (3 4 $ _ ) ( _ . *) (*: ) 0 ( _ _ )
-> flowtree y produces a diagram showing the relationships among each
- x flowtree y argument, operation, and intermediate result in a single
- expression, as associated by the rules of syntax; options may be provided in x.*
Example:
- NB. Piece of cross product of vector pair (v0,: v1) using Fork verb trains flowtree '((1: |. {.)* (_1: |. {:))- ((_1: |. {.)* (1: |. {:)) v0,: v1'
- v0 v1
- └,:─┘
- v0 v1
│ │ └──┬F─┬─F─┬─F─┬─F──┬─F─┬─F─┬F─┬┘
- 1: {. _1: {: _1: {. 1: {:
- └┬─┘ └─┬─┘ └─┬─┘ └┬─┘ |. |. |. |.
- └───┬───┘ └──┬───┘
- * └───────┬───────┘
- - │
- * └───────┬───────┘
- └───┬───┘ └──┬───┘
- └┬─┘ └─┬─┘ └─┬─┘ └┬─┘ |. |. |. |.
-> x trace y lists the arguments and result of each operation as it is
- trace y performed, to x (or _) levels of function calls, showing
- rank, shape, type, and value. Each action type is labeled according to the parse table in Section II.E of the J dictionary.
Example:
- trace '3+i.4'
1 Monad
- 4 0 1 2 3
2 Dyad
- 3 + 0 1 2 3 3 4 5 6 ==============================
3 4 5 6
Remember to double any apostrophes within the phrase. As with many aspects of program development and testing, it is usually wise to avoid using these tools on voluminous amounts of data or numbers of repetitions until simpler cases have been proved.
* Options being considered:
- Size limit on literal values displayed Size limit on diagram produced Switch for display of partial diagram after each parsing stage Switches for minor style variations Switch for direction(s) of flow
)
flownote=: 0 : 0 Comments on flowtree
The flowtree presentation differs in orientation and operation placement from the syntactic diagrams of system function 5!:4 . Unlike them, it will show the steps of an evaluation even when all operands are provided.
The resulting diagram will be most readable when using box-drawing characters (set by Toggle ASCII Box Drawing in the Tools menu, or 9!:7). The choice among fixed-width fonts can also affect readability.
The most relevant diagram will be produced only when any names in the analyzed expression have current globally defined meanings suitable to its execution in, or accessible from, locale jtrace.
The tree depicts a generally downward flow of information (data). An arithmetic formula may be read from left to right, interpreted in J from right to left, and listed from innermost to outermost in the tree form. Where sub-expressions evaluate independently, they appear in their original relative left-right relationship, but perhaps at different levels.
People familiar with machine-register loading or "reverse-Polish" notations may not find this view too unsettling. It is also used in "data-pipeline" programming, where processing of a succession of records (or atoms or rows or layers) is described as a funneling of the data to workstations or agents which perform their individual tasks and pass the results down the pipe. A flow can be split by copying the data to an alternate stream, as performed by Hook or Fork in J. Separate streams may join, as with dyadic verbs in J. A holding tank or buffer, collecting a whole stream before releasing any results, is effected in J by infinite-rank operations such as Sort, At, and the &.: version of Under. Some selections, like Infix, and finite-rank applications perform partial bufferings at the relevant levels of data aggregation.
In attempting to depict operand flow, a verb is enclosed within connecting input and output bars to any adverb, rank specifier, or other conjunction which has full control over both. A Hook or Fork train is capped with a common bus for distributing external arguments to the internal verbs. A verb formed with Bond (&) is marked with an R as a reminder that a "third argument" is possible, serving as a Repetition factor (or "power"), able to cause mysterious errors. A verb formed with Compose (also &) is marked with a C to call attention to the second-argument juggling which is shown there.
These syntactic diagrams give no depiction of the type, amount, or structure of the data which may be processed. They do not obviate the need to understand the meanings of each symbol or named object, especially the argument-delivery actions of adverbs and conjunctions. What it can help with is understanding whether a verb's monadic or dyadic meaning is being called for, and where its argument(s) is(are) coming from. Once a likely flow structure has been designed, then a trace report can show what results are actually created at each "station" in the pipe network.
In any programming language, the interpretation and execution of any statement or program is dependent on the context in which it is defined and executed. It can give useful results only for particular ranges and combinations of the data with which it works, whether built in as literal values, stored as external variables, or supplied as arguments. The actions of called subroutines will vary with the nature of the libraries from which they are pulled.
The fully dynamic interpretive nature of J provides nearly unlimited opportunities for misinterpretation leading to error conditions, lack of results, or--in the worst case--plausible but incorrect results. To analyze a J statement or program accurately, it must be performed in a context as close as possible to that in which is to be used, dependent on:
--loaded scripts --current locale --current search path --libraries available for searching --system parameters modifiable via '!:' --global names defined or redefined by prior execution --local names defined in active functions
All(!) of these conditions are dynamically modifiable in J. Most strikingly, the syntactic structure of a statement can vary as the names appearing in it are redefined as nouns, verbs, adverbs, or conjunctions. An appropriate parse diagram may be obtainable only by inserting a parsing call for a statement in its context, just ahead of, or in place of, the statement itself. The diagram may be written to the screen, to a noun, or to a file, replacing or appending to previous values.
This wide variability makes it more important than in some other languages to include tests for the suitability of arguments and other values, especially since J's built-in operations will return some conceivably useful result whenever the combination of arguments permits.
Execution
Because the syntactic nature of a name can change during a computation, a statement being parsed has to be executed as the report is being generated. To minimize side effects, it is performed in locale jtrace. Because a subexpression may be executed repeatedly within each containing expression, one which contains an assignment may store varying values on successive rounds, leading to results different from what would have been a single execution, and possibly a different parsing structure. (Flowtree executes each verb only once, but beware of successive invocations.) Any global assignment will leave a lasting record in locale jtrace or some explicitly assigned locale. Analyzing a statement which should take forever can still produce some useful result, by activating the output statement at the end of 'connect', to report on intermediate sub-expressions in the statement. )
NB.
NB. Table of J-parsing rules NB. Boxed columns to ue in processing four top values on stack: NB. class pattern, selected positions, rule identifier
(x)=: 2^i.#x=. ;:'noun verb adv conj lpar rpar asgn name mark' any=: _1 avn=: adv + verb + noun cavn=: conj + adv + verb + noun edge=: mark + asgn + lpar
x=. ,: (edge, verb, noun, any ); 0 1 1 0; '0 Monad' x=. x, ((edge+avn), verb, verb, noun ); 0 0 1 1; '1 Monad' x=. x, ((edge+avn), noun, verb, noun ); 0 1 1 1; '2 Dyad' x=. x, ((edge+avn), (verb+noun),adv, any ); 0 1 1 0; '3 Adverb' x=. x, ((edge+avn), (verb+noun),conj, verb+noun); 0 1 1 1; '4 Conj' x=. x, ((edge+avn), (verb+noun),verb, verb ); 0 1 1 1; '5 Trident' x=. x, (edge, cavn, cavn, any ); 0 1 1 0; '6 Bident' x=. x, ((name+noun),asgn, cavn, any ); 1 1 1 0; '7 Is' x=. x, (lpar, cavn, rpar, any ); 1 1 1 0; '8 Paren'
PTpatterns=: > 0{"1 x NB. parse table - patterns PTsubj=: > 1{"1 x NB. "subject to" masks PTactions=: 2{"1 x NB. actions
bwand=: 17 b. NB. bitwise and
prespace=: , ~ e.&'.:'@ {. $ ' '"_ NB. preface a space to a word beginning with . or :
isname=: ({: e. '.:'"_) < {. e. (a.{~ , (i. 26)+/ 65 97)"_ NB. 1 iff a string y from the result of ;: is a name
class=: 3 : 0 NB. the class of the word represented by string y
- if. y-:mark do. mark return. end. if. isname y do. name return. end.
if. 10>i=. (;:'=: =. ( ) m n u v x y')i.<y do.
- i{asgn,asgn,lpar,rpar,6#name return.
(4!:0 <'x' [ ".'x=. ',y){noun,adv,conj,verb
)
show=: 3 : 0
if. (depth>:indent) *. equals *: y-:30$'-' do.
- equals=: y-: 30$'='
smoutput (indent$' '),"1 ] 1 1}._1 _1}.": <y
- equals=: y-: 30$'='
)
encall1=: '('"_ , ] , ' call"'"_ , ] , ')'"_ encall=: encall1&.>^:(isname&> *. 3: = 4!:0)"0 NB. replace function names f in words y by (f call"f)
call=: 1 : 0 NB. for tracing function calls
(5!:5 <u=. 5!:5 <'u') call1 <y : (5!:5 <u=. 5!:5 <'u') call1 x;<y
)
call1=: 4 : 0 NB. call function x on argument(s) y
- 't_x t_y'=. _2{.y
indent=: >:indent show 30$'-' if. 2=#y do. show 5!:5 <'t_x' end. show x show 5!:5 <'t_y' ". 't_z=. ',((2=#y)#'t_x '),'(',(encall&.;: x),') t_y' show 5!:5 <'t_z' show 30$'=' indent=: <:indent t_z
)
NB.
executep=: 4 : 0 NB. execute rule x for stack y for "paren"
- t_b=. x{PTsubj t_x=. t_b # , 4 _1{.y select. x
- case. 0;1;2;5 do.
- t_c=. noun [ t_x=. '(',(;:^:_1 t_x),')'
t_x=. (<'=:') 1}t_x
- ". 't_z=. ',t_x=. '(',(;:^:_1 t_x),')'
t_c=. (4!:0 <'t_z'){noun,adv,conj,verb
t_c=. >1{t_b#,4 1{.y [ t_x=. >1{t_x
- case. 0;1;2;5 do.
)
executet=: 4 : 0 NB. execute rule x for stack y for "trace"
- t_b=. x{PTsubj t_x=. t_b # , 4 _1{.y
show 30{.(15$'-'),' ',(>x{PTactions),' ',15$'-' show&> t_x if. 7 =x do. t_x=. (<'=:') 1}t_x end. if. 2>:x do. t_x=. (encall&.;:&.>_2{t_x) _2}t_x end. if. 6>:x do. t_x=. (<'( '),&.>t_x,&.><' )' end. ". 't_z=. ', ; t_x t_c=. (4!:0 <'t_z'){noun,adv,conj,verb if. noun=t_c do.
t_z=. 5!:5 <'t_z' [ show t_z
t_z=. show 5!:5 <'t_z'
)
NB. ========================================================= aid=: 0 : 0
- 13!:0 ] 1 load '~user/aatrace.ijs'
13!:1 13!:5 top; mid; bot; (< vtall; vmtall; vmball; vball); (toff, moff, boff) vt; vtall; vm; vmtall; vmball; vb; < vball flowtree 'm=. *+ 3' [ m_jtrace_=: 5 flowtree '1 2 3+"0 (5)' flowtree '2 +/ \ 4 7 9'
flowtree '(] +. _1&|.)@ ((+/ \)@ isang)'
- │
- isang ┌@┘
┌┌─┌┐─┌┌─┌┐─┌┴─┴┐ │ + │─│ / │─│ \ │ └─└─┘ └─└─┘ └─┬─┘
- ┌@┘
- └┬F─────┬┘
- │ R│ │ _1 │
│ └&┐ │ │ └┬┘ ] |. └──┬──┘
- +.
- │
- +.
] xl_jtrace_=: 4# < 3# < 'xyz' _ _ flowtree '> (,each)/"2 (1 2 0 3&{"1 each@ (((<<') &[) ,"1 each 0&{"1 (,"0 1 each"1) (1&{"1) ,.each ( {.each@ (2&{"1)))) > (3= > #each xl)# xl' flowtree '+`*@.1'
- │ │
- │ R│ │ _1 │
┌┌─┌┐ ┌┴─┴┐ │+ *│─│ 1│ │└`┘│─│@.┘│ └─└─┘ └─┬─┘
- │
45 80 flowtree '(xx; (((5 50&<.@ (_2&{.@ (1 1&,@ $))) {. ]) > ;._2 (5!:5 <yy), 10{ a.); ($ yy)), Tr_base_' flowtree 'M (({:@] * {.@] { [)({.@])[}) 1 10' oneyr_jtrace_=: (] , ((shift&(-~)@ (_1&{.@ (1&{.))) , ((shift* 1- trate)&*@ (_1&{.)))@ (_1&{.)) flowtree '2 (] , ((shift&(-~)@ (_1&{.@ (1&{.))) , ((shift* 1- trate)&*@ (_1&{.)))@ (_1&{.)) 100000' oneyr 2 2$ 100000
flowtree '((2+ spendcodes i."1 aCERPSU,: bjl)&[@]) (((2&{.@ ]) ,"1 ([ ((+/@ ({.@ ])) ,"1 0 1 ({:@ ])) ([ {"1 ])) ,"1 ([ ((+/@ ({.@ ])) ,"1 0 1 ({:@ ])) ((spn&+@ [) {"1 ])))) ]'
)
NB. ========================================================= x=. ,: (edge, verb, noun, any ); 0 1 1 0; '0 Monad' x=. x, ((edge+avn), verb, verb, noun ); 0 0 1 1; '1 Monad' x=. x, ((edge+avn), noun, verb, noun ); 0 1 1 1; '2 Dyad' x=. x, ((edge+avn), (verb+noun),adv, any ); 0 1 1 0; '3 Adverb' x=. x, ((edge+avn), verb, conj, verb ); 0 1 1 1; '4 ConjVV' x=. x, ((edge+avn), verb, conj, noun ); 0 1 1 1; '5 ConjVN' x=. x, ((edge+avn), noun, conj, verb ); 0 1 1 1; '6 ConjNV' x=. x, ((edge+avn), noun, conj, noun ); 0 1 1 1; '7 ConjNN' x=. x, ((edge+avn), verb, verb, verb ); 0 1 1 1; '8 Fork' x=. x, ((edge+avn), noun, verb, verb ); 0 1 1 1; '9 Trident' x=. x, (edge, cavn, cavn, any ); 0 1 1 0; '10 Bident' x=. x, ((name+noun),asgn, cavn, any ); 1 1 1 0; '11 Is' x=. x, (lpar, cavn, rpar, any ); 1 1 1 0; '12 Paren'
PTApatterns=: > 0{"1 x NB. *augmented* parse table - patterns PTAsubj=: > 1{"1 x NB. "subject to" masks PTAactions=: 2{"1 x NB. actions
NB. Diagrammatic symbols BC=: ' ', 9 6 1 8 10{ 9!:6 HBC=: 0 1 2 3 4 5 6 7 8 10{ 9!:6 VBC=: 0 1 2 3 4 5 6 7 8 9{ 9!:6 BCM=: (5 5$ 1 0 2 0 3 0 0 10 0 0 4 11 5 11 6 0 0 10 0 0 7 0 8 0 9){ ' ', 9!:6 lkix=: ((1 2; 1 0; 1 2), (4 0; 0 2; 4 4),: 1 0; 1 2; 1 0) Argsplit=: (4 0; 0 2; 4 4){ BCM
wid=: _1&{@ (1&,@ $) tomat=: (((_2&{.@ ((2&#@ (*@ (*/@ ]))) , ]) ($@ ]))) $ ,@ ]) botrow=: ({:@ tomat) toprow=: ({.@ tomat) vbarpos=: (I.@ ((_1{ VBC)&=@ ]))"1 catenaries=: ;: ', ,. ,: ; Catenate Stitch Laminate Link'
midpos=: 3 : 0
- NB. Reporting rounded-up midway index for length y
(<.@ (%&2@ ])) y : NB. Reporting midway index rounded from length y adjusted by x x (<.@ (%&2@ ([ + ]))) y
)
trimval=: 4 : 0
- NB. Trim character matrix y to size x, with '...' annotation
x=. 4>. x [ y =. tomat y xs=. ($ y)- x if. 0< 0{ xs do.
- y=. ((- 1+ 0{ xs)}. y), '...'
if. 0< 1{ xs do.
- y=. |: (|: (- 1+ 1{ xs)}."1 y), '...'
)
nobar=: (((1&|.@ (_1 0&,@ (((9{ 9!:6 )&=@ ({."1))# i.@ #))) {"2 ])@ ":) propup=: 4 : 0 ((0>. x- # y)# (1{. y)), y ) topconn=: 3 : 0 if. 0< # vbarpos toprow y do. y return. end. ((- 1+ x midpos , # {. y){. , _1{ VBC), y ) botconn=: 3 : 0 if. 0< # vbarpos botrow y do. y return. end. y, (- 1+ x midpos , # {. y){. , _1{ VBC ) NB. Index of component containing right- or left-most entry (vertical bar) lastvp=: _1&{@ (I.@ (*@ (>@ (#each@ ])))) firstvp=: 0&{@ (I.@ (*@ (>@ (#each@ ])))) endvp=: [ { (I.@ (*@ (>@ (#each@ ])))) NB. 0 or _1 and position list NB. Table of joints and extensions: left, right, up, down (size 3 9) bjoint=: <"0 , 0 2 4{ 0 2 4{"1 BCM tr=. |.each t [ t=. (] , |.@])(< 1 1), (< 2 1), (< 2 1) bjoint=: bjoint,: , |: (,each/)"1 (<"1 (3 2$ t)) ,"0/ <"1 (3 2$ tr) bjoint=: bjoint, , 0 2 4 (<@,"0/) 0 2 4 corner=: 4 : 0 'jnt pos'=. x [ y=. 1>. <. 2$ (, y), 1 if. 9<: typ=. (0{ bjoint)i. < jnt do. 'Unknown joint: ', jnt return. end. jnt=. (< 2; typ){ bjoint [ pos=. 0>. (y- 1)<. 2$ (, pos), 0 0 lrudw=. 1 3 0 2{ | pos, (y- 1)- pos hfl=. 0 1{ > (< 1; typ){ bjoint [ vfl=. 2 3{ > (< 1; typ){ bjoint zh=. BCM{~ jnt, ((1{ lrudw)# (1{ hfl)), (0{ lrudw)# (0{ hfl) zv=. |: tomat }. BCM{~ jnt, ((3{ lrudw)# (1{ vfl)), (2{ lrudw)# (0{ vfl) (- pos)|. zh, zv ) widentop=: 4 : 0 top=. (< ((0= loc)#"1 > loc{ top),"1 ((amt)# ' '),"1 loc#"1 > loc{ top) loc} top wt=. (wid@ >) top tspan=. ({: - {.) vtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt top; tspan; wt; < vtall ) NB. wrapmod=: 4 : 0 mcw=. wid mc=. > 1{ mid [ modm=. > modi{ mid mid=. (modm; mc) (modi, 1)} mid [ vt1=. {. (> modi{ vt), 0 NB. 'Stop' 13!:8 ] 91 mc=. mc,"1 (((< 4 4){ BCM); <. 0.5* # mc) corner (# mc), 1 mc=. (( vb1=. ((2> mcw)*. 3> > _1{ wt)>. vt1- mcw)# ' '),"1 mc mc=. ": < (((0>. (_1+ wid mc)- vt1)# ' '),"1 }. > modi{ top), mc mc=. ((((< 4 0){ BCM); <. 0.5* # mc) corner (# mc), 1),"1 mc mc=. (( vb1=. vt1)# ' '),"1 mc mc=. ": < mc,"1 (3> wid mc)# ' ' [ mc=. ((}. > modi{ top), mc) mid=. |. mid [ vmt=. |. vmt [ vmb=. |. vmb mc=. (( vb1=. 2> mcw)# ' '),"1 mc mc=. ": < mc,"1 ((3> wid mc)# ' ') vmb=. (< 1+ vb1+ (modi= 0)+ <. 0.5* mcw) 1} vmb mc=. ((2 4; (3= # mc){ 2 4; 0 2){ BCM) (< _2 _3; < 0)} mc mid=. (< mc) 1} mid [ vmt=. (< 1+ > vt1) 1} vmt NB. Attaching centered split input leads vmb=. (<
) 0 _1} vmb vmt=. (< , _1 1+"1 0 (<. 0.5* wid mc)) 1} vmt
elseif. modi= 0 do. NB. Left bond
elseif. 1 do. NB. Adverb
end.
mc=. ((< 4 2){ BCM) (< 0; > 1{ vmt)} mc mc=. (((< 1 2){ BCM) (> 1{ vmt)} (wid mc)# ' '), mc mc=. ((< 0 2){ BCM) (< _1; > 1{ vmb)} mc vm=. (~.@ (/: { ]))each vmt,each vmb
hm=. >./ , > #each mid [ wm=. (wid@ >) mid [ mid=. (< mc) 1} mid
top=. (1 0$ ); (1 3$ 1 0 1{ ' ', (< 1 2){ BCM); < 1 0$ bot=. (1 0$ ); (1 1$ (< 1 2){ BCM); < 1 0$ [ ht=. hb=. 1 vt=. ; 0 2; < [ vb=. ; 0; < wt=. (wid@ >) top [ wb=. (wid@ >) bot vtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt vball=. > ; vb+each 0, (0{ wb), +/ 0 1{ wb NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm vmtall=. > ; vmt+each 0, (0{ wm), +/ 0 1{ wm vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm
NB. 'Stop' 13!:8 ] 92
- top; mid; bot; wt; wm; wb; ht; hm; hb; vt; vm; vmt; vmb; vb; vtall; vmtall; vmball; vball
)
wrapio=: 4 : 0
- NB. Preparing verb for special argument delivery (via adverb, rank, etc.) NB. Returns mid; vmt; vmb 'mi mid'=. y [ 'vmt vmb'=. x
wx=. wid mx [ mx=. > mi{ mid vmti=. > mi{ vmt [ vmbi=. > mi{ vmb NB. mx=. (- *./ ({: mx)e. ' ', (< 1 2){ BCM)}. mx if. (wx= 1)+. 0 e. vmti do.
- mx=. ' ',. mx [ vmti=. 1+ vmti [ vmbi=. 1+ vmbi
- NB. Indices extending 1 character outside input columns
vmtis=. (({.@ ]) ([ + _1&[@] + i.@ (3&[@] + ] - [)) ({:@ ])) vmti vmtis=. (vmtis e. i. wx)# vmtis
- vmtis=. i. wx
barslim=. ([ * +)/ *./"1 ] 1,"1 (vmtis{"1 mx) e. ' ', (< 1 2){ BCM if. (0< barslim)*. 0< # vmti do.
mx=. ' ' ((i. barslim)(<@,)"0"1 0 vmti)} mx
NB. 'Stop' 13!:8 ] 103
boxbot=. if. boxpos> barslim do.
- barpos=. barslim mx=. ' ', mx mx0=. barpos{ mx NB. Insure split inputs, if any
if. 2> # vmti do.
vmt=. (< vmti=. , _1 1+"1 0 (1>. vmti)) mi} vmt
NB. Bend plain inputs & output toward right side vcon=. vmti NB. Indices of first input and to right vmtie=. ({. vmti)(([ <: ]) # ]) i. wx mx0=. ((< 2 1){ BCM) vmtie} mx0
- barpos=. boxpos
NB. Bending Hook, Fork, or box inputs & output to right side mx0=. barpos{ mx mxbar=. vmtis{ mx0 NB. Connection positions vcon=. ((] e. vmtis&[) # ]) I. mx0 e. (1 2; 0 0; 0 2; 0 4; 4 2){ BCM if. +./ 'FH' e. mxbar do.
vmtie=. ({. vcon)(([ <: ]) # ]) i. wx vmtie=. 'FH'((-.@ ((] { mx0&[@]) e. [)) # ]) vmtie mx0=. ' ' (((] < ({. vcon)&[@]) # ]) vmtis)} mx0 mx0=. ((< 2 1){ BCM) vmtie} mx0
vcon=. ((] e. vmtis&[) # ]) I. mx0 e. (< 4 2){ BCM boxbot=. {: mx mx=. }: mx
- barpos=. barslim mx=. ' ', mx mx0=. barpos{ mx NB. Insure split inputs, if any
NB. 'Stop' 13!:8 ] 104
mx0=. ((< 0 0){ BCM) vcon} mx0 mxn=. wx{. (vmbi{. boxbot), ((< 4 0){ BCM), wx# ((< 2 1){ BCM) if. # boxbot do.
- mx=. }:"1 mx [ mx0=. }:"1 mx0 [ mxn=. }:"1 mxn
if. 2< (# mx)- barpos do.
mxr=. (barpos# ' '), ((< 0 4){ BCM) mxr=. mxr, (((# mx)- barpos+ 3)# ((< 1 2){ BCM)), (4 0; 0 0; 4 4){ BCM
- mxr=. (barpos# ' '), (2 1; 0 0; 4 4){ BCM
mx=. ((mx0 (< barpos; < < )} mx), mxn),. mxr mx=. (((-. *./ ' '= {. mx), 1)$ ' '), mx
hm=. >./ > #each mid [ wm=. (wid@ >) mid [ mid=. (< mx) mi} mid vmt=. (< ) mi} vmt [ vmb=. (< ) mi} vmb
NB. 'Stop' 13!:8 ] 105
mid; vmt; vmb; hm; < wm
)
spread=: 4 : 0
- NB. Initial arrangement of tokens in display NB. y is two or three pieces of formula. x is up/down arrangement updn=. x NB. Shaping values with attached vertical connectors
'yl yc yr'=. y=. (tomat@ ":)each 3{. y, (< 1 0$ ) NB. Copying values into partitions of top, middle, bottom; left, center, right
'tl tc tr '=. (_1= updn){"0 1 (< 1 0$
),. yl; yc; yr 'ml mc mr '=. ( 0= updn){"0 1 (< 1 0$ ),. yl; yc; yr 'bl bc br '=. ( 1= updn){"0 1 (< 1 0$ ),. yl; yc; yr top=. tl; tc; tr [ mid=. ml; mc; mr [ bot=. bl; bc; br NB. 'top mid bot'=. <"1 (yl; yc; yr) (0 1 2 (<@,)"0 updn+ 1)} 3 3$ < 1 0$ wt=. (wid@ >) top [ wm=. (wid@ >) mid [ wb=. (wid@ >) bot NB. Recording connection columns and extending to common heights vt=. (vbarpos"1@ botrow)each top [ vb=. (vbarpos"1@ toprow)each bot vtt=. (vbarpos"1@ toprow)each top vmt=. (vbarpos"1@ toprow)each mid [ vmb=. (vbarpos"1@ botrow)each mid vm=. (~.@ (/: { ]))each vmt,each vmbvtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt if. 0= # vtall do. NB. Defaulting attachment to right edge
top=. (2{. top), < 2 1$ (< 1 2){ BCM wt=. (wid@ >) top [ vt=. (vbarpos"1@ botrow)each top vtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt NB. , 1+ > (I. 0= updn){ vm +...
NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm
vball=. > ; vb+each 0, (0{ wb), +/ 0 1{ wb if. 0= # vball do. NB. Defaulting output to center
wb=. (wid@ >) bot [ bot=. (< 2 1$ (< 1 2){ BCM) 1} bot vb=. (vbarpos"1@ toprow)each bot vball=. > ; vb+each 0, (0{ wb), +/ 0 1{ wb NB. , 1+ > (I. 0= updn){ vm
ht=. >./ > #each top [ hm=. >./ > #each mid [ hb=. >./ > #each bot
NB. 'Stop' 13!:8 ] 101
- if. minbar do.
ht=. ht- (1< ht) [ top=. (- 1< ht)}.each top hm=. hm- (1< hm)* 2 [ mid=. ( 1< hm)}.each (- 1< hm)}.each mid hb=. hb- (1< hb) [ bot=. ( 1< hb)}.each bot
if. 2> # vtall do. tspan=. _ else. tspan=. ({: - {.) vtall end. if. 1> # vball do. bspan=. else. bspan=. ({: - {.) vball end. tsep=. (1< # vtall)>. 0+ bspan- tspan 'top tspan wt vtall'=. (1; tsep) widentop top; wt; < vt vtt=. (vbarpos"1@ toprow)each top vttall=. > ; vtt+each 0, (0{ wt), +/ 0 1{ wt vmtall=. > ; vmt+each 0, (0{ wm), +/ 0 1{ wm vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm top; mid; bot; wt; wm; wb; vt; vtt; vm; vmt; vmb; vb; ht; hm; hb; vtall; vttall; vmtall; vmball; vball
)
NB.
connect=: 4 : 0
- NB. Connecting outputs with inputs in formatted chunks NB. y is two or three pieces of formula. x is up/down arrangement and rule number NB. E.g.: Left and right parameters sit above center operator, when updn is _1 1 _1 'updn rultyp categs'=. x NB. 0 1 Monad 2 Dyad 3 Adverb 4 5 6 7 Conj 8 Trident 10 Bident 11 Is 12 Paren if. (-. updn-: * updn)+. (-. (# updn)= # y) do.
- smoutput ,: 'Improper connect pattern: ', ": updn return.
NB. if. 18< # rulhist do. NB. aashstk=: 1 NB. smoutput rultyp; wt; < top NB. 'Stop' 13!:8 ] 1 NB. end.
- coff=. toff=. boff=. moff=. 0 [ cdir=. i. 0 [ bar2=. 0 cap=. a:, a:, a: [ hc=. 0 'fgn hyp mdy'=. 0; 0; 0 select. rultyp
case. 0; 1 do. NB. Monad
mid=. (1 0$ ' '); (1 1$ (< 1 2){ BCM); 1 0$ ' ' [ hm=. 1 moff=. _1{ vtall [ boff=. (_1{ vtall)- _1{ vball NB. Save a line above bar or box ??
NB. if. *./ (toprow {. > (1= updn)# bot)e. ' ', 9!:6 do. hm=. 0 end.
NB. 'Stop' 13!:8 ] 11
case. 3 do. NB. Adverb
- NB. mid=. }. }: each mid [ hm=. hm- 2 NB. Enclosing verb, with side links
'mid vmt vmb hm wm'=. (vmt; < vmb) wrapio 0; < mid vmt=. (< ) 0} vmt [ vmb=. (< ) 0} vmb NB. Enclosing adverb, with vertical (only) links
xx=. (wt; vt; vmt; < vmb) wrapmod top; mid; < 1 'top mid bot wt wm wb ht hm hb vt vm vmt vmb vb vtall vmtall vmball vball'=. xx hm=. hm- 1 [ mid=. }.each hm propup each mid
vmtall=. > ; vmt+each 0, (0{ wm), +/ 0 1{ wm vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm moff=. ({: vtall)- {: vmtall boff=. moff+ ({: vmball)- {: vball
NB. 'Stop' 13!:8 ] 33
case. 10 do. NB. Bident
- if. verb= {. categs do.
- NB. Inserting top connector for outer verb wt=. wt+ 2 0 0
top=. (ht propup 2{."1 ] 1 1$ (< 1 2){ BCM); 1}. top vtt=. (< 0) 0} vtt [ vt=. (< 0) 0} vt vtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt
bl=. > {. bot fh=. ((<4 0){ BCM)i.~ , (({. vball)<. _1+ {. wb){"1 ] 2{. bl if. fh< 2<. # bl do.
- NB. ????
if. (tbdiff> 0)*. 1< # vtall do.
- NB. Widening the argument span
- NB. Inserting top connector for outer verb wt=. wt+ 2 0 0
NB. top=. (< (> 0{ top),"1 ((# > 0{ top), 0>. tbdiff)$ ' ') 0} top NB. wt=. wt+ (0>. tbdiff), 0 0 NB. vtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt [ vt=. (< 0) 0} vt NB. tbdiff=. 0
'top tspan wt vtall'=. (0; tbdiff) widentop top; wt; < vt
- end. argoff=. 0 ml=. mc=. mr=. 0 0$ ' ' NB. if. tbdiff= 0 do.
if. tbdiff< 0 do.
- NB. Centering the output span
argoff=. (<. 0.5* | tbdiff)<. ({: vtall)- {. vtall bl=. > {. bot fh=. ((<4 0){ BCM)i.~ , (({. vball)<. _1+ {. wb){"1 ] 2{. bl if. fh< 2<. # bl do.
- ml=. mc=. mr=. 0 0$ ' ' NB. Widen Fork or Hook bus (after removing unused slants) NB. ????
- NB. Centering the output span
'Stop' 13!:8 ] 100
fh=. fh- fh*. 1< # vball [ wbl=. # {. bl=. (fh*. 1< # vball)}. bl bl=. (- wbl+ 0<. argoff- _1+ {. vball){."1 bl vball=. vball+ 0>. (# {. bl)- wbl bl=. ((# {. bl)+ 0>. (argoff- 1- 2| tbdiff)- (# {. bl)- {: vball){."1 bl bl=. ((< (i. 1+ fh); {. vball){ bl) (< (i. 1+ fh); ({. vball)- argoff)} bl bl=. ((< (i. 1+ fh); {: vball){ bl) (< (i. 1+ fh); ({: vball)+ argoff+ 2| tbdiff)} bl bli=. (1+ ({. vball)- argoff)+ i. argoff bl=. (argoff#"1 (,. (fh# '/'), (<2 1){ BCM)) (< (i. 1+ fh); bli)} bl bli=. ({: vball)+ i. argoff+ 2| tbdiff bl=. (argoff#"1 (,. (fh# '/'), (<2 1){ BCM)) (< (i. 1+ fh); bli)} bl bot=. (< bl) 0} bot [ hb=. # bl
- else.
- NB. Interpose connecting bars
ml=. ((* argoff){ (1 2; 4 0){ BCM), (0>. argoff- 1)# (<2 1){ BCM mr=. ((0>. argoff- 1- 2| tbdiff)# (<2 1){ BCM), (1< | tbdiff){ (1 2; 4 4){ BCM mc=. ((* argoff)# (< 0 4){ BCM), ((0>. _1+ ({: vball)- ({. vball))# ' '), (< 0 0){ BCM if. 1= # vball do. mc=. (< 0 2){ BCM end.
- NB. Interpose connecting bars
- end. moff=. {. vtall boff=. moff+ argoff- {. vball
hm=. >./ > #each mid=. tomat each ml; mc; mr NB. Capping with connectors for Hook arguments
vttall=. > ; vtt+each 0, (0{ wt), +/ 0 1{ wt vttloc=. vttall- coff=. {. vttall cc=. ((< 0 2){ BCM) vttloc} (1+ {: vttloc)$ (< 2 1){ BCM cc=. 'H' (((_2&{ - 0&{) vttall)+ <. 0.5* (_1&{ - _2&{) vttall)} cc cc=. ' ', '/',: cc NB. Indicator that right argument is default for left
cl=. (((< 4 0){ BCM); 2 0) corner 3 1 cr=. (((< 4 4){ BCM); 2 0) corner 3 1 cap=. cl; cc; cr [ coff=. _1+ {. vttall [ hc=. 3
NB. 'Stop' 13!:8 ] 109
case. 2; 8; 9; 11 do. NB. Dyad, Fork, Trident, Is
- cat=. ((,@{.)each 1{bot)e. ;: ', ,. ,: ; Catenate Stitch Laminate Link' if. cat do.
- NB. Special format for Catenations if. -. updn-: _1 0 _1 do. NB. Restructuring
- updn=. _1 0 _1 [ uplt=. 1 NB. For dl calculation ovb=. vb; vball xx=. updn spread y 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. xx 'vb vball'=. ovb
vbi=. (> #each vb)# 0 1 2 bot=. (< (< 1 2){ BCM) vbi} bot vball=. 0 [ vb=. (< 0) vbi} vb NB. ? NB. vmall=. 0, , 1 2+"0 1 vmall
- updn=. _1 0 _1 [ uplt=. 1 NB. For dl calculation ovb=. vb; vball xx=. updn spread y 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. xx 'vb vball'=. ovb
jntclr=. (0, (] * 1&>:@ (|@ ])) 0 2{ updn){"0 1 ] ((< 0 2), 2 3$ _2<\ 0 2 0 0 4 0 0 2 0 4 4 4){ BCM NB. Positions (boxed) and widths, left to right (for middle/bottom/top) vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{updn){ (< 3# < i. 0), (< vb), < vt wdu=. (< 0 1)|: > (0 1 2{ updn){ (< i. 0), (< wb), < wt NB. Argument offset outside of destination inputs--in Fork, ... ? argoff=. 0* 8= rultyp NB. Aligning right/left arguments (top) as inputs to verb (bottom) NB. and reconciling widths tspan=. ({: - {.) vtall [ bspan=. ({: - {.) vball mspan=. ({: - {.) vmtall [ moff=. ({. vtall)- {. vmtall
- NB. Special format for Catenations if. -. updn-: _1 0 _1 do. NB. Restructuring
NB. if. rultyp= 2 do. 'Stop' 13!:8 ] 82 end.
- if. cat do.
'ml mc mr'=. mid [ ht=. >./ > #each top NB. (Should not be filling to arg width here, but adjusting later.) NB. ml=. ((0{ wt)- ({. vmtall)+ {. vtall){.!.((< 2 1){ BCM) 1{ jntclr ml=. ((1{ jntclr), (0>. ((0{ wt)- 1+ {. vtall)- {. vmtall)# (< 2 1){ BCM)
xx=. {: > _1{ top if. (+./ (, mc) E. xx)*. +./ ((4 0; 4 4){ BCM)e. xx do.
- NB. Joining successive like catenations on one level NB. Right-grouping only! xb=. +/ *./ \ ' '= xx xx=. (- +/ *./ \ ' '= |. xx)}. xx
mr=. ((xb+ 1- (# mc)- {. vmtall)# (< 2 1){ BCM), ((< 4 2){ BCM), (1+ xb)}. xx ht=. >./ > #each top [ top=. (}:each _1{ top) _1} top mspan=. tspan
mr=. ((0>. tspan- (# ml)+ {: $ mc)# (< 2 1){ BCM), (_1{ jntclr) mspan=. ({: - {.) vmtall
- NB. Joining successive like catenations on one level NB. Right-grouping only! xb=. +/ *./ \ ' '= xx xx=. (- +/ *./ \ ' '= |. xx)}. xx
NB. 'Stop' 13!:8 ] 22
wm=. (wid@ >) mid [ mid=. tomat each ml; mc; mr vmt=. 0; ; _1+ # mr [ vmb=. (< ) 0 _1} vmb vm=. (~.@ (/: { ]))each vmt,each vmb NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm vmtall=. > ; vmt+each 0, (0{ wm), +/ 0 1{ wm vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm
if. (1< +/ wt> 0)*. 0> tbdiff=. tspan- mspan do.
- NB. Widening the argument span (0 or 2 beyond verb span)
NB. vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{updn){ (< 3# < i. 0), (< vb), < vt 'top tspan wt vtall'=. (1; tbdiff) widentop top; wt; < vt
- NB. Widening the argument span (0 or 2 beyond verb span)
NB. 'Stop' 13!:8 ] 23
- else.
- NB. Non-catenation operations tbdiff=. tspan- (bspan+ 2* argoff)
NB. smoutput rultyp; wt; tbdiff; < top NB. 'Stop' 13!:8 ] 83
if. (1< +/ wt> 0)*. 0> tbdiff do.
- NB. Widening the argument span (0 or 2 beyond verb span)
NB. vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{updn){ (< 3# < i. 0), (< vb), < vt 'top tspan wt vtall'=. (1; tbdiff) widentop top; wt; < vt tbdiff=. tspan- (bspan+ 2* argoff)
- NB. Widening the argument span (0 or 2 beyond verb span)
NB. if. rultyp= 8 do. 'Stop' 13!:8 ] 83 end.
- if. tbdiff= 0 do.
- NB. Preparing to connect to double output │ │
ml=. (< 1 2){ BCM mc=. (0>. bspan- 1)# ' ' NB. }. }: > 1{ mid mr=. (< 1 2){ BCM vmt=. vmb=. 0; ; _1+ # mr bar2=. 1
elseif. 0< tbdiff do.
- NB. Testing bottom span lr=. 1 NB. Center pane
txtlr=. (0 _1{ Argsplit)i.~ txt=. {. ] tx=. > lr{ bot capsplit=. 0 if. *./ txtlr<"0 1 $ txt do.
txtlri=. ({. + i.@ (1&-@ (-/))) txtlr cap=. txtlri{ txt capsplit=. *./ (txtlri{ txt)e. Argsplit, 'FH', (< 2 1){ BCM
- NB. Broadening lower cap or split └─ ┬F───┬ ──┘ └── ┬ ──┘ mc=. }. }: cap
ml=. ({. cap), (<. 0.5* tbdiff)# (< 2 1){ BCM mr=. ((>. 0.5* tbdiff)# (< 2 1){ BCM), {: cap hb=. >./ > #each bot [ bot=. (< }. tx) lr} bot vmt=. 0; ; _1+ # mr [ vmb=. ; (0, _1+ # mc); tbdiff=. 0 [ vball=. 1 _1+ vball
- NB. Inserting offset connectors └─── ┐ ┬ ┌ ───┘
argoff=. argoff+ <. 0.5* tbdiff ml=. (1{ jntclr), (0>. argoff- 1)# (< 2 1){ BCM mc=. ((1< bspan)# (< 0 4){ BCM), (0>. bspan- 1)# ' ' mc=. mc, (1< tbdiff)# ((1< bspan){ (0 2; 0 0){ BCM) mr=. ((0>. tspan- argoff+ bspan+ 1)# (< 2 1){ BCM) mr=. mr, (1< tbdiff){ ((< 1 2){ BCM), _1{ jntclr vmt=. 0; ; _1+ # mr [ vmb=. ; (0, _1+ # mc);
hm=. >./ 1, > #each mid=. tomat each ml; mc; mr wm=. (wid@ >) mid vm=. (~.@ (/: { ]))each vmt,each vmb NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm vmtall=. > ; vmt+each 0, (0{ wm), +/ 0 1{ wm vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm NB. Aligning both connectors boff=. moff+ ({. vmball)- {. vball
- NB. Preparing to connect to double output │ │
- end.
NB. if. rultyp= 2 do. 'Stop' 13!:8 ] 24 end.
- if. rultyp= 8 do.
- NB. Capping with connectors for Fork arguments
vttall=. > ; vtt+each 0, ({. wt), +/ 0 1{ wt if. 2> # vttall do.
- NB. 13!:0 ] 1 smoutput '*Ill-formed Fork: '
smoutput ({. top); (1{ bot); < {: top 13!:6 '|Syntax error'
cl=. (((< 4 0){ BCM); 1 0) corner 2 1 cr=. (((< 4 4){ BCM); 1 0) corner 2 1 cc=. ((< 0 2){ BCM) (vttall- coff+ 1)} (1+ -/ _1 0{ vttall)$ (< 2 1){ BCM ccm=. (({. wt)>. (_1+ +/ 0 1{ wt)<. coff+ <. 0.5* # cc)- coff+ 1 cc=. 'F' ccm} cc
- NB. 13!:0 ] 1 smoutput '*Ill-formed Fork: '
- NB. Capping with connectors for Fork arguments
NB. 'Stop' 13!:8 ] 88
- NB. Consolidating with lower caps and/or splits lr=. 0 NB. Left arg
txtlr=. (0 _1{ Argsplit)i.~ txt=. {. ] tx=. }. > lr{ top if. *./ txtlr<"0 1 $ txt do.
txtlri=. ({. + i.@ (1&-@ (-/))) txtlr if. *./ (txtlri{ txt)e. Argsplit, 'FH', (< 2 1){ BCM do.
- NB. Replacing upjoints with horizontals (could sometimes trim off)
nc=. ((< 2 1){ BCM), (}. }: txtlri{ txt), (< 2 1){ BCM cc=. }. nc (txtlri+ (- lr* +/ 0 1{ wt)- coff+ 1)} cc coff=. coff+ 1 blr=. *./ ' '= blrx=. (txtlr+ 0 2){ ' ', txt, ' ' blrx=. (' '= blrx){"0 1 blrx,. (0 0; 0 4){ BCM xx=. (0{ blrx), (((}. }: nc) = (<0 2){ BCM){ (2 1; 2 2){ BCM), _1{ blrx xx=. xx,: ' ', (((}. }: nc) = (<0 2){ BCM){ ' ', (< 1 2){ BCM), ' '
tx=. ((1{ xx),: blr{ xx) (< 0 1; txtlri)} ' ', tx top=. (< tx) lr} top
- NB. Replacing upjoints with horizontals (could sometimes trim off)
txtlr=. (0 _1{ Argsplit)i.~ txt=. {. tx=. }. > lr{ top if. *./ txtlr<"0 1 $ txt do.
txtlri=. ({. + i.@ (1&-@ (-/))) txtlr if. *./ (txtlri{ txt)e. Argsplit, 'FH', (< 2 1){ BCM do.
nc=. ((< 2 1){ BCM), (}. }: txtlri{ txt), (< 2 1){ BCM cc=. }: nc (txtlri+ (- lr* +/ 0 1{ wt)- coff+ 1)} cc blr=. *./ ' '= blrx=. (txtlr+ 0 2){ ' ', txt, ' ' blrx=. (' '= blrx){"0 1 blrx,. (0 0; 0 4){ BCM
xx=. (0{ blrx), (((}. }: nc) = (<0 2){ BCM){ (2 1; 2 2){ BCM), _1{ blrx xx=. xx,: ((nc = (<0 2){ BCM){ ' ', (< 1 2){ BCM)
tx=. ((1{ xx),: blr{ xx) (< 0 1; txtlri)} ' ', tx top=. (< tx) lr} top
ht=. >./ > #each top NB. Trimming any excess verticals top=. ht propup each top top=. (- ht=. ht- *./ (, (toprow@ >) top)e. ' ', (< 1 2){ BCM){.each top top=. (- ht=. ht- *./ (, (toprow@ >) top)e. ' ', (< 1 2){ BCM){.each top top=. (- ht=. ht- *./ (, (toprow@ >) top)e. ' ', (< 1 2){ BCM){.each top cap=. cl; (' ',: cc); cr [ hc=. 2
- else.
- NB. Saving a line above bar or box
if. *./ (toprow , > (1= updn)# bot)e. ' ', 9!:6 do.
- hm=. hm* 1= # vball
- NB. Saving a line above bar or box
NB. 'Stop' 13!:8 ] 77
case. 4; 5; 6; 7 do. NB. Conjunction
- NB. 1 0 _1 VV 4: " ` ^: & &. &.: &: . .. .: : :.
- @ @:
NB. 1 0 _1 VN 5: " ` ^: & ;. !. d. D. D: L: S: T. NB. _1 0 1 NV 6: " ` @. & NB. _1 0 _1 NN 7: " @. : ;. !: : D. H. NB. Selecting from up/dn joint (col.) for center/left/right (row)
jntclr=. ((] * 1&>:@ (|@ ])) 1 0 2{ updn){"0 1 ] ((< 0 2), 2 3$ _2<\ 0 2 0 0 4 0 0 2 0 4 4 4){ BCM NB. Widths l/c/r, for middle/bottom/top in each vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{ updn){ (< 3# < i. 0), (< vb), < vt wdu=. (< 0 1)|: > (0 1 2{ updn){ (< i. 0), (< wb), < wt cdir=. - _1{ updn NB. Verb-last/first indicator (_1 1) uplt=. _1= {. updn [ uprt=. _1= {: updn 'ml mc mr'=. mid
if. -. ((< 0 0){ BCM)e. mc do.
- conjs=. '" @. ` !: : ;. :.
.. .: & ' flags=. 'ran age tie fgn mdy cut obv adv eve odd bon ' conjs=. conjs, '^: !. &. &.: &: `: L: S: d. D. D: H: T: ' flags=. flags, 'pow fit und una app evo lva spr dv0 drv sec hyp tay ' conjs=. conjs, 'Rank Agenda Tie Foreign Dyad Cut Obverse Adverse ' flags=. flags, 'Ran Age Tie Fgn Mdy Cut Obv Adv ' conjs=. conjs, 'Even Odd Bond Power Fit Under Appose' flags=. flags, 'Eve Odd Bon Pow Fit Und App ' (flags)=. (< , {. mc)(<@ =) ;: conjs ran=. ran+. Ran [ age=. age+. Age [ cut=. cut+. Cut
NB. >>> More needed here!
- 'cmp bon'=. ((bon+. Bon+. app+. App)*. 4= rultyp); (bon+. Bon)*. rultyp e. 5 6
- end.
NB. 'Stop' 13!:8 ] 47
- sideconn=. 0 [ msep=. 0 if. cmp do.
- NB. Special action for Compose of two verbs if. -. updn-: 1 0 _1 do. NB. Restructuring
- updn=. 1 0 _1 [ uplt=. 1 NB. For dl calculation xx=. updn spread y 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. xx
jntclr=. ((] * 1&>:@ (|@ ])) 1 0 2{ updn){"0 1 ] ((< 0 2), 2 3$ _2<\ 0 2 0 0 4 0 0 2 0 4 4 4){ BCM vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{ updn){ (< 3# < i. 0), (< vb), < vt wdu=. (< 0 1)|: > (0 1 2{ updn){ (< i. 0), (< wb), < wt
- NB. Special action for Compose of two verbs if. -. updn-: 1 0 _1 do. NB. Restructuring
NB. 'Stop' 13!:8 ] 40
NB. Inserting top connector for outer verb ??? x u&v y ↔ (v x) u (v y) !!! wt=. wt+ cdir|. 0 2 0 xx=. ht propup tomat (2* cdir){."1 (< 1 2){ BCM top=. (< xx) (0<. cdir)} top vt=. (< 0>. - cdir) (0<. cdir)} vt vtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt
NB. 'Stop' 13!:8 ] 42
if. singleb=. 2> # vballo=. vball do.
- NB. Inserting verb-input splitter for compose or bond(s)
bi=. {. I. 1= updn NB. 0<. cdir vbx=. (0= {. vbxo)+ , _1 1+"1 0 vbxo=. > bi{ vb bx=. wbx{."1 ((0 e. vballo)# ' '),"1 > bi{ bot [ wbx=. (bi{ wb)>. 1+ {: vbx vb=. (< vbx) bi} vb [ wb=. (# {. bx) bi} wb vball=. > ; vb+each 0, (0{ wb), +/ 0 1{ wb
xx=. ((0{ vball)# ' '), ((< 4 0){ BCM), ((_1- -/ vbx)# (< 2 1){ BCM), (< 4 4){ BCM xx=. ((< 0 2){ BCM) (1>. vballo)} xx bx=. xx, (((-. minbar)>. hb- 1+ # bx)# tomat (- 1+ 0{ vbxo){. (< 1 2){ BCM), bx hb=. >./ 1, > #each bot [ bot=. (< bx) bi} bot vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{ updn){ (< 3# < i. 0), (< vb), < vt
xx=. hm propup (- msep){."1 > (0<. cdir){ mid xx=. (bon{ 'CR') (< _1, (1+ cdir){ 1 _ _2)} xx mid=. (< (- 0>. cdir)|."1 xx,. hm propup 1 1$ (< 1 2){ BCM) (0<. cdir)} mid wm=. (wid@ >) mid [ hm=. >./ 1, > #each mid vmt=. (; (- 0<. cdir)* _1+ (0<. cdir){ wm) (1, 0<. cdir)} vmt vmb=. (; 0, _1+ (0<. cdir){ wm) (1, 0<. cdir)} vmb vm=. (~.@ (/: { ]))each vmt,each vmb NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm NB. > ; vm+each +/ \ 0, _1}. wm
- NB. Inserting verb-input splitter for compose or bond(s)
- elseif. ran+. cut+. pow+. bon+. und+. una+. fit+. evo+. lva+. spr do.
- NB. Treating as adverb (controlling arguments and result) if. 6~: rultyp do.
- if. -. updn-: 0 0 _1 do.
- updn=. 0 0 _1 [ uplt=. 1 NB. For dl calculation xx=. updn spread y 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. xx
- if. -. updn-: _1 0 0 do.
- updn=. _1 0 0 [ uplt=. 1 NB. For dl calculation xx=. updn spread y 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. xx
'mid vmt vmb hm wm'=. (vmt; < vmb) wrapio (uprt- 1); < mid vm=. (~.@ (/: { ]))each vmt,each vmb NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm vmtall=. > ; vmt+each 0, (0{ wm), +/ 0 1{ wm vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm
xx=. (wt; vt; vmt; < vmb) wrapmod top; mid; < - uprt 'top mid bot wt wm wb ht hm hb vt vm vmt vmb vb vtall vmtall vmball vball'=. xx if. bon do. mid=. (< 'R' (< 1; 1+ {. > 1{ vmt)} > 1{ mid) 1} mid end.
- if. -. updn-: 0 0 _1 do.
- NB. Treating as adverb (controlling arguments and result) if. 6~: rultyp do.
NB. 'Stop' 13!:8 ] 42
- moff=. ({. vtall)- {. vmtall [ boff=. moff+ ({. vmball)- {. vball sideconn=. 1
- elseif. tie do.
- if. -. updn-: _1 0 _1 do. NB. Restructuring
- updn=. _1 0 _1 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. updn spread y uplt=. uprt=. 1
jntclr=. ((] * 1&>:@ (|@ ])) 1 0 2{ updn){"0 1 ] ((< 0 2), 2 3$ _2<\ 0 2 0 0 4 0 0 2 0 4 4 4){ BCM
vmt=. (0; ), _1{ vt vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{ updn){ (< 3# < i. 0), (< vb), < vt wdu=. (< 0 1)|: > (0 1 2{ updn){ (< i. 0), (< wb), < wt 'ml mc mr'=. mid
- updn=. _1 0 _1 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. updn spread y uplt=. uprt=. 1
- if. -. updn-: _1 0 _1 do. NB. Restructuring
NB. 'Stop' 13!:8 ] 64
- NB. Join successive ties on one level
if. mc e. xx=. {: > 0{ top do.
- argoff=. 0{ vtall xtb=. +/ *./ \ ' '= |. xx
ml=. ((< 2 1){ BCM) (- 1+ i. xtb)} ((< 4 2){ BCM) (- 1+ xtb)} xx top=. (}:each {. top) 0} top vmt=. (< {. vtall) 0} vmt [ moff=. 0
argoff=. 0>. ({. wt)- 1+ ({. vtall)+ <. 0.5* wid mc ml=. (1{ jntclr), argoff# (< 2 1){ BCM [ moff=. {. vtall
if. mc e. xx=. {: > _1{ top do.
- xlb=. +/ *./ \ ' '= xx
mr=. ((< 2 1){ BCM) (i. xlb)} ((< 4 2){ BCM) xlb} xx top=. (}:each _1{ top) _1} top NB. Connecting to rightmost tie--all join equally vmb=. (; (xx i. mc)+ <. 0.5* wid mc) 1 _1} vmb
mr=. ((0>. tspan- (# ml)+ wid mc)# (< 2 1){ BCM), (_1{ jntclr)
hm=. >./ 1, > #each mid=. tomat each ml; mc; mr wm=. (wid@ >) mid [ ht=. >./ > #each top vm=. (~.@ (/: { ]))each vmt,each vmb vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm boff=. moff+ ({. vmball)- ({. vball) sideconn=. 1
- argoff=. 0{ vtall xtb=. +/ *./ \ ' '= |. xx
NB. 'Stop' 13!:8 ] 65
- elseif. age+. obv+. adv+. eve+. odd do.
- if. -. updn-: 0 0 _1 do.
- updn=. 0 0 _1 [ uplt=. 1 NB. For dl calculation xx=. updn spread y 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. xx
'mid vmt vmb hm wm'=. (vmt; < vmb) wrapio 0; < mid
xx=. (wt; vt; vmt; < vmb) wrapmod top; mid; < _1 'top mid bot wt wm wb ht hm hb vt vm vmt vmb vb vtall vmtall vmball vball'=. xx sideconn=. 1
- if. -. updn-: 0 0 _1 do.
NB. 'Stop' 13!:8 ] 66
- elseif. fgn+ mdy+. dv0+. drv+. sec+. tay do.
- if. -. updn-: _1 0 _1 do. NB. Restructuring
- updn=. _1 0 _1 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. updn spread y uplt=. uprt=. 1
jntclr=. ((] * 1&>:@ (|@ ])) 1 0 2{ updn){"0 1 ] ((< 0 2), 2 3$ _2<\ 0 2 0 0 4 0 0 2 0 4 4 4){ BCM
vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{ updn){ (< 3# < i. 0), (< vb), < vt wdu=. (< 0 1)|: > (0 1 2{ updn){ (< i. 0), (< wb), < wt
- updn=. _1 0 _1 'top mid bot wt wm wb vt vtt vm vmt vmb vb ht hm hb vtall vttall vmtall vmball vball'=. updn spread y uplt=. uprt=. 1
- if. -. updn-: _1 0 _1 do. NB. Restructuring
NB. 'Stop' 13!:8 ] 67
- elseif. 1 do.
if. 3< # mc do.
- NB. Enclosing compound conjunction
xx=. (wt; vt; vmt; < vmb) wrapmod top; mid; < 1 'top mid bot wt wm wb ht hm hb vt vm vmt vmb vb vtall vmtall vmball vball'=. xx
- NB. Enclosing compound conjunction
NB. 'Stop' 13!:8 ] 68
NB. ??? [ vmt=. (< ) 1} vmt [ vmb=. (< ) 1} vmb
- end.
- end. NB. ...of some special cases 'ml mc mr'=. mid if. -. sideconn do.
- NB. Attaching side connectors
ml=. (( 1{ jntclr); (<. 0.5* # mc), (> 0{ vmdu)) corner (# mc), 1 mr=. ((_1{ jntclr); (<. 0.5* # mc), (> 2{ vmdu)) corner (# mc), 1 hm=. >./ 1, > #each mid=. tomat each ml; mc; mr wm=. (wid@ >) mid vmt=. ((_1= updn)# < 0) (I. _1= updn)} vmt vmb=. (( 1= updn)# < 0) (I. 1= updn)} vmb vmt=. (< ) 1} vmt [ vmb=. (< (-. 1 e. updn)# > 1{ vmb) 1} vmb NB. vm=. (~.@ (/: { ]))each vmt,each vmb NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm
- NB. Attaching side connectors
NB. 'Stop' 13!:8 ] 43
- NB. Aligning conjunctor to top argument and bottom verb
vmtall=. > ; vmt+each 0, (0{ wm), +/ 0 1{ wm vmball=. > ; vmb+each 0, (0{ wm), +/ 0 1{ wm moff=. ({: vtall)- {: vmtall boff=. moff+ ({: vmball)- {: vball NB. Rightward distances from argument to receiver (should have dr=0) dr=. (boff+ {: vball)- ({: vtall)+ ({: vmball)- {: vmtall dl=. (boff+ {. vball)- ({. vtall)+ ({. vmball)- {. vmtall
NB. 'Stop' 13!:8 ] 43
- if. (dl~: 0)*. dr~: 0 do. 'Misalignment' 13!:8 ] 49 end.
if. (1< +/ wt> 0)*. (dl< 0)+. dr> 0 do.
- NB. Widening the top span
NB. top=. (< (((# > 1{ top), dr- dl)$ ' '),"1 > 1{ top) 1} top NB. wt=. (wid@ >) top NB. vtall=. > ; vt+each 0, (0{ wt), +/ 0 1{ wt
'top tspan wt vtall'=. (1; dr- dl) widentop top; wt; < vt vmdu=. > 0 1 2{each ((] * 1&>:@ (|@ ])) 0 1 2{ updn){ (< 3# < i. 0), (< vb), < vt boff=. boff+ dr- dl [ moff=. moff+ dr- dl
dl=. dl+ (_1 _1-: 0 _1{ updn)>. dr- dl [ dr=. 0<. dr
- end.
if. (dl> 0)*. -. ran+. bon+. cut+. pow+. und+. una+. fit+. evo+. lva+. spr+. tie do.
- NB. Widening the middle connector
dlrh=. <. 0.5* dl- dr mc=. (- dlrh)|."1 mc,. (# mc){. (- 1+ <. 0.5* # mc){. (1, dl- dr)$ (< 2 1){ BCM mid=. (< mc) 1} mid [ wm=. (# {. mc) 1} wm NB. vmall=. > ; vm+each 0, (0{ wm), +/ 0 1{ wm moff=. moff- (dl- dr) [ boff=. boff- uprt* (dl- dr)- dlrh dl=. dr
hm=. >./ > #each mid mid=. hm propup each mid if. minbar do. NB. Trimming any vertical excess
mid=. (- hm=. hm- *./ (, (toprow@ >) mid)e. ' ', (< 1 2){ BCM){.each mid
- NB. Widening the middle connector
NB. 'Stop' 13!:8 ] 44
- case. do.
- ('Unknown rultyp: ', ": rultyp) 13!:8 ] 99
- end. NB. select rultyp
tbb=. +./ |."1 > top (|.@ (+./"1@ (-.@ e.)))each < ' ', (< 1 2){ BCM ht=. (# tbb) - 0>. {. <: I. tbb
NB. 'Stop' 13!:8 ] 97
'coff toff moff boff'=. (coff; toff; moff; boff)-each <./ coff, toff, moff, boff
zc=. (- hc){. (coff{. ' '),"1 > (,./each)/ tomat each cap zt=. ((ht, toff){. ' '),"1 > (,./each)/ ht propup each top NB. (- ht){. ? zm=. ((hm, moff){. ' '),"1 > (,./each)/ hm propup each mid zb=. ((hb, boff){. ' '),"1 > (,./each)/ hb{.each bot if. *./ minbar, bar2, ((boff+ vball) { {. zb)e. (4 0; 4 4; 1 2){ BCM do. zm=. 0 1$ ' ' end. bbl=. +/ *./ \ ' '= {. zb NB. Leading blanks in top row of bottom
if. ((((< 1 2){ BCM), '/')-: 2{. bbl}. {. zb) do.
- NB. Reshaping Hook bus and slants zb=. }. zb
bfeed=. ({. zb)= (< 0 2){ BCM bmid=. ('H'~: {. zb)*. (0, +/\ }: bfeed)*. |. 0, +/\ }: |. bfeed if. -. ' '= {. bbl}. {: zm do.
- bnew=. (((0 4); 0 0){ BCM) (({. , {:) I. bfeed) } ' ' (I. bmid)} {. zb
bnew=. ((< 0 0){ BCM) (I. bfeed) } ' ' (i. {. I. bfeed)} {. zb
- NB. Reshaping Hook bus and slants zb=. }. zb
NB. smoutput rultyp, fgn, hyp, mdy
- out=. zc, zt, zm, zb
NB. 'Stop' 13!:8 ] 98
- if. ((rultyp e. 7)*. fgn+. hyp)+. (rultyp e. 4)*. mdy do.
- NB. Enclosing trimmed conjunctor, spacing vttall=. 1+ (vbarpos"1@ toprow) out
if. 1> # vttall do. vttall=. , 1+ <. 0.5* # {. out end. vball=. 1+ (vbarpos"1@ botrow) out
out=. ": < }. }: out NB. Inserting input/output feeds in box edges
if. 1< # vttall do.
xx=. ((< 2 2){ BCM) vttall} 0{ out
- xx=. ((4 0; 0 2; 4 4){ BCM) (_1 0 1+ {. vttall)} 0{ out vttall=. _1 1+ {. vttall
out=. ((((< 1 2){ BCM) vttall} (# xx)# ' '),: xx), }. out
xx=. ((< 2 2){ BCM) vball} {: out out=. (}: out), xx,: (((< 1 2){ BCM) vball} (# xx)# ' ')
- NB. Enclosing trimmed conjunctor, spacing vttall=. 1+ (vbarpos"1@ toprow) out
NB. 'Stop' 13!:8 ] 97
- out
)
executef=: 4 : 0
- NB. Executing rule x on stack y for "flowtree"
NB. Adapted for tree production by ArtAnger in 2010 NB. smoutput nobar ": x; 4{. y t_b=. x{ PTAsubj NB. Rule mask
t_c=. > , t_b# 4 1{. y NB. Categories t_x=. , t_b# {."1 ] 4 _2{. y NB. Formulas t_t=. , t_b# 4 _1{. y NB. Tree diagrams NB. smoutput ": x; t_c; t_x; < t_t
- if. x= 12 do.
- NB. (lpar, cavn, rpar, any ); 1 1 1 0; '12 Paren'
t_t=. > 1{ t_t t_x=. > 1{ t_x t_c=. 1{ t_c ". 't_z=. ', t_x
- case. 0; 1 do.
- NB. (edge, verb, noun, any ); 0 1 1 0; '0 Monad' NB. ((edge+avn), verb, verb, noun ); 0 0 1 1; '1 Monad' t_t=. (1 _1; x; t_c) connect t_t
- NB. ((edge+avn), noun, verb, noun ); 0 1 1 1; '2 Dyad' NB. ((edge+avn), verb, verb, verb ); 0 1 1 1; '8 Fork' NB. ((edge+avn), noun, verb, verb ); 0 1 1 1; '9 Trident' t_t=. (_1 1 _1; x; t_c) connect t_t
- NB. ((name+noun),asgn, cavn, any ); 1 1 1 0; '11 Is' t_t=. (_1 1 _1; x; t_c) connect t_t
- NB. ((edge+avn), (verb+noun),adv, any ); 0 1 1 0; '3 Adverb' t_t=. (0 0; x; t_c) connect t_t
- NB. ((edge+avn), verb, conj, verb ); 0 1 1 1; '4 ConjVV' NB. ((edge+avn), verb, conj, noun ); 0 1 1 1; '5 ConjVN' NB. ((edge+avn), noun, conj, verb ); 0 1 1 1; '6 ConjNV' NB. ((edge+avn), noun, conj, noun ); 0 1 1 1; '7 ConjNN' if. x= 6 do.
- t_t=. (_1 0 1; x; t_c) connect t_t elseif. x= 4 do. t_t=. (1 0 _1; x; t_c) connect t_t
- t_t=. (1 0 _1; x; t_c) connect t_t
- t_t=. (_1 0 _1; x; t_c) connect t_t
- NB. (edge, cavn, cavn, any ); 0 1 1 0; '10 Bident' t_t=. (1 _1; x; t_c) connect t_t
smoutput 1 1}._1 _1}.": < 'Unknown case: ', ": x smoutput 4{. y t_c=. t_x=. t_t=. < 'Unknown case' 'Unknown case' 13!:8 ] 98 return.
- ". 't_z=. ', t_x=. ;:^:_1 t_x
smoutput 13!:12 NB. Error message, stack top smoutput 20 30 trimval each"1 0 ] 4{. ((t_b i. 1){. y), (t_c; t_x; maxsize trimval t_t), (1+ t_b i: 1)}. y return.
t_c=. (4!:0 < 't_z'){ noun, adv, conj, verb
- NB. (lpar, cavn, rpar, any ); 1 1 1 0; '12 Paren'
t_x=. '(', (5!:5 <'t_z'), ')' t_t=. maxsize trimval t_t NB. show t_t if. aashstk do.
- smoutput 4{. ((t_b i. 1){. y), (t_c; t_x; maxdisp trimval t_t), (1+ t_b i: 1)}. y
- if. x= 12 do.
)
NB. ========================================================= NB. Each row of queue or stack shows: NB. a value classification NB. a value expression (evaluation result, when in flowtree mode) NB. a formatted tree (when in flowtree mode) NB. "parse" initializes queue, then processes to stack, compacting both
movep=: 3 : 0 NB. move from queue to stack for "paren"
- 'queue stack'=. y 't_c t_x'=. {:queue if. (name~:t_c)+.asgn=0 0{::stack do.
- stack=. ({: queue),stack
t_c=. (4!:0 <t_x){noun,adv,conj,verb stack=. (t_c;t_x),stack
(}:queue);<stack
)
movef=: 3 : 0 NB. move from queue to stack for "flowtree"
- 'queue stack'=. y 't_c t_x t_t'=. {: queue
- if. (name= t_c)*. asgn~: 0 0{
- stack do.
t_c=. (4!:0 < t_x){ noun, adv, conj, verb NB. 1 4 8 2 None(_1)->verb
t_t=. botconn (topconn`(' '&,@ (maxlit&trimval))) @. notconn tomat t_t stack=. (t_c; t_x; t_t), stack (}: queue); < stack
)
movet=: 3 : 0 NB. move from queue to stack for "trace"
- 'queue stack'=. y 't_c t_x'=. {:queue if. (name~:t_c)+.asgn=0 0{::stack do.
- stack=. ({:queue),stack
t_c=. (4!:0 <t_x){noun,adv,conj,verb if. t_c~:verb do. t_x=. 5!:5 <t_x end. stack=. (t_c;t_x),stack
(}:queue);<stack
)
NB.
NB. stack=: parse mode; depth, or size, limit; sentence NB. mode: 'trace' or 'paren' or 'flowtree' NB. depth: depth limit for function calls to trace NB. sentence: character string to be parsed NB. stack: stack at the end of the parse
parse=: 3 : 0 NB. for paren, trace
- 't_mode t_d t_sent'=. y
queue=. (mark;) , (class&.> ,. prespace&.>) ;: t_sent stack=. 4 2$mark; depth=: 1+t_d indent=: 1 equals=: 0 if. 'trace' -: t_mode do.
- execute=. executet move=. movet
- execute=. executep move=. movep
t_i=. 1 1 1 1 i.~ * PTpatterns bwand"1 ,>4 1{.stack if. t_i<#PTpatterns do. NB. a pattern fits; execute the action
try.
- stack=. t_i execute stack
catchd.
smoutput 13!:12 NB. Error message, stack top smoutput ": 20 30 trimval each >4 1{.stack return.
end.
- else. NB. no pattern fits; move from queue to stack
- if. 0=#queue do. break. end.
'queue stack'=. move queue;<stack
- if. 0=#queue do. break. end.
- end.
assert. * (mark+cavn,0) bwand >(<1 2;0){stack [ 'stack must be empty or have a single noun, verb, adverb, or conj' show 30$'=' stack
)
parsef=: 3 : 0 NB. for flowtree
- 't_mode t_max t_sent'=. y
undef=. }. , ' ',"1 ~. > (_1= 4!:0 ;: t_sent)# ;: t_sent if. 0< # undef do.
TrSusp=: 13!:17 13!:0 ] 1 smoutput '*Undefined names: ', undef smoutput ' Suspending in locale jtrace for optional re-definition using (=:) .' smoutput ' Then enter 13!:5 ' to resume, or 13!:0 TrSusp to clear.' 'jtrace' 13!:8 ] 1 13!:0 TrSusp
queue=. (mark; ; ), (class&.> ,. prespace&.> ,. ":&.>) ;: t_sent nondel=. -. ({."1 queue)e. mark, lpar, rpar instat=. (+/ nondel), # ,/ nondel# _1{"1 queue
stack=. 4 3$ mark; ; maxsize=: 5 10>. >. t_max depth=: 0 indent=: 1 equals=: 0 if. 'trace' -: t_mode do.
- execute=. executet move=. movet
- execute=. executep move=. movep
NB. elseif. 'parenpyr' -: t_mode do. NB. execute=. executepp NB. move=. movep
- elseif. 'flowtree' -: t_mode do.
- execute=. executef move=. movef
while. -. (< 'Unknown case')e. 4 1{. stack do.
t_i=. 1 1 1 1 i.~ * PTApatterns bwand"1 , > 4 1{. stack if. t_i< # PTApatterns do. NB. a pattern fits; execute the action
- stack=. t_i execute stack
- if. 0= # queue do. break. end.
'queue stack'=. move queue; < stack
assert. * (mark+cavn,0) bwand >(<1 2;0){stack [ 'Probable input Syntax error' NB. stack must be empty or have a single noun, verb, adverb, or conj
((4": instat, $ _1{ stack), 13{ a.) 1!:3 < 'treestat.str' if. showval do. smoutput ". > (< 1 1){ stack end. _1{"1 stack
)
flowstat=: 3 : 0
- NB. Explore relationships among characters, tokens, and diagram size
ts=. 1!:1 <'treestat.str' tsr=. ". ] ;. _2 ts tsrn=. ~. tsr tsrn /: tsrn
)
NB.
pdepthn=: (+/\@ (1 _1 0&({~)@ ('()'&i.@ ((2&|@ (+/\@ ('&=))){"0 0 1 ('&(,.~)))))) CP=: {@(,&<)
pyr=: 3 : 0 NB. pyramid of parenthetical levels
dep=. (+/\@ (1 _1 0&({~)@ ('()'&i.@ ((2&|@ (+/\@ ('&=))){"0 0 1 ('&(,.~)))))) y bal=. (*./ 0<: dep)*. 0= {: dep dep=. 0>. dep z=. ((1+ >./ dep), # y)$ ' ' z=. |. y (<"1 ( dep+ 0, 0>. 2 -/\ dep),. i. # dep)} z NB. Optional vertical doubling of parentheses: NB. z=. |. y (<"1 (0>. dep+ 0<. 2 -/\ 0, dep),. i. # dep)} |. z
zb=. (|. i. 1+ >./ dep)<"0 1 dep zb=. zb<. 0,. }:"1 zb zbi=. , (i. # zb) CP i. {: $ zb zbu=. (zbi{ zb)# zbi (- bal)}. ('_' zbu} z), '*Unbalanced'
)
paren=: 3 : 0 NB. full parenthesization of sentence y
>(<1 1){parse 'paren';;y
)
parenpyr=: 3 : 0 NB. pyramidal parenthesization of sentence y
(|.@ ((":@ (,.@ (i.@ (#@]))))),. ' '&[ ,. ]) }: pyr >(<1 1){parse 'paren';;y
)
flowtree=: 3 : 0 NB. syntactic diagram of sentence y
- _ _ flowtree y NB. default size limit : pp=. pyr y if. '*Unbalanced' E. {: pp do.
- pp
- steps=: 0 NB. no intermediate output showval=: 0 NB. hide computed value minbar=: 1 NB. minimize connecting bars maxlit=: 5 15 NB. maximum display size of input literals maxsize=: x NB. maximum storage shape of every diagram maxdisp=: x NB. maximum display shape of diagram output
- 1{
- parsef 'flowtree'; x; y
)
trace=: 3 : 0 NB. execution trace to depth x (_ default) of sentence y
- _ trace y :
". >(<1 1){parse 'trace';x;y
)
paren_z_=: paren_jtrace_ pyr_z_=: pyr_jtrace_ parenpyr_z_=: parenpyr_jtrace_ flowtree_z_=: flowtree_jtrace_ trace_z_=: trace_jtrace_
