# Path: nesasm/directives.py
# def d_inesprg(arg, cart):
# def d_ineschr(arg, cart):
# def d_inesmap(arg, cart):
# def d_inesmir(arg, cart):
# def d_bank(arg, cart):
# def d_org(arg, cart):
# def d_db(arg, cart):
# def d_dw(arg, cart):
# def d_incbin(arg, cart):
# def d_rsset(arg, cart):
# def d_rs(arg, cart):
# 
# Path: nesasm/cartridge.py
# class Cartridge(object):
# 
#     def __init__(self):
#         self.banks = {}
#         self.bank_id = 0
#         self.pc = 0
#         self.inespgr = 1
#         self.ineschr = 1
#         self.inesmap = 1
#         self.inesmir = 1
#         self.rs = 0
#         self.path = ''
# 
#     def nes_get_header(self):
#         unused = [0, 0, 0, 0, 0, 0, 0, 0]
#         header = []
#         header.extend(NES_ID)
#         header.append(self.inespgr)
#         header.append(self.ineschr)
#         header.append(self.inesmir)
#         header.append(self.inesmap)
#         header.extend(unused)
#         return header
# 
#     def set_iNES_prg(self, inespgr):
#         self.inespgr = inespgr
# 
#     def set_iNES_chr(self, ineschr):
#         self.ineschr = ineschr
# 
#     def set_iNES_map(self, inesmap):
#         self.inesmap = inesmap
# 
#     def set_iNES_mir(self, inesmir):
#         self.inesmir = inesmir
# 
#     def set_bank_id(self, _id):
#         if _id not in self.banks:
#             self.banks[_id] = dict(code=[], start=None, size=(1024 * 8))
#         self.bank_id = _id
# 
#     def set_org(self, org):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         if not self.banks[self.bank_id]['start']:
#             self.banks[self.bank_id]['start'] = org
#             self.pc = org
#         else:
#             while self.pc < org:
#                 self.append_code([0xff])
#             self.pc = org
# 
#     def append_code(self, code):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         for c in code:
#             assert c <= 0xff
#         self.banks[self.bank_id]['code'].extend(code)
#         self.pc += len(code)
# 
#     def get_code(self):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         return self.banks[self.bank_id]['code']
# 
#     def get_ines_code(self):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         opcodes = []
#         nes_header = self.nes_get_header()
#         opcodes.extend(nes_header)
#         try:
#             _values = self.banks.itervalues
#         except AttributeError:
#             _values = self.banks.values
#         for bank in _values():
#             for _ in range(len(bank['code']), bank['size']):
#                 bank['code'].append(0xff)
#             opcodes.extend(bank['code'])
#         return opcodes


from re import match, I
from lexical import analyse
from nesasm.c6502 import opcodes, address_mode_def
from nesasm.directives import directive_list
from nesasm.cartridge import Cartridge
    from os.path import dirname, splitext, realpath
import io

def get_constants(ast, cart):
    constants = {}
    for leaf in ast:
        if ('S_CONSTANT' == leaf['type']):
            name = leaf['children'][0]['value']
            value =get_value(leaf['children'][2])
            constants[name] = value
    return constants

def semantic(ast, iNES=False, cart=None):
    if cart is None:
        cart = Cartridge()
    labels = get_labels(ast, cart)
    constants = get_constants(ast, cart)
    address = 0
    # translate statments to opcode
    for leaf in ast:
        if leaf['type'] == 'S_RS':
            labels[leaf['children'][0]['value']] = cart.rs
            cart.rs += get_value(leaf['children'][2], labels, constants)
        elif leaf['type'] == 'S_CONSTANT':
            pass
        elif leaf['type'] == 'S_DIRECTIVE':
            directive = leaf['children'][0]['value']
            if len(leaf['children']) == 2:
                argument = get_value(leaf['children'][1], labels, constants)
            else:
                argument = leaf['children'][1:]
            if directive in directive_list:

# Path: nesasm/directives.py
# def d_inesprg(arg, cart):
# def d_ineschr(arg, cart):
# def d_inesmap(arg, cart):
# def d_inesmir(arg, cart):
# def d_bank(arg, cart):
# def d_org(arg, cart):
# def d_db(arg, cart):
# def d_dw(arg, cart):
# def d_incbin(arg, cart):
# def d_rsset(arg, cart):
# def d_rs(arg, cart):
# 
# Path: nesasm/cartridge.py
# class Cartridge(object):
# 
#     def __init__(self):
#         self.banks = {}
#         self.bank_id = 0
#         self.pc = 0
#         self.inespgr = 1
#         self.ineschr = 1
#         self.inesmap = 1
#         self.inesmir = 1
#         self.rs = 0
#         self.path = ''
# 
#     def nes_get_header(self):
#         unused = [0, 0, 0, 0, 0, 0, 0, 0]
#         header = []
#         header.extend(NES_ID)
#         header.append(self.inespgr)
#         header.append(self.ineschr)
#         header.append(self.inesmir)
#         header.append(self.inesmap)
#         header.extend(unused)
#         return header
# 
#     def set_iNES_prg(self, inespgr):
#         self.inespgr = inespgr
# 
#     def set_iNES_chr(self, ineschr):
#         self.ineschr = ineschr
# 
#     def set_iNES_map(self, inesmap):
#         self.inesmap = inesmap
# 
#     def set_iNES_mir(self, inesmir):
#         self.inesmir = inesmir
# 
#     def set_bank_id(self, _id):
#         if _id not in self.banks:
#             self.banks[_id] = dict(code=[], start=None, size=(1024 * 8))
#         self.bank_id = _id
# 
#     def set_org(self, org):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         if not self.banks[self.bank_id]['start']:
#             self.banks[self.bank_id]['start'] = org
#             self.pc = org
#         else:
#             while self.pc < org:
#                 self.append_code([0xff])
#             self.pc = org
# 
#     def append_code(self, code):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         for c in code:
#             assert c <= 0xff
#         self.banks[self.bank_id]['code'].extend(code)
#         self.pc += len(code)
# 
#     def get_code(self):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         return self.banks[self.bank_id]['code']
# 
#     def get_ines_code(self):
#         if self.bank_id not in self.banks:
#             self.set_bank_id(self.bank_id)
#         opcodes = []
#         nes_header = self.nes_get_header()
#         opcodes.extend(nes_header)
#         try:
#             _values = self.banks.itervalues
#         except AttributeError:
#             _values = self.banks.values
#         for bank in _values():
#             for _ in range(len(bank['code']), bank['size']):
#                 bank['code'].append(0xff)
#             opcodes.extend(bank['code'])
#         return opcodes


# Path: nesasm/compiler.py
from re import match, I
from lexical import analyse
from nesasm.c6502 import opcodes, address_mode_def
from nesasm.directives import directive_list
from nesasm.cartridge import Cartridge
    from os.path import dirname, splitext, realpath
import io


def get_constants(ast, cart):
    constants = {}
    for leaf in ast:
        if ('S_CONSTANT' == leaf['type']):
            name = leaf['children'][0]['value']
            value =get_value(leaf['children'][2])
            constants[name] = value
    return constants

def semantic(ast, iNES=False, cart=None):
    if cart is None:
        cart = Cartridge()
    labels = get_labels(ast, cart)
    constants = get_constants(ast, cart)
    address = 0
    # translate statments to opcode
    for leaf in ast:
        if leaf['type'] == 'S_RS':
            labels[leaf['children'][0]['value']] = cart.rs
            cart.rs += get_value(leaf['children'][2], labels, constants)
        elif leaf['type'] == 'S_CONSTANT':
            pass
        elif leaf['type'] == 'S_DIRECTIVE':
            directive = leaf['children'][0]['value']
            if len(leaf['children']) == 2:
                argument = get_value(leaf['children'][1], labels, constants)
            else:
                argument = leaf['children'][1:]
            if directive in directive_list:

                directive_list[directive](argument, cart)