# Path: msp430/asm/infix2postfix.py
# def infix2postfix(expression, variable_prefix='', scanner=Scanner, precedence=default_precedence):
#     """\
#     Convert an expression to postfix notation (RPN), respecting parentheses and
#     operator precedences.
# 
#     >>> infix2postfix(u'1+2')
#     u'1 2 +'
#     >>> infix2postfix(u'1+ 2')
#     u'1 2 +'
# 
#     # >>> infix2postfix(u'1 +2') # interpreted as unary plus, OK
#     # u'1 2 +
# 
#     >>> infix2postfix(u'1 + 2')
#     u'1 2 +'
#     >>> infix2postfix(u'1+2*3')
#     u'1 2 3 * +'
#     >>> infix2postfix(u'(1+2)*3')
#     u'1 2 + 3 *'
# 
#     # unary operators
#     >>> infix2postfix(u'( -1+2) * -3-4')
#     u'1 neg 2 + 3 neg * 4 -'
# 
#     >>> infix2postfix(u'1/2 == 3')
#     u'1 2 / 3 =='
#     >>> infix2postfix(u'1 < 2 or 3 < 4')
#     u'1 2 < 3 4 < or'
#     >>> infix2postfix(u'1 <= 2 and 3 >= 4')
#     u'1 2 <= 3 4 >= and'
#     >>> infix2postfix(u'7 & 3 != 0')
#     u'7 3 & 0 !='
# 
#     >>> infix2postfix('not 4 + 1')
#     u'4 1 + not'
#     >>> infix2postfix('~A + 1')
#     u'A ~ 1 +'
# 
#     """
#     operator_stack = []
#     output_string = []
#     s = scanner(expression)
#     while True:
#         token_type, token = s.scan()
#         #~ print token_type, token
#         if token_type is None: break
#         elif token_type == 'LEFT':
#             operator_stack.append(token)
#         elif token_type == 'VARIABLE':
#             output_string.append(u'%s%s' % (variable_prefix, token))
#         elif token_type == 'NUMBER':
#             output_string.append(token)
#         elif token_type == 'OPERATOR' or token_type == 'UNARYOPERATOR':
#             if (not operator_stack
#                     or operator_stack[-1] == '('
#                     or precedence[operator_stack[-1]] < precedence[token]):
#                 operator_stack.append(token)
#             else:
#                 while True:
#                     output_string.append(operator_stack.pop())
#                     if (not operator_stack
#                             or operator_stack[-1] == '('
#                             or precedence[token] >= precedence[operator_stack[-1]]):
#                         break
#                 operator_stack.append(token)
#         elif token_type == 'RIGHT':
#             while operator_stack[-1] != '(':
#                 output_string.append(operator_stack.pop())
#             operator_stack.pop()    # the '(' itself
#         else:
#             raise ValueError(u'unknown token: %r' % (token_type,))
# 
#     if '(' in operator_stack:
#         raise ValueError('Unbalanced (, )')
#     while operator_stack:
#         output_string.append(operator_stack.pop())
# 
#     return u' '.join(unicode(s) for s in output_string)


import re
import sys
import codecs
from msp430.asm.infix2postfix import infix2postfix
    from optparse import OptionParser
# Copyright (c) 2001-2010 Chris Liechti <cliechti@gmx.net>
# All Rights Reserved.
# Simplified BSD License (see LICENSE.txt for full text)

"""\
Assembler for TI MSP430.

Inputs are '.s' files. output is '.o4' for 'ld.py'
"""



# regular expressions used to parse the asm source
re_line_hint = re.compile(r'#[\t ]+(\d+)[\t ]+"(.+)"')

re_comment = re.compile(r'(;|//).*$')

re_asmstatement = re.compile(r'''^
    (((?P<LABEL>\S+):)?)
    ([\t ]*(?P<INSN>\.?\w+)(?P<MODE>\.\w)?
      ([\t ]+(?P<OPERANDS>.*)
      )?
    )?''', re.VERBOSE|re.IGNORECASE|re.UNICODE)

re_expression = re.compile(r'(?P<NAME>\w+?)=(?P<EXPR>.+)', re.UNICODE)

re_operand = re.compile(r'''
        (?P<STRING>         "(?P<STR>[^"\\]*?(\\.[^"\\]*?)*?)"     ) |
        (?P<SPACE>          \s+                         ) |
        (?P<DELIMITER>      ,                           ) |

# Path: msp430/asm/infix2postfix.py
# def infix2postfix(expression, variable_prefix='', scanner=Scanner, precedence=default_precedence):
#     """\
#     Convert an expression to postfix notation (RPN), respecting parentheses and
#     operator precedences.
# 
#     >>> infix2postfix(u'1+2')
#     u'1 2 +'
#     >>> infix2postfix(u'1+ 2')
#     u'1 2 +'
# 
#     # >>> infix2postfix(u'1 +2') # interpreted as unary plus, OK
#     # u'1 2 +
# 
#     >>> infix2postfix(u'1 + 2')
#     u'1 2 +'
#     >>> infix2postfix(u'1+2*3')
#     u'1 2 3 * +'
#     >>> infix2postfix(u'(1+2)*3')
#     u'1 2 + 3 *'
# 
#     # unary operators
#     >>> infix2postfix(u'( -1+2) * -3-4')
#     u'1 neg 2 + 3 neg * 4 -'
# 
#     >>> infix2postfix(u'1/2 == 3')
#     u'1 2 / 3 =='
#     >>> infix2postfix(u'1 < 2 or 3 < 4')
#     u'1 2 < 3 4 < or'
#     >>> infix2postfix(u'1 <= 2 and 3 >= 4')
#     u'1 2 <= 3 4 >= and'
#     >>> infix2postfix(u'7 & 3 != 0')
#     u'7 3 & 0 !='
# 
#     >>> infix2postfix('not 4 + 1')
#     u'4 1 + not'
#     >>> infix2postfix('~A + 1')
#     u'A ~ 1 +'
# 
#     """
#     operator_stack = []
#     output_string = []
#     s = scanner(expression)
#     while True:
#         token_type, token = s.scan()
#         #~ print token_type, token
#         if token_type is None: break
#         elif token_type == 'LEFT':
#             operator_stack.append(token)
#         elif token_type == 'VARIABLE':
#             output_string.append(u'%s%s' % (variable_prefix, token))
#         elif token_type == 'NUMBER':
#             output_string.append(token)
#         elif token_type == 'OPERATOR' or token_type == 'UNARYOPERATOR':
#             if (not operator_stack
#                     or operator_stack[-1] == '('
#                     or precedence[operator_stack[-1]] < precedence[token]):
#                 operator_stack.append(token)
#             else:
#                 while True:
#                     output_string.append(operator_stack.pop())
#                     if (not operator_stack
#                             or operator_stack[-1] == '('
#                             or precedence[token] >= precedence[operator_stack[-1]]):
#                         break
#                 operator_stack.append(token)
#         elif token_type == 'RIGHT':
#             while operator_stack[-1] != '(':
#                 output_string.append(operator_stack.pop())
#             operator_stack.pop()    # the '(' itself
#         else:
#             raise ValueError(u'unknown token: %r' % (token_type,))
# 
#     if '(' in operator_stack:
#         raise ValueError('Unbalanced (, )')
#     while operator_stack:
#         output_string.append(operator_stack.pop())
# 
#     return u' '.join(unicode(s) for s in output_string)


# Path: msp430/asm/as.py
import re
import sys
import codecs
from msp430.asm.infix2postfix import infix2postfix
    from optparse import OptionParser

# Copyright (c) 2001-2010 Chris Liechti <cliechti@gmx.net>
# All Rights Reserved.
# Simplified BSD License (see LICENSE.txt for full text)

"""\
Assembler for TI MSP430.

Inputs are '.s' files. output is '.o4' for 'ld.py'
"""



# regular expressions used to parse the asm source
re_line_hint = re.compile(r'#[\t ]+(\d+)[\t ]+"(.+)"')

re_comment = re.compile(r'(;|//).*$')

re_asmstatement = re.compile(r'''^
    (((?P<LABEL>\S+):)?)
    ([\t ]*(?P<INSN>\.?\w+)(?P<MODE>\.\w)?
      ([\t ]+(?P<OPERANDS>.*)
      )?
    )?''', re.VERBOSE|re.IGNORECASE|re.UNICODE)

re_expression = re.compile(r'(?P<NAME>\w+?)=(?P<EXPR>.+)', re.UNICODE)

re_operand = re.compile(r'''
        (?P<STRING>         "(?P<STR>[^"\\]*?(\\.[^"\\]*?)*?)"     ) |
        (?P<SPACE>          \s+                         ) |
        (?P<DELIMITER>      ,                           ) |

        (?P<IMMEDIATE>      \#(?P<IMM_VAL>[^,"]+)       ) |