Project

General

Profile

« Previous | Next » 

Revision 10

Added by Willibald K. over 9 years ago

restrict list casts to something useful - i.e. inner type sizes must not change; this is in preparation for faster dynamic list support in momut; deep casts now need to be done manually by the user via fold operations or can be added again to the ooas compiler as a more expensive, special cast operator; also more cleaning of Qualitative Action System stuff

View differences:

trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/types/Type.java
(m_kind == TypeKind.EnumeratedType && o instanceof ValuedEnumType);
}
public boolean IsQualitative()
{
return (m_kind == TypeKind.QrType);
}
/**
* returns the number of unique values of the type (2 for bool, etc..)
*/
......
case ListType:
final ListType listt1 = (ListType)type1;
final ListType listt2 = (ListType)type2;
int maxelems = listt1.maxNumberOfElements() > listt2.maxNumberOfElements() ?
listt1.maxNumberOfElements() : listt2.maxNumberOfElements();
// empty lists lack type, so take the other one
if ((listt1.innerType().kind() == TypeKind.Null) && (listt2.innerType().kind() != TypeKind.Null))
return listt2;
......
return listt1;
else
{
final Type subtype = Type.CoverType(listt1.innerType(), listt2.innerType());
if (subtype != null)
/**
* A cover type of a list must not change the size of the inner type. Mostly what is
* allowed to change is the list length.
*/
Type subtype = listt1.innerType();
final boolean castAllowed = TypeEqual(listt1.innerType(), listt2.innerType())
|| ( (listt1.innerType().kind() == TypeKind.OoActionSystemType)
&& (subtype = Type.CoverType(listt1.innerType(), listt2.innerType())) != null);
if (castAllowed){
final int maxelems = listt1.maxNumberOfElements() > listt2.maxNumberOfElements()
? listt1.maxNumberOfElements()
: listt2.maxNumberOfElements();
return new ListType(subtype, maxelems, null);
else
} else
return null;
// // The code blow allowed for bit-size changes of the inner type and is considered too
// // liberal. If a deep cast is necessary, the user might want to do a fold operation
// // instead..
// final Type subtype = Type.CoverType(listt1.innerType(), listt2.innerType());
// if (subtype != null)
// return new ListType(subtype, maxelems, null);
// else
// return null;
}
case MapType:
final MapType mapt1 = (MapType)type1;
......
{
final Type sub1 = Type.CoverType(mapt1.fromType(), mapt2.fromType());
final Type sub2 = Type.CoverType(mapt2.toType(), mapt2.toType());
maxelems = mapt1.maxNumberOfElements() > mapt2.maxNumberOfElements() ?
final int maxelems = mapt1.maxNumberOfElements() > mapt2.maxNumberOfElements() ?
mapt1.maxNumberOfElements() : mapt2.maxNumberOfElements();
if (sub1 != null && sub2 != null)
return new MapType(sub1, sub2, maxelems, null);
else
return null;
}
case QrType:
return null; /*if refs are equal, we do not reach this statement! see above..*/
case TupleType:
final TupleType tuplet1 = (TupleType)type1;
final TupleType tuplet2 = (TupleType)type2;
......
return (mapt1.maxNumberOfElements() < mapt2.maxNumberOfElements()) ||
Type.FirstTypeLessRange(mapt1.fromType(), mapt2.fromType()) ||
Type.FirstTypeLessRange(mapt1.toType(), mapt2.toType());
case QrType:
return false;
case TupleType:
final TupleType tuplet1 = (TupleType)type1;
final TupleType tuplet2 = (TupleType)type2;
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/types/TypeKind.java
EnumeratedType(3),
ListType(4),
MapType(5),
QrType(6),
// QrType(6), // no longer supported
TupleType(7),
FunctionType(8),
OoActionSystemType(9),
trunk/compiler/ooasCompiler/src/org/momut/ooas/codegen/cadp/CadpType.java
EmitDefinition((OoActionSystemType)aType, emitter);
break;
case QrType:
throw new UnsupportedOperationException(); // does not make sense in CADP target.
case FunctionType:
emittedTypes.add(typename);
EmitDefinition((FunctionType)aType, emitter);
trunk/compiler/ooasCompiler/src/org/momut/ooas/codegen/java/JavaExpression.java
@Override
public void visit(TypeExpression typeExpression)
{
if (typeExpression.type().kind() != TypeKind.QrType &&
typeExpression.type().kind() != TypeKind.EnumeratedType)
if (typeExpression.type().kind() != TypeKind.EnumeratedType)
m_emitter.Append(GetIdentifierString(typeExpression.type().identifier().tokenText()));
}
......
@Override
public void visit(AccessExpression accessExpression)
{
if (accessExpression.left().type().kind() != TypeKind.QrType &&
accessExpression.left().type().kind() != TypeKind.EnumeratedType)
if (accessExpression.left().type().kind() != TypeKind.EnumeratedType)
{
// enums and qr types are directly (statically) converted to nums...
VisitSub(accessExpression.left(), accessExpression);
trunk/compiler/ooasCompiler/src/org/momut/ooas/codegen/prolog/OoaPrologExpression.java
case minus: // T_MINUS:
return "-";
case less:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? "<" : "#<";
return !isNumericBinary(expression) ? "<" : "#<";
case lessequal:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? "=<" : "#=<";
return !isNumericBinary(expression) ? "=<" : "#=<";
case greater:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? ">" : "#>";
return !isNumericBinary(expression) ? ">" : "#>";
case greaterequal:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? ">=" : "#>=";
return !isNumericBinary(expression) ? ">=" : "#>=";
case equal:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? "==" : "#=";
return !isNumericBinary(expression) ? "==" : "#=";
case notequal:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? "\\=" : "#\\=";
return !isNumericBinary(expression) ? "\\=" : "#\\=";
case and: // T_AND:
throw new NotImplementedException(); // implemented in binaryoperator
case or: // T_OR:
......
@Override
public void visit(TypeExpression typeExpression)
{
if (typeExpression.type().kind() != TypeKind.QrType &&
typeExpression.type().kind() != TypeKind.EnumeratedType)
if (typeExpression.type().kind() != TypeKind.EnumeratedType)
{
m_emitter.Append(GetIdentifierString(typeExpression.type().identifier()));
}
......
@Override
public void visit(AccessExpression accessExpression)
{
if (accessExpression.left().type().kind() != TypeKind.QrType &&
accessExpression.left().type().kind() != TypeKind.EnumeratedType &&
!(accessExpression.right().kind() == ExpressionKind.Identifier &&
((IdentifierExpression)accessExpression.right()).identifier().kind() == IdentifierKind.AttributeIdentifier))
if (accessExpression.left().type().kind() != TypeKind.EnumeratedType &&
!(accessExpression.right().kind() == ExpressionKind.Identifier &&
((IdentifierExpression)accessExpression.right()).identifier().kind() == IdentifierKind.AttributeIdentifier))
{
assert(m_tmpVars.size() == 0);
// enums and qr types are directly (statically) converted to nums...
......
default:
m_emitter.Append(leftcode.toString());
m_emitter.Append(rightcode.toString());
if (binaryOperator.left().type().kind() == TypeKind.QrType)
m_emitter.Append("qEval");
//m_emitter.Append("(");
if (binaryOperator.type().kind() == TypeKind.IntType)
m_emitter.AppendLine(String.format(" %s #= (%s %s %s), ",
trunk/compiler/ooasCompiler/src/org/momut/ooas/codegen/prolog/OoaPrologIdentifier.java
m_emitter.Append(String.format("list_%s%s", typeIdentifier.prefix, typeIdentifier.tokenText()));
break;
case QrType:
case MapType:
throw new NotImplementedException();
trunk/compiler/ooasCompiler/src/org/momut/ooas/codegen/prolog/OoaPrologVisitor.java
import org.momut.ooas.ast.identifiers.TypeIdentifier;
import org.momut.ooas.ast.statements.Block;
import org.momut.ooas.ast.types.FunctionType;
import org.momut.ooas.ast.types.TypeKind;
import org.momut.ooas.ast.types.FunctionType.FunctionTypeEnum;
import org.momut.ooas.codegen.OoasCodeEmitter;
import org.momut.ooas.parser.ParserState;
......
x.Accept(pid);
x.type().Accept(ptype);
final String key = x.type().kind() != TypeKind.QrType ? ptype.toString() : "qvar";
final String key = ptype.toString();
if (varlist.containsKey(key))
{
varlist.get(key).append(String.format(", %s", pid.toString()));
trunk/compiler/ooasCompiler/src/org/momut/ooas/codegen/prologsymbolic/OoaPrologSymbolicExpression.java
case minus: // T_MINUS:
return "-";
case less:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? "<" : "#<";
return !isNumericBinary(expression) ? "<" : "#<";
case lessequal:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? "=<" : "#=<";
return !isNumericBinary(expression) ? "=<" : "#=<";
case greater:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? ">" : "#>";
return !isNumericBinary(expression) ? ">" : "#>";
case greaterequal:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? ">=" : "#>=";
return !isNumericBinary(expression) ? ">=" : "#>=";
case equal:
return resultingType.kind() == TypeKind.QrType ? "==" : "#=";
return "#=";
case notequal:
return resultingType.kind() == TypeKind.QrType || !isNumericBinary(expression) ? "\\=" : "#\\=";
return !isNumericBinary(expression) ? "\\=" : "#\\=";
case and: // T_AND:
throw new NotImplementedException(); // implemented in binaryoperator
case or: // T_OR:
......
default:
m_emitter.Append(leftcode.toString());
m_emitter.Append(rightcode.toString());
if (binaryOperator.left().type().kind() == TypeKind.QrType)
m_emitter.Append("qEval");
//do not use #= for assignments in the symbolic backend
m_emitter.AppendLine(String.format(" %s = (%s %s %s), ",
trunk/compiler/ooasCompiler/src/org/momut/ooas/parser/ooaCustomParser.java
doError(varname, String.format("%s lacks initializer!", varname.getText()));
}
if (isObs == true || isCtr == true)
if (aType.kind() != TypeKind.QrType)
doError(varname, "'obs' or 'ctr' on attributes only allowed for qualitative types");
doError(varname, "'obs' or 'ctr' on attributes not allowed");
final AttributeIdentifier var = new AttributeIdentifier(varname, aType, GetScope(), anExpr, isStatic, isObs, isCtr);
trunk/compiler/ooasCompiler/src/org/momut/ooas/visitors/OoaResolveExpressionsVisitor.java
case greaterequal:
case less:
case lessequal:
if (!((rt.IsNumeric() && lt.IsNumeric()) || (rt.IsQualitative() && lt.IsQualitative())))
return Error(expression, "Operator expects LHS and RHS to be numeric or qualitative");
if (!((rt.IsNumeric() && lt.IsNumeric()) ))
return Error(expression, "Operator expects LHS and RHS to be numeric");
cover = Type.CoverType(lt, rt);
if (cover != null)
{

Also available in: Unified diff