def _processSpecialC(self, specStr): """Pre-process 'if' statements and names of 'abs' and 'sign' functions, as well as logical operators. """ qspec = QuantSpec('spec', specStr, treatMultiRefs=False) qspec.mapNames({'abs': 'fabs', 'sign': 'signum', 'mod': 'fmod', 'and': '&&', 'or': '||', 'not': '!', 'True': 1, 'False': 0, 'if': '__rhs_if', 'max': '__maxof', 'min': '__minof'}) qtoks = qspec.parser.tokenized # default value new_specStr = str(qspec) # NOTE: This simple iterative parsing of the arguments means that # user cannot nest calls to min() or max() with eachother if '__minof' in qtoks: new_specStr = "" num = qtoks.count('__minof') n_ix = -1 ix_continue = 0 for _ in range(num): n_ix = qtoks[n_ix + 1:].index('__minof') + n_ix + 1 new_specStr += "".join(qtoks[ix_continue:n_ix]) rbrace_ix = findEndBrace(qtoks[n_ix + 1:]) + n_ix + 1 ix_continue = rbrace_ix + 1 #assert qtoks[n_ix+2] == '[', "Error in min() syntax" #assert qtoks[rbrace_ix-1] == ']', "Error in min() syntax" #new_specStr += "".join(qtoks[n_ix+3:rbrace_ix-1]) + ")" num_args = qtoks[n_ix + 2:ix_continue].count(',') + 1 if num_args > 4: raise NotImplementedError( "Max of more than 4 arguments not currently supported in C") new_specStr += '__minof%s(' % str(num_args) new_specStr += "".join( [q for q in qtoks[n_ix + 2:ix_continue] if q not in ('[', ']')]) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec('spec', new_specStr) qtoks = qspec.parser.tokenized if '__maxof' in qtoks: new_specStr = "" num = qtoks.count('__maxof') n_ix = -1 ix_continue = 0 for _ in range(num): n_ix = qtoks[n_ix + 1:].index('__maxof') + n_ix + 1 new_specStr += "".join(qtoks[ix_continue:n_ix]) rbrace_ix = findEndBrace(qtoks[n_ix + 1:]) + n_ix + 1 ix_continue = rbrace_ix + 1 #assert qtoks[n_ix+2] == '[', "Error in max() syntax" #assert qtoks[rbrace_ix-1] == ']', "Error in max() syntax" #new_specStr += "".join(qtoks[n_ix+3:rbrace_ix-1]) + ")" num_args = qtoks[n_ix + 2:ix_continue].count(',') + 1 if num_args > 4: raise NotImplementedError( "Min of more than 4 arguments not currently supported in C") new_specStr += '__maxof%s(' % str(num_args) new_specStr += "".join( [q for q in qtoks[n_ix + 2:ix_continue] if q not in ('[', ']')]) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec('spec', new_specStr) qtoks = qspec.parser.tokenized return new_specStr
def _processSpecialC(self, specStr): """Pre-process 'if' statements and names of 'abs' and 'sign' functions, as well as logical operators. """ qspec = QuantSpec("spec", specStr, treatMultiRefs=False) qspec.mapNames( { "abs": "fabs", "sign": "signum", "mod": "fmod", "and": "&&", "or": "||", "not": "!", "True": 1, "False": 0, "max": "__maxof", "min": "__minof", } ) qtoks = qspec.parser.tokenized # default value new_specStr = str(qspec) if "if" in qtoks: new_specStr = "" num_ifs = qtoks.count("if") if_ix = -1 ix_continue = 0 for _ in range(num_ifs): if_ix = qtoks[if_ix + 1 :].index("if") + if_ix + 1 new_specStr += "".join(qtoks[ix_continue:if_ix]) + "__rhs_if(" rbrace_ix = findEndBrace(qtoks[if_ix + 1 :]) + if_ix + 1 ix_continue = rbrace_ix + 1 new_specStr += "".join(qtoks[if_ix + 2 : ix_continue]) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec("spec", new_specStr) qtoks = qspec.parser.tokenized if "__minof" in qtoks: new_specStr = "" num = qtoks.count("__minof") n_ix = -1 ix_continue = 0 for _ in range(num): n_ix = qtoks[n_ix + 1 :].index("__minof") + n_ix + 1 new_specStr += "".join(qtoks[ix_continue:n_ix]) rbrace_ix = findEndBrace(qtoks[n_ix + 1 :]) + n_ix + 1 ix_continue = rbrace_ix + 1 # assert qtoks[n_ix+2] == '[', "Error in min() syntax" # assert qtoks[rbrace_ix-1] == ']', "Error in min() syntax" # new_specStr += "".join(qtoks[n_ix+3:rbrace_ix-1]) + ")" num_args = qtoks[n_ix + 2 : ix_continue].count(",") + 1 if num_args > 4: raise NotImplementedError( "Max of more than 4 arguments not currently supported in C" ) new_specStr += "__minof%s(" % str(num_args) new_specStr += "".join( [q for q in qtoks[n_ix + 2 : ix_continue] if q not in ("[", "]")] ) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec("spec", new_specStr) qtoks = qspec.parser.tokenized if "__maxof" in qtoks: new_specStr = "" num = qtoks.count("__maxof") n_ix = -1 ix_continue = 0 for _ in range(num): n_ix = qtoks[n_ix + 1 :].index("__maxof") + n_ix + 1 new_specStr += "".join(qtoks[ix_continue:n_ix]) rbrace_ix = findEndBrace(qtoks[n_ix + 1 :]) + n_ix + 1 ix_continue = rbrace_ix + 1 # assert qtoks[n_ix+2] == '[', "Error in max() syntax" # assert qtoks[rbrace_ix-1] == ']', "Error in max() syntax" # new_specStr += "".join(qtoks[n_ix+3:rbrace_ix-1]) + ")" num_args = qtoks[n_ix + 2 : ix_continue].count(",") + 1 if num_args > 4: raise NotImplementedError( "Min of more than 4 arguments not currently supported in C" ) new_specStr += "__maxof%s(" % str(num_args) new_specStr += "".join( [q for q in qtoks[n_ix + 2 : ix_continue] if q not in ("[", "]")] ) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec("spec", new_specStr) qtoks = qspec.parser.tokenized return new_specStr
def _processSpecialC(self, specStr): """Pre-process 'if' statements and names of 'abs' and 'sign' functions, as well as logical operators. """ qspec = QuantSpec('spec', specStr, treatMultiRefs=False) qspec.mapNames({'abs': 'fabs', 'sign': 'signum', 'mod': 'fmod', 'and': '&&', 'or': '||', 'not': '!', 'True': 1, 'False': 0, 'max': '__maxof', 'min': '__minof'}) qtoks = qspec.parser.tokenized # default value new_specStr = str(qspec) if 'if' in qtoks: new_specStr = "" num_ifs = qtoks.count('if') if_ix = -1 ix_continue = 0 for _ in range(num_ifs): if_ix = qtoks[if_ix + 1:].index('if') + if_ix + 1 new_specStr += "".join(qtoks[ix_continue:if_ix]) + "__rhs_if(" rbrace_ix = findEndBrace(qtoks[if_ix + 1:]) + if_ix + 1 ix_continue = rbrace_ix + 1 new_specStr += "".join(qtoks[if_ix + 2:ix_continue]) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec('spec', new_specStr) qtoks = qspec.parser.tokenized if '__minof' in qtoks: new_specStr = "" num = qtoks.count('__minof') n_ix = -1 ix_continue = 0 for _ in range(num): n_ix = qtoks[n_ix + 1:].index('__minof') + n_ix + 1 new_specStr += "".join(qtoks[ix_continue:n_ix]) rbrace_ix = findEndBrace(qtoks[n_ix + 1:]) + n_ix + 1 ix_continue = rbrace_ix + 1 #assert qtoks[n_ix+2] == '[', "Error in min() syntax" #assert qtoks[rbrace_ix-1] == ']', "Error in min() syntax" #new_specStr += "".join(qtoks[n_ix+3:rbrace_ix-1]) + ")" num_args = qtoks[n_ix + 2:ix_continue].count(',') + 1 if num_args > 4: raise NotImplementedError( "Max of more than 4 arguments not currently supported in C") new_specStr += '__minof%s(' % str(num_args) new_specStr += "".join( [q for q in qtoks[n_ix + 2:ix_continue] if q not in ('[', ']')]) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec('spec', new_specStr) qtoks = qspec.parser.tokenized if '__maxof' in qtoks: new_specStr = "" num = qtoks.count('__maxof') n_ix = -1 ix_continue = 0 for _ in range(num): n_ix = qtoks[n_ix + 1:].index('__maxof') + n_ix + 1 new_specStr += "".join(qtoks[ix_continue:n_ix]) rbrace_ix = findEndBrace(qtoks[n_ix + 1:]) + n_ix + 1 ix_continue = rbrace_ix + 1 #assert qtoks[n_ix+2] == '[', "Error in max() syntax" #assert qtoks[rbrace_ix-1] == ']', "Error in max() syntax" #new_specStr += "".join(qtoks[n_ix+3:rbrace_ix-1]) + ")" num_args = qtoks[n_ix + 2:ix_continue].count(',') + 1 if num_args > 4: raise NotImplementedError( "Min of more than 4 arguments not currently supported in C") new_specStr += '__maxof%s(' % str(num_args) new_specStr += "".join( [q for q in qtoks[n_ix + 2:ix_continue] if q not in ('[', ']')]) new_specStr += "".join(qtoks[ix_continue:]) qspec = QuantSpec('spec', new_specStr) qtoks = qspec.parser.tokenized return new_specStr