001: /*
002: * @(#)TranslateContext.java 1.4 05/06/13
003: *
004: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.compiler;
010:
011: import java.util.HashSet;
012: import java.util.Set;
013:
014: import pnuts.lang.Context;
015:
016: /**
017: */
018: class TranslateContext extends Context {
019:
020: Frame rootEnv = new Frame();
021: Frame env = rootEnv;
022:
023: void openFrame(String func, String locals[]) {
024: env = new Frame(locals, func, env, false);
025: }
026:
027: void closeFrame() {
028: env = env.parent;
029: }
030:
031: Reference findReference(String symbol) {
032: String sym = symbol.intern();
033: Frame f = env;
034: while (f != null) {
035: Reference r = f.findReference(sym);
036: if (r != null) {
037: return r;
038: }
039: f = f.parent;
040: }
041: return null;
042: }
043:
044: Reference getReference(String symbol) {
045: String sym = symbol.intern();
046: Frame f = env;
047: while (f != null) {
048: Reference r = f.getReference(sym, true);
049: if (r != null) {
050: return r;
051: }
052: f = f.parent;
053: }
054: return null;
055: }
056:
057: int declare(String symbol) {
058: env.declare(symbol, 0, 0);
059: return 0;
060: }
061:
062: void openScope(String locals[]) {
063: env.openLocal();
064: for (int i = 0; i < locals.length; i++) {
065: env._declare(locals[i], 0);
066: }
067: }
068:
069: void closeScope() {
070: env.closeLocal();
071: }
072:
073: void openBranchEnv() {
074: env.openBranchEnv();
075: }
076:
077: void addBranch() {
078: env.addBranch();
079: }
080:
081: void closeBranchEnv() {
082: env.closeBranchEnv();
083: }
084:
085: void addToFreeVarSet(String symbol) {
086: Frame f = env;
087: while (f.parent != rootEnv) {
088: f = f.parent;
089: }
090: Set set = (Set) f.attr;
091: if (set == null) {
092: set = new HashSet();
093: f.attr = set;
094: }
095: set.add(symbol);
096: }
097:
098: Set getFreeVarSet() {
099: return (Set) env.attr;
100: }
101: }
|