summaryrefslogtreecommitdiff
path: root/sources/scalac/transformer/matching/PatternMatcher.java
diff options
context:
space:
mode:
authorMatthias Zenger <mzenger@gmail.com>2003-06-12 09:02:17 +0000
committerMatthias Zenger <mzenger@gmail.com>2003-06-12 09:02:17 +0000
commitf69094bc715d919a3203d759bcdc49663c1aa523 (patch)
treec33b0e1fba1d66c6f502b77b66532698a697e2a5 /sources/scalac/transformer/matching/PatternMatcher.java
parent329c70cae6c2450244a57be3ce86bd04a9965c3b (diff)
downloadscala-f69094bc715d919a3203d759bcdc49663c1aa523.tar.gz
scala-f69094bc715d919a3203d759bcdc49663c1aa523.tar.bz2
scala-f69094bc715d919a3203d759bcdc49663c1aa523.zip
Fixed two bugs:
- Bug 35 in bugtracking - Sequences in class constructors
Diffstat (limited to 'sources/scalac/transformer/matching/PatternMatcher.java')
-rw-r--r--sources/scalac/transformer/matching/PatternMatcher.java126
1 files changed, 70 insertions, 56 deletions
diff --git a/sources/scalac/transformer/matching/PatternMatcher.java b/sources/scalac/transformer/matching/PatternMatcher.java
index 3426a52424..139c04a17a 100644
--- a/sources/scalac/transformer/matching/PatternMatcher.java
+++ b/sources/scalac/transformer/matching/PatternMatcher.java
@@ -158,6 +158,7 @@ public class PatternMatcher {
}
public ConstrPat makeConstrPat(int pos, Type type) {
+ System.out.println("making constructor pattern for " + type);
ConstrPat node = new ConstrPat(newVar(pos, type));
node.pos = pos;
node.type = type;
@@ -173,6 +174,7 @@ public class PatternMatcher {
}
public ConstantPat makeConstantPat(int pos, Type type, Object value) {
+ System.out.println("making constant pattern for " + type + " of value " + value);
ConstantPat node = new ConstantPat(value);
node.pos = pos;
node.type = type;
@@ -180,6 +182,7 @@ public class PatternMatcher {
}
public VariablePat makeVariablePat(int pos, Tree tree) {
+ System.out.println("making variable pattern for " + tree.type);
VariablePat node = new VariablePat(tree);
node.pos = pos;
node.type = tree.type;
@@ -351,6 +354,8 @@ public class PatternMatcher {
return ts;
}
return args;
+ case Sequence(Tree[] ts):
+ return ts;
default:
return Tree.EMPTY_ARRAY;
}
@@ -378,62 +383,62 @@ public class PatternMatcher {
protected PatternNode patternNode(Tree tree, Header header, CaseEnv env) {
switch (tree) {
- case Apply(Tree fn, Tree[] args): // pattern with args
- if (args.length == 1 && (tree.type.symbol().flags & Modifiers.CASE) == 0)
- switch (args[0]) {
- case Sequence(Tree[] ts):
- return makeSequencePat(tree.pos, tree.type, ts.length);
- }
- return makeConstrPat(tree.pos, getConstrType(tree.type));
- case Typed(Ident(Name name), Tree tpe): // variable pattern
- PatternNode node =
- (header.type.isSubType(getConstrType(tpe.type))) ?
- makeDefaultPat(tree.pos, getConstrType(tpe.type))
- : makeConstrPat(tree.pos, getConstrType(tpe.type));
- if ((env != null) && (name != WILDCARD_N))
- switch (node) {
- case ConstrPat(Symbol casted):
- env.newBoundVar(
- tree.pos,
- ((Tree.Typed)tree).expr.symbol(),
- getConstrType(tpe.type),
- make.Ident(tree.pos, casted.name).
- setType(typeOf(casted)).
- setSymbol(casted));
- break;
- default:
- env.newBoundVar(
- tree.pos,
- ((Tree.Typed)tree).expr.symbol(),
- getConstrType(tpe.type),
- header.selector);
- }
- return node;
- case Ident(Name name): // pattern without args or variable
- if (tree.symbol().isPrimaryConstructor())
- return makeConstrPat(tree.pos, getConstrType(tree.type));
- else if (name.isVariable()) {
- if ((env != null) && (name != WILDCARD_N))
- env.newBoundVar(
- tree.pos,
- tree.symbol(),
- getConstrType(tree.type),
- header.selector);
- return makeDefaultPat(tree.pos, getConstrType(header.type));
- } else
- return makeVariablePat(tree.pos, tree);
- case Select(_, Name name): // variable
- if (tree.symbol().isPrimaryConstructor())
- return makeConstrPat(tree.pos, getConstrType(tree.type));
- else
- return makeVariablePat(tree.pos, tree);
- case Literal(Object value):
- return makeConstantPat(tree.pos, getConstrType(tree.type), value);
- case Sequence(Tree[] ts):
- return makeSequencePat(tree.pos, tree.type, ts.length);
- default:
- new scalac.ast.printer.TextTreePrinter().print(tree).flush();
- throw new ApplicationError(tree);
+ case Apply(Tree fn, Tree[] args): // pattern with args
+ if (args.length == 1 && (tree.type.symbol().flags & Modifiers.CASE) == 0)
+ switch (args[0]) {
+ case Sequence(Tree[] ts):
+ return makeSequencePat(tree.pos, tree.type, ts.length);
+ }
+ return makeConstrPat(tree.pos, getConstrType(tree.type));
+ case Typed(Ident(Name name), Tree tpe): // variable pattern
+ PatternNode node =
+ (header.type.isSubType(getConstrType(tpe.type))) ?
+ makeDefaultPat(tree.pos, getConstrType(tpe.type))
+ : makeConstrPat(tree.pos, getConstrType(tpe.type));
+ if ((env != null) && (name != WILDCARD_N))
+ switch (node) {
+ case ConstrPat(Symbol casted):
+ env.newBoundVar(
+ tree.pos,
+ ((Tree.Typed)tree).expr.symbol(),
+ getConstrType(tpe.type),
+ make.Ident(tree.pos, casted.name).
+ setType(typeOf(casted)).
+ setSymbol(casted));
+ break;
+ default:
+ env.newBoundVar(
+ tree.pos,
+ ((Tree.Typed)tree).expr.symbol(),
+ getConstrType(tpe.type),
+ header.selector);
+ }
+ return node;
+ case Ident(Name name): // pattern without args or variable
+ if (tree.symbol().isPrimaryConstructor())
+ return makeConstrPat(tree.pos, getConstrType(tree.type));
+ else if (name.isVariable()) {
+ if ((env != null) && (name != WILDCARD_N))
+ env.newBoundVar(
+ tree.pos,
+ tree.symbol(),
+ getConstrType(tree.type),
+ header.selector);
+ return makeDefaultPat(tree.pos, getConstrType(header.type));
+ } else
+ return makeVariablePat(tree.pos, tree);
+ case Select(_, Name name): // variable
+ if (tree.symbol().isPrimaryConstructor())
+ return makeConstrPat(tree.pos, getConstrType(tree.type));
+ else
+ return makeVariablePat(tree.pos, tree);
+ case Literal(Object value):
+ return makeConstantPat(tree.pos, getConstrType(tree.type), value);
+ case Sequence(Tree[] ts):
+ return makeSequencePat(tree.pos, tree.type, ts.length);
+ default:
+ new scalac.ast.printer.TextTreePrinter().print(tree).flush();
+ throw new ApplicationError(tree);
}
}
@@ -465,6 +470,15 @@ public class PatternMatcher {
return pval.equals(qval);
}
return false;
+ case VariablePat(Tree tree):
+ switch (q) {
+ case VariablePat(Tree other):
+ return (tree.symbol() != null) &&
+ (tree.symbol().kind != Kinds.NONE) &&
+ (tree.symbol().kind != Kinds.ERROR) &&
+ (tree.symbol() == other.symbol());
+ }
+ return false;
}
return false;
}