# 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
        ; Example 5-14.  Simple 16-bit square root.
        ;
        ; Returns the 8-bit square root in $20 of the
        ; 16-bit number in $20 (low) and $21 (high). The
        ; remainder is in location $21.

        sqrt16: LDY #$01     ; lsby of first odd number = 1
                STY $22
                DEY
                STY $23      ; msby of first odd number (sqrt = 0)
        again:  SEC
                LDA $20      ; save remainder in X register
                TAX          ; subtract odd lo from integer lo
                SBC $22
                STA $20
                LDA $21      ; subtract odd hi from integer hi
                SBC $23
                STA $21      ; is subtract result negative?
                BCC nomore   ; no. increment square root
                INY
                LDA $22      ; calculate next odd number
                ADC #$01
                STA $22
                BCC again
                INC $23
                JMP again
        nomore: STY $20      ; all done, store square root
                STX $21      ; and remainder
                RTS
        '''

# 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

        ; Example 5-14.  Simple 16-bit square root.
        ;
        ; Returns the 8-bit square root in $20 of the
        ; 16-bit number in $20 (low) and $21 (high). The
        ; remainder is in location $21.

        sqrt16: LDY #$01     ; lsby of first odd number = 1
                STY $22
                DEY
                STY $23      ; msby of first odd number (sqrt = 0)
        again:  SEC
                LDA $20      ; save remainder in X register
                TAX          ; subtract odd lo from integer lo
                SBC $22
                STA $20
                LDA $21      ; subtract odd hi from integer hi
                SBC $23
                STA $21      ; is subtract result negative?
                BCC nomore   ; no. increment square root
                INY
                LDA $22      ; calculate next odd number
                ADC #$01
                STA $22
                BCC again
                INC $23
                JMP again
        nomore: STY $20      ; all done, store square root
                STX $21      ; and remainder
                RTS
        '''

        tokens = list(lexical(example_5_14))