[Nice-commit] Nice/src/bossa/syntax analyse.nice,1.120,1.121 constant.nice,1.5,1.6 enum.nice,1.9,1.1
Brought to you by:
bonniot
From: Arjan B. <ar...@us...> - 2004-12-31 18:40:56
|
Update of /cvsroot/nice/Nice/src/bossa/syntax In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31454/F:/nice/src/bossa/syntax Modified Files: analyse.nice constant.nice enum.nice pattern.nice Removed Files: ConstantExp.java Log Message: Converted ConstantExp. Index: constant.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/constant.nice,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** constant.nice 14 Dec 2004 20:18:05 -0000 1.5 --- constant.nice 31 Dec 2004 18:40:45 -0000 1.6 *************** *** 11,14 **** --- 11,272 ---- /**************************************************************************/ + package bossa.syntax; + + import bossa.util.*; + + /** + Constant expressions (values) of basic types. + + */ + public abstract class ConstantExp extends Expression + { + public final ?Object value; + private String representation; + public ?mlsub.typing.TypeConstructor tc = null; + + ?LocatedString className = null; + + /* + ConstantExp() + { + } + + ConstantExp(Polytype type, TypeConstructor tc, Object value, String representation, + Location location) + { + this.type = type; + this.tc = tc; + this.value = value; + this.representation = representation; + setLocation(location); + } + + ConstantExp(TypeConstructor tc, Object value, String representation, + Location location) + { + this(new mlsub.typing.Polytype(Monotype.sure(new mlsub.typing.MonotypeConstructor(tc,null))), + tc, value, representation, location); + } + + ConstantExp(TypeConstructor tc, String representation, Location location) + { + this(tc, null, representation, location); + } + + public ConstantExp(Object value) + { + this.value = value; + this.representation = value.toString(); + } + */ + + computeType() + { + // The type is known at creation. + } + + toString() = representation; + + equals(ConstantExp other) = this.value.equals(other.value); + + } + + public abstract class NumberConstantExp extends ConstantExp + { + override Number value; + + public ConstantExp makeNegative() + { + LocatedString newRepres = new LocatedString("-"+representation, this.location()); + + if (value instanceof Float || value instanceof Double) + return createFloatConstantExp(newRepres); + + return createIntegerConstantExp(newRepres); + } + + compile() + { + return new gnu.expr.QuoteExp(value, nice.tools.code.Types.javaType(type)); + } + } + + public class IntegerConstantExp extends NumberConstantExp + { + public long longValue() + { + let Number val = cast(value); + return val.longValue(); + } + + isZero() + { + let Number val = cast(value); + return val.intValue() == 0; + } + + } + + public class FloatConstantExp extends NumberConstantExp + { + + } + + public class SymbolConstantExp extends ConstantExp + { + override VarSymbol value; + + compile() + { + let VarSymbol val = cast(value); + return val.compile(); + } + } + + ConstantExp createSymbolConstantExp(?mlsub.typing.TypeConstructor tc, VarSymbol sym, String repres, Location loc) + { + let res = new SymbolConstantExp(tc: tc, value: sym, representation: repres); + res.setLocation(loc); + return res; + } + + public ConstantExp createIntegerConstantExp(LocatedString representation) + { + String rep = representation.toString(); + + int lastCharIndex = rep.length() - 1; + char last = rep.charAt(lastCharIndex); + boolean isLong = last == 'l' || last == 'L'; + if (isLong) + rep = rep.substring(0, lastCharIndex); + + try { + long value = parseInteger(removeUnderscores(rep)); + + mlsub.typing.Polytype type; + mlsub.typing.TypeConstructor tc; + Number object; + + if (Byte.MIN_VALUE <= value <= Byte.MAX_VALUE && !isLong) + { + type = notNull(nice.tools.typing.PrimitiveType.bytePolytype); + tc = notNull(nice.tools.typing.PrimitiveType.byteTC); + object = new Byte(byte(value)); + } + else if (Short.MIN_VALUE <= value <= Short.MAX_VALUE && !isLong) + { + type = notNull(nice.tools.typing.PrimitiveType.shortPolytype); + tc = notNull(nice.tools.typing.PrimitiveType.shortTC); + object = new Short(short(value)); + } + else if (Integer.MIN_VALUE <= value <= Integer.MAX_VALUE && !isLong) + { + type = notNull(nice.tools.typing.PrimitiveType.intPolytype); + tc = notNull(nice.tools.typing.PrimitiveType.intTC); + object = value; + } + else + { + type = notNull(nice.tools.typing.PrimitiveType.longPolytype); + tc = notNull(nice.tools.typing.PrimitiveType.longTC); + object = value; + } + + let res = new IntegerConstantExp(tc: tc, value: object, + representation: ""+value+(isLong ? "L" : "")); + res.type = type; + res.setLocation(representation.location()); + return res; + } + catch(NumberFormatException e) { + e.printStackTrace(); + throw User.error(representation, rep + " is not a valid number"); + } + } + + ConstantExp createIntConstantExp(int value, Location loc) + { + let res = new IntegerConstantExp(tc: nice.tools.typing.PrimitiveType.intTC, + value: value, representation: value.toString()); + res.type = nice.tools.typing.PrimitiveType.intPolytype; + res.setLocation(loc); + return res; + } + + ConstantExp createLongConstantExp(long value) + { + return new IntegerConstantExp(tc: nice.tools.typing.PrimitiveType.longTC, + value: value, representation: value.toString()); + } + + + private long parseInteger(String rep) + { + int radix = 10; + int index = 0; + boolean negative = false; + + // Leading minus + if (rep.startsWith("-")) { + negative = true; + index++; + } + + // Radix specifier + if (rep.startsWith("0x", index) || rep.startsWith("0X", index)) + { + index += 2; + radix = 16; + } + else if (rep.startsWith("#", index)) + { + index++; + radix = 16; + } + else if (rep.startsWith("0", index) && rep.length() > 1 + index) + { + index++; + radix = 8; + } + + if (rep.startsWith("-", index)) + throw new NumberFormatException("Negative sign in wrong position"); + + long result = new java.math.BigInteger(rep.substring(index), radix).longValue(); + + if (negative) + result = -result; + + return result; + } + + private String removeUnderscores(String s) + { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i<s.length(); i++) + if (s.charAt(i) != '_') + sb.append(s.charAt(i)); + + return sb.toString(); + } + + public ConstantExp createFloatConstantExp(LocatedString representation) + { + String repres = removeUnderscores(representation.toString()); + if (repres.endsWith("F") || repres.endsWith("f")) + { + float value = Float.parseFloat(repres); + let res = new FloatConstantExp(tc: nice.tools.typing.PrimitiveType.floatTC, value: value, representation:value+"f"); + res.type = nice.tools.typing.PrimitiveType.floatPolytype; + res.setLocation(representation.location()); + return res; + } + + double value = Double.parseDouble(repres); + let res = new FloatConstantExp(tc: nice.tools.typing.PrimitiveType.doubleTC, value: value, representation: value+""); + res.type = nice.tools.typing.PrimitiveType.doublePolytype; + res.setLocation(representation.location()); + return res; + } /** *************** *** 40,44 **** ConstantExp createNullExp(bossa.util.Location loc) { ! let res = new NullExp(); res.setLocation(loc); return res; --- 298,302 ---- ConstantExp createNullExp(bossa.util.Location loc) { ! let res = new NullExp(value: null, representation: "null"); res.setLocation(loc); return res; *************** *** 57,72 **** public ConstantExp createBooleanConstant(boolean value, Location location) { ! return new BooleanConstantExp(nice.tools.typing.PrimitiveType.boolTC, ! value, value ? "true" : "false", location, compiledValue: notNull(value ? gnu.expr.QuoteExp.trueExp : gnu.expr.QuoteExp.falseExp)); } class CharConstantExp extends ConstantExp { ! longValue() { Character val = cast(this.value); return int(val.charValue()); } } --- 315,338 ---- public ConstantExp createBooleanConstant(boolean value, Location location) { ! let res = new BooleanConstantExp(tc: nice.tools.typing.PrimitiveType.boolTC, ! value: value, representation: value ? "true" : "false", compiledValue: notNull(value ? gnu.expr.QuoteExp.trueExp : gnu.expr.QuoteExp.falseExp)); + res.type = nice.tools.typing.PrimitiveType.boolPolytype; + res.setLocation(location); + return res; } class CharConstantExp extends ConstantExp { ! public long longValue() { Character val = cast(this.value); return int(val.charValue()); } + + compile() + { + return new gnu.expr.QuoteExp(value, nice.tools.code.Types.javaType(type)); + } } *************** *** 77,82 **** User.error(value, "Invalid character constant: " + value); ! return new CharConstantExp(nice.tools.typing.PrimitiveType.charTC, ! s[0], "'" + s + "'", value.location()); } --- 343,356 ---- User.error(value, "Invalid character constant: " + value); ! return createCharConstant(s[0], notNull(value.location())); ! } ! ! public ConstantExp createCharConstant(char value, Location loc) ! { ! let res = new CharConstantExp(tc: nice.tools.typing.PrimitiveType.charTC, ! value: value, representation: "'" + value + "'"); ! res.type = nice.tools.typing.PrimitiveType.charPolytype; ! res.setLocation(loc); ! return res; } *************** *** 87,96 **** public class VoidConstantExp extends ConstantExp { { this.className = voidName; - this.value = gnu.mapping.Values.empty; } ! toString() = "{}"; } --- 361,374 ---- public class VoidConstantExp extends ConstantExp { + override value = gnu.mapping.Values.empty; + override representation = "{}"; { this.className = voidName; } ! compile() ! { ! return new gnu.expr.QuoteExp(value, nice.tools.code.Types.javaType(type)); ! } } *************** *** 105,109 **** final String escapedValue; ! toString() = "\""+escapedValue+"\""; } --- 383,390 ---- final String escapedValue; ! compile() ! { ! return new gnu.expr.QuoteExp(value, nice.tools.code.Types.javaType(type)); ! } } *************** *** 118,124 **** value = escapeEOL(value); ! let res = new StringConstantExp(escapedValue: value); ! res.value = unescapeLiteral(value); ! res.className = stringClassName; return res; } --- 399,403 ---- value = escapeEOL(value); ! let res = new StringConstantExp(value: unescapeLiteral(value), escapedValue: value, representation: "\""+value+"\"" , className: stringClassName); return res; } *************** *** 209,222 **** } ! void setRepresentedType(mlsub.typing.Polytype type, ?gnu.bytecode.Type bytecodeType) { ! this.value = bytecodeType; ! this.representedType = type.getMonotype(); ! this.type = new mlsub.typing.Polytype (type.getConstraint(), Monotype.sure(new mlsub.typing.MonotypeConstructor (nice.tools.typing.PrimitiveType.classTC, [type.getMonotype()]))); } --- 488,504 ---- } ! TypeConstantExp setRepresentedType(mlsub.typing.Polytype type, ?gnu.bytecode.Type bytecodeType) { ! let res = new TypeConstantExp(value: bytecodeType, representation: this.representation); ! res.isExpression = this.isExpression; ! res.isLiteral = this.isLiteral; ! res.representedType = type.getMonotype(); ! res.type = new mlsub.typing.Polytype (type.getConstraint(), Monotype.sure(new mlsub.typing.MonotypeConstructor (nice.tools.typing.PrimitiveType.classTC, [type.getMonotype()]))); + return res; } *************** *** 224,228 **** { if (isLiteral) ! return super; gnu.bytecode.Type type = cast(value); --- 506,510 ---- { if (isLiteral) ! return new gnu.expr.QuoteExp(value, nice.tools.code.Types.javaType(type)); gnu.bytecode.Type type = cast(value); *************** *** 248,252 **** public TypeConstantExp createTypeConstantExp(LocatedString name) { ! return new TypeConstantExp(null, null, name, name.toString(), name.location()); } --- 530,536 ---- public TypeConstantExp createTypeConstantExp(LocatedString name) { ! let res = new TypeConstantExp(value: name, representation: name.toString()); ! res.setLocation(name.location()); ! return res; } *************** *** 271,276 **** if (type instanceof gnu.bytecode.ClassType) { ! TypeConstantExp res = new TypeConstantExp(name); ! res.setRepresentedType(universalPolytype(tc, true), type); res.setLocation(root == null ? name.location() : root.location()); return res; --- 555,559 ---- if (type instanceof gnu.bytecode.ClassType) { ! TypeConstantExp res = new TypeConstantExp(value: name, representation: name.toString()).setRepresentedType(universalPolytype(tc, true), type); res.setLocation(root == null ? name.location() : root.location()); return res; Index: enum.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/enum.nice,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** enum.nice 18 Dec 2004 19:41:01 -0000 1.9 --- enum.nice 31 Dec 2004 18:40:45 -0000 1.10 *************** *** 178,183 **** args.add(new Arguments.Argument(createStringConstantExp(name.toString()), new LocatedString("name",name.location))); ! args.add(new Arguments.Argument(new ConstantExp(nice.tools.typing.PrimitiveType.intTC, ordinal, ! ordinal.toString(), name.location()), new LocatedString("ordinal", name.location))); for (int i = 0; i < fields.size(); i++) --- 178,182 ---- args.add(new Arguments.Argument(createStringConstantExp(name.toString()), new LocatedString("name",name.location))); ! args.add(new Arguments.Argument(createIntConstantExp(ordinal, notNull(name.location())), new LocatedString("ordinal", name.location))); for (int i = 0; i < fields.size(); i++) Index: pattern.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/pattern.nice,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** pattern.nice 30 Dec 2004 18:49:01 -0000 1.14 --- pattern.nice 31 Dec 2004 18:40:45 -0000 1.15 *************** *** 295,304 **** additional: additional, loc: value.location(), atValue: value); ! if (value.tc == nice.tools.typing.PrimitiveType.boolTC) return new BoolPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); ! if (value.tc == nice.tools.typing.PrimitiveType.charTC) return new CharPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); return new IntPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); } --- 295,305 ---- additional: additional, loc: value.location(), atValue: value); ! if (value instanceof BooleanConstantExp) return new BoolPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); ! if (value instanceof CharConstantExp) return new CharPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); + assert value instanceof IntegerConstantExp; return new IntPattern(tc: value.tc, additional: additional, loc: value.location(), atValue: value); } *************** *** 392,395 **** --- 393,398 ---- public class CharPattern extends ValuePattern { + override CharConstantExp atValue; + setDomainTC(domaintc) { *************** *** 404,407 **** --- 407,412 ---- public class IntPattern extends ValuePattern { + override IntegerConstantExp atValue; + setDomainTC(domaintc) { *************** *** 447,459 **** hi = val+1; ! while(values.any(ConstantExp ce => (ce.value instanceof Number) && (ce.longValue == lo))) lo--; ! values.add(new ConstantExp(new Long(lo))); ! while(values.any(ConstantExp ce => (ce.value instanceof Number) && (ce.longValue == hi))) hi++; ! values.add(new ConstantExp(new Long(hi))); } --- 452,464 ---- hi = val+1; ! while(values.any(ConstantExp ce => (ce instanceof IntegerConstantExp) && (ce.longValue() == lo))) lo--; ! values.add(createLongConstantExp(lo)); ! while(values.any(ConstantExp ce => (ce instanceof IntegerConstantExp) && (ce.longValue() == hi))) hi++; ! values.add(createLongConstantExp(hi)); } *************** *** 474,478 **** let EnumSymbol esym = cast(atValue.value); for (sym : esym.getDefinition().symbols) ! values.add(new ConstantExp(null, tc, sym, sym.name.toString(), this.location)); } --- 479,483 ---- let EnumSymbol esym = cast(atValue.value); for (sym : esym.getDefinition().symbols) ! values.add(createSymbolConstantExp(notNull(tc), sym, sym.name.toString(), this.location)); } *************** *** 498,513 **** 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()); } --- 503,516 ---- matches(EnumPattern p, tag) = (tag != null) && mlsub.typing.Typing.testRigidLeq(tag, p.tc); + matchesValue(Pattern p, ConstantExp val) = false; matchesValue(VariablePattern p, ConstantExp val) = true; matchesValue(ValuePattern p, ConstantExp val) = p.atValue.equals(val); ! matchesValue(IntPattern p, IntegerConstantExp val) { ! return p.atValue.longValue() == val.longValue(); } ! matchesValue(IntComparePattern p, IntegerConstantExp val) { ! return matches(p.kind, val.longValue(), p.atValue.longValue()); } *************** *** 738,743 **** NewExp val = cast(symbol.getValue()); return new EnumPattern(name: pattern.name, tc: val.tc, loc: pattern.location(), atValue: ! new ConstantExp(null, pattern.tc, symbol, ! notNull(pattern.name).toString(), pattern.location())); } --- 741,745 ---- NewExp val = cast(symbol.getValue()); return new EnumPattern(name: pattern.name, tc: val.tc, loc: pattern.location(), atValue: ! createSymbolConstantExp(pattern.tc, symbol, notNull(pattern.name).toString(), pattern.location())); } *************** *** 759,765 **** else if (symbol.getValue() instanceof NewExp) { ! NewExp val = cast(symbol.getValue()); return new ReferencePattern(name: pattern.name, tc: val.tc, loc: pattern.location(), atValue: ! new ConstantExp(null, pattern.tc, symbol, notNull(pattern.name).toString(), pattern.location())); } --- 761,767 ---- else if (symbol.getValue() instanceof NewExp) { ! NewExp val = cast(symbol.getValue()); return new ReferencePattern(name: pattern.name, tc: val.tc, loc: pattern.location(), atValue: ! createSymbolConstantExp(pattern.tc, symbol, notNull(pattern.name).toString(), pattern.location())); } *************** *** 826,837 **** { if (name[0] == '\'') ! return createPattern(new CharConstantExp(nice.tools.typing.PrimitiveType.charTC, ! name[1], name, loc)); if (name[0] == '-') ! return createPattern(ConstantExp.makeNumber(new LocatedString(name))); if (name[0] == '+') ! return createPattern(ConstantExp.makeNumber(new LocatedString(name.substring(1)))); if (name[0] == '\"') --- 828,838 ---- { if (name[0] == '\'') ! return createPattern(createCharConstant(name[1], loc)); if (name[0] == '-') ! return createPattern(createIntegerConstantExp(new LocatedString(name))); if (name[0] == '+') ! return createPattern(createIntegerConstantExp(new LocatedString(name.substring(1)))); if (name[0] == '\"') *************** *** 842,846 **** { let prefix = name.substring(0, (name[1] == '=') ? 2 : 1); ! return createPattern(prefix, null, ConstantExp.makeNumber( new LocatedString(name.substring(prefix.length()))), null, loc); } --- 843,847 ---- { let prefix = name.substring(0, (name[1] == '=') ? 2 : 1); ! return createPattern(prefix, null, createIntegerConstantExp( new LocatedString(name.substring(prefix.length()))), null, loc); } --- ConstantExp.java DELETED --- Index: analyse.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/bossa/syntax/analyse.nice,v retrieving revision 1.120 retrieving revision 1.121 diff -C2 -d -r1.120 -r1.121 *** analyse.nice 25 Dec 2004 23:26:20 -0000 1.120 --- analyse.nice 31 Dec 2004 18:40:45 -0000 1.121 *************** *** 676,682 **** (name.toString(), name.location()); ! e.setRepresentedType(type, bytecodeType); ! ! return e; } --- 676,680 ---- (name.toString(), name.location()); ! return e.setRepresentedType(type, bytecodeType); } |