# Path: wrd/parser.py
# def parse_command(data):
#   
#   header = data.peek("uint:8")
#   
#   if not header == CMD_MARKER:
#     raise InvalidWrdHeader("Command 0x%02X does not begin with 0x%02X" % (header, CMD_MARKER))
#   
#   data.read("uint:8")
#   op = data.read("uint:8")
#   
#   params = OrderedDict()
#   
#   if op in OP_PARAMS:
#     param_info = OP_PARAMS[op]
#     
#     # If it's a custom function, use that.
#     if isinstance(param_info, basestring):
#       if param_info in globals():
#         params = globals()[param_info](data)
#       else:
#         raise Exception("Function %s not defined." % param_info)
#     
#     # Otherwise, just use the param list.
#     else:
#       param_names = [info[0] for info in param_info]
#       param_types = [info[1] for info in param_info]
#       
#       param_vals  = data.readlist(param_types)
#       
#       for name, val in zip(param_names, param_vals):
#         
#         if name == None:
#           try:
#             params[None].append(val)
#           except:
#             params[None] = [val]
#         else:
#           params[name] = val
#   
#   else:
#     print "Unknown opcode encountered: 0x%02X" % op
#     
#   return op, params
# 
# class InvalidWrdHeader(Exception):
#   pass


import bitstring
from bitstring import BitStream, ConstBitStream
from wrd.ops import *
from wrd.parser import parse_command, InvalidWrdHeader
################################################################################
### Converts a list of commands to the binary format used by the game.
################################################################################
def to_bin(commands):

  data  = BitStream()
  lines = 0
  
  for op, params in commands:
    if op == WRD_HEADER:
      continue
    
    if not op in OP_PARAMS:
      # raise Exception("Unknown op: 0x%02X" % op)
      print "Unknown op: 0x%02X" % op
      continue
    
    param_info = OP_PARAMS[op]
    
    # If it has a custom parsing function, use the equivalent packing function.
    if isinstance(param_info, basestring):
      command = globals()[OP_FUNCTIONS[op]](**params)
      data.append(command)
    
    else:
      if op == WRD_SHOW_LINE:
        lines += 1
      
      data.append(bitstring.pack("uint:8, uint:8", CMD_MARKER, op))
      

# Path: wrd/parser.py
# def parse_command(data):
#   
#   header = data.peek("uint:8")
#   
#   if not header == CMD_MARKER:
#     raise InvalidWrdHeader("Command 0x%02X does not begin with 0x%02X" % (header, CMD_MARKER))
#   
#   data.read("uint:8")
#   op = data.read("uint:8")
#   
#   params = OrderedDict()
#   
#   if op in OP_PARAMS:
#     param_info = OP_PARAMS[op]
#     
#     # If it's a custom function, use that.
#     if isinstance(param_info, basestring):
#       if param_info in globals():
#         params = globals()[param_info](data)
#       else:
#         raise Exception("Function %s not defined." % param_info)
#     
#     # Otherwise, just use the param list.
#     else:
#       param_names = [info[0] for info in param_info]
#       param_types = [info[1] for info in param_info]
#       
#       param_vals  = data.readlist(param_types)
#       
#       for name, val in zip(param_names, param_vals):
#         
#         if name == None:
#           try:
#             params[None].append(val)
#           except:
#             params[None] = [val]
#         else:
#           params[name] = val
#   
#   else:
#     print "Unknown opcode encountered: 0x%02X" % op
#     
#   return op, params
# 
# class InvalidWrdHeader(Exception):
#   pass


# Path: wrd/bin.py
import bitstring
from bitstring import BitStream, ConstBitStream
from wrd.ops import *
from wrd.parser import parse_command, InvalidWrdHeader

################################################################################
### Converts a list of commands to the binary format used by the game.
################################################################################
def to_bin(commands):

  data  = BitStream()
  lines = 0
  
  for op, params in commands:
    if op == WRD_HEADER:
      continue
    
    if not op in OP_PARAMS:
      # raise Exception("Unknown op: 0x%02X" % op)
      print "Unknown op: 0x%02X" % op
      continue
    
    param_info = OP_PARAMS[op]
    
    # If it has a custom parsing function, use the equivalent packing function.
    if isinstance(param_info, basestring):
      command = globals()[OP_FUNCTIONS[op]](**params)
      data.append(command)
    
    else:
      if op == WRD_SHOW_LINE:
        lines += 1
      
      data.append(bitstring.pack("uint:8, uint:8", CMD_MARKER, op))
      

      unnamed_param_id = 0