Skip to content

Instantly share code, notes, and snippets.

@adamdoupe

adamdoupe/ast.scala

Created Oct 24, 2012
Embed
What would you like to do?
Generating AST for CS260 F12
import org.scalacheck._
import org.scalacheck.Prop._
import pllab.lwnn._
import pllab.lwnn.syntax._
object AbstractStateSpecification extends Properties("States") {
import Prop.forAll
import Gen._
import Arbitrary.arbitrary
def genVar = for {
name <- oneOf("x", "y", "z", "adam")
} yield Var(name)
def genVars = Gen.containerOf1[List,Var](genVar)
def genAST (depth: Int = 1): Gen[Stmt] = for {
vars <- genVars
sequence <- genSeq(depth)
} yield Decl(vars, Seq(sequence))
def genSeq (depth: Int = 1) = Gen.containerOf1[List,Stmt](genStatement(depth))
def genRange = for {
a <- Gen.choose(-1000, 1000)
b <- Gen.choose(a, a+1000)
} yield Range(a, b)
def genBinOp (depth: Int = 1) = for {
bop <- genBop
e1 <- genExpression(depth)
e2 <- genExpression(depth)
} yield Binop(bop, e1, e2)
def genStatement (depth: Int = 1): Gen[Stmt] = if (depth <= 1) oneOf(genVar, genRange) else oneOf(genIf(depth-1), genWhile(depth-1), genAssign(depth-1), genRange, genVar, genBinOp(depth-1))
def genExpression (depth: Int = 1) : Gen[Exp] = if (depth <= 1) oneOf(genVar, genRange) else oneOf(genRange, genVar, genBinOp(depth-1))
def genAssign(depth: Int = 1) = for {
theVar <- genVar
exp <- genExpression(depth)
} yield Assign(theVar, exp)
def genWhile(depth: Int = 1) = for {
exp <- genExpression(depth)
seq <- genSeq(depth)
} yield While(exp, Seq(seq))
def genIf (depth: Int = 1) = for {
exp <- genExpression(depth)
trueBranch <- genSeq(depth)
falseBranch <- genSeq(depth)
} yield If(exp, Seq(trueBranch), Seq(falseBranch))
def genBop = oneOf(⌜+⌝, ⌜−⌝, ⌜×⌝, ⌜÷⌝, ⌜=⌝, ⌜≠⌝, ⌜<⌝, ⌜≤⌝, ⌜∧⌝, ⌜∨⌝)
property("always terminates mod") = forAll(genAST(2)){ ast: Stmt {
val mop = calculateMOPforAST( ast, Mod )
true
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment