/**
* 32-bit raw bit stream.
*
* bits.length must be equal to 32
* bits.head must be an MSB of the value.
*/
sealed trait BinaryVal {
val bits: List[Boolean]
}
// 32-bit raw int
case class BinaryInt(bits: List[Boolean]) extends BinaryVal
// 32-bit raw float
case class BinaryFloat(bits: List[Boolean]) extends BinaryVal
BinaryVal.scala
sealed trait BinaryVal {
val bits : List[Boolean]
}
case class BinaryInt(bits:List[Boolean]) extends BinaryVal
case class BinaryFloat(bits:List[Boolean]) extends BinaryVal
/**
* Instance of IValue[BinaryVal] and ToFloat[BinaryVal]
* This should convert 32-bit binary stream to integer or float.
*
* You can use some JVM functions to implement it, such as:
* java.lang.Float.floatToIntBits(yourFloat)
* java.lang.Integer.toBinaryString(intBits)
* java.lang.Long.parseLong(myString, 2).toInt
* java.lang.Float.intBitsToFloat(intBits)
*
* See https://stackoverflow.com/questions/5157664/java-how-to-convert-a-string-of-binary-values-to-a-float-and-vice-versa
*/
object BinaryValImpl {
// Instance of IValue[BinaryImpl]
implicit val binaryValImpl: IValue[BinaryVal] = ???
// Instance of ToFloat[BinaryVal]
implicit val binaryValToFloatImpl: ToFloat[BinaryVal] = ???
}
BinaryValImpl.scala
object BinaryValImpl{
implicit val binaryValImpl : IValue[BinaryVal] = ???
implicit val binaryValToFloatImpl: ToFloat[BinaryVal] =???
}
이 코드를 채우려면 일단 IValue, ToFloat 이 뭔지 알아야 될 것 같다.
Immval.scala
/**
* Immediate values
*/
sealed trait ImmVal
case class ImmInt(n: Int) extends ImmVal
case class ImmFloat(f: Float) extends ImmVal
ImmValImpl.scala
/**
* Implementation of the naive eager evaluation.
*/
object ImmValImpl {
// Instance of IValue[ImmVal]
implicit val immValImpl: IValue[ImmVal] = ???
// Instance of ToFloat[Immval]
implicit val immValToFloatImpl: ToFloat[ImmVal] = ???
}
이것도 IValue가 뭔지 알아야 될 것 같다.
IValue.scala
/**
* Type class for the abstract value for the evaluator.
* The abstract value can be either integer or float value.
*/
trait IValue[V] {
/**
* Generate an abstract value from the given literal integer
*/
def genInt(n: Int): V
/**
* Generate an abstract value from the given literal float
*/
def genFloat(f: Float): V
/**
* Matching function with handlers.
* If the v is integer, call handleInt(v), otherwise, call handleFloat(v)
*
* @param v target value
* @param handleInt integer handler
* @param handleFloat float handler
* @return result of the one of the handlers
*/
def matchVal(v: V)(
handleInt: Int => V,
handleFloat: Float => V
): V
def matchInt(v: V)(handleInt: Int => V): V = {
matchVal(v)(handleInt, _ => throw new Exception("expected Int, got Float") )
}
def matchFloat(v: V)(handleFloat: Float => V): V = {
matchVal(v)(_ => throw new Exception("expected Float, got Int"), handleFloat)
}
}
trait IValue[V] {
def genInt(n:Int):V
--> Generate an abstract value from the given literal integer
def genFloat(f: Float): V
--> Generate an abstract value from the given literal float
}
'학교 공부 > 프로그래밍원리' 카테고리의 다른 글
과제 및 프로젝트 (0) | 2022.12.26 |
---|---|
12/6 다시 푸는 Assignment 1 (0) | 2022.12.06 |
HW3 -(4) StackedArray 구현하기 (0) | 2022.11.27 |
HW3 - (3) Matrix 구현하기 (0) | 2022.11.26 |
HW 3- (2) reshape 구현하기 (0) | 2022.11.26 |