Search in sources :

Example 1 with MATLABPreInputVarInit

use of com.rockwellcollins.atc.agree.codegen.ast.MATLABPreInputVarInit in project AGREE by loonwerks.

the class LustreToMATLABExprVisitor method visit.

@Override
public MATLABExpr visit(UnaryExpr e) {
    MATLABExpr expr = e.expr.accept(this);
    String opName = e.op.name();
    MATLABUnaryOp op = MATLABUnaryOp.fromName(opName);
    if (op == null) {
        if (opName.equals("PRE")) {
            if (expr instanceof MATLABIdExpr) {
                // function call for the following AGREE unary operator
                // PRE ("pre");
                String varName = ((MATLABIdExpr) expr).id;
                String preVarName = updateName("pre_" + varName, "");
                // no duplicate addition
                if (!persistentVarMap.containsKey(preVarName)) {
                    // init based on varName
                    if (inputSet.contains(varName)) {
                        persistentVarInits.add(new MATLABPreInputVarInit(preVarName, varName));
                        persistentVarMap.put(preVarName, new MATLABIdExpr(varName));
                    } else // if the var is a local variable
                    // the init needs to be associated with a default value of the type
                    // instead of varName, as the varName may be assigned in the same equation
                    // the preVar first gets used
                    {
                        // add preVar init based on default value of the type
                        if (localVarTypeMap.containsKey(varName)) {
                            MATLABType type = localVarTypeMap.get(varName);
                            persistentVarInits.add(new MATLABPreLocalVarInit(preVarName, new MATLABTypeInitExpr(type)));
                            persistentVarMap.put(preVarName, new MATLABIdExpr(varName));
                        } else {
                            throw new IllegalArgumentException();
                        }
                    }
                }
                return new MATLABIdExpr(preVarName);
            } else {
                throw new IllegalArgumentException();
            }
        } else {
            throw new IllegalArgumentException();
        }
    } else {
        return new MATLABUnaryExpr(op, expr);
    }
}
Also used : MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) MATLABUnaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryExpr) MATLABPreLocalVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABPreLocalVarInit) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) MATLABPreInputVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABPreInputVarInit) MATLABUnaryOp(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryOp) MATLABTypeInitExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr) MATLABType(com.rockwellcollins.atc.agree.codegen.ast.MATLABType)

Aggregations

MATLABPreInputVarInit (com.rockwellcollins.atc.agree.codegen.ast.MATLABPreInputVarInit)1 MATLABPreLocalVarInit (com.rockwellcollins.atc.agree.codegen.ast.MATLABPreLocalVarInit)1 MATLABType (com.rockwellcollins.atc.agree.codegen.ast.MATLABType)1 MATLABExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr)1 MATLABIdExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr)1 MATLABTypeInitExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr)1 MATLABUnaryExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryExpr)1 MATLABUnaryOp (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryOp)1