PageRenderTime 117ms CodeModel.GetById 107ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/natives/grammar/AGDefType.java

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