|
| 1 | +import quoted._ |
| 2 | + |
| 3 | +object Macros { |
| 4 | + |
| 5 | + inline def theTestBlock : Unit = ${ theTestBlockImpl } |
| 6 | + |
| 7 | + def theTestBlockImpl(given qctx: QuoteContext) : Expr[Unit] = { |
| 8 | + import qctx.tasty.{_,given} |
| 9 | + |
| 10 | + // simple smoke test |
| 11 | + val sym1 : Symbol = Symbol.newMethod( |
| 12 | + rootContext.owner, |
| 13 | + "sym1", |
| 14 | + MethodType(List("a","b"))( |
| 15 | + _ => List(typeOf[Int], typeOf[Int]), |
| 16 | + _ => typeOf[Int])) |
| 17 | + assert(sym1.isDefDef) |
| 18 | + assert(sym1.name == "sym1") |
| 19 | + val sym1Statements : List[Statement] = List( |
| 20 | + DefDef(sym1, { |
| 21 | + case List() => { |
| 22 | + case List(List(a, b)) => |
| 23 | + Some('{ ${ a.seal.asInstanceOf[Expr[Int]] } - ${ b.seal.asInstanceOf[Expr[Int]] } }.unseal) |
| 24 | + } |
| 25 | + }), |
| 26 | + '{ assert(${ Apply(Ref(sym1), List(Literal(Constant(2)), Literal(Constant(3)))).seal.asInstanceOf[Expr[Int]] } == -1) }.unseal) |
| 27 | + |
| 28 | + // test for no argument list (no Apply node) |
| 29 | + val sym2 : Symbol = Symbol.newMethod( |
| 30 | + rootContext.owner, |
| 31 | + "sym2", |
| 32 | + ByNameType(typeOf[Int])) |
| 33 | + assert(sym2.isDefDef) |
| 34 | + assert(sym2.name == "sym2") |
| 35 | + val sym2Statements : List[Statement] = List( |
| 36 | + DefDef(sym2, { |
| 37 | + case List() => { |
| 38 | + case List() => |
| 39 | + Some(Literal(Constant(2))) |
| 40 | + } |
| 41 | + }), |
| 42 | + '{ assert(${ Ref(sym2).seal.asInstanceOf[Expr[Int]] } == 2) }.unseal) |
| 43 | + |
| 44 | + // test for multiple argument lists |
| 45 | + val sym3 : Symbol = Symbol.newMethod( |
| 46 | + rootContext.owner, |
| 47 | + "sym3", |
| 48 | + MethodType(List("a"))( |
| 49 | + _ => List(typeOf[Int]), |
| 50 | + mt => MethodType(List("b"))( |
| 51 | + _ => List(mt.param(0)), |
| 52 | + _ => mt.param(0)))) |
| 53 | + assert(sym3.isDefDef) |
| 54 | + assert(sym3.name == "sym3") |
| 55 | + val sym3Statements : List[Statement] = List( |
| 56 | + DefDef(sym3, { |
| 57 | + case List() => { |
| 58 | + case List(List(a), List(b)) => |
| 59 | + Some(a) |
| 60 | + } |
| 61 | + }), |
| 62 | + '{ assert(${ Apply(Apply(Ref(sym3), List(Literal(Constant(3)))), List(Literal(Constant(3)))).seal.asInstanceOf[Expr[Int]] } == 3) }.unseal) |
| 63 | + |
| 64 | + // test for recursive references |
| 65 | + val sym4 : Symbol = Symbol.newMethod( |
| 66 | + rootContext.owner, |
| 67 | + "sym4", |
| 68 | + MethodType(List("x"))( |
| 69 | + _ => List(typeOf[Int]), |
| 70 | + _ => typeOf[Int])) |
| 71 | + assert(sym4.isDefDef) |
| 72 | + assert(sym4.name == "sym4") |
| 73 | + val sym4Statements : List[Statement] = List( |
| 74 | + DefDef(sym4, { |
| 75 | + case List() => { |
| 76 | + case List(List(x)) => |
| 77 | + Some('{ |
| 78 | + if ${ x.seal.asInstanceOf[Expr[Int]] } == 0 |
| 79 | + then 0 |
| 80 | + else ${ Apply(Ref(sym4), List('{ ${ x.seal.asInstanceOf[Expr[Int]] } - 1 }.unseal)).seal.asInstanceOf[Expr[Int]] } |
| 81 | + }.unseal) |
| 82 | + } |
| 83 | + }), |
| 84 | + '{ assert(${ Apply(Ref(sym4), List(Literal(Constant(4)))).seal.asInstanceOf[Expr[Int]] } == 0) }.unseal) |
| 85 | + |
| 86 | + // test for nested functions (one symbol is the other's parent, and we use a Closure) |
| 87 | + val sym5 : Symbol = Symbol.newMethod( |
| 88 | + rootContext.owner, |
| 89 | + "sym5", |
| 90 | + MethodType(List("x"))( |
| 91 | + _ => List(typeOf[Int]), |
| 92 | + _ => typeOf[Int=>Int])) |
| 93 | + assert(sym5.isDefDef) |
| 94 | + assert(sym5.name == "sym5") |
| 95 | + val sym5Statements : List[Statement] = List( |
| 96 | + DefDef(sym5, { |
| 97 | + case List() => { |
| 98 | + case List(List(x)) => |
| 99 | + Some { |
| 100 | + val sym51 : Symbol = Symbol.newMethod( |
| 101 | + sym5, |
| 102 | + "sym51", |
| 103 | + MethodType(List("x"))( |
| 104 | + _ => List(typeOf[Int]), |
| 105 | + _ => typeOf[Int])) |
| 106 | + Block( |
| 107 | + List( |
| 108 | + DefDef(sym51, { |
| 109 | + case List() => { |
| 110 | + case List(List(xx)) => |
| 111 | + Some('{ ${ x.seal.asInstanceOf[Expr[Int]] } - ${ xx.seal.asInstanceOf[Expr[Int]] } }.unseal) |
| 112 | + } |
| 113 | + })), |
| 114 | + Closure(Ref(sym51), None)) |
| 115 | + } |
| 116 | + } |
| 117 | + }), |
| 118 | + '{ assert(${ Apply(Ref(sym5), List(Literal(Constant(5)))).seal.asInstanceOf[Expr[Int=>Int]] }(4) == 1) }.unseal) |
| 119 | + |
| 120 | + // test mutually recursive definitions |
| 121 | + val sym6_1 : Symbol = Symbol.newMethod( |
| 122 | + rootContext.owner, |
| 123 | + "sym6_1", |
| 124 | + MethodType(List("x"))( |
| 125 | + _ => List(typeOf[Int]), |
| 126 | + _ => typeOf[Int])) |
| 127 | + val sym6_2 : Symbol = Symbol.newMethod( |
| 128 | + rootContext.owner, |
| 129 | + "sym6_2", |
| 130 | + MethodType(List("x"))( |
| 131 | + _ => List(typeOf[Int]), |
| 132 | + _ => typeOf[Int])) |
| 133 | + assert(sym6_1.isDefDef) |
| 134 | + assert(sym6_2.isDefDef) |
| 135 | + assert(sym6_1.name == "sym6_1") |
| 136 | + assert(sym6_2.name == "sym6_2") |
| 137 | + val sym6Statements : List[Statement] = List( |
| 138 | + DefDef(sym6_1, { |
| 139 | + case List() => { |
| 140 | + case List(List(x)) => |
| 141 | + Some { |
| 142 | + '{ |
| 143 | + println(s"sym6_1: ${ ${ x.seal.asInstanceOf[Expr[Int]] } }") |
| 144 | + if ${ x.seal.asInstanceOf[Expr[Int]] } == 0 |
| 145 | + then 0 |
| 146 | + else ${ Apply(Ref(sym6_2), List('{ ${ x.seal.asInstanceOf[Expr[Int]] } - 1 }.unseal)).seal.asInstanceOf[Expr[Int]] } |
| 147 | + }.unseal |
| 148 | + } |
| 149 | + } |
| 150 | + }), |
| 151 | + DefDef(sym6_2, { |
| 152 | + case List() => { |
| 153 | + case List(List(x)) => |
| 154 | + Some { |
| 155 | + '{ |
| 156 | + println(s"sym6_2: ${ ${ x.seal.asInstanceOf[Expr[Int]] } }") |
| 157 | + if ${ x.seal.asInstanceOf[Expr[Int]] } == 0 |
| 158 | + then 0 |
| 159 | + else ${ Apply(Ref(sym6_1), List('{ ${ x.seal.asInstanceOf[Expr[Int]] } - 1 }.unseal)).seal.asInstanceOf[Expr[Int]] } |
| 160 | + }.unseal |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + }), |
| 165 | + '{ assert(${ Apply(Ref(sym6_2), List(Literal(Constant(6)))).seal.asInstanceOf[Expr[Int]] } == 0) }.unseal) |
| 166 | + |
| 167 | + // test polymorphic methods by synthesizing an identity method |
| 168 | + val sym7 : Symbol = Symbol.newMethod( |
| 169 | + rootContext.owner, |
| 170 | + "sym7", |
| 171 | + PolyType(List("T"))( |
| 172 | + tp => List(TypeBounds(typeOf[Nothing], typeOf[Any])), |
| 173 | + tp => MethodType(List("t"))( |
| 174 | + _ => List(tp.param(0)), |
| 175 | + _ => tp.param(0)))) |
| 176 | + assert(sym7.isDefDef) |
| 177 | + assert(sym7.name == "sym7") |
| 178 | + val sym7Statements : List[Statement] = List( |
| 179 | + DefDef(sym7, { |
| 180 | + case List(t) => { |
| 181 | + case List(List(x)) => |
| 182 | + Some(Typed(x, Inferred(t))) |
| 183 | + } |
| 184 | + }), |
| 185 | + '{ assert(${ Apply(TypeApply(Ref(sym7), List(Inferred(typeOf[Int]))), List(Literal(Constant(7)))).seal.asInstanceOf[Expr[Int]] } == 7) }.unseal) |
| 186 | + |
| 187 | + Block( |
| 188 | + sym1Statements ++ |
| 189 | + sym2Statements ++ |
| 190 | + sym3Statements ++ |
| 191 | + sym4Statements ++ |
| 192 | + sym5Statements ++ |
| 193 | + sym6Statements ++ |
| 194 | + sym7Statements ++ |
| 195 | + List('{ println("Ok") }.unseal), |
| 196 | + Literal(Constant(()))).seal.asInstanceOf[Expr[Unit]] |
| 197 | + } |
| 198 | +} |
| 199 | + |
0 commit comments