Search in sources :

Example 1 with MATLABIdExpr

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

the class LustreToMATLABExprVisitor method visit.

@Override
public MATLABExpr visit(RecordExpr e) {
    // Create a local variable with the record type id as prefix
    localVarIndex++;
    String localVarName = updateName(e.id + "_var" + localVarIndex, "");
    // add assignment to assign the fields of the variable
    // to the value specified in the RecordExpr
    SortedMap<String, MATLABExpr> fields = new TreeMap<>(new StringNaturalOrdering());
    Iterator<Entry<String, Expr>> iterator = e.fields.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, Expr> entry = iterator.next();
        // get the type for the field
        MATLABType type = recordTypeMap.get(e.id).get(updateName(entry.getKey(), e.id));
        // conduct explicit type cast if a field value is a constant of double type or int type
        MATLABTypeCastExprVisitor typeCastVisitor = new MATLABTypeCastExprVisitor(type);
        MATLABExpr fieldExpr = typeCastVisitor.visit(entry.getValue().accept(this));
        fields.put(updateName(entry.getKey(), e.id), fieldExpr);
    }
    localBusVarInits.add(new MATLABLocalBusVarInit(localVarName, null, fields));
    // In the expression that uses the RecordExpr, just reference the local variable
    return new MATLABIdExpr(localVarName);
}
Also used : MATLABLocalBusVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) TreeMap(java.util.TreeMap) MATLABType(com.rockwellcollins.atc.agree.codegen.ast.MATLABType) Entry(java.util.Map.Entry) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) MATLABTypeInitExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) TupleExpr(jkind.lustre.TupleExpr) UnaryExpr(jkind.lustre.UnaryExpr) MATLABUnaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryExpr) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) CondactExpr(jkind.lustre.CondactExpr) MATLABBinaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBinaryExpr) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) Expr(jkind.lustre.Expr) CastExpr(jkind.lustre.CastExpr) IntExpr(jkind.lustre.IntExpr) MATLABDoubleExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABDoubleExpr) MATLABBusElementExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBusElementExpr) MATLABTypeCastExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeCastExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) RecordExpr(jkind.lustre.RecordExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealExpr(jkind.lustre.RealExpr) MATLABBoolExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBoolExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) ArrayExpr(jkind.lustre.ArrayExpr) MATLABIntExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIntExpr) MATLABArrayAccessExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABArrayAccessExpr) IdExpr(jkind.lustre.IdExpr) ArrayUpdateExpr(jkind.lustre.ArrayUpdateExpr) FunctionCallExpr(jkind.lustre.FunctionCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) StringNaturalOrdering(jkind.util.StringNaturalOrdering) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr)

Example 2 with MATLABIdExpr

use of com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr 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)

Example 3 with MATLABIdExpr

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

the class LustreToMATLABExprVisitor method visit.

@Override
public MATLABExpr visit(RecordAccessExpr e) {
    MATLABExpr busExpr = e.record.accept(this);
    MATLABIdExpr elementExpr = new MATLABIdExpr(updateName(e.field, busExpr.toString()));
    return new MATLABBusElementExpr(busExpr, elementExpr);
}
Also used : MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) MATLABBusElementExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBusElementExpr)

Example 4 with MATLABIdExpr

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

the class LustreToMATLABExprVisitor method visit.

@Override
public MATLABExpr visit(RecordUpdateExpr e) {
    MATLABIdExpr recordIdExpr = (MATLABIdExpr) e.record.accept(this);
    // Assign the specific field of the variable created from the recordExpr associated with it
    // to the value specified in the RecordUpdateExpr
    SortedMap<String, MATLABExpr> fields = new TreeMap<>(new StringNaturalOrdering());
    // get the type for the field
    Expr curExpr = e;
    while (curExpr instanceof RecordUpdateExpr) {
        curExpr = ((RecordUpdateExpr) curExpr).record;
    }
    MATLABType type = null;
    String recordName = "";
    if (curExpr instanceof RecordExpr) {
        recordName = ((RecordExpr) curExpr).id;
        if (recordTypeMap.get(recordName) != null) {
            String fieldName = e.field;
            fieldName = updateName(fieldName, recordName);
            type = recordTypeMap.get(recordName).get(fieldName);
        }
    } else {
        recordName = recordIdExpr.id;
    }
    // conduct explicit type cast if a field value is a constant of double type or int type
    MATLABTypeCastExprVisitor typeCastVisitor = new MATLABTypeCastExprVisitor(type);
    MATLABExpr fieldExpr = typeCastVisitor.visit(e.value.accept(this));
    fields.put(updateName(e.field, recordName), fieldExpr);
    String originalVar = null;
    if (e.record instanceof IdExpr) {
        originalVar = updateName(e.record.toString(), "");
    } else {
        originalVar = updateName(recordIdExpr.id.split("_var")[0] + "_var" + localVarIndex, "");
    }
    localVarIndex++;
    String newVar = updateName(recordIdExpr.id.split("_var")[0] + "_var" + localVarIndex, "");
    localBusVarInits.add(new MATLABLocalBusVarInit(originalVar, newVar, fields));
    // In the expression that uses the RecordUpdateExpr, just reference the variable
    return new MATLABIdExpr(newVar);
}
Also used : MATLABLocalBusVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) IdExpr(jkind.lustre.IdExpr) TreeMap(java.util.TreeMap) MATLABType(com.rockwellcollins.atc.agree.codegen.ast.MATLABType) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) MATLABTypeInitExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) TupleExpr(jkind.lustre.TupleExpr) UnaryExpr(jkind.lustre.UnaryExpr) MATLABUnaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryExpr) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) CondactExpr(jkind.lustre.CondactExpr) MATLABBinaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBinaryExpr) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) Expr(jkind.lustre.Expr) CastExpr(jkind.lustre.CastExpr) IntExpr(jkind.lustre.IntExpr) MATLABDoubleExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABDoubleExpr) MATLABBusElementExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBusElementExpr) MATLABTypeCastExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeCastExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) RecordExpr(jkind.lustre.RecordExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealExpr(jkind.lustre.RealExpr) MATLABBoolExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBoolExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) ArrayExpr(jkind.lustre.ArrayExpr) MATLABIntExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIntExpr) MATLABArrayAccessExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABArrayAccessExpr) IdExpr(jkind.lustre.IdExpr) ArrayUpdateExpr(jkind.lustre.ArrayUpdateExpr) FunctionCallExpr(jkind.lustre.FunctionCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) StringNaturalOrdering(jkind.util.StringNaturalOrdering) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) RecordExpr(jkind.lustre.RecordExpr)

Example 5 with MATLABIdExpr

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

the class MATLABPrettyPrintVisitor method visit.

@Override
public Void visit(MATLABPrimaryFunction primaryFunction) {
    // write function header
    write("function " + primaryFunction.name);
    write("(");
    // write inputs, separated by ","
    Iterator<MATLABIdExpr> iterator = primaryFunction.inputs.iterator();
    while (iterator.hasNext()) {
        iterator.next().accept(this);
        if (iterator.hasNext()) {
            write(", ");
        }
    }
    write(")");
    newline();
    newline();
    // write preVar declarations and initializations
    for (MATLABPersistentVarDecl persistentVarDecl : primaryFunction.persistentVarDecl) {
        persistentVarDecl.accept(this);
    }
    newline();
    // write statements
    for (MATLABStatement statement : primaryFunction.statements) {
        statement.accept(this);
    }
    newline();
    write("end");
    newline();
    newline();
    // write function definitions
    for (MATLABFunction function : primaryFunction.functions) {
        function.accept(this);
    }
    return null;
}
Also used : MATLABPersistentVarDecl(com.rockwellcollins.atc.agree.codegen.ast.MATLABPersistentVarDecl) MATLABStatement(com.rockwellcollins.atc.agree.codegen.ast.MATLABStatement) MATLABFunction(com.rockwellcollins.atc.agree.codegen.ast.MATLABFunction) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr)

Aggregations

MATLABIdExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr)6 MATLABExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr)5 MATLABType (com.rockwellcollins.atc.agree.codegen.ast.MATLABType)4 MATLABLocalBusVarInit (com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit)3 MATLABBusElementExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBusElementExpr)3 MATLABTypeInitExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr)3 MATLABUnaryExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryExpr)3 TreeMap (java.util.TreeMap)3 Expr (jkind.lustre.Expr)3 StringNaturalOrdering (jkind.util.StringNaturalOrdering)3 MATLABFunction (com.rockwellcollins.atc.agree.codegen.ast.MATLABFunction)2 MATLABPersistentVarDecl (com.rockwellcollins.atc.agree.codegen.ast.MATLABPersistentVarDecl)2 MATLABStatement (com.rockwellcollins.atc.agree.codegen.ast.MATLABStatement)2 MATLABArrayAccessExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABArrayAccessExpr)2 MATLABBinaryExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBinaryExpr)2 MATLABBoolExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBoolExpr)2 MATLABDoubleExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABDoubleExpr)2 MATLABIntExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIntExpr)2 MATLABTypeCastExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeCastExpr)2 ArrayAccessExpr (jkind.lustre.ArrayAccessExpr)2