diff --git a/kll.py b/kll.py
index 88ae72a..61fda0c 100755
--- a/kll.py
+++ b/kll.py
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
-# KLL Compiler
-# Keyboard Layout Langauge
-#
-# Copyright (C) 2014-2015 by Jacob Alexander
+'''
+KLL Compiler
+Keyboard Layout Langauge
+'''
+
+# Copyright (C) 2014-2016 by Jacob Alexander
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -21,32 +23,27 @@
import argparse
import importlib
-import io
import os
-import re
import sys
-import token
-from pprint import pformat
from re import VERBOSE
-from tokenize import generate_tokens
from kll_lib.containers import *
from kll_lib.hid_dict import *
from funcparserlib.lexer import make_tokenizer, Token, LexerError
-from funcparserlib.parser import (some, a, many, oneplus, skip, finished, maybe, skip, forward_decl, NoParseError)
+from funcparserlib.parser import (some, a, many, oneplus, finished, maybe, skip, NoParseError)
### Decorators ###
- ## Print Decorator Variables
+## Print Decorator Variables
ERROR = '\033[5;1;31mERROR\033[0m:'
- ## Python Text Formatting Fixer...
- ## Because the creators of Python are averse to proper capitalization.
+## Python Text Formatting Fixer...
+## Because the creators of Python are averse to proper capitalization.
textFormatter_lookup = {
"usage: " : "Usage: ",
"optional arguments" : "Optional Arguments",
@@ -150,12 +147,12 @@ def tokenize( string ):
( 'String', ( r'"[^"]*"', ) ),
( 'SequenceString', ( r"'[^']*'", ) ),
( 'Operator', ( r'=>|:\+|:-|::|:|=', ) ),
+ ( 'Number', ( r'(-[ \t]*)?((0x[0-9a-fA-F]+)|(0|([1-9][0-9]*)))', VERBOSE ) ),
( 'Comma', ( r',', ) ),
( 'Dash', ( r'-', ) ),
( 'Plus', ( r'\+', ) ),
( 'Parenthesis', ( r'\(|\)', ) ),
( 'None', ( r'None', ) ),
- ( 'Number', ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ),
( 'Name', ( r'[A-Za-z_][A-Za-z_0-9]*', ) ),
( 'VariableContents', ( r'''[^"' ;:=>()]+''', ) ),
( 'EndOfLine', ( r';', ) ),
@@ -171,13 +168,13 @@ def tokenize( string ):
### Parsing ###
- ## Map Arrays
+## Map Arrays
macros_map = Macros()
variables_dict = Variables()
capabilities_dict = Capabilities()
- ## Parsing Functions
+## Parsing Functions
def make_scanCode( token ):
scanCode = int( token[1:], 0 )
@@ -257,7 +254,7 @@ def make_consCode_number( token ):
def make_sysCode_number( token ):
return make_hidCode_number( 'SysCode', token )
- # Replace key-word with None specifier (which indicates a noneOut capability)
+ # Replace key-word with None specifier (which indicates a noneOut capability)
def make_none( token ):
return [[[('NONE', 0)]]]
@@ -322,7 +319,7 @@ def make_unseqString( token ):
def make_number( token ):
return int( token, 0 )
- # Range can go from high to low or low to high
+# Range can go from high to low or low to high
def make_scanCode_range( rangeVals ):
start = rangeVals[0]
end = rangeVals[1]
@@ -334,9 +331,9 @@ def make_scanCode_range( rangeVals ):
# Iterate from start to end, and generate the range
return list( range( start, end + 1 ) )
- # Range can go from high to low or low to high
- # Warn on 0-9 for USBCodes (as this does not do what one would expect) TODO
- # Lookup USB HID tags and convert to a number
+# Range can go from high to low or low to high
+# Warn on 0-9 for USBCodes (as this does not do what one would expect) TODO
+# Lookup USB HID tags and convert to a number
def make_hidCode_range( type, rangeVals ):
# Check if already integers
if isinstance( rangeVals[0], int ):
@@ -371,7 +368,7 @@ def make_consCode_range( rangeVals ):
return make_hidCode_range( 'ConsCode', rangeVals )
- ## Base Rules
+## Base Rules
const = lambda x: lambda _: x
unarg = lambda f: lambda x: f(*x)
@@ -389,7 +386,7 @@ def listElem( item ):
def listToTuple( items ):
return tuple( items )
- # Flatten only the top layer (list of lists of ...)
+# Flatten only the top layer (list of lists of ...)
def oneLayerFlatten( items ):
mainList = []
for sublist in items:
@@ -398,14 +395,27 @@ 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 ):
+ '''
+ Capability arguments may need to be expanded
+ (e.g. 1 16 bit argument needs to be 2 8 bit arguments for the state machine)
+
+ If the number is negative, determine width of the final value, mask to max, subtract,
+ then convert to multiple bytes
+ '''
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
+
+ # Set last bit if value is negative
+ if num < 0:
+ max_val = 2 ** (argLen * 8)
+ num += max_val
+
+ # XXX Yes, little endian from how the uC structs work
+ byteForm = num.to_bytes( argLen, byteorder='little' )
# For each sub-argument, split into byte-sized chunks
for byte in range( 0, argLen ):
@@ -413,8 +423,8 @@ def capArgExpander( items ):
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 ] ],