]> git.donarmstrong.com Git - kiibohd-kll.git/blobdiff - kll.py
Fixing kll permutation expansion bug.
[kiibohd-kll.git] / kll.py
diff --git a/kll.py b/kll.py
index 1e477a1efb0103ad65f45923ebdd38f8f609ab6a..57fd37aa37ce39783f7ae4d442601782d009fbdf 100755 (executable)
--- a/kll.py
+++ b/kll.py
@@ -61,12 +61,17 @@ argparse._ = textFormatter_gettext
 
 ### Argument Parsing ###
 
+def checkFileExists( filename ):
+       if not os.path.isfile( filename ):
+               print ( "{0} {1} does not exist...".format( ERROR, filename ) )
+               sys.exit( 1 )
+
 def processCommandLineArgs():
        # Setup argument processor
        pArgs = argparse.ArgumentParser(
                usage="%(prog)s [options] <file1>...",
                description="Generates .h file state tables and pointer indices from KLL .kll files.",
-               epilog="Example: {0} TODO".format( os.path.basename( sys.argv[0] ) ),
+               epilog="Example: {0} mykeyboard.kll -d colemak.kll -p hhkbpro2.kll -p symbols.kll".format( os.path.basename( sys.argv[0] ) ),
                formatter_class=argparse.RawTextHelpFormatter,
                add_help=False,
 )
@@ -79,16 +84,24 @@ def processCommandLineArgs():
        pArgs.add_argument( '-b', '--backend', type=str, default="kiibohd",
                help="Specify target backend for the KLL compiler.\n"
                "Default: kiibohd" )
+       pArgs.add_argument( '-d', '--default', type=str, nargs='+',
+               help="Specify .kll files to layer on top of the default map to create a combined map." )
        pArgs.add_argument( '-p', '--partial', type=str, nargs='+', action='append',
                help="Specify .kll files to generate partial map, multiple files per flag.\n"
                "Each -p defines another partial map.\n"
                "Base .kll files (that define the scan code maps) must be defined for each partial map." )
-       pArgs.add_argument( '-t', '--template', type=str, default="templateKeymap.h",
+       pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h",
                help="Specify template used to generate the keymap.\n"
-               "Default: templateKeymap.h" )
-       pArgs.add_argument( '-o', '--output', type=str, default="templateKeymap.h",
+               "Default: templates/kiibohdKeymap.h" )
+       pArgs.add_argument( '--defines-template', type=str, default="templates/kiibohdDefs.h",
+               help="Specify template used to generate kll_defs.h.\n"
+               "Default: templates/kiibohdDefs.h" )
+       pArgs.add_argument( '-o', '--output', type=str, default="generatedKeymap.h",
                help="Specify output file. Writes to current working directory by default.\n"
                "Default: generatedKeymap.h" )
+       pArgs.add_argument( '--defines-output', type=str, default="kll_defs.h",
+               help="Specify output path for kll_defs.h. Writes to current working directory by default.\n"
+               "Default: kll_defs.h" )
        pArgs.add_argument( '-h', '--help', action="help",
                help="This message." )
 
@@ -96,24 +109,26 @@ def processCommandLineArgs():
        args = pArgs.parse_args()
 
        # Parameters
-       defaultFiles = args.files
+       baseFiles = args.files
+       defaultFiles = args.default
        partialFileSets = args.partial
+       if defaultFiles is None:
+               defaultFiles = []
        if partialFileSets is None:
                partialFileSets = [[]]
 
        # Check file existance
+       for filename in baseFiles:
+               checkFileExists( filename )
+
        for filename in defaultFiles:
-               if not os.path.isfile( filename ):
-                       print ( "{0} {1} does not exist...".format( ERROR, filename ) )
-                       sys.exit( 1 )
+               checkFileExists( filename )
 
        for partial in partialFileSets:
                for filename in partial:
-                       if not os.path.isfile( filename ):
-                               print ( "{0} {1} does not exist...".format( ERROR, filename ) )
-                               sys.exit( 1 )
+                       checkFileExists( filename )
 
-       return (defaultFiles, partialFileSets, args.backend, args.template, args.output)
+       return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.defines_template, args.output, args.defines_output)
 
 
 
@@ -133,10 +148,10 @@ def tokenize( string ):
                ( 'CodeEnd',          ( r'\]', ) ),
                ( 'String',           ( r'"[^"]*"', VERBOSE ) ),
                ( 'SequenceString',   ( r"'[^']*'", ) ),
+               ( 'Operator',         ( r'=>|:\+|:-|:|=', ) ),
                ( 'Comma',            ( r',', ) ),
                ( 'Dash',             ( r'-', ) ),
                ( 'Plus',             ( r'\+', ) ),
-               ( 'Operator',         ( r'=>|:|=', ) ),
                ( 'Parenthesis',      ( r'\(|\)', ) ),
                ( 'Number',           ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ),
                ( 'Name',             ( r'[A-Za-z_][A-Za-z_0-9]*', ) ),
@@ -155,9 +170,8 @@ def tokenize( string ):
 ### Parsing ###
 
  ## Map Arrays
-scanCode_map      = [ None ] * 0xFF # Define 8 bit address width
-usbCode_map       = [ None ] * 0xFF # Define 8 bit address width
-variable_dict     = dict()
+macros_map        = Macros()
+variables_dict    = Variables()
 capabilities_dict = Capabilities()
 
 
@@ -247,6 +261,9 @@ def make_seqString( token ):
 def make_string( token ):
        return token[1:-1]
 
+def make_unseqString( token ):
+       return token[1:-1]
+
 def make_number( token ):
        return int( token, 0 )
 
@@ -301,6 +318,9 @@ eol         = a( Token( 'EndOfLine', ';' ) )
 def listElem( item ):
        return [ item ]
 
+def listToTuple( items ):
+       return tuple( items )
+
   # Flatten only the top layer (list of lists of ...)
 def oneLayerFlatten( items ):
        mainList = []
@@ -310,6 +330,21 @@ def oneLayerFlatten( items ):
 
        return mainList
 
+  # Capability arguments may need to be expanded (e.g. 1 16 bit argument needs to be 2 8 bit arguments for the state machine)
+def capArgExpander( items ):
+       newArgs = []
+       # For each defined argument in the capability definition
+       for arg in range( 0, len( capabilities_dict[ items[0] ][1] ) ):
+               argLen = capabilities_dict[ items[0] ][1][ arg ][1]
+               num = items[1][ arg ]
+               byteForm = num.to_bytes( argLen, byteorder='little' ) # XXX Yes, little endian from how the uC structs work
+
+               # For each sub-argument, split into byte-sized chunks
+               for byte in range( 0, argLen ):
+                       newArgs.append( byteForm[ byte ] )
+
+       return tuple( [ items[0], tuple( newArgs ) ] )
+
   # Expand ranges of values in the 3rd dimension of the list, to a list of 2nd lists
   # i.e. [ sequence, [ combo, [ range ] ] ] --> [ [ sequence, [ combo ] ], <option 2>, <option 3> ]
 def optionExpansion( sequences ):
@@ -352,31 +387,61 @@ def optionExpansion( sequences ):
                                curLeafList[ leaf ] = 0
                                if leaf + 1 < len( curLeafList ):
                                        curLeafList[ leaf + 1 ] += 1
-                               break
 
        return expandedSequences
 
 
- ## Evaluation Rules
+# Converts USB Codes into Capabilities
+def usbCodeToCapability( items ):
+       # Items already converted to variants using optionExpansion
+       for variant in range( 0, len( items ) ):
+               # Sequence of Combos
+               for sequence in range( 0, len( items[ variant ] ) ):
+                       for combo in range( 0, len( items[ variant ][ sequence ] ) ):
+                               # Only convert if an integer, otherwise USB Code doesn't need converting
+                               if isinstance( items[ variant ][ sequence ][ combo ], int ):
+                                       # Use backend capability name and a single argument
+                                       items[ variant ][ sequence ][ combo ] = tuple( [ backend.usbCodeCapability(), tuple( [ items[ variant ][ sequence ][ combo ] ] ) ] )
 
-def eval_scanCode( trigger, result ):
-       # Convert to lists of lists of lists to tuples of tuples of tuples
-       trigger = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in trigger )
-       result  = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in result )
+       return items
 
-       # Add to the base scanCode map, overwrite if already defined
-#      if scanCode_map[ trigger ] != None:
-#              print ( "ScanCodeMap - Replacing '{0}' with '{1}' -> {2}".format( scanCode_map[ trigger ], result, trigger ) )
-#      scanCode_map[ trigger ] = result
 
-def eval_usbCode( trigger, result ):
-       # Check if trigger is list
+ ## Evaluation Rules
 
-       # Add to the base usbCode map, overwrite if already defined
-       if usbCode_map[ trigger ] != None:
-               print ( "USBCodeMap - Replacing '{0}' with '{1}' -> {2}".format( usbCode_map[ trigger ], result, trigger ) )
-       usbCode_map[ trigger ] = result
-       print ( trigger )
+def eval_scanCode( triggers, operator, results ):
+       # Convert to lists of lists of lists to tuples of tuples of tuples
+       # Tuples are non-mutable, and can be used has index items
+       triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers )
+       results  = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in results )
+
+       # Iterate over all combinations of triggers and results
+       for trigger in triggers:
+               for result in results:
+                       # Append Case
+                       if operator == ":+":
+                               macros_map.appendScanCode( trigger, result )
+
+                       # Remove Case
+                       elif operator == ":-":
+                               macros_map.removeScanCode( trigger, result )
+
+                       # Replace Case
+                       elif operator == ":":
+                               macros_map.replaceScanCode( trigger, result )
+
+def eval_usbCode( triggers, operator, results ):
+       # Convert to lists of lists of lists to tuples of tuples of tuples
+       # Tuples are non-mutable, and can be used has index items
+       triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers )
+       results  = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in results )
+
+       # Iterate over all combinations of triggers and results
+       for trigger in triggers:
+               scanCodes = macros_map.lookupUSBCodes( trigger )
+               for scanCode in scanCodes:
+                       for result in results:
+                               # Cache assignment until file finishes processing
+                               macros_map.cacheAssignment( operator, scanCode, result )
 
 def eval_variable( name, content ):
        # Content might be a concatenation of multiple data types, convert everything into a single string
@@ -384,31 +449,36 @@ def eval_variable( name, content ):
        for item in content:
                assigned_content += str( item )
 
-       variable_dict[ name ] = assigned_content
+       variables_dict.assignVariable( name, assigned_content )
 
 def eval_capability( name, function, args ):
        capabilities_dict[ name ] = [ function, args ]
 
+def eval_define( name, cdefine_name ):
+       variables_dict.defines[ name ] = cdefine_name
+
 map_scanCode   = unarg( eval_scanCode )
 map_usbCode    = unarg( eval_usbCode )
 
 set_variable   = unarg( eval_variable )
 set_capability = unarg( eval_capability )
+set_define     = unarg( eval_define )
 
 
  ## Sub Rules
 
-usbCode   = tokenType('USBCode') >> make_usbCode
-scanCode  = tokenType('ScanCode') >> make_scanCode
-name      = tokenType('Name')
-number    = tokenType('Number') >> make_number
-comma     = tokenType('Comma')
-dash      = tokenType('Dash')
-plus      = tokenType('Plus')
-content   = tokenType('VariableContents')
-string    = tokenType('String') >> make_string
-unString  = tokenType('String') # When the double quotes are still needed for internal processing
-seqString = tokenType('SequenceString') >> make_seqString
+usbCode     = tokenType('USBCode') >> make_usbCode
+scanCode    = tokenType('ScanCode') >> make_scanCode
+name        = tokenType('Name')
+number      = tokenType('Number') >> make_number
+comma       = tokenType('Comma')
+dash        = tokenType('Dash')
+plus        = tokenType('Plus')
+content     = tokenType('VariableContents')
+string      = tokenType('String') >> make_string
+unString    = tokenType('String') # When the double quotes are still needed for internal processing
+seqString   = tokenType('SequenceString') >> make_seqString
+unseqString = tokenType('SequenceString') >> make_unseqString # For use with variables
 
   # Code variants
 code_end = tokenType('CodeEnd')
@@ -435,36 +505,40 @@ usbCode_combo       = oneplus( ( usbCode_expanded | usbCode_elem ) + skip( maybe
 usbCode_sequence    = oneplus( ( usbCode_combo | seqString ) + skip( maybe( comma ) ) ) >> oneLayerFlatten
 
   # Capabilities
-capFunc_arguments = number + skip( maybe( comma ) )
-capFunc_elem      = name + skip( parenthesis('(') ) + many( capFunc_arguments ) + skip( parenthesis(')') ) >> listElem
+capFunc_arguments = many( number + skip( maybe( comma ) ) ) >> listToTuple
+capFunc_elem      = name + skip( parenthesis('(') ) + capFunc_arguments + skip( parenthesis(')') ) >> capArgExpander >> listElem
 capFunc_combo     = oneplus( ( usbCode_expanded | usbCode_elem | capFunc_elem ) + skip( maybe( plus ) ) ) >> listElem
 capFunc_sequence  = oneplus( ( capFunc_combo | seqString ) + skip( maybe( comma ) ) ) >> oneLayerFlatten
 
   # Trigger / Result Codes
 triggerCode_outerList    = scanCode_sequence >> optionExpansion
-triggerUSBCode_outerList = usbCode_sequence >> optionExpansion
-resultCode_outerList     = capFunc_sequence >> optionExpansion
+triggerUSBCode_outerList = usbCode_sequence >> optionExpansion >> usbCodeToCapability
+resultCode_outerList     = capFunc_sequence >> optionExpansion >> usbCodeToCapability
 
 
  ## Main Rules
 
 #| <variable> = <variable contents>;
-variable_contents   = name | content | string | number | comma | dash
+variable_contents   = name | content | string | number | comma | dash | unseqString
 variable_expression = name + skip( operator('=') ) + oneplus( variable_contents ) + skip( eol ) >> set_variable
 
 #| <capability name> => <c function>;
 capability_arguments  = name + skip( operator(':') ) + number + skip( maybe( comma ) )
 capability_expression = name + skip( operator('=>') ) + name + skip( parenthesis('(') ) + many( capability_arguments ) + skip( parenthesis(')') ) + skip( eol ) >> set_capability
 
+#| <define name> => <c define>;
+define_expression = name + skip( operator('=>') ) + name + skip( eol ) >> set_define
+
 #| <trigger> : <result>;
-scanCode_expression = triggerCode_outerList + skip( operator(':') ) + resultCode_outerList + skip( eol ) >> map_scanCode
-usbCode_expression  = triggerUSBCode_outerList + skip( operator(':') ) + resultCode_outerList + skip( eol ) #>> map_usbCode
+operatorTriggerResult = operator(':') | operator(':+') | operator(':-')
+scanCode_expression   = triggerCode_outerList + operatorTriggerResult + resultCode_outerList + skip( eol ) >> map_scanCode
+usbCode_expression    = triggerUSBCode_outerList + operatorTriggerResult + resultCode_outerList + skip( eol ) >> map_usbCode
 
 def parse( tokenSequence ):
        """Sequence(Token) -> object"""
 
        # Top-level Parser
-       expression = scanCode_expression | usbCode_expression | variable_expression | capability_expression
+       expression = scanCode_expression | usbCode_expression | variable_expression | capability_expression | define_expression
 
        kll_text = many( expression )
        kll_file = maybe( kll_text ) + skip( finished )
@@ -473,32 +547,63 @@ def parse( tokenSequence ):
 
 
 
+def processKLLFile( filename ):
+       with open( filename ) as file:
+               data = file.read()
+               tokenSequence = tokenize( data )
+               #print ( pformat( tokenSequence ) ) # Display tokenization
+               tree = parse( tokenSequence )
+
+
+
 ### Main Entry Point ###
 
 if __name__ == '__main__':
-       (defaultFiles, partialFileSets, backend_name, template, output) = processCommandLineArgs()
+       (baseFiles, defaultFiles, partialFileSets, backend_name, template, defines_template, output, defines_output) = processCommandLineArgs()
 
        # Load backend module
        global backend
        backend_import = importlib.import_module( "backends.{0}".format( backend_name ) )
-       backend = backend_import.Backend( template )
+       backend = backend_import.Backend( template, defines_template )
+
+       # Process base layout files
+       for filename in baseFiles:
+               variables_dict.setCurrentFile( filename )
+               processKLLFile( filename )
+       macros_map.completeBaseLayout() # Indicates to macros_map that the base layout is complete
+       variables_dict.baseLayoutFinished()
 
-       #TODO Move elsewhere
+       # Default combined layer
        for filename in defaultFiles:
-               with open( filename ) as file:
-                       data = file.read()
-
-                       tokenSequence = tokenize( data )
-                       print ( pformat( tokenSequence ) )
-                       tree = parse( tokenSequence )
-                       #print ( tree )
-                       #print ( scanCode_map )
-                       #print ( usbCode_map )
-                       print ( variable_dict )
-                       print ( capabilities_dict )
-
-       # TODO Move
-       backend.process( capabilities_dict )
+               variables_dict.setCurrentFile( filename )
+               processKLLFile( filename )
+               # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
+               macros_map.replayCachedAssignments()
+
+       # Iterate through additional layers
+       for partial in partialFileSets:
+               # Increment layer for each -p option
+               macros_map.addLayer()
+               variables_dict.incrementLayer() # DefaultLayer is layer 0
+
+               # Iterate and process each of the file in the layer
+               for filename in partial:
+                       variables_dict.setCurrentFile( filename )
+                       processKLLFile( filename )
+
+               # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
+               macros_map.replayCachedAssignments()
+               # Remove un-marked keys to complete the partial layer
+               macros_map.removeUnmarked()
+
+       # Do macro correlation and transformation
+       macros_map.generate()
+
+       # Process needed templating variables using backend
+       backend.process( capabilities_dict, macros_map, variables_dict )
+
+       # Generate output file using template and backend
+       backend.generate( output, defines_output )
 
        # Successful Execution
        sys.exit( 0 )