Esempio n. 1
0
 def call(self, model, tree):
     args = tree.value[1:]
     ret = None
     while interp.isTrue(interp.getv(interp.interpret(model, args[0]))):
         for arg in args[1:]:
             ret = interp.interpret(model, arg)
     return ret
Esempio n. 2
0
 def call(self, model, tree):
     args = tree.value[1:]
     if interp.isTrue(interp.getv(interp.interpret(model, args[0]))):
         return interp.interpret(model, args[1])
     else:
         if (len(args)==3):
             return interp.interpret(model, args[2])
         else:
             return interp.lcad_nil
Esempio n. 3
0
 def call(self, model, tree):
     args = tree.value[1:]
     ret = interp.lcad_nil
     for arg in args:
         nodes = arg.value
         if interp.isTrue(interp.getv(interp.interpret(model, nodes[0]))):
             for node in nodes[1:]:
                 ret = interp.interpret(model, node)
             return ret
     return ret
Esempio n. 4
0
 def call(self, model, tree):
     if (self.numberArgs(tree) > 1):
         model.pushGroup(self.getArg(model, tree, 0))
         val = interp.interpret(model, tree.value[2:])
         model.popGroup()
         return val
     else:
         return None
Esempio n. 5
0
 def call(self, model, tree):
     if (self.numberArgs(tree) > 1):
         model.pushGroup(self.getArg(model, tree, 0))
         val = interp.interpret(model, tree.value[2:])
         model.popGroup()
         return val
     else:
         return None
Esempio n. 6
0
def ldrawCoeffsToMatrix(model, coeffs):

    m = numpy.identity(4).view(lcadTypes.LCadMatrix)
    for x in mapping:
        val = interp.getv(interp.interpret(model, coeffs[x[0]]))
        if not isinstance(val, numbers.Number):
            raise lce.WrongTypeException("number", type(val))
        m[x[1]] = val
    return m
Esempio n. 7
0
def ldrawCoeffsToMatrix(model, coeffs):

    m = numpy.identity(4).view(lcadTypes.LCadMatrix)
    for x in mapping:
        val = interp.getv(interp.interpret(model, coeffs[x[0]]))
        if not isinstance(val, numbers.Number):
            raise lce.WrongTypeException("number", type(val))
        m[x[1]] = val
    return m
Esempio n. 8
0
def exe(string):
    """
    Wrap interpreter call for convenience.
    """
    lenv = interpreter.LEnv(add_built_ins = True)
    model = interpreter.Model()
    ast = lexerParser.parse(string, "test")
    interpreter.createLexicalEnv(lenv, ast)
    sym = interpreter.interpret(model, ast)
    return interpreter.getv(sym)
Esempio n. 9
0
    def call(self, model, tree):
        args = tree.value[1:]
        ret = None
        kv_pairs = izip(*[iter(args)]*2)
        for sym_node, val_node in kv_pairs:

            sym = interp.interpret(model, sym_node)
            if not isinstance(sym, interp.Symbol):
                raise lce.CannotSetException(type(sym))
            if (sym.name in interp.builtin_functions):
                print("Warning, overwriting builtin function:", sym.name, "!!")
            if (sym.name in interp.builtin_symbols):
                if not (sym.name in interp.mutable_symbols):
                    raise lce.CannotOverrideBuiltIn()

            val = interp.getv(interp.interpret(model, val_node))
            sym.setv(val)
            ret = val
        return ret
Esempio n. 10
0
    def call(self, model, tree):

        if tree.initialized:
            return
        else:
            tree.initialized = True

        args = tree.value[1:]
        local = True if (args[-1].value == ":local") else False
        if local:
            args = args[:-1]
        for arg in args:

            # Load module with it's own lexical environment and model.
            module_lenv = interp.LEnv(add_built_ins = True)
            module_model = interp.Model(False)
            for path in self.paths:
                filename = path + arg.value + ".lcad"
                if os.path.exists(filename):
                    with open(filename) as fp:
                        module_ast = lexerParser.parse(fp.read(), filename)
                        interp.createLexicalEnv(module_lenv, module_ast)
                        interp.interpret(module_model, module_ast)
                    break
            else:
                raise lce.FileNotFoundException(arg.value + ".lcad")

            # Add symbols from the modules lexical environment
            # to the main lexical environment.
            lenv = tree.lenv.parent
            for sym_name in module_lenv.symbols:
                if (not sym_name in interp.builtin_symbols) and (not sym_name in interp.builtin_functions):
                    if local:
                        interp.checkOverride(lenv, sym_name, module_lenv.symbols[sym_name].filename)
                        lenv.symbols[sym_name] = module_lenv.symbols[sym_name]
                    else:
                        full_name = arg.value + ":" + sym_name
                        interp.checkOverride(lenv, full_name)
                        lenv.symbols[full_name] = module_lenv.symbols[sym_name]
Esempio n. 11
0
    def call(self, model, tree):
        loop_args = tree.value[1].value
        inc_var = tree.lenv.symbols[loop_args[0].value]

        # Iterate over list.
        arg1 = interp.getv(interp.interpret(model, loop_args[1]))
        if ((len(loop_args)==2) and (isinstance(arg1, list))):
            ret = None
            for elt in arg1:
                inc_var.setv(elt)
                for node in tree.value[2:]:
                    ret = interp.interpret(model, node)
            return ret

        # "Normal" iteration.
        else:
            start = 0
            inc = 1
            if (len(loop_args)==2):
                stop = interp.getv(arg1)
            elif (len(loop_args)==3):
                start = interp.getv(interp.interpret(model, loop_args[1]))
                stop = interp.getv(interp.interpret(model, loop_args[2]))
            else:
                start = interp.getv(interp.interpret(model, loop_args[1]))
                inc = interp.getv(interp.interpret(model, loop_args[2]))
                stop = interp.getv(interp.interpret(model, loop_args[3]))

            # loop.
            ret = None
            cur = start
            while(cur < stop):
                inc_var.setv(cur)
                for node in tree.value[2:]:
                    ret = interp.interpret(model, node)
                cur += inc
            return ret
        print("end")
Esempio n. 12
0
    def call(self, model, tree):
        args = tree.value[1:]

        # Symbols are created in the lexical environment of the parent expression.
        lenv = tree.lenv.parent

        # Functions have already been created (so that they can be called out of
        # order), just return the function.
        if (len(args) == 3):
            tree.initialized = True
            return lenv.symbols[args[0].value].getv()

        # Create Symbols.
        if ((len(args)%2) == 0):
            ret = None
            kv_pairs = izip(*[iter(args)]*2)
            for key, node in kv_pairs:

                # FIXME: Maybe something more appropriate for the error, like can only create symbols?
                if not isinstance(key, lexerParser.LCadSymbol):
                    raise lce.CannotSetException(type(key))

                symbol_name = key.value
                if not tree.initialized:
                    try:
                        interp.checkOverride(lenv, symbol_name)
                    except lce.SymbolNotDefined:
                        pass

                if not (symbol_name in lenv.symbols):
                    lenv.symbols[symbol_name] = interp.Symbol(symbol_name, tree.filename)

                val = interp.getv(interp.interpret(model, node))
                lenv.symbols[symbol_name].setv(val)
                ret = val
            tree.initialized = True
            return ret