Jan. 26th, 2017

Sympl

Jan. 26th, 2017 03:15 am
xacid: (Default)
Read more... )
  trait ExprAst[F[_]] extends Expr[F ~> Id] {
    def alg[A](f: F[A]): Expr[A]
    object ExprAst {
      type Ast = F ~> Id
      case class Oper(a: Ast, f: String, b: Ast) extends Ast {
        def apply[A](fa: F[A]): Id[A] = alg(fa).oper(a(fa), f, b(fa))
      }
      case class Parens(a: Ast) extends Ast {
        def apply[A](fa: F[A]): Id[A] = alg(fa).parens(a(fa))
      }
    }
    import ExprAst._
    def parens(a: Ast): Ast = Parens(a)
    def oper(a: Ast, f: String, b: Ast): Ast = (a, f) match {
      case (Oper(a1, op1@("+" | "-"), b1), op2@("*" | "/")) =>
        oper(a1, op1, oper(b1, op2, b))
      case _ => Oper(a, f, b)
    }
  }

  trait ExprParser[F[_]] extends ExprAst[F] with AParser {
    self: Parser =>
    import ExprAst._
    def operChar = CharPredicate('~', '!', '@', '#', '$', '%',
      'ˆ', '&', '*', '-', '+', '/', '?', '<', '>', ':', '.', ',')
    def Parens(p: => Rule1[Ast]): Rule1[Ast] =
      rule('(' ~ sp ~ p ~ sp ~ ')' ~> (parens _))
    def Oper: Rule1[String] = rule(atomic(capture(operChar.+)))
    def Expr: Rule1[Ast] = rule(Operand ~
      oneOrMore(sp ~ Oper ~ sp ~ Operand ~> (oper _)))
    def Operand: Rule1[Ast]
  }
Read more... )

Profile

xacid: (Default)
xacid

August 2017

S M T W T F S
  123 45
67891011 12
13 141516171819
20212223242526
2728293031  

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 19th, 2017 05:04 pm
Powered by Dreamwidth Studios