暗黙の型変換付き四則演算計算機のコンパイラ(スタックマシン用)

doubleとintの暗黙の型変換付き四則演算のコンパイラを作りました。
ターゲットとなるマシンは仮想的なS(Stack)とC(Code)の2つが存在しているだけのSCVMにしました。
理由はSECDマシン風のマシンならパターンマッチングを使って短く美しく実装出来るためです。

このプログラムでは以下のような処理を行っています。

構文木演算子順位法で読み込み(read)
2パターンマッチングで抽象構文木に変換(st2ast)
3パターンマッチングで暗黙の型変換(implicitConversion)
4ASTをSCVMのコード列であるListにコンパイル(compile)
5SCVMで実行(scvm.exec)

暗黙の型変換は関数表と変換表を元に変換しています。

要するに暗黙の型変換が出来たので、パーサとコンパイラVMくっつけて動かしてみたってわけです。

package t

object t {
  def main(argv:Array[String]) {
    apply("2+3.0*1.2")
    apply("(2.0+3)/1.5")

  }
  def apply(s:String) {
    val st = read(s)
    println("st "+st)
    val ast = st2ast(st)
    println("ast "+ast)
    val tast = implicitConversion(ast)
    println("tast "+tast)
    val codes = compile(tast)
    println("codes "+codes)
    val result = scvm.exec(codes)
    println("result "+result)
  }
}

object read {
  val ints = """(?s)^[\t\r\n ]*([1-9][0-9]*)(.*$)""".r
  val bytes = """(?s)^[\t\r\n ]*([1-9][0-9]*b)(.*$)""".r
  val doubles = """(?s)^[\t\r\n ]*([0-9]+\.[0-9]*)(.*$)""".r
  val ns = """(?s)^[\t\r\n ]*([a-zA-Z_][a-zA-Z_0-9]*|[\(\)\{\}+/*\-,=;]|)(.*$)""".r
  def apply(str:String):Any = {
    var src = str
    var token:Any = ""
    var ptoken:Any = ""
    def lex():Any = {
      ptoken = token
      src match {
        case doubles(a,b) => token = a.toDouble; src = b
        case bytes(a,b) => token = a.substring(0,a.length-1).toByte; src = b
        case ints(a,b) => token = a.toInt; src = b
        case ns(a,b) => token = a; src = b
      }
      ptoken
    }
    def eat(e:Any):Any = {
      if(lex() != e) {
        throw new Exception("syntax error. found unexpected token "+ptoken)
      }
      ptoken
    }
    lex()
    def prs(a:Any):Any = a match {
      case "(" => (0, "p",")")
      case _ => -1
    }
    def ins(a:Any):Any = a match {
      case "+" => (10,"l")
      case "-" => (10,"l")
      case "*" => (20,"l")
      case "/" => (20,"l")
      case "(" => (0,"p",")")
      case _ => -1
    }
    def exp(p:Int):Any = {
      if(token ==")" || token == "}") return "void"
      def pr(t:Any):Any = {
        val op = t
        prs(op) match {
          case (np:Int,"p",ep) => val e = exp(np); (op,e,eat(ep))
          case _ => op
        }
      }
      var t = pr(lex())
      def in(t:Any):Any = {
        ins(token) match {
          case (np:Int,"l") if(np > p) => val op = lex(); in(t, op, exp(np))
          case (np:Int,"p",ep) => val sp = lex(); val e = exp(np); in(t, sp, e, eat(ep))
          case _ => t
        }
      }
      in(t)
    }
    exp(0)
  }
}

trait TType
case class TFloat() extends TType
case class TInt() extends TType
case class TNone() extends TType
case class TStr() extends TType
case class TFun(prms:List[TType],rc:TType) extends TType
sealed abstract case class E(t:TType)
case class EId(override val t:TType, s:String) extends E(t)
case class ECast(override val t:TType, a:E) extends E(t)
case class EInt(override val t:TType, a:scala.Int) extends E(t)
case class EFloat(override val t:TType, a:Double) extends E(t)
case class ECall(override val t:TType, f:E, a:List[E]) extends E(t)

object st2ast {
  def apply(st:Any):E = st match {
    case st:Int => EInt(TInt(), st)
    case st:Double => EFloat(TFloat(), st)
    case (a,"+",b)=> ECall(TNone(), EId(TNone(),"add"), List(st2ast(a),st2ast(b)))
    case (a,"-",b)=> ECall(TNone(), EId(TNone(),"sub"), List(st2ast(a),st2ast(b)))
    case (a,"*",b)=> ECall(TNone(), EId(TNone(),"mul"), List(st2ast(a),st2ast(b)))
    case (a,"/",b)=> ECall(TNone(), EId(TNone(),"div"), List(st2ast(a),st2ast(b)))
    case ("(",a,")") => apply(a)
  }
}

object implicitConversion {
  def apply(e:E):E = f(e)

  def main(argv:Array[String]) {
    println("*"+f(ECall(TNone(),EId(TNone(),"add"),List(EFloat(TFloat(),1),EInt(TInt(),2)))))
    println("*"+f(ECall(TNone(),EId(TNone(),"add"),List(EInt(TInt(),1),EInt(TInt(),2)))))
    println("*"+f(ECall(TNone(),EId(TNone(),"add"), List(ECall(TNone(),EId(TNone(),"add"),List(EFloat(TFloat(),
1),EInt(TInt(),1))),EInt(TInt(),2)))))
  }
  def f(e:E):E = e match {
    case ECast(t,a) => ECast(t, f(a))
    case EInt(t,i) => EInt(TInt(), i)
    case EFloat(t,i) => EFloat(TFloat(), i)
    case EId(t,a) => EId(t,a)
    case ECall(t,EId(tid,id),xs1) =>
      val xs = xs1.map(f)
     // 暗黙の型変換つき型チェック
      def typeCheck(ts:List[TType],es:List[E]):Option[List[E]] = {
       (ts,es) match {
          case (List(),List()) => Some(List())
          case (t::ts,e::es) =>
            typeCheck(ts,es) match {
              case None => None
              case Some(e2) =>
                val t2 = e match {
                  case ECall(TFun(_,r),_,_) => r
                  case e:E => e.t
                }
                if(t==t2) Some(e::e2)
                else if(implicitConversions.contains(t->t2)) Some(implicitConversions(t->t2)(e)::e2)
                else None
            }
          case _ => None
        }
      }
      // 関数を全て取り出す
      def fns(funs:List[TFun]):Option[E] = funs match {
        case List() => None
        case (i@TFun(l,r))::ls =>
          typeCheck(l, xs) match {
            case None => fns(ls)
            case Some(e) => Some(ECall(r, EId(i,id), e))
          }
      }
      fns(functions(id)) match {
        case None=> throw new Exception("not found method "+e)
        case Some(e) => e
      }
  }


  // 関数定義表

  var functions = Map[String,List[TFun]](
    "add"->List(
      TFun(List(TInt(),TInt()),TInt()),
      TFun(List(TStr(),TStr()),TStr()),
      TFun(List(TFloat(),TFloat()),TFloat())
    ),
    "sub"->List(
      TFun(List(TInt(),TInt()),TInt()),
      TFun(List(TStr(),TStr()),TStr()),
      TFun(List(TFloat(),TFloat()),TFloat())
    ),
    "mul"->List(
      TFun(List(TInt(),TInt()),TInt()),
      TFun(List(TStr(),TStr()),TStr()),
      TFun(List(TFloat(),TFloat()),TFloat())
    ),
    "div"->List(
      TFun(List(TInt(),TInt()),TInt()),
      TFun(List(TStr(),TStr()),TStr()),
      TFun(List(TFloat(),TFloat()),TFloat())
    )
  )

  // 暗黙の型変換の表
  var implicitConversions = Map[(TType,TType),(E)=>E] (
    (TInt(),TStr()) -> ((a:E)=>ECast(TStr(),a)),
    (TFloat(),TStr()) -> ((a:E)=>ECast(TStr(),a)),
    (TFloat(),TInt()) -> ((a:E)=>ECast(TFloat(),a))
  )
}

object compile {

  def apply(e:E):List[Any] = f(e,List[Any]())

  def typeNames(t:TType):String = t match {
    case TFun(_,a)=> typeNames(a)
    case TFloat()=>"d"
    case TInt()=>"i"
    case _ =>throw new Exception("compile error unknown type "+t)
  }
  def f(e:E,l:List[Any]):List[Any] = e match {
    case EId(t,s)=> throw new Exception("compile error")
    case ECast(t,a)=> f(a,("cast"+typeNames(a.t)+"2"+typeNames(t))::l)
    case EInt(t,a)=> ("pushi",a)::l
    case EFloat(t,a)=> ("pushd",a)::l
    case ECall(t,EId(_,n),a)=> a.foldLeft((n+typeNames(t))::l){case(l2,a)=>f(a,l2)}
  }
}

object scvm {
  def exec(c:List[Any]) = f(List[Any](), c)
  
  def f(s:List[Any], c:List[Any]):Any = (s,c) match {
    case (s,("pushi", n)::c) => f(n::s, c)
    case (s,("pushd", n)::c) => f(n::s, c)

    case ((a:Int)::(b:Int)::s,"addi"::c) => f((a+b)::s, c)
    case ((a:Double)::(b:Double)::s,"addd"::c) => f((a+b)::s, c)

    case ((a:Int)::(b:Int)::s,"subi"::c) => f((a-b)::s, c)
    case ((a:Double)::(b:Double)::s,"subd"::c) => f((a-b)::s, c)

    case ((a:Int)::(b:Int)::s,"muli"::c) => f((a*b)::s, c)
    case ((a:Double)::(b:Double)::s,"muld"::c) => f((a*b)::s, c)

    case ((a:Int)::(b:Int)::s,"divi"::c) => f((a/b)::s, c)
    case ((a:Double)::(b:Double)::s,"divd"::c) => f((a/b)::s, c)

    case ((a:Int)::s,"casti2d"::c) => f(a.toDouble::s, c)
    case (s::ss,_) => s
    case (List(),_) => ()
  }
}