PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/objects/natives/grammar/AGImport.java

http://ambienttalk.googlecode.com/
Java | 215 lines | 122 code | 26 blank | 67 comment | 24 complexity | 9ab0aae66329fb015e1d61d0fbef4581 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGImport.java created on 6-mrt-2007 at 21:34:54
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.objects.natives.grammar;
 29
 30import edu.vub.at.eval.Evaluator;
 31import edu.vub.at.eval.Import;
 32import edu.vub.at.exceptions.InterpreterException;
 33import edu.vub.at.exceptions.XIllegalArgument;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.grammar.ATExpression;
 37import edu.vub.at.objects.grammar.ATImport;
 38import edu.vub.at.objects.natives.NATTable;
 39import edu.vub.at.objects.natives.NATText;
 40import edu.vub.util.TempFieldGenerator;
 41
 42import java.util.HashMap;
 43import java.util.HashSet;
 44import java.util.Hashtable;
 45import java.util.Set;
 46
 47/**
 48 *  The public interface to a native import AST component, which is of the form:
 49 * 'import <expression> (alias (symbol := symbol)+ )? (exclude symbol (, symbol)* )?'
 50 *
 51 * @see edu.vub.at.objects.grammar.ATImport
 52 *
 53 * @author tvcutsem
 54 */
 55public class AGImport extends NATAbstractGrammar implements ATImport {
 56
 57	private final ATExpression importedObjectExp_;
 58
 59	private final ATExpression aliasDeclarations_;
 60	
 61	private final ATExpression excludesDeclarations_;
 62	
 63	/**
 64	 * For efficiency purposes, the table form of the alias mapping is preprocessed
 65	 * into a hashtable format.
 66	 */
 67	private Hashtable aliasedSymbols_; // maps ATSymbols to ATSymbols
 68	
 69	/**
 70	 * For efficiency purposes, the table form of the excluded symbols is preprocessed
 71	 * into a hashset format.
 72	 */
 73	private HashSet excludedSymbols_; // contains ATSymbols
 74	
 75	/** create a new import statement. The alias and excludes declaration tables may still contain quoted expressions */
 76	public AGImport(ATExpression importedObjectExp, ATExpression aliasDeclarations, ATExpression excludesDeclarations) {
 77		importedObjectExp_ = importedObjectExp;
 78		aliasDeclarations_ = aliasDeclarations;
 79		excludesDeclarations_ = excludesDeclarations;
 80		// cannot already preprocess the import statement here, as it may contain
 81		// unquotes which need to be evaluated to symbols first
 82	}
 83	
 84	public ATExpression base_aliasedSymbols() throws InterpreterException {
 85		return aliasDeclarations_;
 86	}
 87
 88	public ATExpression base_excludedSymbols() throws InterpreterException {
 89		return excludesDeclarations_;
 90	}
 91
 92	public ATExpression base_importedObjectExpression() throws InterpreterException {
 93		return importedObjectExp_;
 94	}
 95
 96	/**
 97	 * AGIMPORT(exp,aliases,excludes).eval(ctx) =
 98	 *   import(exp.eval(ctx), ctx, preprocess(aliases), preprocess(excludes)) ;
 99	 *   NIL
100	 */
101	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
102		if (aliasedSymbols_ == null) {
103			aliasedSymbols_ = Import.preprocessAliases(aliasDeclarations_.asTable());
104			excludedSymbols_ = Import.preprocessExcludes(excludesDeclarations_.asTable());	
105		}
106		return Import.performImport(importedObjectExp_.meta_eval(ctx), ctx, aliasedSymbols_, excludedSymbols_);
107	}
108
109	/**
110	 * Quoting an import statement results in a new quoted import statement.
111	 * 
112	 * AGIMPORT(exp,aliases,excludes).quote(ctx) = AGIMPORT(exp.quote(ctx), aliases.quote(ctx), excludes.quote(ctx))
113	 */
114	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
115		return new AGImport(importedObjectExp_.meta_quote(ctx).asExpression(),
116							aliasDeclarations_.meta_quote(ctx).asExpression(),
117							excludesDeclarations_.meta_quote(ctx).asExpression());
118	}
119	
120	public NATText meta_print() throws InterpreterException {
121		StringBuffer expression = new StringBuffer("import " + importedObjectExp_.meta_print().javaValue);
122		if (aliasDeclarations_ != NATTable.EMPTY) {
123			expression.append(" alias ");
124			if (aliasDeclarations_.isTable()) {
125				ATObject[] aliases = aliasDeclarations_.asNativeTable().elements_;
126				// append first alias
127				printAliasBinding(expression, aliases[0].asNativeTable());
128				for (int i = 1; i < aliases.length; i++) {
129					// append rest of the aliases
130					expression.append(",");
131					printAliasBinding(expression, aliases[i].asNativeTable());
132				}
133			} else {
134				// list of aliases is a quatation
135				expression.append(aliasDeclarations_.meta_print().javaValue);
136			}
137		}
138		if (excludesDeclarations_ != NATTable.EMPTY) {
139			expression.append(" exclude ");
140			if (excludesDeclarations_.isTable()) {
141				expression.append(Evaluator.printElements(excludesDeclarations_.asNativeTable().elements_, "", ",", "").javaValue);
142			} else {
143				// list of excluded symbols is a quotation
144				expression.append(excludesDeclarations_.meta_print().javaValue);
145			}
146		}
147		
148		return NATText.atValue(expression.toString());
149	}
150	
151	private void printAliasBinding(StringBuffer buff, NATTable aliasBinding) throws InterpreterException {
152		ATObject[] binding = aliasBinding.elements_;
153		if (binding.length != 2) {
154			throw new XIllegalArgument("Alias binding of import statement is not a table of size two: " + aliasBinding.meta_print().javaValue);
155		}
156		buff.append(binding[0].meta_print().javaValue).append(" := ").append(binding[1].meta_print().javaValue);
157	}
158	
159	
160	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
161		StringBuffer expression = new StringBuffer("import " + importedObjectExp_.impl_asUnquotedCode(objectMap).javaValue);
162		if (aliasDeclarations_ != NATTable.EMPTY) {
163			expression.append(" alias ");
164			if (aliasDeclarations_.isTable()) {
165				ATObject[] aliases = aliasDeclarations_.asNativeTable().elements_;
166				// append first alias
167				codeAliasBinding(objectMap, expression, aliases[0].asNativeTable());
168				for (int i = 1; i < aliases.length; i++) {
169					// append rest of the aliases
170					expression.append(",");
171					codeAliasBinding(objectMap, expression, aliases[i].asNativeTable());
172				}
173			} else {
174				// list of aliases is a quatation
175				expression.append(aliasDeclarations_.impl_asUnquotedCode(objectMap).javaValue);
176			}
177		}
178		if (excludesDeclarations_ != NATTable.EMPTY) {
179			expression.append(" exclude ");
180			if (excludesDeclarations_.isTable()) {
181				expression.append(Evaluator.codeElements(objectMap, excludesDeclarations_.asNativeTable().elements_, "", ",", "").javaValue);
182			} else {
183				// list of excluded symbols is a quotation
184				expression.append(excludesDeclarations_.impl_asUnquotedCode(objectMap).javaValue);
185			}
186		}
187		
188		return NATText.atValue(expression.toString());
189	}
190	
191	private void codeAliasBinding(TempFieldGenerator objectMap, StringBuffer buff, NATTable aliasBinding) throws InterpreterException {
192		ATObject[] binding = aliasBinding.elements_;
193		if (binding.length != 2) {
194			throw new XIllegalArgument("Alias binding of import statement is not a table of size two: " + aliasBinding.impl_asUnquotedCode(objectMap).javaValue);
195		}
196		buff.append(binding[0].impl_asUnquotedCode(objectMap).javaValue).append(" := ").append(binding[1].impl_asUnquotedCode(objectMap).javaValue);
197	}
198	
199	
200	/**
201	 * FV(import objExp alias nam1 := nam2 exclude nam3) = FV(objExp)
202	 */
203	public Set impl_freeVariables() throws InterpreterException {
204        return importedObjectExp_.impl_freeVariables();
205	}
206	
207	
208	public Set impl_quotedFreeVariables() throws InterpreterException {
209		Set qfv = importedObjectExp_.impl_quotedFreeVariables();
210		qfv.addAll(aliasDeclarations_.impl_quotedFreeVariables());
211		qfv.addAll(excludesDeclarations_.impl_quotedFreeVariables());
212		return qfv;
213	}
214	
215}