Archived
1
0

Adding define support to KLL compiler.

- Variable support is not complete
- However PartialMap support is basically ready
This commit is contained in:
Jacob Alexander 2014-11-20 13:52:58 -08:00
parent db654bc56a
commit 6454917b11
5 changed files with 138 additions and 35 deletions

View File

@ -34,6 +34,7 @@ from kll_lib.containers import *
## Print Decorator Variables ## Print Decorator Variables
ERROR = '\033[5;1;31mERROR\033[0m:' ERROR = '\033[5;1;31mERROR\033[0m:'
WARNING = '\033[5;1;33mWARNING\033[0m:'
@ -42,12 +43,13 @@ ERROR = '\033[5;1;31mERROR\033[0m:'
class Backend: class Backend:
# Initializes backend # Initializes backend
# Looks for template file and builds list of fill tags # Looks for template file and builds list of fill tags
def __init__( self, templatePath ): def __init__( self, templatePath, definesTemplatePath ):
# Does template exist? # Does template exist?
if not os.path.isfile( templatePath ): if not os.path.isfile( templatePath ):
print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) ) print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
sys.exit( 1 ) sys.exit( 1 )
self.definesTemplatePath = definesTemplatePath
self.templatePath = templatePath self.templatePath = templatePath
self.fill_dict = dict() self.fill_dict = dict()
@ -58,6 +60,11 @@ class Backend:
match = re.findall( '<\|([^|>]+)\|>', line ) match = re.findall( '<\|([^|>]+)\|>', line )
for item in match: for item in match:
self.tagList.append( item ) self.tagList.append( item )
with open( definesTemplatePath, 'r' ) as openFile:
for line in openFile:
match = re.findall( '<\|([^|>]+)\|>', line )
for item in match:
self.tagList.append( item )
# USB Code Capability Name # USB Code Capability Name
@ -73,13 +80,13 @@ class Backend:
# Processes content for fill tags and does any needed dataset calculations # Processes content for fill tags and does any needed dataset calculations
def process( self, capabilities, macros ): def process( self, capabilities, macros, variables ):
## Information ## ## Information ##
# TODO # TODO
self.fill_dict['Information'] = "// This file was generated by the kll compiler, DO NOT EDIT.\n" self.fill_dict['Information'] = "// This file was generated by the kll compiler, DO NOT EDIT.\n"
self.fill_dict['Information'] += "// Generation Date: {0}\n".format( "TODO" ) self.fill_dict['Information'] += "// Generation Date: {0}\n".format( "TODO" )
self.fill_dict['Information'] += "// Compiler arguments: {0}\n".format( "TODO" ) self.fill_dict['Information'] += "// Compiler arguments: {0}\n".format( "TODO" )
self.fill_dict['Information'] += "// KLL Backend: {0}\n".format( "TODO" ) self.fill_dict['Information'] += "// KLL Backend: {0}\n".format( "kiibohd" )
self.fill_dict['Information'] += "// KLL Git Rev: {0}\n".format( "TODO" ) self.fill_dict['Information'] += "// KLL Git Rev: {0}\n".format( "TODO" )
self.fill_dict['Information'] += "//\n" self.fill_dict['Information'] += "//\n"
self.fill_dict['Information'] += "// - Base Layer -\n" self.fill_dict['Information'] += "// - Base Layer -\n"
@ -87,6 +94,25 @@ class Backend:
self.fill_dict['Information'] += "// - Partial Layers -\n" self.fill_dict['Information'] += "// - Partial Layers -\n"
## Variable Information ##
self.fill_dict['VariableInformation'] = ""
# Iterate through the variables, output, and indicate the last file that modified it's value
# Output separate tables per file, per table and overall
# TODO
## Defines ##
self.fill_dict['Defines'] = ""
# Iterate through defines and lookup the variables
for define in variables.defines.keys():
if define in variables.overallVariables.keys():
self.fill_dict['Defines'] += "\n#define {0} {1}".format( variables.defines[ define ], variables.overallVariables[ define ] )
else:
print( "{0} '{1}' not defined...".format( WARNING, define ) )
## Capabilities ## ## Capabilities ##
self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n" self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"
@ -272,9 +298,9 @@ class Backend:
# Generates the output keymap with fill tags filled # Generates the output keymap with fill tags filled
def generate( self, filepath ): def generate( self, outputPath, definesOutputPath ):
# Process each line of the template, outputting to the target path # Process each line of the template, outputting to the target path
with open( filepath, 'w' ) as outputFile: with open( outputPath, 'w' ) as outputFile:
with open( self.templatePath, 'r' ) as templateFile: with open( self.templatePath, 'r' ) as templateFile:
for line in templateFile: for line in templateFile:
# TODO Support multiple replacements per line # TODO Support multiple replacements per line
@ -290,3 +316,20 @@ class Backend:
else: else:
outputFile.write( line ) outputFile.write( line )
# Process each line of the defines template, outputting to the target path
with open( definesOutputPath, 'w' ) as outputFile:
with open( self.definesTemplatePath, 'r' ) as templateFile:
for line in templateFile:
# TODO Support multiple replacements per line
# TODO Support replacement with other text inline
match = re.findall( '<\|([^|>]+)\|>', line )
# If match, replace with processed variable
if match:
outputFile.write( self.fill_dict[ match[ 0 ] ] )
outputFile.write("\n")
# Otherwise, just append template to output file
else:
outputFile.write( line )

View File

@ -1,7 +1,14 @@
Name = colemak; Name = colemak;
Author = "HaaTa (Jacob Alexander) 2014"; Author = "HaaTa (Jacob Alexander) 2014";
KLL = 0.3; KLL = 0.3;
mydefine = "stuffs here";
mydefine2 = '"stuffs here"'; # For outputting c define strings
mynumber = 414;
mydefine => myCdef;
mydefine2 => myCdef2;
mydefine3 => myCdef3;
mynumber => myCnumber;
usbKeyOut => Output_usbCodeSend_capability( usbCode : 1 ); usbKeyOut => Output_usbCodeSend_capability( usbCode : 1 );
myCapability2 => myFunc2(); myCapability2 => myFunc2();
myCapability3 => myFunc3( myArg1 : 2 ); myCapability3 => myFunc3( myArg1 : 2 );

44
kll.py
View File

@ -93,9 +93,15 @@ def processCommandLineArgs():
pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h", pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h",
help="Specify template used to generate the keymap.\n" help="Specify template used to generate the keymap.\n"
"Default: templates/kiibohdKeymap.h" ) "Default: templates/kiibohdKeymap.h" )
pArgs.add_argument( '-o', '--output', type=str, default="templateKeymap.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" help="Specify output file. Writes to current working directory by default.\n"
"Default: generatedKeymap.h" ) "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", pArgs.add_argument( '-h', '--help', action="help",
help="This message." ) help="This message." )
@ -122,7 +128,7 @@ def processCommandLineArgs():
for filename in partial: for filename in partial:
checkFileExists( filename ) checkFileExists( filename )
return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.output) return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.defines_template, args.output, args.defines_output)
@ -165,7 +171,7 @@ def tokenize( string ):
## Map Arrays ## Map Arrays
macros_map = Macros() macros_map = Macros()
variable_dict = dict() variables_dict = Variables()
capabilities_dict = Capabilities() capabilities_dict = Capabilities()
@ -255,6 +261,9 @@ def make_seqString( token ):
def make_string( token ): def make_string( token ):
return token[1:-1] return token[1:-1]
def make_unseqString( token ):
return token[1:-1]
def make_number( token ): def make_number( token ):
return int( token, 0 ) return int( token, 0 )
@ -441,16 +450,20 @@ def eval_variable( name, content ):
for item in content: for item in content:
assigned_content += str( item ) assigned_content += str( item )
variable_dict[ name ] = assigned_content variables_dict.assignVariable( name, assigned_content )
def eval_capability( name, function, args ): def eval_capability( name, function, args ):
capabilities_dict[ 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_scanCode = unarg( eval_scanCode )
map_usbCode = unarg( eval_usbCode ) map_usbCode = unarg( eval_usbCode )
set_variable = unarg( eval_variable ) set_variable = unarg( eval_variable )
set_capability = unarg( eval_capability ) set_capability = unarg( eval_capability )
set_define = unarg( eval_define )
## Sub Rules ## Sub Rules
@ -466,6 +479,7 @@ content = tokenType('VariableContents')
string = tokenType('String') >> make_string string = tokenType('String') >> make_string
unString = tokenType('String') # When the double quotes are still needed for internal processing unString = tokenType('String') # When the double quotes are still needed for internal processing
seqString = tokenType('SequenceString') >> make_seqString seqString = tokenType('SequenceString') >> make_seqString
unseqString = tokenType('SequenceString') >> make_unseqString # For use with variables
# Code variants # Code variants
code_end = tokenType('CodeEnd') code_end = tokenType('CodeEnd')
@ -506,13 +520,16 @@ resultCode_outerList = capFunc_sequence >> optionExpansion >> usbCodeToCapab
## Main Rules ## Main Rules
#| <variable> = <variable contents>; #| <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 variable_expression = name + skip( operator('=') ) + oneplus( variable_contents ) + skip( eol ) >> set_variable
#| <capability name> => <c function>; #| <capability name> => <c function>;
capability_arguments = name + skip( operator(':') ) + number + skip( maybe( comma ) ) 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 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>; #| <trigger> : <result>;
operatorTriggerResult = operator(':') | operator(':+') | operator(':-') operatorTriggerResult = operator(':') | operator(':+') | operator(':-')
scanCode_expression = triggerCode_outerList + operatorTriggerResult + resultCode_outerList + skip( eol ) >> map_scanCode scanCode_expression = triggerCode_outerList + operatorTriggerResult + resultCode_outerList + skip( eol ) >> map_scanCode
@ -522,7 +539,7 @@ def parse( tokenSequence ):
"""Sequence(Token) -> object""" """Sequence(Token) -> object"""
# Top-level Parser # 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_text = many( expression )
kll_file = maybe( kll_text ) + skip( finished ) kll_file = maybe( kll_text ) + skip( finished )
@ -543,20 +560,23 @@ def processKLLFile( filename ):
### Main Entry Point ### ### Main Entry Point ###
if __name__ == '__main__': if __name__ == '__main__':
(baseFiles, defaultFiles, partialFileSets, backend_name, template, output) = processCommandLineArgs() (baseFiles, defaultFiles, partialFileSets, backend_name, template, defines_template, output, defines_output) = processCommandLineArgs()
# Load backend module # Load backend module
global backend global backend
backend_import = importlib.import_module( "backends.{0}".format( backend_name ) ) 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 # Process base layout files
for filename in baseFiles: for filename in baseFiles:
variables_dict.setCurrentFile( filename )
processKLLFile( filename ) processKLLFile( filename )
macros_map.completeBaseLayout() # Indicates to macros_map that the base layout is complete macros_map.completeBaseLayout() # Indicates to macros_map that the base layout is complete
variables_dict.baseLayoutFinished()
# Default combined layer # Default combined layer
for filename in defaultFiles: for filename in defaultFiles:
variables_dict.setCurrentFile( filename )
processKLLFile( filename ) processKLLFile( filename )
# Apply assignment cache, see 5.1.2 USB Codes for why this is necessary # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
macros_map.replayCachedAssignments() macros_map.replayCachedAssignments()
@ -565,9 +585,13 @@ if __name__ == '__main__':
for partial in partialFileSets: for partial in partialFileSets:
# Increment layer for each -p option # Increment layer for each -p option
macros_map.addLayer() macros_map.addLayer()
variables_dict.incrementLayer() # DefaultLayer is layer 0
# Iterate and process each of the file in the layer # Iterate and process each of the file in the layer
for filename in partial: for filename in partial:
variables_dict.setCurrentFile( filename )
processKLLFile( filename ) processKLLFile( filename )
# Apply assignment cache, see 5.1.2 USB Codes for why this is necessary # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
macros_map.replayCachedAssignments() macros_map.replayCachedAssignments()
# Remove un-marked keys to complete the partial layer # Remove un-marked keys to complete the partial layer
@ -577,10 +601,10 @@ if __name__ == '__main__':
macros_map.generate() macros_map.generate()
# Process needed templating variables using backend # Process needed templating variables using backend
backend.process( capabilities_dict, macros_map ) backend.process( capabilities_dict, macros_map, variables_dict )
# Generate output file using template and backend # Generate output file using template and backend
backend.generate( output ) backend.generate( output, defines_output )
# Successful Execution # Successful Execution
sys.exit( 0 ) sys.exit( 0 )

View File

@ -277,19 +277,48 @@ class Variables:
# Container for variables # Container for variables
# Stores three sets of variables, the overall combined set, per layer, and per file # Stores three sets of variables, the overall combined set, per layer, and per file
def __init__( self ): def __init__( self ):
pass # Dictionaries of variables
self.baseLayout = dict()
self.fileVariables = dict()
self.layerVariables = [ dict() ]
self.overallVariables = dict()
self.defines = dict()
def baseLayerFinished( self ): self.currentFile = ""
pass self.currentLayer = 0
self.baseLayoutEnabled = True
def baseLayoutFinished( self ):
self.baseLayoutEnabled = False
def setCurrentFile( self, name ): def setCurrentFile( self, name ):
# Store using filename and current layer # Store using filename and current layer
pass self.currentFile = name
self.fileVariables[ name ] = dict()
def setCurrentLayer( self, layer ): # If still processing BaseLayout
if self.baseLayoutEnabled:
self.baseLayout['*LayerFiles'] = name
# Set for the current layer
else:
self.layerVariables[ self.currentLayer ]['*LayerFiles'] = name
def incrementLayer( self ):
# Store using layer index # Store using layer index
pass self.currentLayer += 1
self.layerVariables.append( dict() )
def assignVariable( self, key, value ): def assignVariable( self, key, value ):
pass # Overall set of variables
self.overallVariables[ key ] = value
# If still processing BaseLayout
if self.baseLayoutEnabled:
self.baseLayout[ key ] = value
# Set for the current layer
else:
self.layerVariables[ self.currentLayer ][ key ] = value
# File context variables
self.fileVariables[ self.currentFile ][ key ] = value

View File

@ -17,8 +17,8 @@
<|Information|> <|Information|>
#ifndef __generatedKeymap_h #ifndef __kiibohdKeymap_h
#define __generatedKeymap_h #define __kiibohdKeymap_h
// ----- Includes ----- // ----- Includes -----