diff options
author | paltherr <paltherr@epfl.ch> | 2003-10-10 16:17:45 +0000 |
---|---|---|
committer | paltherr <paltherr@epfl.ch> | 2003-10-10 16:17:45 +0000 |
commit | 2bb320eee9ca370c34c1195055b19283230ffe42 (patch) | |
tree | 82566a5f95b957f7620483fcbe8ef271abc49270 | |
parent | 1ec30351bf51d9f13f3bb62003c699b3445299d8 (diff) | |
download | scala-2bb320eee9ca370c34c1195055b19283230ffe42.tar.gz scala-2bb320eee9ca370c34c1195055b19283230ffe42.tar.bz2 scala-2bb320eee9ca370c34c1195055b19283230ffe42.zip |
- Added method flatten
- Added methods mkBlock
-rw-r--r-- | sources/scalac/ast/TreeGen.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/sources/scalac/ast/TreeGen.java b/sources/scalac/ast/TreeGen.java index 0b3a747004..2e0e339470 100644 --- a/sources/scalac/ast/TreeGen.java +++ b/sources/scalac/ast/TreeGen.java @@ -528,6 +528,39 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { //######################################################################## // Public Methods - Building expressions + /** Flattens the given tree array by inlining Block nodes. */ + public Tree[] flatten(Tree[] trees) { + boolean copy = false; + int length = trees.length; + for (int i = 0; i < trees.length; i++) { + switch (trees[i]) { + case Empty: + copy = true; + length -= 1; + continue; + case Block(Tree[] stats): + if (stats.length == 0) continue; // preserve unit literals + copy = true; + length += stats.length; + continue; + } + } + if (!copy) return trees; + Tree[] clone = new Tree[length]; + for (int i = 0, o = 0; i < trees.length; i++) { + switch (trees[i]) { + case Empty: + continue; + case Block(Tree[] stats): + if (stats.length == 0) break; // preserve unit literals + for (int j = 0; j < stats.length; j++) clone[o++] = stats[j]; + continue; + } + clone[o++] = trees[i]; + } + return clone; + } + /** Builds an instance test with given value and type. */ public Tree mkIsInstanceOf(int pos, Tree value, Type type) { return mkApplyT_(pos, Select(value, definitions.IS), new Type[]{type}); @@ -544,6 +577,18 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { return mkAsInstanceOf(value.pos, value, type); } + /** Builds an expression with given non-empty tree array. */ + public Tree mkBlock(int pos, Tree[] trees) { + assert trees.length != 0; + Tree[] flatten = flatten(trees); + assert flatten.length != 0: Debug.show(trees); + return Block(pos, flatten); + } + public Tree mkBlock(Tree[] trees) { + assert trees.length != 0; + return mkBlock(trees[0].pos, trees); + } + /** Builds a Template node with given symbol, parents and body. */ public Template Template(int pos, Symbol local, Tree[]parents, Tree[]body){ Template tree = make.Template(pos, local, parents, body); |