/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
- /**
- * AmbientTalk/2 Project
- * AGDefType.java created on 18-feb-2007 at 14:09:27
- * (c) Programming Technology Lab, 2006 - 2007
- * Authors: Tom Van Cutsem & Stijn Mostinckx
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
- package edu.vub.at.objects.natives.grammar;
- import edu.vub.at.eval.Evaluator;
- import edu.vub.at.exceptions.InterpreterException;
- import edu.vub.at.objects.ATContext;
- import edu.vub.at.objects.ATObject;
- import edu.vub.at.objects.ATTable;
- import edu.vub.at.objects.ATTypeTag;
- import edu.vub.at.objects.grammar.ATApplication;
- import edu.vub.at.objects.grammar.ATDefType;
- import edu.vub.at.objects.grammar.ATSymbol;
- import edu.vub.at.objects.mirrors.NativeClosure;
- import edu.vub.at.objects.natives.NATPatternType;
- import edu.vub.at.objects.natives.NATTable;
- import edu.vub.at.objects.natives.NATText;
- import edu.vub.at.objects.natives.NATTypeTag;
- /**
- * The native AST node for the code:
- *
- * deftype a <: b, c;
- *
- * @author tvcutsem
- */
- public final class AGDefType extends NATAbstractGrammar implements ATDefType {
- private final ATSymbol typeName_;
- private final ATTable typeArgs_;
- private final ATTable parentTypeExpressions_;
-
- public AGDefType(ATSymbol typeName, ATTable parentTypeExpressions) {
- typeName_ = typeName;
- typeArgs_ = NATTable.EMPTY;
- parentTypeExpressions_ = parentTypeExpressions;
- }
- public AGDefType(ATApplication typePattern, ATTable parentTypeExpressions) throws InterpreterException {
- typeName_ = typePattern.base_function().asSymbol();
- typeArgs_ = typePattern.base_arguments();
- parentTypeExpressions_ = parentTypeExpressions;
- }
-
- public ATTable base_parentTypeExpressions() {
- return parentTypeExpressions_;
- }
- public ATSymbol base_typeName() {
- return typeName_;
- }
-
- /**
- * Defines a new type in the current scope. The return value is
- * the new type.
- *
- * AGDEFTYPE(nam,parentExps).eval(ctx) =
- * ctx.scope.addField(nam, TYPETAG(nam, map eval(ctx) over parentExps))
- */
- public ATObject meta_eval(final ATContext ctx) throws InterpreterException {
- ATTypeTag type;
-
- // parentTypeExpressions_.map: { |parent| (reflect: parent).eval(ctx).base.asType() }
- ATTable parentTypes = parentTypeExpressions_.base_map_(new NativeClosure(this) {
- public ATObject base_apply(ATTable args) throws InterpreterException {
- return get(args,1).meta_eval(ctx).asTypeTag();
- }
- });
-
- if(typeArgs_ != NATTable.EMPTY) {
- type = new NATPatternType(typeName_, typeArgs_);
- } else {
- type = NATTypeTag.atValue(typeName_, parentTypes);
- }
-
- ctx.base_lexicalScope().meta_defineField(typeName_, type);
-
- return type;
- }
- /**
- * Quoting a type definition results in a new quoted type definition.
- *
- * AGDEFTYPE(nam,parentExps).quote(ctx) = AGDEFTYPE(nam.quote(ctx), parentExps.quote(ctx))
- */
- public ATObject meta_quote(ATContext ctx) throws InterpreterException {
- return new AGDefType(typeName_.meta_quote(ctx).asSymbol(), parentTypeExpressions_.meta_quote(ctx).asTable());
- }
-
- public NATText meta_print() throws InterpreterException {
- String thisType =
- "deftype " + typeName_.meta_print().javaValue +
- (typeArgs_==NATTable.EMPTY?"":(Evaluator.printElements(typeArgs_.asNativeTable(), "(", ", ", ")")).javaValue);
- if (parentTypeExpressions_ == NATTable.EMPTY) {
- return NATText.atValue(thisType);
- } else {
- return NATText.atValue(thisType + " <: " +
- Evaluator.printElements(parentTypeExpressions_.asNativeTable(), "", ",", "").javaValue);
- }
- }
-
- }