use of jkind.lustre.RecordExpr in project AGREE by loonwerks.
the class AgreeUtils method getInitValueFromType.
public static Expr getInitValueFromType(Type type) {
if (type instanceof NamedType) {
return getInitValueFromType((NamedType) type);
}
if (type instanceof RecordType) {
RecordType recordType = (RecordType) type;
Map<String, Expr> fieldMap = new HashMap<>();
for (Entry<String, Type> entry : recordType.fields.entrySet()) {
Expr subExpr = getInitValueFromType(entry.getValue());
fieldMap.put(entry.getKey(), subExpr);
}
return new RecordExpr(recordType.id, fieldMap);
}
throw new AgreeException("AGREE cannot figure out an initial type for Lustre type: " + type.getClass());
}
use of jkind.lustre.RecordExpr 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);
}
use of jkind.lustre.RecordExpr 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);
}
use of jkind.lustre.RecordExpr in project AGREE by loonwerks.
the class ExpressionFlattener method flattenRecordExpression.
private static void flattenRecordExpression(final Expr expr, final RecordType recordType, final Map<String, Expr> results) {
if (expr instanceof RecordUpdateExpr) {
final RecordUpdateExpr updateExpr = (RecordUpdateExpr) expr;
// Handle base record
flattenRecordExpression(updateExpr.record, recordType, results);
// Handle the update
results.put(updateExpr.field, updateExpr.value);
} else if (expr instanceof RecordExpr) {
final RecordExpr recordExpr = (RecordExpr) expr;
results.putAll(recordExpr.fields);
} else {
// IdExpr
for (final String field : recordType.fields.keySet()) {
results.put(field, new RecordAccessExpr(expr, field));
}
}
}
Aggregations