/interpreter/tags/at2dist041108/src/edu/vub/at/objects/natives/grammar/AGDefExternalField.java
Java | 122 lines | 56 code | 14 blank | 52 comment | 0 complexity | 3c4f330c45e644cb2b0bafa06ded7d74 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * AGDefExternalField.java created on 16-nov-2006 at 8:31:45 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.exceptions.XIllegalOperation; 33import edu.vub.at.objects.ATContext; 34import edu.vub.at.objects.ATObject; 35import edu.vub.at.objects.grammar.ATDefExternalField; 36import edu.vub.at.objects.grammar.ATDefinition; 37import edu.vub.at.objects.grammar.ATExpression; 38import edu.vub.at.objects.grammar.ATSymbol; 39import edu.vub.at.objects.natives.NATText; 40 41import java.util.HashSet; 42import java.util.Set; 43 44/** 45 * Represents the abstract grammar for defining external fields. 46 * 47 * @author tvcutsem 48 */ 49public class AGDefExternalField extends AGDefinition implements ATDefExternalField { 50 51 private final ATSymbol rcvNam_; 52 private final ATSymbol name_; 53 private final ATExpression valueExp_; 54 55 public AGDefExternalField(ATSymbol rcv, ATSymbol name, ATExpression value) { 56 rcvNam_ = rcv; 57 name_ = name; 58 valueExp_ = value; 59 } 60 61 public ATSymbol base_receiver() { return rcvNam_; } 62 public ATSymbol base_name() { return name_; } 63 public ATExpression base_valueExpression() { return valueExp_; } 64 65 /** 66 * Defines a new field in the object denoted by the receiver symbol. 67 * The return value is the value of the field. 68 * 69 * AGDEFEXTFLD(rcv,nam,val).eval(ctx) = 70 * rcv.eval(ctx).addField(nam, val.eval(ctx)) 71 * 72 * @throws XIllegalOperation if the receiver is an instance of a native type (whose field maps are sealed). 73 */ 74 public ATObject meta_eval(ATContext ctx) throws InterpreterException { 75 ATObject receiver = rcvNam_.meta_eval(ctx); 76 ATObject val = valueExp_.meta_eval(ctx); 77 receiver.meta_defineField(name_, val); 78 return val; 79 } 80 81 /** 82 * Quoting an external field definition results in a new quoted external field definition. 83 * 84 * AGDEFEXTFLD(rcv,nam,val).quote(ctx) = AGDEFEXTFLD(rcv.quote(ctx), nam.quote(ctx), val.quote(ctx)) 85 */ 86 public ATObject meta_quote(ATContext ctx) throws InterpreterException { 87 return new AGDefExternalField(rcvNam_.meta_quote(ctx).asSymbol(), 88 name_.meta_quote(ctx).asSymbol(), 89 valueExp_.meta_quote(ctx).asExpression()); 90 } 91 92 public NATText meta_print() throws InterpreterException { 93 return NATText.atValue("def " + rcvNam_.meta_print().javaValue 94 + "." + name_.meta_print().javaValue 95 + " := " + valueExp_.meta_print().javaValue); 96 } 97 98 /** 99 * IV(def o.x := val) = { } 100 */ 101 public Set impl_introducedVariables() throws InterpreterException { 102 return new HashSet(); 103 } 104 105 /** 106 * FV(def o.x := val) = FV(val) U { o } 107 */ 108 public Set impl_freeVariables() throws InterpreterException { 109 Set fvValueExp = valueExp_.impl_freeVariables(); 110 fvValueExp.add(rcvNam_); 111 return fvValueExp; 112 } 113 114 115 public Set impl_quotedFreeVariables() throws InterpreterException { 116 Set qfv = valueExp_.impl_quotedFreeVariables(); 117 qfv.addAll(rcvNam_.impl_quotedFreeVariables()); 118 qfv.addAll(name_.impl_quotedFreeVariables()); 119 return qfv; 120 } 121 122}