From 209116f66a23750f2278bd3cc65c8fe4131c4b7c Mon Sep 17 00:00:00 2001 From: Minero Aoki Date: Sun, 10 Feb 2008 18:59:21 +0000 Subject: [PATCH] * net/loveruby/cflat/ast/Scope.java: use LinkedHashMap to unify entities and entitiesMap (entities should be ordered). * net/loveruby/cflat/ast/Frame.java: ditto. * net/loveruby/cflat/ast/ToplevelScope.java: ditto. * net/loveruby/cflat/ast/Scope.java: remove unused method #entities. Use #variables instead. * net/loveruby/cflat/ast/Frame.java: ditto. * net/loveruby/cflat/ast/ConstantTable.java: use * net/loveruby/cflat/compiler/TypeResolver.java: remove useless code. git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@3887 1b9489fe-b721-0410-924e-b54b9192deb8 --- ChangeLog | 19 +++++ net/loveruby/cflat/ast/ConstantTable.java | 7 +- net/loveruby/cflat/ast/Frame.java | 4 +- net/loveruby/cflat/ast/Scope.java | 72 ++++++++++--------- net/loveruby/cflat/ast/ToplevelScope.java | 18 ++++- net/loveruby/cflat/compiler/TypeResolver.java | 8 --- 6 files changed, 79 insertions(+), 49 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4fad59f..7ea863f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +Mon Feb 11 03:59:19 2008 Minero Aoki + + * net/loveruby/cflat/ast/Scope.java: use LinkedHashMap to unify + entities and entitiesMap (entities should be ordered). + + * net/loveruby/cflat/ast/Frame.java: ditto. + + * net/loveruby/cflat/ast/ToplevelScope.java: ditto. + + * net/loveruby/cflat/ast/Scope.java: remove unused method + #entities. Use #variables instead. + + * net/loveruby/cflat/ast/Frame.java: ditto. + + * net/loveruby/cflat/ast/ConstantTable.java: use + + * net/loveruby/cflat/compiler/TypeResolver.java: remove useless + code. + Mon Feb 11 03:08:16 2008 Minero Aoki * net/loveruby/cflat/ast/Scope.java: ban all allocate* methods, diff --git a/net/loveruby/cflat/ast/ConstantTable.java b/net/loveruby/cflat/ast/ConstantTable.java index 37da526..a91e606 100644 --- a/net/loveruby/cflat/ast/ConstantTable.java +++ b/net/loveruby/cflat/ast/ConstantTable.java @@ -1,13 +1,12 @@ package net.loveruby.cflat.ast; import java.util.*; -public class ConstantTable -{ - protected HashMap table; +public class ConstantTable { + protected Map table; protected long id; public ConstantTable() { - table = new HashMap(); + table = new LinkedHashMap(); id = 0; } diff --git a/net/loveruby/cflat/ast/Frame.java b/net/loveruby/cflat/ast/Frame.java index 22e3e9a..88e0aac 100644 --- a/net/loveruby/cflat/ast/Frame.java +++ b/net/loveruby/cflat/ast/Frame.java @@ -11,7 +11,7 @@ public class Frame extends Scope { Iterator scopes = allChildren(); while (scopes.hasNext()) { Scope s = (Scope)scopes.next(); - Iterator vars = s.variables(); + Iterator vars = s.entities.values().iterator(); while (vars.hasNext()) { DefinedVariable var = (DefinedVariable)vars.next(); if (var.isPrivate()) { @@ -27,7 +27,7 @@ public class Frame extends Scope { } public Iterator localVariables() { - return bodyScope().entities(); + return bodyScope().variables(); } private Scope bodyScope() { diff --git a/net/loveruby/cflat/ast/Scope.java b/net/loveruby/cflat/ast/Scope.java index 5544b6c..ffa69a1 100644 --- a/net/loveruby/cflat/ast/Scope.java +++ b/net/loveruby/cflat/ast/Scope.java @@ -6,8 +6,7 @@ import java.util.*; public class Scope { protected Scope parent; protected List children; - protected List entities; - protected Map entitiesMap; + protected Map entities; protected long numAllEntities; public Scope(Scope up) { @@ -15,8 +14,7 @@ public class Scope { if (up != null) up.addChild(this); children = new ArrayList(); numAllEntities = -1; - entities = new ArrayList(); - entitiesMap = new HashMap(); + entities = new LinkedHashMap(); } public boolean isToplevel() { @@ -61,21 +59,20 @@ public class Scope { /** Declare variable or function in this scope. */ // #@@range/declareEntity{ public void declareEntity(Entity ent) { - if (entitiesMap.containsKey(ent.name())) { + if (entities.containsKey(ent.name())) { throw new Error("duplicated entity: " + ent.name()); } - entities.add(ent); - entitiesMap.put(ent.name(), ent); + entities.put(ent.name(), ent); } // #@@} public boolean isDefinedLocally(String name) { - return entitiesMap.containsKey(name); + return entities.containsKey(name); } // #@@range/get{ public Entity get(String name) throws SemanticException { - Entity ent = (Entity)entitiesMap.get(name); + Entity ent = (Entity)entities.get(name); if (ent != null) { return ent; } @@ -85,26 +82,30 @@ public class Scope { } // #@@} - // Returns all function local variables defined in this scope. - // Does includes all nested local variables. - // Does NOT include static local variables. - public Iterator allVariables() { - return allEntities().iterator(); - } - - public Iterator variables() { - return entities(); - } - public long numEntities() { return entities.size(); } - // Returns local variables defined in this scope itself. - // Does NOT include nested local variables. + // Returns local variables defined in this scope. + // Does includes all nested local variables. // Does NOT include static local variables. - public Iterator entities() { - return entities.iterator(); + public Iterator variables() { + return variablesList().iterator(); + } + + protected List variablesList() { + List result = new ArrayList(); + Iterator ents = entities.values().iterator(); + while (ents.hasNext()) { + Entity ent = (Entity)ents.next(); + if (ent instanceof DefinedVariable) { + DefinedVariable var = (DefinedVariable)ent; + if (!var.isPrivate()) { + result.add(var); + } + } + } + return result; } public long numAllEntities() { @@ -120,27 +121,34 @@ public class Scope { return numAllEntities; } + // Returns all function local variables defined in this scope. + // Does includes all nested local variables. + // Does NOT include static local variables. + public Iterator allVariables() { + return allEntities().iterator(); + } + protected List allEntities() { List result = new ArrayList(); - Iterator cs = allChildren(); - while (cs.hasNext()) { - Scope c = (Scope)cs.next(); - result.addAll(c.entities); + Iterator scopes = allChildren(); + while (scopes.hasNext()) { + Scope s = (Scope)scopes.next(); + result.addAll(s.variablesList()); } return result; } public void checkReferences(ErrorHandler h) { - Iterator ents = entities.iterator(); + Iterator ents = entities.values().iterator(); while (ents.hasNext()) { Entity ent = (Entity)ents.next(); if (ent.isDefined() && ent.isPrivate() && !ent.isRefered()) { h.warn(ent.location(), "unused variable: " + ent.name()); } } - Iterator cs = children.iterator(); - while (cs.hasNext()) { - Scope s = (Scope)cs.next(); + Iterator scopes = children.iterator(); + while (scopes.hasNext()) { + Scope s = (Scope)scopes.next(); s.checkReferences(h); } } diff --git a/net/loveruby/cflat/ast/ToplevelScope.java b/net/loveruby/cflat/ast/ToplevelScope.java index a87dcfc..fd6642a 100644 --- a/net/loveruby/cflat/ast/ToplevelScope.java +++ b/net/loveruby/cflat/ast/ToplevelScope.java @@ -17,7 +17,7 @@ public class ToplevelScope extends Scope { /** Searches and gets entity searching scopes upto ToplevelScope. */ // #@@range/get{ public Entity get(String name) throws SemanticException { - Entity ent = (Entity)entitiesMap.get(name); + Entity ent = (Entity)entities.get(name); if (ent == null) { throw new SemanticException("unresolved reference: " + name); } @@ -25,10 +25,22 @@ public class ToplevelScope extends Scope { } // #@@} + public Iterator variables() { + throw new Error("TopScope#variables called"); + } + public Iterator allVariables() { throw new Error("TopScope#allVariables called"); } + public long numAllEntities() { + throw new Error("TopScope#numAllEntities called"); + } + + protected List allEntities() { + throw new Error("TopScope#allEntities called"); + } + protected List staticLocalVariables() { if (staticLocalVariables == null) { staticLocalVariables = new ArrayList(); @@ -61,7 +73,7 @@ public class ToplevelScope extends Scope { public List globalVariables() { List result = new ArrayList(); List src = new ArrayList(); - src.addAll(entities); + src.addAll(entities.values()); src.addAll(staticLocalVariables()); Iterator ents = src.iterator(); while (ents.hasNext()) { @@ -82,7 +94,7 @@ public class ToplevelScope extends Scope { public List commonSymbols() { List result = new ArrayList(); List src = new ArrayList(); - src.addAll(entities); + src.addAll(entities.values()); src.addAll(staticLocalVariables()); Iterator ents = src.iterator(); while (ents.hasNext()) { diff --git a/net/loveruby/cflat/compiler/TypeResolver.java b/net/loveruby/cflat/compiler/TypeResolver.java index 561c068..388445c 100644 --- a/net/loveruby/cflat/compiler/TypeResolver.java +++ b/net/loveruby/cflat/compiler/TypeResolver.java @@ -76,7 +76,6 @@ public class TypeResolver extends Visitor { public void visit(DefinedFunction func) { resolveFunctionHeader(func); - //resolveLocalVariables(func); visitNode(func.body()); } @@ -93,13 +92,6 @@ public class TypeResolver extends Visitor { } } - protected void resolveLocalVariables(DefinedFunction func) { - Iterator vars = func.localVariables(); - while (vars.hasNext()) { - visit((DefinedVariable)vars.next()); - } - } - public void visit(AddressNode node) { super.visit(node); // to avoid SemanticError which occurs when getting type of