   #! is the getter word for that register with stack effect
   #! ( cpu -- value ). The second item is the setter word with
   #! stack effect ( value cpu -- ).
-  "AF" token  
-  "BC" token <|>
-  "DE" token <|>
-  "HL" token <|>
-  "SP" token <|> [ register-lookup ] <@ ;
+  <EBNF
+    main=("AF" | "BC" | "DE" | "HL" | "SP") => [[ register-lookup ]]
+  EBNF> ;
 
 : all-registers ( -- parser )
   #! Return a parser that can parse the format
   #! for 8 bit or 16 bit registers. 
-  8-bit-registers 16-bit-registers <|> ;
+  [ 16-bit-registers , 8-bit-registers , ] choice* ;
 
 : indirect ( parser -- parser )
   #! Given a parser, return a parser which parses the original
   #! wrapped in brackets, representing an indirect reference.
   #! eg. BC -> (BC). The value of the original parser is left in
   #! the parse tree.
-  "(" token swap &> ")" token <& ;
+  "(" ")" surrounded-by ;
 
 : generate-instruction ( vector string -- quot )
   #! Generate the quotation for an instruction, given the instruction in 

  #! is the getter word for that register with stack effect
  #! ( cpu -- value ). The second item is the setter word with
  #! stack effect ( value cpu -- ).
  "A" token 
  "B" token  <|>
  "C" token  <|>
  "D" token  <|>
  "E" token  <|>
  "H" token  <|>
  "L" token  <|> [ register-lookup ] <@ ;

: all-flags ( -- parser )
  #! A parser for 16-bit flags. 
  "NZ" token  
  "NC" token <|>
  "PO" token <|>
  "PE" token <|> 
  "Z" token <|> 
  "C" token <|> 
  "P" token <|> 
  "M" token <|> [ flag-lookup ] <@ ;

: 16-bit-registers ( -- parser )
  #! A parser for 16-bit registers. On a successfull parse the

  "RRA" simple-instruction ;

: DEC-R-instruction ( -- parser )
  [
    "DEC-R" "DEC" complex-instruction ,
    8-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: DEC-RR-instruction ( -- parser )
  [
    "DEC-RR" "DEC" complex-instruction ,
    16-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: DEC-(RR)-instruction ( -- parser )
  [
    "DEC-(RR)" "DEC" complex-instruction ,
    16-bit-registers indirect sp ,
  ] seq* [ first2 swap curry ] action ;

: POP-RR-instruction ( -- parser )
  [
    "POP-RR" "POP" complex-instruction ,
    all-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: PUSH-RR-instruction ( -- parser )
  [
    "PUSH-RR" "PUSH" complex-instruction ,
    all-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: INC-R-instruction ( -- parser )
  [
    "INC-R" "INC" complex-instruction ,
    8-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: INC-RR-instruction ( -- parser )
  [
    "INC-RR" "INC" complex-instruction ,
    16-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;
   
: INC-(RR)-instruction  ( -- parser )
  [
    "INC-(RR)" "INC" complex-instruction ,
    all-registers indirect sp ,
  ] seq* [ first2 swap curry ] action ;

: RET-F|FF-instruction ( -- parser )
  [
    "RET-F|FF" "RET" complex-instruction ,  
    all-flags sp ,
  ] seq* [ first2 swap curry ] action ;

: AND-N-instruction ( -- parser )
  [
    "AND-N" "AND" complex-instruction ,
    "n" token sp hide ,
  ] seq* [ first { } swap curry ] action ;

: AND-R-instruction  ( -- parser )
  [
    "AND-R" "AND" complex-instruction ,
    8-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: AND-(RR)-instruction  ( -- parser )
  [
    "AND-(RR)" "AND" complex-instruction ,
    16-bit-registers indirect sp ,
  ] seq* [ first2 swap curry ] action ;

: XOR-N-instruction ( -- parser )
  [
    "XOR-N" "XOR" complex-instruction ,
    "n" token sp hide ,
  ] seq* [ first { } swap curry  ] action ;

: XOR-R-instruction  ( -- parser )
  [
    "XOR-R" "XOR" complex-instruction ,
    8-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: XOR-(RR)-instruction  ( -- parser )
  [
    "XOR-(RR)" "XOR" complex-instruction ,
    16-bit-registers indirect sp ,
  ] seq* [ first2 swap curry ] action ;

: OR-N-instruction ( -- parser )
  [
    "OR-N" "OR" complex-instruction ,
    "n" token sp hide ,
  ] seq* [ first { } swap curry  ] action ;

: OR-R-instruction  ( -- parser )
  [
    "OR-R" "OR" complex-instruction ,
    8-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: OR-(RR)-instruction  ( -- parser )
  [
    "OR-(RR)" "OR" complex-instruction ,
    16-bit-registers indirect sp ,
  ] seq* [ first2 swap curry ] action ;

: CP-N-instruction ( -- parser )
  [
    "CP-N" "CP" complex-instruction ,
    "n" token sp hide ,
  ] seq* [ first { } swap curry ] action ;

: CP-R-instruction  ( -- parser )
  [
    "CP-R" "CP" complex-instruction ,
    8-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;

: CP-(RR)-instruction  ( -- parser )
  [
    "CP-(RR)" "CP" complex-instruction ,
    16-bit-registers indirect sp ,
  ] seq* [ first2 swap curry ] action ;

: ADC-R,N-instruction ( -- parser )
  [
    "ADC-R,N" "ADC" complex-instruction ,
    8-bit-registers sp ,
    ",n" token hide ,
  ] seq* [ first2 swap curry ] action ;  

: ADC-R,R-instruction ( -- parser )
  [
    "ADC-R,R" "ADC" complex-instruction ,
    8-bit-registers sp ,
    "," token hide , 
    8-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: ADC-R,(RR)-instruction ( -- parser )
  [
    "ADC-R,(RR)" "ADC" complex-instruction ,
    8-bit-registers sp ,
    "," token hide , 
    16-bit-registers indirect ,
  ] seq* [ first3 append swap curry ] action ;  

: SBC-R,N-instruction ( -- parser )
  [
    "SBC-R,N" "SBC" complex-instruction ,
    8-bit-registers sp ,
    ",n" token hide ,
  ] seq* [ first2 swap curry ] action ;  

: SBC-R,R-instruction ( -- parser )
  [
    "SBC-R,R" "SBC" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    8-bit-registers ,
  ] seq* [ first3 append swap curry  ] action ;  

: SBC-R,(RR)-instruction ( -- parser )
  [
    "SBC-R,(RR)" "SBC" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    16-bit-registers indirect ,
  ] seq* [ first3 append swap curry  ] action ;  

: SUB-R-instruction ( -- parser )
  [
    "SUB-R" "SUB" complex-instruction ,
    8-bit-registers sp ,
  ] seq* [ first2 swap curry ] action ;  

: SUB-(RR)-instruction ( -- parser )
  [
    "SUB-(RR)" "SUB" complex-instruction ,
    16-bit-registers indirect sp ,
  ] seq* [ first2 swap curry ] action ;  

: SUB-N-instruction ( -- parser )
  [
    "SUB-N" "SUB" complex-instruction ,
    "n" token sp hide ,
  ] seq* [ first { } swap curry  ] action ;

: ADD-R,N-instruction ( -- parser )
  [
    "ADD-R,N" "ADD" complex-instruction ,
    8-bit-registers sp ,
    ",n" token hide ,
  ] seq* [ first2 swap curry ] action ;  

: ADD-R,R-instruction ( -- parser )
  [
    "ADD-R,R" "ADD" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    8-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: ADD-RR,RR-instruction ( -- parser )
  [
    "ADD-RR,RR" "ADD" complex-instruction ,
    16-bit-registers sp ,
    "," token hide , 
    16-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: ADD-R,(RR)-instruction ( -- parser )
  [
    "ADD-R,(RR)" "ADD" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    16-bit-registers indirect ,
  ] seq* [ first3 append swap curry ] action ;  
  
: LD-RR,NN-instruction ( -- parser )
  #! LD BC,nn
  [
    "LD-RR,NN" "LD" complex-instruction ,
    16-bit-registers sp ,
    ",nn" token hide ,
  ] seq* [ first2 swap curry ] action ;

: LD-R,N-instruction ( -- parser )
  #! LD B,n
  [
    "LD-R,N" "LD" complex-instruction ,
    8-bit-registers sp ,
    ",n" token hide ,
  ] seq* [ first2 swap curry ] action ;
  
: LD-(RR),N-instruction ( -- parser ) 
  [
    "LD-(RR),N" "LD" complex-instruction ,
    16-bit-registers indirect sp , 
    ",n" token hide ,
  ] seq* [ first2 swap curry ] action ;

: LD-(RR),R-instruction ( -- parser )
  #! LD (BC),A
  [
    "LD-(RR),R" "LD" complex-instruction ,
    16-bit-registers indirect sp ,
    "," token hide ,
    8-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: LD-R,R-instruction ( -- parser )
  [
    "LD-R,R" "LD" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    8-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: LD-RR,RR-instruction ( -- parser )
  [
    "LD-RR,RR" "LD" complex-instruction ,
    16-bit-registers sp ,
    "," token hide ,
    16-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: LD-R,(RR)-instruction ( -- parser )
  [
    "LD-R,(RR)" "LD" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    16-bit-registers indirect ,
  ] seq* [ first3 append swap curry ] action ;  

: LD-(NN),RR-instruction ( -- parser )
  [
    "LD-(NN),RR" "LD" complex-instruction ,
    "nn" token indirect sp hide ,
    "," token hide ,
    16-bit-registers ,
  ] seq* [ first2 swap curry ] action ;

: LD-(NN),R-instruction ( -- parser )
  [
    "LD-(NN),R" "LD" complex-instruction ,
    "nn" token indirect sp hide ,
    "," token hide ,
    8-bit-registers ,
  ] seq* [ first2 swap curry ] action ;

: LD-RR,(NN)-instruction ( -- parser )
  [
    "LD-RR,(NN)" "LD" complex-instruction ,
    16-bit-registers sp ,
    "," token hide ,
    "nn" token indirect hide ,
  ] seq* [ first2 swap curry ] action ;

: LD-R,(NN)-instruction ( -- parser )
  [
    "LD-R,(NN)" "LD" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    "nn" token indirect hide ,
  ] seq* [ first2 swap curry ] action ;

: OUT-(N),R-instruction ( -- parser )
  [
    "OUT-(N),R" "OUT" complex-instruction ,
    "n" token indirect sp hide ,
    "," token hide ,
    8-bit-registers ,
  ] seq* [ first2 swap curry ] action ;

: IN-R,(N)-instruction ( -- parser )
  [
    "IN-R,(N)" "IN" complex-instruction ,
    8-bit-registers sp ,
    "," token hide ,
    "n" token indirect hide ,
  ] seq* [ first2 swap curry ] action ;

: EX-(RR),RR-instruction ( -- parser )
  [
    "EX-(RR),RR" "EX" complex-instruction ,
    16-bit-registers indirect sp , 
    "," token hide ,
    16-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: EX-RR,RR-instruction ( -- parser )
  [
    "EX-RR,RR" "EX" complex-instruction ,
    16-bit-registers sp , 
    "," token hide ,
    16-bit-registers ,
  ] seq* [ first3 append swap curry ] action ;  

: 8080-generator-parser ( -- parser )
  [ 
    NOP-instruction  ,
    RST-0-instruction , 
    RST-8-instruction , 
    RST-10H-instruction , 
    RST-18H-instruction , 
    RST-20H-instruction , 
    RST-28H-instruction , 
    RST-30H-instruction , 
    RST-38H-instruction , 
    JP-F|FF,NN-instruction , 
    JP-NN-instruction , 
    JP-(RR)-instruction , 
    CALL-F|FF,NN-instruction , 
    CALL-NN-instruction , 
    CPL-instruction , 
    CCF-instruction , 
    SCF-instruction , 
    DAA-instruction , 
    RLA-instruction , 
    RRA-instruction , 
    RLCA-instruction , 
    RRCA-instruction , 
    HALT-instruction , 
    DI-instruction , 
    EI-instruction , 
    AND-N-instruction , 
    AND-R-instruction , 
    AND-(RR)-instruction , 
    XOR-N-instruction , 
    XOR-R-instruction , 
    XOR-(RR)-instruction , 
    OR-N-instruction , 
    OR-R-instruction , 
    OR-(RR)-instruction , 
    CP-N-instruction , 
    CP-R-instruction , 
    CP-(RR)-instruction , 
    DEC-RR-instruction , 
    DEC-R-instruction , 
    DEC-(RR)-instruction , 
    POP-RR-instruction , 
    PUSH-RR-instruction , 
    INC-RR-instruction , 
    INC-R-instruction , 
    INC-(RR)-instruction ,
    LD-RR,NN-instruction , 
    LD-RR,RR-instruction , 
    LD-R,N-instruction , 
    LD-R,R-instruction , 
    LD-(RR),N-instruction , 
    LD-(RR),R-instruction , 
    LD-R,(RR)-instruction , 
    LD-(NN),RR-instruction , 
    LD-(NN),R-instruction , 
    LD-RR,(NN)-instruction , 
    LD-R,(NN)-instruction , 
    ADC-R,(RR)-instruction , 
    ADC-R,N-instruction , 
    ADC-R,R-instruction , 
    ADD-R,N-instruction , 
    ADD-R,(RR)-instruction , 
    ADD-R,R-instruction , 
    ADD-RR,RR-instruction , 
    SBC-R,N-instruction , 
    SBC-R,R-instruction , 
    SBC-R,(RR)-instruction , 
    SUB-R-instruction , 
    SUB-(RR)-instruction , 
    SUB-N-instruction , 
    RET-F|FF-instruction , 
    RET-NN-instruction ,
    OUT-(N),R-instruction ,
    IN-R,(N)-instruction ,
    EX-(RR),RR-instruction ,
    EX-RR,RR-instruction ,
  ] choice* [ call ] action ;

: instruction-quotations ( string -- emulate-quot )
  #! Given an instruction string, return the emulation quotation for
  #! it. This will later be expanded to produce the disassembly and
  #! assembly quotations.
  8080-generator-parser parse ;

SYMBOL: last-instruction
SYMBOL: last-opcode
