[Nice-commit] Nice/src/bossa/syntax pattern.nice,1.1,1.2
Brought to you by:
bonniot
From: Arjan B. <ar...@us...> - 2004-08-14 11:14:24
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25287/F:/nice/src/bossa/syntax Modified Files: pattern.nice Log Message: Regrouped methods in pattern.nice. Index: pattern.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/pattern.nice,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** pattern.nice 7 Aug 2004 14:20:29 -0000 1.1 --- pattern.nice 14 Aug 2004 11:14:15 -0000 1.2 *************** *** 98,111 **** public class VariablePattern extends Pattern { - matches(tag) = true; - - matchesValue(ConstantExp val) = true; - - toString() = (name != null) ? notNull(name).toString() : "_"; - - bytecodeRepresentation() = "@_"; - - matchTest(parameter, dispatchMade) = notNull(gnu.expr.QuoteExp.trueExp); - atAny() = true; } --- 98,101 ---- *************** *** 113,144 **** public class NullPattern extends Pattern { - matches(tag) = tag == PrimitiveType.nullTC; - - matchesValue(ConstantExp val) = false; - - toString() = "null"; - - bytecodeRepresentation() = "@NULL"; - - matchTest(parameter, dispatchMade) = nice.tools.code.Gen.isNullExp(parameter); - atNull() = true; } ! public class NotNullPattern extends Pattern ! { ! matches(tag) = tag != PrimitiveType.nullTC; ! ! matchesValue(ConstantExp val) = false; ! ! toString() = "!null"; ! ! bytecodeRepresentation() = "@NONNULL"; ! ! matchTest(parameter, dispatchMade) ! { ! return nice.tools.code.Gen.boolNotExp(nice.tools.code.Gen.isNullExp(parameter)); ! } ! } public class TypePattern extends Pattern --- 103,110 ---- public class NullPattern extends Pattern { atNull() = true; } ! public class NotNullPattern extends Pattern {} public class TypePattern extends Pattern *************** *** 162,212 **** } - matches(tag) - { - if (tag == null) - return false; - - if (exactlyAt) - return mlsub.typing.Typing.testRigidLeq(tag, tc) && mlsub.typing.Typing.testRigidLeq(tc, tag); - - return mlsub.typing.Typing.testRigidLeq(tag, tc); - } - - matchesValue(ConstantExp val) = false; - - toString() - { - StringBuffer res = new StringBuffer(); - res.append(exactlyAt ? "#" : (name != null ? "" : "@")); - - if (typeConstructor != null) - res.append(typeConstructor); - else if (tc != null) - res.append(tc); - - if (name != null) - res.append(" " + name); - - return res.toString(); - } - - bytecodeRepresentation() - { - return (exactlyAt ? "#" : "@") + notNull(tc).toString().replace('$','.'); - } - - matchTest(parameter, dispatchMade) - { - if (dispatchMade && ! exactlyAt) - return notNull(gnu.expr.QuoteExp.trueExp); - - let ct = nice.tools.code.Types.javaType(tc); - - if (exactlyAt) - return nice.tools.code.Gen.isOfClass(parameter, ct); - - return nice.tools.code.Gen.instanceOfExp(parameter, ct); - } - exactlyAtType() = exactlyAt; } --- 128,131 ---- *************** *** 216,225 **** final ConstantExp atValue; - matches(tag) = false; - - matchesValue(ConstantExp val) = atValue.equals(val); - - toString() = atValue.toString(); - addValues(values) { --- 135,138 ---- *************** *** 238,253 **** } - matches(tag) = tag == PrimitiveType.boolTC; - - bytecodeRepresentation() = atValue.isTrue() ? "@true" : "@false"; - - matchTest(parameter, dispatchMade) - { - if (atValue.isFalse()) - return nice.tools.code.Gen.boolNotExp(parameter); - - return cast(parameter); - } - atTypeMatchingValue() = true; } --- 151,154 ---- *************** *** 256,275 **** { override StringConstantExp atValue; - - bytecodeRepresentation() { - return "@\"" + atValue.escapedValue + "\""; - } - - matchTest(parameter, dispatchMade) - { - let String val = cast(atValue.value); - return nice.tools.code.Gen.stringEquals(val, parameter); - } } public class CharPattern extends ValuePattern { - bytecodeRepresentation() = "@\'" + atValue.value + "\'"; - setDomainTC(domaintc) { --- 157,164 ---- *************** *** 280,297 **** User.error(this.location, "Character value patterns are not allowed for methods where the declared parameter isn't a primitive type."); } - - matchTest(parameter, dispatchMade) - { - return nice.tools.code.Gen.integerComparison("Eq", parameter, atValue.longValue()); - } } public class IntPattern extends ValuePattern { - matchesValue(ConstantExp val) - { - return (val.value instanceof Number) && atValue.longValue() == val.longValue(); - } - setDomainTC(domaintc) { --- 169,176 ---- *************** *** 310,323 **** } User.error(this.location, "Integer value patterns are not allowed for methods where the declared parameter isn't a primitive type."); - } - - bytecodeRepresentation() = "@" + (atValue.longValue() >= 0 ? "+" : "") + atValue; - - matchTest(parameter, dispatchMade) - { - return nice.tools.code.Gen.integerComparison("Eq", parameter, atValue.longValue()); - } - } --- 189,193 ---- *************** *** 336,355 **** ?LocatedString refName; - matches(tag) = tag != null && mlsub.typing.Typing.testRigidLeq(tag, PrimitiveType.longTC); - - matchesValue(ConstantExp val) - { - return val.value instanceof Number && matches(kind, val.longValue(), atValue.longValue()); - } - - toString() = (name!=null? notNull(name).toString() : "") + kind.prefix + atValue; - - bytecodeRepresentation() = "@" + kind.prefix + atValue; - - matchTest(parameter, dispatchMade) - { - return nice.tools.code.Gen.integerComparison(kind.abbrev, parameter, atValue.longValue()); - } - addValues(values) { --- 206,209 ---- *************** *** 376,392 **** atTypeMatchingValue() = true; - } public class ReferencePattern extends ValuePattern { - - bytecodeRepresentation() = "@=" + name; - - matchTest(parameter, dispatchMade) - { - return nice.tools.code.Gen.referenceEqualsExp(atValue.compile(), parameter); - } - getName() = cast(null); } --- 230,237 ---- *************** *** 394,399 **** public class EnumPattern extends ReferencePattern { - matches(tag) = (tag != null) && mlsub.typing.Typing.testRigidLeq(tag, tc); - addValues(values) { --- 239,242 ---- *************** *** 407,410 **** --- 250,285 ---- } + matches(VariablePattern p, tag) = true; + matches(NullPattern p, tag) = tag == PrimitiveType.nullTC; + matches(NotNullPattern p, tag) = tag != PrimitiveType.nullTC; + matches(TypePattern p, tag) + { + if (tag == null) + return false; + + if (p.exactlyAt) + return mlsub.typing.Typing.testRigidLeq(tag, p.tc) && mlsub.typing.Typing.testRigidLeq(p.tc, tag); + + return mlsub.typing.Typing.testRigidLeq(tag, p.tc); + } + matches(ValuePattern p, tag) = false; + matches(BoolPattern p, tag) = tag == PrimitiveType.boolTC; + matches(IntComparePattern p, tag) = tag != null && mlsub.typing.Typing.testRigidLeq(tag, PrimitiveType.longTC); + matches(EnumPattern p, tag) = (tag != null) && mlsub.typing.Typing.testRigidLeq(tag, p.tc); + + matchesValue(VariablePattern p, ConstantExp val) = true; + matchesValue(NullPattern p, ConstantExp val) = false; + matchesValue(NotNullPattern p, ConstantExp val) = false; + matchesValue(TypePattern p, ConstantExp val) = false; + matchesValue(ValuePattern p, ConstantExp val) = p.atValue.equals(val); + matchesValue(IntPattern p, ConstantExp val) + { + return (val.value instanceof Number) && p.atValue.longValue() == val.longValue(); + } + matchesValue(IntComparePattern p, ConstantExp val) + { + return val.value instanceof Number && matches(p.kind, val.longValue(), p.atValue.longValue()); + } + /** Order on pattern that is compatible with the specificity of patterns. *************** *** 517,520 **** --- 392,463 ---- } + matchTest(VariablePattern p, parameter, dispatchMade) = gnu.expr.QuoteExp.trueExp; + matchTest(NullPattern p, parameter, dispatchMade) = nice.tools.code.Gen.isNullExp(parameter); + matchTest(NotNullPattern p, parameter, dispatchMade) + { + return nice.tools.code.Gen.boolNotExp(nice.tools.code.Gen.isNullExp(parameter)); + } + matchTest(TypePattern p, parameter, dispatchMade) + { + if (dispatchMade && ! p.exactlyAt) + return gnu.expr.QuoteExp.trueExp; + + let ct = nice.tools.code.Types.javaType(p.tc); + + if (p.exactlyAt) + return nice.tools.code.Gen.isOfClass(parameter, ct); + + return nice.tools.code.Gen.instanceOfExp(parameter, ct); + } + matchTest(BoolPattern p, parameter, dispatchMade) + { + if (p.atValue.isFalse()) + return nice.tools.code.Gen.boolNotExp(parameter); + + return cast(parameter); + } + matchTest(StringPattern p, parameter, dispatchMade) + { + let String val = cast(p.atValue.value); + return nice.tools.code.Gen.stringEquals(val, parameter); + } + matchTest(CharPattern p, parameter, dispatchMade) + { + return nice.tools.code.Gen.integerComparison("Eq", parameter, p.atValue.longValue()); + } + matchTest(IntPattern p, parameter, dispatchMade) + { + return nice.tools.code.Gen.integerComparison("Eq", parameter, p.atValue.longValue()); + } + matchTest(IntComparePattern p, parameter, dispatchMade) + { + return nice.tools.code.Gen.integerComparison(p.kind.abbrev, parameter, p.atValue.longValue()); + } + matchTest(ReferencePattern p, parameter, dispatchMade) + { + return nice.tools.code.Gen.referenceEqualsExp(p.atValue.compile(), parameter); + } + + toString(VariablePattern p) = (p.name != null) ? notNull(p.name).toString() : "_"; + toString(NullPattern p) = "null"; + toString(NotNullPattern p) = "!null"; + toString(TypePattern p) + { + let res = new StringBuffer(); + res.append(p.exactlyAt ? "#" : (p.name != null ? "" : "@")); + + if (p.typeConstructor != null) + res.append(p.typeConstructor); + else if (p.tc != null) + res.append(p.tc); + + if (p.name != null) + res.append(" " +p.name); + + return res.toString(); + } + toString(ValuePattern p) = p.atValue.toString(); + toString(IntComparePattern p) = (p.name!=null? notNull(p.name).toString() : "") + p.kind.prefix + p.atValue; + private ?VarSymbol findRefSymbol(LocatedString refName) *************** *** 597,665 **** } public ?Pattern readPattern(String rep, int[]/*ref*/ pos) { ! if (pos[0] >= rep.length()) return null; ! if (rep.charAt(pos[0]) != '@' && rep.charAt(pos[0]) != '#') ! Internal.error("Invalid pattern representation at character " + pos[0] + ": " + rep); ! boolean exact = rep.charAt(pos[0]) == '#'; ! int start = pos[0]+1; ! pos[0] += 1; int len = rep.length(); ! if (rep.charAt(pos[0]) == '\'') { //char literal patterns are 3 chars ! pos[0] += 3; } ! else if (rep.charAt(pos[0]) == '\"') { //we need to skip possible '@' or '#' content of the string literal ! pos[0] += 2; ! while(pos[0] < len && ! ! ( ( rep.charAt(pos[0]) == '@' || rep.charAt(pos[0]) != '#') ! && rep.charAt(pos[0]-1) == '\"' ! && rep.charAt(pos[0]-2) != '\\') ) ! pos[0] += 1; } else { ! while(pos[0] < len && rep.charAt(pos[0]) != '@' && rep.charAt(pos[0]) != '#') ! pos[0] += 1; } ! String name = rep.substring(start, pos[0]); let loc = Location.nowhere(); if (name.length() > 1) { ! if (name.charAt(0) == '\'') return createPattern(new ConstantExp(PrimitiveType.charTC, ! new Character(name.charAt(1)), name, loc)); ! if (name.charAt(0) == '-') return createPattern(ConstantExp.makeNumber(new LocatedString(name, loc))); ! if (name.charAt(0) == '+') return createPattern(ConstantExp.makeNumber(new LocatedString( name.substring(1), loc))); ! if (name.charAt(0) == '\"') return createPattern(ConstantExp.makeString(new LocatedString( name.substring(1,name.length()-1), loc))); ! if (name.charAt(0) == '<' || name.charAt(0) == '>') { ! let prefix = (name.charAt(1) == '=') ? name.substring(0,2) : name.substring(0,1); return createPattern(prefix, null, ConstantExp.makeNumber( new LocatedString(name.substring(prefix.length()), loc)), null, loc); } ! if (name.charAt(0) == '=') return resolveGlobalConstants(new VariablePattern(new LocatedString(name.substring(1), loc), loc)); - } --- 540,623 ---- } + bytecodeRepresentation(VariablePattern p) = "@_"; + bytecodeRepresentation(NullPattern p) = "@NULL"; + bytecodeRepresentation(NotNullPattern p) = "@NONNULL"; + bytecodeRepresentation(TypePattern p) + { + return (p.exactlyAt ? "#" : "@") + notNull(p.tc).toString().replace('$','.'); + } + bytecodeRepresentation(BoolPattern p) = p.atValue.isTrue() ? "@true" : "@false"; + bytecodeRepresentation(StringPattern p) = "@\"" + p.atValue.escapedValue + "\""; + bytecodeRepresentation(CharPattern p) = "@\'" + p.atValue.value + "\'"; + bytecodeRepresentation(IntPattern p) = "@" + (p.atValue.longValue() >= 0 ? "+" : "") + p.atValue; + bytecodeRepresentation(IntComparePattern p) = "@" + p.kind.prefix + p.atValue; + bytecodeRepresentation(ReferencePattern p) = "@=" + p.name; + public ?Pattern readPattern(String rep, int[]/*ref*/ pos) { ! int cpos = pos[0]; ! ! if (cpos >= rep.length()) return null; ! if (rep[cpos] != '@' && rep[cpos] != '#') ! Internal.error("Invalid pattern representation at character " + cpos + ": " + rep); ! boolean exact = rep[cpos] == '#'; ! int start = ++cpos; int len = rep.length(); ! if (rep[cpos] == '\'') { //char literal patterns are 3 chars ! cpos += 3; } ! else if (rep[cpos] == '\"') { //we need to skip possible '@' or '#' content of the string literal ! cpos += 2; ! while(cpos < len && ! ! ( ( rep[cpos] == '@' || rep[cpos] != '#') ! && rep[cpos-1] == '\"' ! && rep[cpos-2] != '\\') ) ! cpos++; } else { ! while(cpos < len && rep[cpos] != '@' && rep[cpos] != '#') ! cpos++; } ! String name = rep.substring(start, cpos); ! pos[0] = cpos; let loc = Location.nowhere(); if (name.length() > 1) { ! if (name[0] == '\'') return createPattern(new ConstantExp(PrimitiveType.charTC, ! new Character(name[1]), name, loc)); ! if (name[0] == '-') return createPattern(ConstantExp.makeNumber(new LocatedString(name, loc))); ! if (name[0] == '+') return createPattern(ConstantExp.makeNumber(new LocatedString( name.substring(1), loc))); ! if (name[0] == '\"') return createPattern(ConstantExp.makeString(new LocatedString( name.substring(1,name.length()-1), loc))); ! if (name[0] == '<' || name[0] == '>') { ! let prefix = name.substring(0, (name[1] == '=') ? 2 : 1); return createPattern(prefix, null, ConstantExp.makeNumber( new LocatedString(name.substring(prefix.length()), loc)), null, loc); } ! if (name[0] == '=') return resolveGlobalConstants(new VariablePattern(new LocatedString(name.substring(1), loc), loc)); } *************** *** 678,686 **** // This can happen if the class exists only in a later version // of the JDK. ! throw new Unknown(); let mlsub.typing.TypeConstructor tc = cast(sym); return new TypePattern(null, tc, Location.nowhere(), exactlyAt:exact); } - - public class Unknown extends RuntimeException {} --- 636,642 ---- // This can happen if the class exists only in a later version // of the JDK. ! throw new Pattern.Unknown(); let mlsub.typing.TypeConstructor tc = cast(sym); return new TypePattern(null, tc, Location.nowhere(), exactlyAt:exact); } |