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

http://ambienttalk.googlecode.com/ · Java · 127 lines · 58 code · 17 blank · 52 comment · 6 complexity · 235a4607036fa85f4d0357fbce22c9ce MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGMultiAssignment.java created on 18-aug-2006 at 9:31:38
  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. */
  28. package edu.vub.at.objects.natives.grammar;
  29. import edu.vub.at.eval.PartialBinder;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.objects.ATContext;
  32. import edu.vub.at.objects.ATObject;
  33. import edu.vub.at.objects.ATTable;
  34. import edu.vub.at.objects.grammar.ATExpression;
  35. import edu.vub.at.objects.grammar.ATMultiAssignment;
  36. import edu.vub.at.objects.natives.NATText;
  37. import edu.vub.util.TempFieldGenerator;
  38. import java.util.HashMap;
  39. import java.util.HashSet;
  40. import java.util.Set;
  41. /**
  42. * @author tvc
  43. *
  44. * The native implementation of a multiple assignment AG element.
  45. */
  46. public final class AGMultiAssignment extends NATAbstractGrammar implements ATMultiAssignment {
  47. private final ATTable parameters_;
  48. private final ATExpression valueExp_;
  49. private final PartialBinder binderPartialFunction_;
  50. public AGMultiAssignment(ATTable par, ATExpression val) throws InterpreterException {
  51. parameters_ = par;
  52. valueExp_ = val;
  53. binderPartialFunction_ = PartialBinder.calculateResidual("multi-assignment", par);
  54. }
  55. public ATTable base_parameters() { return parameters_; }
  56. public ATExpression base_valueExpression() { return valueExp_; }
  57. /**
  58. * To evaluate a multiple assignment, evaluate the right hand side to a table
  59. * and assign the parameters on the left hand side to the 'arguments' of the right hand side,
  60. * almost as if they were bound during a function call (the parameters are assigned instead of defined).
  61. *
  62. * AGMULTIASS(par,val).eval(ctx) = assign(ctx.scope, par, val.eval(ctx))
  63. *
  64. * @return the evaluated arguments table
  65. */
  66. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  67. ATTable args = valueExp_.meta_eval(ctx).asTable();
  68. PartialBinder.assignArgsToParams(binderPartialFunction_, ctx, args);
  69. return args;
  70. }
  71. /**
  72. * AGMULTIASS(par,val).quote(ctx) = AGMULTIASS(par.quote(ctx), val.quote(ctx))
  73. */
  74. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  75. return new AGMultiAssignment(parameters_.meta_quote(ctx).asTable(), valueExp_.meta_quote(ctx).asExpression());
  76. }
  77. public NATText meta_print() throws InterpreterException {
  78. return NATText.atValue(parameters_.meta_print().javaValue + " := " + valueExp_.meta_print().javaValue);
  79. }
  80. public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
  81. return NATText.atValue(parameters_.impl_asUnquotedCode(objectMap).javaValue + " := " + valueExp_.impl_asUnquotedCode(objectMap).javaValue);
  82. }
  83. /**
  84. * FV([var1, var2 := exp1, @ rest] := exp2) =
  85. * { var1, var2, rest } U FV(exp1) U FV(exp2)
  86. */
  87. public Set impl_freeVariables() throws InterpreterException {
  88. Set fvValExp = valueExp_.impl_freeVariables();
  89. ATObject[] params = parameters_.asNativeTable().elements_;
  90. for (int i = 0; i < params.length; i++) {
  91. if (params[i].isSymbol()) {
  92. // Mandatory arguments, e.g. x
  93. fvValExp.add(params[i].asSymbol());
  94. } else if (params[i].isVariableAssignment()) {
  95. // Optional args or rest args
  96. // Note: for optional args, both assigned var and free vars
  97. // of the initialization expression are considered free vars
  98. fvValExp.addAll(params[i].asExpression().impl_freeVariables());
  99. } else if (params[i].isSplice()) {
  100. fvValExp.add(params[i].asSplice().base_expression().asSymbol());
  101. }
  102. }
  103. return fvValExp;
  104. }
  105. public Set impl_quotedFreeVariables() throws InterpreterException {
  106. Set qfv = parameters_.impl_quotedFreeVariables();
  107. qfv.addAll(valueExp_.impl_quotedFreeVariables());
  108. return qfv;
  109. }
  110. }