# 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
# -*- coding: utf-8 -*-
'''
Those tests are based on examples from:
http://nesdev.parodius.com/6502guid.txt
'''



class GuideTest(unittest.TestCase):

    def test_example_16_bit_subtraction_routine(self):
        ex_2 = '''
            SEC         ;clear the carry
            LDA $20     ;get the low byte of the first number
            SBC $22     ;add to it the low byte of the second
            STA $24     ;store in the low byte of the result
            LDA $21     ;get the high byte of the first number
            SBC $23     ;add to it the high byte of the second, plus carry
            STA $25     ;store in high byte of the result
            '''
        tokens = list(lexical(ex_2))
        self.assertEquals(21, len(tokens))

# 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

# -*- coding: utf-8 -*-
'''
Those tests are based on examples from:
http://nesdev.parodius.com/6502guid.txt
'''



class GuideTest(unittest.TestCase):

    def test_example_16_bit_subtraction_routine(self):
        ex_2 = '''
            SEC         ;clear the carry
            LDA $20     ;get the low byte of the first number
            SBC $22     ;add to it the low byte of the second
            STA $24     ;store in the low byte of the result
            LDA $21     ;get the high byte of the first number
            SBC $23     ;add to it the high byte of the second, plus carry
            STA $25     ;store in high byte of the result
            '''
        tokens = list(lexical(ex_2))
        self.assertEquals(21, len(tokens))

        ast = syntax(tokens)