PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/orc/lib/builtin/structured/Tuples.scala

https://github.com/laurenyew/cOrcS
Scala | 72 lines | 46 code | 7 blank | 19 comment | 11 complexity | a887a1a4276c36aea3a76e88da7f463a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. //
  2. // Tuples.scala -- Implementations of tuple manipulation sites
  3. // Project OrcScala
  4. //
  5. // $Id: Tuples.scala 2933 2011-12-15 16:26:02Z jthywissen $
  6. //
  7. // Created by dkitchin on March 31, 2011.
  8. //
  9. // Copyright (c) 2011 The University of Texas at Austin. All rights reserved.
  10. //
  11. // Use and redistribution of this file is governed by the license terms in
  12. // the LICENSE file found in the project's top-level directory and also found at
  13. // URL: http://orc.csres.utexas.edu/license.shtml .
  14. //
  15. package orc.lib.builtin.structured
  16. import orc.error.runtime.ArgumentTypeMismatchException
  17. import orc.error.compiletime.typing.TupleSizeException
  18. import orc.error.compiletime.typing.ArgumentTypecheckingException
  19. import orc.error.compiletime.typing.ExpectedType
  20. import orc.values.{ OrcRecord, OrcValue, OrcTuple }
  21. import orc.values.sites._
  22. import orc.types._
  23. object TupleConstructor extends TotalSite with TypedSite {
  24. override def name = "Tuple"
  25. def evaluate(args: List[AnyRef]) = OrcTuple(args)
  26. def orcType() = new SimpleCallableType with StrictType {
  27. def call(argTypes: List[Type]) = { TupleType(argTypes) }
  28. }
  29. }
  30. /*
  31. * Verifies that a Tuple t has a given number of elements.
  32. * If the check succeeds, the Some(t) is returned,
  33. * else None.
  34. */
  35. object TupleArityChecker extends PartialSite2 with TypedSite {
  36. override def name = "TupleArityChecker"
  37. def eval(x: AnyRef, y: AnyRef) =
  38. (x, y) match {
  39. case (OrcTuple(elems), arity: BigInt) =>
  40. if (elems.size == arity) {
  41. Some(OrcTuple(elems))
  42. } else {
  43. None
  44. }
  45. case (_: OrcTuple, a) => throw new ArgumentTypeMismatchException(1, "Integer", if (a != null) a.getClass().toString() else "null")
  46. case (a, _) => throw new ArgumentTypeMismatchException(0, "Tuple", if (a != null) a.getClass().toString() else "null")
  47. }
  48. def orcType() = new BinaryCallableType with StrictType {
  49. def call(r: Type, s: Type): Type = {
  50. (r, s) match {
  51. case (t @ TupleType(elements), IntegerConstantType(i)) => {
  52. if (elements.size != i) {
  53. throw new TupleSizeException(i.toInt, elements.size)
  54. }
  55. OptionType(t)
  56. }
  57. case (t: TupleType, IntegerType) => {
  58. OptionType(t)
  59. }
  60. case (_: TupleType, t) => throw new ArgumentTypecheckingException(1, IntegerType, t)
  61. case (t, _) => throw new ArgumentTypecheckingException(0, ExpectedType("of some tuple"), t)
  62. }
  63. }
  64. }
  65. }