示例#1
0
    def evaluate(self, expression):
        assert expression.startswith("$") and expression.endswith("$"), \
            expression
        expression = expression[1:-1]
        m = name_match(expression)
        if m:
            type, expr = m.group(1, 2)
        else:
            type = "path"
            expr = expression

        if type in ("string", "str"):
            return expr
        if type in ("path", "var", "global", "local"):
            return self.evaluatePathOrVar(expr)
        if type == "not":
            return not self.evaluate(expr)
        if type == "exists":
            return expr in self.locals or expr in self.globals
        if type == "python":
            try:
                return eval(expr, self.globals, self.locals)
            except:
                raise TALExpressionError("evaluation error in %s" % repr(expr))
        if type == "position":
            # Insert the current source file name, line number,
            # and column offset.
            if self.position:
                lineno, offset = self.position
            else:
                lineno, offset = None, None
            return '%s (%s,%s)' % (self.source_file, lineno, offset)
        raise TALExpressionError("unrecognized expression: " +
                                 repr(expression))
示例#2
0
 def evaluatePathOrVar(self, expr):
     expr = expr.strip()
     if expr in self.locals:
         return self.locals[expr]
     elif expr in self.globals:
         return self.globals[expr]
     else:
         raise TALExpressionError("unknown variable: %s" % repr(expr))
示例#3
0
 def findMacroFile(self, macroName):
     if not macroName:
         raise TALExpressionError("empty macro name")
     i = macroName.rfind('/')
     if i < 0:
         # No slash -- must be a locally defined macro
         return None, macroName
     else:
         # Up to last slash is the filename
         fileName = macroName[:i]
         localName = macroName[i+1:]
         return fileName, localName
示例#4
0
 def evaluateMacro(self, macroName):
     assert macroName.startswith("$") and macroName.endswith("$"), \
         macroName
     macroName = macroName[1:-1]
     file, localName = self.findMacroFile(macroName)
     if not file:
         # Local macro
         macro = self.macros[localName]
     else:
         # External macro
         import driver
         program, macros = driver.compilefile(file)
         macro = macros.get(localName)
         if not macro:
             raise TALExpressionError("macro %s not found in file %s" %
                                      (localName, file))
     return macro