PageRenderTime 79ms CodeModel.GetById 72ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build270707/src/edu/vub/at/objects/natives/grammar/AGDefType.java

http://ambienttalk.googlecode.com/
Java | 106 lines | 48 code | 11 blank | 47 comment | 3 complexity | 3d6f50c71407a5e36730456a43dcc1b2 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.ATSymbol;
 38import edu.vub.at.objects.mirrors.NativeClosure;
 39import edu.vub.at.objects.natives.NATTable;
 40import edu.vub.at.objects.natives.NATText;
 41import edu.vub.at.objects.natives.NATTypeTag;
 42
 43/**
 44 * The native AST node for the code:
 45 *
 46 * deftype a <: b, c;
 47 *
 48 * @author tvcutsem
 49 */
 50public final class AGDefType extends NATAbstractGrammar implements ATDefType {
 51
 52	private final ATSymbol typeName_;
 53	private final ATTable parentTypeExpressions_;
 54	
 55	public AGDefType(ATSymbol typeName, ATTable parentTypeExpressions) {
 56		typeName_ = typeName;
 57		parentTypeExpressions_ = parentTypeExpressions;
 58	}
 59
 60	public ATTable base_parentTypeExpressions() {
 61		return parentTypeExpressions_;
 62	}
 63
 64	public ATSymbol base_typeName() {
 65		return typeName_;
 66	}
 67	
 68	/**
 69	 * Defines a new type in the current scope. The return value is
 70	 * the new type.
 71	 * 
 72	 * AGDEFTYPE(nam,parentExps).eval(ctx) =
 73	 *   ctx.scope.addField(nam, TYPETAG(nam, map eval(ctx) over parentExps))
 74	 */
 75	public ATObject meta_eval(final ATContext ctx) throws InterpreterException {
 76		// parentTypeExpressions_.map: { |parent| (reflect: parent).eval(ctx).base.asType() }
 77		ATTable parentTypes = parentTypeExpressions_.base_map_(new NativeClosure(this) {
 78			public ATObject base_apply(ATTable args) throws InterpreterException {
 79				return get(args,1).meta_eval(ctx).asTypeTag();
 80			}
 81		});
 82		
 83		ATTypeTag type = NATTypeTag.atValue(typeName_, parentTypes);
 84		ctx.base_lexicalScope().meta_defineField(typeName_, type);
 85		return type;
 86	}
 87
 88	/**
 89	 * Quoting a type definition results in a new quoted type definition.
 90	 * 
 91	 * AGDEFTYPE(nam,parentExps).quote(ctx) = AGDEFTYPE(nam.quote(ctx), parentExps.quote(ctx))
 92	 */
 93	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 94		return new AGDefType(typeName_.meta_quote(ctx).asSymbol(), parentTypeExpressions_.meta_quote(ctx).asTable());
 95	}
 96	
 97	public NATText meta_print() throws InterpreterException {
 98		if (parentTypeExpressions_ == NATTable.EMPTY) {
 99			return NATText.atValue("deftype " + typeName_.meta_print().javaValue);
100		} else {
101			return NATText.atValue("deftype " + typeName_.meta_print().javaValue + " <: " +
102					Evaluator.printElements(parentTypeExpressions_.asNativeTable(), "", ",", "").javaValue);
103		}
104	}
105	
106}