PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/grammar/AGDefType.java

http://ambienttalk.googlecode.com/
Java | 147 lines | 76 code | 18 blank | 53 comment | 6 complexity | 759ae7705c7e7a0c8d533ab2de27c3df MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGDefType.java created on 18-feb-2007 at 14:09:27
  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.exceptions.InterpreterException;
 32import edu.vub.at.objects.ATContext;
 33import edu.vub.at.objects.ATObject;
 34import edu.vub.at.objects.ATTable;
 35import edu.vub.at.objects.ATTypeTag;
 36import edu.vub.at.objects.grammar.ATDefType;
 37import edu.vub.at.objects.grammar.ATDefinition;
 38import edu.vub.at.objects.grammar.ATSymbol;
 39import edu.vub.at.objects.mirrors.NativeClosure;
 40import edu.vub.at.objects.natives.NATTable;
 41import edu.vub.at.objects.natives.NATText;
 42import edu.vub.at.objects.natives.NATTypeTag;
 43import edu.vub.util.TempFieldGenerator;
 44
 45import java.util.HashSet;
 46import java.util.Set;
 47
 48/**
 49 * The native AST node for the code:
 50 *
 51 * deftype a <: b, c;
 52 *
 53 * @author tvcutsem
 54 */
 55public final class AGDefType extends AGDefinition implements ATDefType {
 56
 57	private final ATSymbol typeName_;
 58	private final ATTable parentTypeExpressions_;
 59	
 60	public AGDefType(ATSymbol typeName, ATTable parentTypeExpressions) {
 61		typeName_ = typeName;
 62		parentTypeExpressions_ = parentTypeExpressions;
 63	}
 64
 65	public ATTable base_parentTypeExpressions() {
 66		return parentTypeExpressions_;
 67	}
 68
 69	public ATSymbol base_typeName() {
 70		return typeName_;
 71	}
 72	
 73	/**
 74	 * Defines a new type in the current scope. The return value is
 75	 * the new type.
 76	 * 
 77	 * AGDEFTYPE(nam,parentExps).eval(ctx) =
 78	 *   ctx.scope.addField(nam, TYPETAG(nam, map eval(ctx) over parentExps))
 79	 */
 80	public ATObject meta_eval(final ATContext ctx) throws InterpreterException {
 81		// parentTypeExpressions_.map: { |parent| (reflect: parent).eval(ctx).base.asType() }
 82		ATTable parentTypes = parentTypeExpressions_.base_map_(new NativeClosure(this) {
 83			public ATObject base_apply(ATTable args) throws InterpreterException {
 84				return get(args,1).meta_eval(ctx).asTypeTag();
 85			}
 86		});
 87		
 88		ATTypeTag type = NATTypeTag.atValue(typeName_, parentTypes);
 89		ctx.base_lexicalScope().meta_defineField(typeName_, type);
 90		return type;
 91	}
 92
 93	/**
 94	 * Quoting a type definition results in a new quoted type definition.
 95	 * 
 96	 * AGDEFTYPE(nam,parentExps).quote(ctx) = AGDEFTYPE(nam.quote(ctx), parentExps.quote(ctx))
 97	 */
 98	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 99		return new AGDefType(typeName_.meta_quote(ctx).asSymbol(), parentTypeExpressions_.meta_quote(ctx).asTable());
100	}
101	
102	public NATText meta_print() throws InterpreterException {
103		if (parentTypeExpressions_ == NATTable.EMPTY) {
104			return NATText.atValue("deftype " + typeName_.meta_print().javaValue);
105		} else {
106			return NATText.atValue("deftype " + typeName_.meta_print().javaValue + " <: " +
107					Evaluator.printElements(parentTypeExpressions_.asNativeTable(), "", ",", "").javaValue);
108		}
109	}
110	
111	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
112		if (parentTypeExpressions_ == NATTable.EMPTY) {
113			return NATText.atValue("deftype " + typeName_.impl_asUnquotedCode(objectMap).javaValue);
114		} else {
115			return NATText.atValue("deftype " + typeName_.impl_asUnquotedCode(objectMap).javaValue + " <: " +
116					Evaluator.codeElements(objectMap, parentTypeExpressions_.asNativeTable(), "", ",", "").javaValue);
117		}
118	}
119	
120	public ATDefinition asDefinition() { return this; }
121
122	/**
123	 * IV(deftype nam <: exps) = { T1 }
124	 */
125	public Set impl_introducedVariables() throws InterpreterException {
126		Set singleton = new HashSet();
127		singleton.add(typeName_);
128		return singleton;
129	}
130
131	/**
132	 * FV(deftype nam <: exps) = FV(exps) \ { nam }
133	 */
134	public Set impl_freeVariables() throws InterpreterException {
135		Set fvParentType = parentTypeExpressions_.impl_freeVariables();
136		fvParentType.remove(typeName_);
137		return fvParentType;
138	}
139	
140	
141	public Set impl_quotedFreeVariables() throws InterpreterException {
142		Set qfv = parentTypeExpressions_.impl_quotedFreeVariables();
143		qfv.addAll(typeName_.impl_quotedFreeVariables());
144		return qfv;
145	}
146	
147}