PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/laurenyew/cOrcS
Scala | 94 lines | 67 code | 8 blank | 19 comment | 6 complexity | b3f77a241a07c7d1f932de02b19c8197 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. //
  2. // Records.scala -- Implementations of record manipulation sites
  3. // Project OrcScala
  4. //
  5. // $Id: Records.scala 2934 2011-12-15 16:48:24Z 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. /*
  16. * Input to a RecordConstructor is a sequence of pairs.
  17. * Each pair is a (field, site) mapping.
  18. * Example: ( (.x,Site(x)), (.y, Site(y)), (.z, Site(z)) )
  19. */
  20. package orc.lib.builtin.structured
  21. import orc.error.runtime.ArgumentTypeMismatchException
  22. import orc.error.compiletime.typing.ArgumentTypecheckingException
  23. import orc.error.compiletime.typing.RecordShapeMismatchException
  24. import orc.error.compiletime.typing.ExpectedType
  25. import orc.values.{ OrcRecord, OrcValue, OrcTuple, Field }
  26. import orc.values.sites._
  27. import orc.types._
  28. import orc.util.OptionMapExtension._
  29. object RecordConstructor extends TotalSite with TypedSite {
  30. override def name = "Record"
  31. override def evaluate(args: List[AnyRef]) = {
  32. val valueMap = new scala.collection.mutable.HashMap[String, AnyRef]()
  33. args.zipWithIndex map
  34. {
  35. case (v: AnyRef, i: Int) =>
  36. v match {
  37. case OrcTuple(List(Field(key), value: AnyRef)) =>
  38. valueMap += ((key, value))
  39. case _ => throw new ArgumentTypeMismatchException(i, "(Field, _)", if (v != null) v.getClass().getCanonicalName() else "null")
  40. }
  41. }
  42. OrcRecord(scala.collection.immutable.HashMap.empty ++ valueMap)
  43. }
  44. def orcType() = new SimpleCallableType with StrictType {
  45. def call(argTypes: List[Type]) = {
  46. val bindings =
  47. (argTypes.zipWithIndex) map {
  48. case (TupleType(List(FieldType(f), t)), _) => (f, t)
  49. case (t, i) => throw new ArgumentTypecheckingException(i, TupleType(List(ExpectedType("of some field"), Top)), t)
  50. }
  51. RecordType(bindings.toMap)
  52. }
  53. }
  54. }
  55. object RecordMatcher extends PartialSite with TypedSite {
  56. override def name = "RecordMatcher"
  57. override def evaluate(args: List[AnyRef]): Option[AnyRef] =
  58. args match {
  59. case List(OrcRecord(entries), shape @ _*) => {
  60. val matchedValues: Option[List[AnyRef]] =
  61. shape.toList.zipWithIndex optionMap {
  62. case (Field(f), _) => entries get f
  63. case (a, i) => throw new ArgumentTypeMismatchException(i + 1, "Field", if (a != null) a.getClass().toString() else "null")
  64. }
  65. matchedValues map { OrcValue.letLike }
  66. }
  67. case List(_, _*) => None
  68. case _ => throw new AssertionError("Record match internal failure (RecordMatcher.evaluate match error on args list)")
  69. }
  70. def orcType() = new SimpleCallableType with StrictType {
  71. def call(argTypes: List[Type]): Type = {
  72. argTypes match {
  73. case List(rt @ RecordType(entries), shape @ _*) => {
  74. val matchedElements =
  75. shape.toList.zipWithIndex map {
  76. case (FieldType(f), _) => entries.getOrElse(f, throw new RecordShapeMismatchException(rt, f))
  77. case (t, i) => throw new ArgumentTypecheckingException(i + 1, ExpectedType("of some field"), t)
  78. }
  79. letLike(matchedElements)
  80. }
  81. case List(t, _*) => throw new ArgumentTypecheckingException(0, RecordType(Nil.toMap), t)
  82. case _ => throw new AssertionError("Record type checking internal failure (RecordMatcher.orcType.<new SimpleCallableType with StrictType>.call match error)")
  83. }
  84. }
  85. }
  86. }