# Path: nesasm/compiler.py
# def lexical(code):
#     return analyse(code, asm65_tokens)
# 
# def syntax(tokens):
#     tokens = list(tokens)
#     ast = []
#     x = 0  # consumed
#     # debug = 0
#     labels = []
#     move = 0
#     while x < len(tokens):
#         if t_label(tokens, x):
#             labels.append(get_value(tokens[x]))
#             x += 1
#         elif t_endline(tokens, x):
#             x += 1
#         else:
#             for bnf in asm65_bnf:
#                 leaf = {}
#                 look_ahead = 0
#                 move = 0
#                 for i in bnf['bnf']:
#                     move = i(tokens, x + look_ahead)
#                     if not move:
#                         break
#                     look_ahead += 1
#                 if move:
#                     if len(labels) > 0:
#                         leaf['labels'] = labels
#                         labels = []
#                     size = 0
#                     look_ahead = 0
#                     for b in bnf['bnf']:
#                         size += b(tokens, x + look_ahead)
#                         look_ahead += 1
#                     leaf['children'] = tokens[x: x + size]
#                     leaf['type'] = bnf['type']
#                     ast.append(leaf)
#                     x += size
#                     break
#             if not move:
#                 # TODO: deal with erros like on nodeNES
#                 # walk = 0
#                 raise Exception('Invalid Syntax')
#                 # rip syntax until the end of the line
# 
#     return ast


import unittest
from nesasm.compiler import lexical, syntax
                INY
                JMP delete
        deccnt: LDA ($30,X)  ; update element count of list
                SBC #$01
                STA ($30,X)
                RTS
        '''
        tokens = list(lexical(example_4_2))
        self.assertEquals(96, len(tokens))

    def test_example_5_6(self):
        """
        example_5_6 = '''
        ; Example 5-6.  16-bit by 16-bit unsigned multiply
        ;
        ; Multiply $22 (low) and $23 (high) by $20 (low) and
        ; $21 (high) producing a 32-bit result in $24 (low) to $27 (high)
        ;

        mlt16:  LDA #$00     ; clear p2 and p3 of product
                STA $26
                STA $27
                LDX #$16     ; multiplier bit count = 16
        nxtbt:  LSR $21      ; shift two-byte multiplier right
                ROR $20
                BCC align    ; multiplier = 1?
                LDA $26      ; yes. fetch p2
                CLC
                ADC $22      ; and add m0 to it
                STA $26      ; store new p2

# Path: nesasm/compiler.py
# def lexical(code):
#     return analyse(code, asm65_tokens)
# 
# def syntax(tokens):
#     tokens = list(tokens)
#     ast = []
#     x = 0  # consumed
#     # debug = 0
#     labels = []
#     move = 0
#     while x < len(tokens):
#         if t_label(tokens, x):
#             labels.append(get_value(tokens[x]))
#             x += 1
#         elif t_endline(tokens, x):
#             x += 1
#         else:
#             for bnf in asm65_bnf:
#                 leaf = {}
#                 look_ahead = 0
#                 move = 0
#                 for i in bnf['bnf']:
#                     move = i(tokens, x + look_ahead)
#                     if not move:
#                         break
#                     look_ahead += 1
#                 if move:
#                     if len(labels) > 0:
#                         leaf['labels'] = labels
#                         labels = []
#                     size = 0
#                     look_ahead = 0
#                     for b in bnf['bnf']:
#                         size += b(tokens, x + look_ahead)
#                         look_ahead += 1
#                     leaf['children'] = tokens[x: x + size]
#                     leaf['type'] = bnf['type']
#                     ast.append(leaf)
#                     x += size
#                     break
#             if not move:
#                 # TODO: deal with erros like on nodeNES
#                 # walk = 0
#                 raise Exception('Invalid Syntax')
#                 # rip syntax until the end of the line
# 
#     return ast


# Path: nesasm/tests/guide_test.py
import unittest
from nesasm.compiler import lexical, syntax

                INY
                JMP delete
        deccnt: LDA ($30,X)  ; update element count of list
                SBC #$01
                STA ($30,X)
                RTS
        '''
        tokens = list(lexical(example_4_2))
        self.assertEquals(96, len(tokens))

    def test_example_5_6(self):
        """
        example_5_6 = '''
        ; Example 5-6.  16-bit by 16-bit unsigned multiply
        ;
        ; Multiply $22 (low) and $23 (high) by $20 (low) and
        ; $21 (high) producing a 32-bit result in $24 (low) to $27 (high)
        ;

        mlt16:  LDA #$00     ; clear p2 and p3 of product
                STA $26
                STA $27
                LDX #$16     ; multiplier bit count = 16
        nxtbt:  LSR $21      ; shift two-byte multiplier right
                ROR $20
                BCC align    ; multiplier = 1?
                LDA $26      ; yes. fetch p2
                CLC
                ADC $22      ; and add m0 to it
                STA $26      ; store new p2

                LDA $27      ; fetch p3