KLL Compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

kiibohd.py 20KB

KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
KLL Compiler Re-Write This was many months of efforts in re-designing how the KLL compiler should work. The major problem with the original compiler was how difficult it was to extend language wise. This lead to many delays in KLL 0.4 and 0.5 being implemented. The new design is a multi-staged compiler, where even tokenization occurs over multiple stages. This allows individual parsing and token regexes to be expressed more simply without affect other expressions. Another area of change is the concept of Contexts. In the original KLL compiler the idea of a cache assigned was "hacked" on when I realized the language was "broken" (after nearly finishing the compiler). Since assignment order is generally considered not to matter for keymappings, I created a "cached" assignment where the whole file is read into a sub-datastructure, then apply to the master datastructure. Unfortunately, this wasn't really all that clear, so it was annoying to work with. To remedy this, I created KLL Contexts, which contain information about a group of expressions. Not only can these groups can be merged with other Contexts, they have historical data about how they were generated allowing for errors very late in processing to be pin-pointed back to the offending kll file. Backends work nearly the same as they did before. However, all call-backs for capability evaluations have been removed. This makes the interface much cleaner as Contexts can only be symbolically merged now. (Previously datastructures did evaluation merges where the ScanCode or Capability was looked up right before passing to the backend, but this required additional information from the backend). Many of the old parsing and tokenization rules have been reused, along with the hid_dict.py code. The new design takes advantage of processor pools to handle multithreading where it makes sense. For example, all specified files are loaded into ram simulatenously rather than sparingly reading from. The reason for this is so that each Context always has all the information it requires at all times. kll - Program entry point (previously kll.py) - Very small now, does some setting up of command-line args - Most command-line args are specified by the corresponding processing stage common/channel.py - Pixel Channel container classes common/context.py - Context container classes - As is usual with other files, blank classes inherit a base class - These blank classes are identified by the class name itself to handle special behaviour - And if/when necessary functions are re-implemented - MergeConext class facilitates merging of contexts while maintaining lineage common/expression.py - Expression container classes * Expression base class * AssignmentExpression * NameAssociationExpression * DataAssociationExpression * MapExpression - These classes are used to store expressions after they have finished parsing and tokenization common/file.py - Container class for files being read by the KLL compiler common/emitter.py - Base class for all KLL emitters - TextEmitter for dealing with text file templates common/hid_dict.py - Slightly modified version of kll_lib/hid_dict.py common/id.py - Identification container classes - Used to indentify different types of elements used within the KLL language common/modifier.py - Container classes for animation and pixel change functions common/organization.py - Data structure merging container classes - Contains all the sub-datastructure classes as well - The Organization class handles the merge orchestration and expression insertion common/parse.py - Parsing rules for funcparserlib - Much of this file was taken from the original kll.py - Many changes to support the multi-stage processing and support KLL 0.5 common/position.py - Container class dealing with physical positions common/schedule.py - Container class dealing with scheduling and timing events common/stage.py - Contains ControlStage and main Stage classes * CompilerConfigurationStage * FileImportStage * PreprocessorStage * OperationClassificationStage * OperationSpecificsStage * OperationOrganizationStage * DataOrganziationStage * DataFinalizationStage * DataAnalysisStage * CodeGenerationStage * ReportGenerationStage - Each of these classes controls the life-cycle of each stage - If multi-threading is desired, it must be handled within the class * The next stage will not start until the current stage is finished - Errors are handled such that as many errors as possible are recorded before forcing an exit * The exit is handled at the end of each stage if necessary - Command-line arguments for each stage can be defined if necessary (they are given their own grouping) - Each stage can pull variables and functions from other stages if necessary using a name lookup * This means you don't have to worry about over-arching datastructures emitters/emitters.py - Container class for KLL emitters - Handles emitter setup and selection emitters/kiibohd/kiibohd.py - kiibohd .h file KLL emitter - Re-uses some backend code from the original KLL compiler funcparserlib/parser.py - Added debug mode control examples/assignment.kll examples/defaultMapExample.kll examples/example.kll examples/hhkbpro2.kll examples/leds.kll examples/mapping.kll examples/simple1.kll examples/simple2.kll examples/simpleExample.kll examples/state_scheduling.kll - Updating/Adding rules for new compiler and KLL 0.4 + KLL 0.5 support
7 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #!/usr/bin/env python3
  2. '''
  3. KLL Kiibohd .h/.c File Emitter
  4. '''
  5. # Copyright (C) 2016 by Jacob Alexander
  6. #
  7. # This file is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This file is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  19. ### Imports ###
  20. import re
  21. import sys
  22. from datetime import date
  23. from common.emitter import Emitter, TextEmitter
  24. from common.hid_dict import kll_hid_lookup_dictionary
  25. ### Decorators ###
  26. ## Print Decorator Variables
  27. ERROR = '\033[5;1;31mERROR\033[0m:'
  28. WARNING = '\033[5;1;33mWARNING\033[0m:'
  29. ### Classes ###
  30. class Kiibohd( Emitter, TextEmitter ):
  31. '''
  32. Kiibohd .h file emitter for KLL
  33. '''
  34. # List of required capabilities
  35. requiredCapabilities = {
  36. 'CONS' : 'consCtrlOut',
  37. 'NONE' : 'noneOut',
  38. 'SYS' : 'sysCtrlOut',
  39. 'USB' : 'usbKeyOut',
  40. }
  41. def __init__( self, control ):
  42. '''
  43. Emitter initialization
  44. @param control: ControlStage object, used to access data from other stages
  45. '''
  46. Emitter.__init__( self, control )
  47. TextEmitter.__init__( self )
  48. # Defaults
  49. self.map_template = "templates/kiibohdKeymap.h"
  50. self.pixel_template = "templates/kiibohdPixelmap.c"
  51. self.def_template = "templates/kiibohdDefs.h"
  52. self.map_output = "generatedKeymap.h"
  53. self.pixel_output = "generatedPixelmap.c"
  54. self.def_output = "kll_defs.h"
  55. self.fill_dict = {}
  56. def command_line_args( self, args ):
  57. '''
  58. Group parser for command line arguments
  59. @param args: Name space of processed arguments
  60. '''
  61. self.def_template = args.def_template
  62. self.map_template = args.map_template
  63. self.pixel_template = args.pixel_template
  64. self.def_output = args.def_output
  65. self.map_output = args.map_output
  66. self.pixel_output = args.pixel_output
  67. def command_line_flags( self, parser ):
  68. '''
  69. Group parser for command line options
  70. @param parser: argparse setup object
  71. '''
  72. # Create new option group
  73. group = parser.add_argument_group('\033[1mKiibohd Emitter Configuration\033[0m')
  74. group.add_argument( '--def-template', type=str, default=self.def_template,
  75. help="Specify KLL define .h file template.\n"
  76. "\033[1mDefault\033[0m: {0}\n".format( self.def_template )
  77. )
  78. group.add_argument( '--map-template', type=str, default=self.map_template,
  79. help="Specify KLL map .h file template.\n"
  80. "\033[1mDefault\033[0m: {0}\n".format( self.map_template )
  81. )
  82. group.add_argument( '--pixel-template', type=str, default=self.pixel_template,
  83. help="Specify KLL pixel map .c file template.\n"
  84. "\033[1mDefault\033[0m: {0}\n".format( self.pixel_template )
  85. )
  86. group.add_argument( '--def-output', type=str, default=self.def_output,
  87. help="Specify KLL define .h file output.\n"
  88. "\033[1mDefault\033[0m: {0}\n".format( self.def_output )
  89. )
  90. group.add_argument( '--map-output', type=str, default=self.map_output,
  91. help="Specify KLL map .h file output.\n"
  92. "\033[1mDefault\033[0m: {0}\n".format( self.map_output )
  93. )
  94. group.add_argument( '--pixel-output', type=str, default=self.pixel_output,
  95. help="Specify KLL map .h file output.\n"
  96. "\033[1mDefault\033[0m: {0}\n".format( self.pixel_output )
  97. )
  98. def output( self ):
  99. '''
  100. Final Stage of Emitter
  101. Generate desired outputs from templates
  102. '''
  103. # Load define template and generate
  104. self.load_template( self.def_template )
  105. self.generate( self.def_output )
  106. # Load keymap template and generate
  107. self.load_template( self.map_template )
  108. self.generate( self.map_output )
  109. # Load pixelmap template and generate
  110. self.load_template( self.pixel_template )
  111. self.generate( self.pixel_output )
  112. def process( self ):
  113. '''
  114. Emitter Processing
  115. Takes KLL datastructures and Analysis results then populates the fill_dict
  116. The fill_dict is used populate the template files.
  117. '''
  118. # Acquire Datastructures
  119. early_contexts = self.control.stage('DataOrganizationStage').contexts
  120. base_context = self.control.stage('DataFinalizationStage').base_context
  121. default_context = self.control.stage('DataFinalizationStage').default_context
  122. partial_contexts = self.control.stage('DataFinalizationStage').partial_contexts
  123. full_context = self.control.stage('DataFinalizationStage').full_context
  124. # Build string list of compiler arguments
  125. compilerArgs = ""
  126. for arg in sys.argv:
  127. if "--" in arg or ".py" in arg:
  128. compilerArgs += "// {0}\n".format( arg )
  129. else:
  130. compilerArgs += "// {0}\n".format( arg )
  131. # Build a string of modified files, if any
  132. gitChangesStr = "\n"
  133. if len( self.control.git_changes ) > 0:
  134. for gitFile in self.control.git_changes:
  135. gitChangesStr += "// {0}\n".format( gitFile )
  136. else:
  137. gitChangesStr = " None\n"
  138. # Prepare BaseLayout and Layer Info
  139. configLayoutInfo = ""
  140. if 'ConfigurationContext' in early_contexts.keys():
  141. contexts = early_contexts['ConfigurationContext'].query_contexts( 'AssignmentExpression', 'Array' )
  142. for sub in contexts:
  143. name = sub[0].data['Name'].value
  144. configLayoutInfo += "// {0}\n// {1}\n".format( name, sub[1].parent.path )
  145. genericLayoutInfo = ""
  146. if 'GenericContext' in early_contexts.keys():
  147. contexts = early_contexts['GenericContext'].query_contexts( 'AssignmentExpression', 'Array' )
  148. for sub in contexts:
  149. name = sub[0].data['Name'].value
  150. genericLayoutInfo += "// {0}\n// {1}\n".format( name, sub[1].parent.path )
  151. baseLayoutInfo = ""
  152. if 'BaseMapContext' in early_contexts.keys():
  153. contexts = early_contexts['BaseMapContext'].query_contexts( 'AssignmentExpression', 'Array' )
  154. for sub in contexts:
  155. name = sub[0].data['Name'].value
  156. baseLayoutInfo += "// {0}\n// {1}\n".format( name, sub[1].parent.path )
  157. defaultLayerInfo = ""
  158. if 'DefaultMapContext' in early_contexts.keys():
  159. contexts = early_contexts['DefaultMapContext'].query_contexts( 'AssignmentExpression', 'Array' )
  160. for sub in contexts:
  161. name = sub[0].data['Name'].value
  162. defaultLayerInfo += "// {0}\n// {1}\n".format( name, sub[1].parent.path )
  163. partialLayersInfo = ""
  164. partial_context_list = [
  165. ( item[1].layer, item[0] )
  166. for item in early_contexts.items()
  167. if 'PartialMapContext' in item[0]
  168. ]
  169. for layer, tag in sorted( partial_context_list, key=lambda x: x[0] ):
  170. partialLayersInfo += "// Layer {0}\n".format( layer + 1 )
  171. contexts = early_contexts[ tag ].query_contexts( 'AssignmentExpression', 'Array' )
  172. for sub in contexts:
  173. name = sub[0].data['Name'].value
  174. partialLayersInfo += "// {0}\n// {1}\n".format( name, sub[1].parent.path )
  175. ## Information ##
  176. self.fill_dict['Information'] = "// This file was generated by the kll compiler, DO NOT EDIT.\n"
  177. self.fill_dict['Information'] += "// Generation Date: {0}\n".format( date.today() )
  178. self.fill_dict['Information'] += "// KLL Emitter: {0}\n".format(
  179. self.control.stage('CompilerConfigurationStage').emitter
  180. )
  181. self.fill_dict['Information'] += "// KLL Version: {0}\n".format( self.control.version )
  182. self.fill_dict['Information'] += "// KLL Git Changes:{0}".format( gitChangesStr )
  183. self.fill_dict['Information'] += "// Compiler arguments:\n{0}".format( compilerArgs )
  184. self.fill_dict['Information'] += "//\n"
  185. self.fill_dict['Information'] += "// - Configuration File -\n{0}".format( configLayoutInfo )
  186. self.fill_dict['Information'] += "// - Generic Files -\n{0}".format( genericLayoutInfo )
  187. self.fill_dict['Information'] += "// - Base Layer -\n{0}".format( baseLayoutInfo )
  188. self.fill_dict['Information'] += "// - Default Layer -\n{0}".format( defaultLayerInfo )
  189. self.fill_dict['Information'] += "// - Partial Layers -\n{0}".format( partialLayersInfo )
  190. ## Defines ##
  191. self.fill_dict['Defines'] = ""
  192. # Iterate through defines and lookup the variables
  193. defines = full_context.query( 'NameAssociationExpression', 'Define' )
  194. variables = full_context.query( 'AssignmentExpression', 'Variable' )
  195. for dkey, dvalue in sorted( defines.data.items() ):
  196. if dvalue.name in variables.data.keys():
  197. # TODO Handle arrays
  198. if not isinstance( variables.data[ dvalue.name ].value, list ):
  199. self.fill_dict['Defines'] += "\n#define {0} {1}".format(
  200. dvalue.association,
  201. variables.data[ dvalue.name ].value.replace( '\n', ' \\\n' ),
  202. )
  203. else:
  204. print( "{0} '{1}' not defined...".format( WARNING, dvalue.name ) )
  205. ## Capabilities ##
  206. self.fill_dict['CapabilitiesFuncDecl'] = ""
  207. self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"
  208. self.fill_dict['CapabilitiesIndices'] = "typedef enum CapabilityIndex {\n"
  209. # Keys are pre-sorted
  210. capabilities = full_context.query( 'NameAssociationExpression', 'Capability' )
  211. for dkey, dvalue in sorted( capabilities.data.items() ):
  212. funcName = dvalue.association.name
  213. argByteWidth = dvalue.association.total_arg_bytes()
  214. self.fill_dict['CapabilitiesList'] += "\t{{ {0}, {1} }},\n".format( funcName, argByteWidth )
  215. self.fill_dict['CapabilitiesFuncDecl'] += \
  216. "void {0}( uint8_t state, uint8_t stateType, uint8_t *args );\n".format( funcName )
  217. self.fill_dict['CapabilitiesIndices'] += "\t{0}_index,\n".format( funcName )
  218. self.fill_dict['CapabilitiesList'] += "};"
  219. self.fill_dict['CapabilitiesIndices'] += "} CapabilityIndex;"
  220. return
  221. ## Results Macros ##
  222. self.fill_dict['ResultMacros'] = ""
  223. # Iterate through each of the result macros
  224. for result in range( 0, len( macros.resultsIndexSorted ) ):
  225. self.fill_dict['ResultMacros'] += "Guide_RM( {0} ) = {{ ".format( result )
  226. # Add the result macro capability index guide (including capability arguments)
  227. # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
  228. for sequence in range( 0, len( macros.resultsIndexSorted[ result ] ) ):
  229. # If the sequence is longer than 1, prepend a sequence spacer
  230. # Needed for USB behaviour, otherwise, repeated keys will not work
  231. if sequence > 0:
  232. # <single element>, <usbCodeSend capability>, <USB Code 0x00>
  233. self.fill_dict['ResultMacros'] += "1, {0}, 0x00, ".format( capabilities.getIndex( self.capabilityLookup('USB') ) )
  234. # For each combo in the sequence, add the length of the combo
  235. self.fill_dict['ResultMacros'] += "{0}, ".format( len( macros.resultsIndexSorted[ result ][ sequence ] ) )
  236. # For each combo, add each of the capabilities used and their arguments
  237. for combo in range( 0, len( macros.resultsIndexSorted[ result ][ sequence ] ) ):
  238. resultItem = macros.resultsIndexSorted[ result ][ sequence ][ combo ]
  239. # Add the capability index
  240. self.fill_dict['ResultMacros'] += "{0}, ".format( capabilities.getIndex( resultItem[0] ) )
  241. # Add each of the arguments of the capability
  242. for arg in range( 0, len( resultItem[1] ) ):
  243. # Special cases
  244. if isinstance( resultItem[1][ arg ], str ):
  245. # If this is a CONSUMER_ element, needs to be split into 2 elements
  246. # AC_ and AL_ are other sections of consumer control
  247. if re.match( r'^(CONSUMER|AC|AL)_', resultItem[1][ arg ] ):
  248. tag = resultItem[1][ arg ].split( '_', 1 )[1]
  249. if '_' in tag:
  250. tag = tag.replace( '_', '' )
  251. try:
  252. lookupNum = kll_hid_lookup_dictionary['ConsCode'][ tag ][1]
  253. except KeyError as err:
  254. print ( "{0} {1} Consumer HID kll bug...please report.".format( ERROR, err ) )
  255. raise
  256. byteForm = lookupNum.to_bytes( 2, byteorder='little' ) # XXX Yes, little endian from how the uC structs work
  257. self.fill_dict['ResultMacros'] += "{0}, {1}, ".format( *byteForm )
  258. continue
  259. # None, fall-through disable
  260. elif resultItem[0] is self.capabilityLookup('NONE'):
  261. continue
  262. self.fill_dict['ResultMacros'] += "{0}, ".format( resultItem[1][ arg ] )
  263. # If sequence is longer than 1, append a sequence spacer at the end of the sequence
  264. # Required by USB to end at sequence without holding the key down
  265. if len( macros.resultsIndexSorted[ result ] ) > 1:
  266. # <single element>, <usbCodeSend capability>, <USB Code 0x00>
  267. self.fill_dict['ResultMacros'] += "1, {0}, 0x00, ".format( capabilities.getIndex( self.capabilityLookup('USB') ) )
  268. # Add list ending 0 and end of list
  269. self.fill_dict['ResultMacros'] += "0 };\n"
  270. self.fill_dict['ResultMacros'] = self.fill_dict['ResultMacros'][:-1] # Remove last newline
  271. ## Result Macro List ##
  272. self.fill_dict['ResultMacroList'] = "const ResultMacro ResultMacroList[] = {\n"
  273. # Iterate through each of the result macros
  274. for result in range( 0, len( macros.resultsIndexSorted ) ):
  275. self.fill_dict['ResultMacroList'] += "\tDefine_RM( {0} ),\n".format( result )
  276. self.fill_dict['ResultMacroList'] += "};"
  277. ## Result Macro Record ##
  278. self.fill_dict['ResultMacroRecord'] = "ResultMacroRecord ResultMacroRecordList[ ResultMacroNum ];"
  279. ## Trigger Macros ##
  280. self.fill_dict['TriggerMacros'] = ""
  281. # Iterate through each of the trigger macros
  282. for trigger in range( 0, len( macros.triggersIndexSorted ) ):
  283. self.fill_dict['TriggerMacros'] += "Guide_TM( {0} ) = {{ ".format( trigger )
  284. # Add the trigger macro scan code guide
  285. # See kiibohd controller Macros/PartialMap/kll.h for exact formatting details
  286. for sequence in range( 0, len( macros.triggersIndexSorted[ trigger ][0] ) ):
  287. # For each combo in the sequence, add the length of the combo
  288. self.fill_dict['TriggerMacros'] += "{0}, ".format( len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) )
  289. # For each combo, add the key type, key state and scan code
  290. for combo in range( 0, len( macros.triggersIndexSorted[ trigger ][0][ sequence ] ) ):
  291. triggerItemId = macros.triggersIndexSorted[ trigger ][0][ sequence ][ combo ]
  292. # Lookup triggerItem in ScanCodeStore
  293. triggerItemObj = macros.scanCodeStore[ triggerItemId ]
  294. triggerItem = triggerItemObj.offset( macros.interconnectOffset )
  295. # TODO Add support for Analog keys
  296. # TODO Add support for LED states
  297. self.fill_dict['TriggerMacros'] += "0x00, 0x01, 0x{0:02X}, ".format( triggerItem )
  298. # Add list ending 0 and end of list
  299. self.fill_dict['TriggerMacros'] += "0 };\n"
  300. self.fill_dict['TriggerMacros'] = self.fill_dict['TriggerMacros'][ :-1 ] # Remove last newline
  301. ## Trigger Macro List ##
  302. self.fill_dict['TriggerMacroList'] = "const TriggerMacro TriggerMacroList[] = {\n"
  303. # Iterate through each of the trigger macros
  304. for trigger in range( 0, len( macros.triggersIndexSorted ) ):
  305. # Use TriggerMacro Index, and the corresponding ResultMacro Index
  306. self.fill_dict['TriggerMacroList'] += "\tDefine_TM( {0}, {1} ),\n".format( trigger, macros.triggersIndexSorted[ trigger ][1] )
  307. self.fill_dict['TriggerMacroList'] += "};"
  308. ## Trigger Macro Record ##
  309. self.fill_dict['TriggerMacroRecord'] = "TriggerMacroRecord TriggerMacroRecordList[ TriggerMacroNum ];"
  310. ## Max Scan Code ##
  311. self.fill_dict['MaxScanCode'] = "#define MaxScanCode 0x{0:X}".format( macros.overallMaxScanCode )
  312. ## Interconnect ScanCode Offset List ##
  313. self.fill_dict['ScanCodeInterconnectOffsetList'] = "const uint8_t InterconnectOffsetList[] = {\n"
  314. for offset in range( 0, len( macros.interconnectOffset ) ):
  315. self.fill_dict['ScanCodeInterconnectOffsetList'] += "\t0x{0:02X},\n".format( macros.interconnectOffset[ offset ] )
  316. self.fill_dict['ScanCodeInterconnectOffsetList'] += "};"
  317. ## Max Interconnect Nodes ##
  318. self.fill_dict['InterconnectNodeMax'] = "#define InterconnectNodeMax 0x{0:X}\n".format( len( macros.interconnectOffset ) )
  319. ## Default Layer and Default Layer Scan Map ##
  320. self.fill_dict['DefaultLayerTriggerList'] = ""
  321. self.fill_dict['DefaultLayerScanMap'] = "const nat_ptr_t *default_scanMap[] = {\n"
  322. # Iterate over triggerList and generate a C trigger array for the default map and default map array
  323. for triggerList in range( macros.firstScanCode[0], len( macros.triggerList[0] ) ):
  324. # Generate ScanCode index and triggerList length
  325. self.fill_dict['DefaultLayerTriggerList'] += "Define_TL( default, 0x{0:02X} ) = {{ {1}".format( triggerList, len( macros.triggerList[0][ triggerList ] ) )
  326. # Add scanCode trigger list to Default Layer Scan Map
  327. self.fill_dict['DefaultLayerScanMap'] += "default_tl_0x{0:02X}, ".format( triggerList )
  328. # Add each item of the trigger list
  329. for triggerItem in macros.triggerList[0][ triggerList ]:
  330. self.fill_dict['DefaultLayerTriggerList'] += ", {0}".format( triggerItem )
  331. self.fill_dict['DefaultLayerTriggerList'] += " };\n"
  332. self.fill_dict['DefaultLayerTriggerList'] = self.fill_dict['DefaultLayerTriggerList'][:-1] # Remove last newline
  333. self.fill_dict['DefaultLayerScanMap'] = self.fill_dict['DefaultLayerScanMap'][:-2] # Remove last comma and space
  334. self.fill_dict['DefaultLayerScanMap'] += "\n};"
  335. ## Partial Layers and Partial Layer Scan Maps ##
  336. self.fill_dict['PartialLayerTriggerLists'] = ""
  337. self.fill_dict['PartialLayerScanMaps'] = ""
  338. # Iterate over each of the layers, excluding the default layer
  339. for layer in range( 1, len( macros.triggerList ) ):
  340. # Prepare each layer
  341. self.fill_dict['PartialLayerScanMaps'] += "// Partial Layer {0}\n".format( layer )
  342. self.fill_dict['PartialLayerScanMaps'] += "const nat_ptr_t *layer{0}_scanMap[] = {{\n".format( layer )
  343. self.fill_dict['PartialLayerTriggerLists'] += "// Partial Layer {0}\n".format( layer )
  344. # Iterate over triggerList and generate a C trigger array for the layer
  345. for triggerList in range( macros.firstScanCode[ layer ], len( macros.triggerList[ layer ] ) ):
  346. # Generate ScanCode index and triggerList length
  347. self.fill_dict['PartialLayerTriggerLists'] += "Define_TL( layer{0}, 0x{1:02X} ) = {{ {2}".format( layer, triggerList, len( macros.triggerList[ layer ][ triggerList ] ) )
  348. # Add scanCode trigger list to Default Layer Scan Map
  349. self.fill_dict['PartialLayerScanMaps'] += "layer{0}_tl_0x{1:02X}, ".format( layer, triggerList )
  350. # Add each item of the trigger list
  351. for trigger in macros.triggerList[ layer ][ triggerList ]:
  352. self.fill_dict['PartialLayerTriggerLists'] += ", {0}".format( trigger )
  353. self.fill_dict['PartialLayerTriggerLists'] += " };\n"
  354. self.fill_dict['PartialLayerTriggerLists'] += "\n"
  355. self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last comma and space
  356. self.fill_dict['PartialLayerScanMaps'] += "\n};\n\n"
  357. self.fill_dict['PartialLayerTriggerLists'] = self.fill_dict['PartialLayerTriggerLists'][:-2] # Remove last 2 newlines
  358. self.fill_dict['PartialLayerScanMaps'] = self.fill_dict['PartialLayerScanMaps'][:-2] # Remove last 2 newlines
  359. ## Layer Index List ##
  360. self.fill_dict['LayerIndexList'] = "const Layer LayerIndex[] = {\n"
  361. # Iterate over each layer, adding it to the list
  362. for layer in range( 0, len( macros.triggerList ) ):
  363. # Lookup first scancode in map
  364. firstScanCode = macros.firstScanCode[ layer ]
  365. # Generate stacked name
  366. stackName = ""
  367. if '*NameStack' in variables.layerVariables[ layer ].keys():
  368. for name in range( 0, len( variables.layerVariables[ layer ]['*NameStack'] ) ):
  369. stackName += "{0} + ".format( variables.layerVariables[ layer ]['*NameStack'][ name ] )
  370. stackName = stackName[:-3]
  371. # Default map is a special case, always the first index
  372. if layer == 0:
  373. self.fill_dict['LayerIndexList'] += '\tLayer_IN( default_scanMap, "D: {1}", 0x{0:02X} ),\n'.format( firstScanCode, stackName )
  374. else:
  375. self.fill_dict['LayerIndexList'] += '\tLayer_IN( layer{0}_scanMap, "{0}: {2}", 0x{1:02X} ),\n'.format( layer, firstScanCode, stackName )
  376. self.fill_dict['LayerIndexList'] += "};"
  377. ## Layer State ##
  378. self.fill_dict['LayerState'] = "uint8_t LayerState[ LayerNum ];"