view src/plparser/PropertyListScope.java @ 1:b149a5aa465a

Parser is written
author kono@ie.u-ryukyu.ac.jp
date Sat, 28 Aug 2010 17:39:34 +0900
parents
children
line wrap: on
line source

package plparser;

import java.util.TreeMap;

/*
 * Scope mechanism for local variable
 *    define("<>(x)","~(true& ~x)");
 *    previous x token is stored in an association list
 *    pop() remove local x token and restore previous x.
 *    previous x may be null. We cannot use this scope
 *    for quantifiers since our macro evaluator already
 *    convert everything in symbols.
 */
public class PropertyListScope<Node> {
	public TreeMap<String,Token<Node>> scope;
	public Dictionary<Node> dict;
	public PropertyListScope<Node> prev;
	

	public PropertyListScope(PropertyListScope<Node>prev, Dictionary<Node> dict) {
		this.dict = dict;
		this.prev = prev;
		this.scope = new TreeMap<String,Token<Node>>();
	}
	

	// enter the scope
	public PropertyListScope<Node> push() {
		return new PropertyListScope<Node>(this,dict);
	}
	
	// exit the scope
	public PropertyListScope<Node> pop() {
		// restore local variable name
		for(String name: scope.keySet()) {
			Token<Node> t = scope.get(name);
			dict.put(name, t); // overwrite
		}
		return prev;
	}
	
	// make new local name in this scope
	public Token<Node> getLocalName(String name) {
		Token<Node> n = new Token<Node>(name);
		Token<Node> t=dict.get(name);
		scope.put(name, t); // remember original
		dict.put(name,n);   // overwrite entry with new one
		return n;
	}
}