mirror of
https://github.com/fluencelabs/aqua.git
synced 2024-12-04 22:50:18 +00:00
LiteralType
This commit is contained in:
parent
f197996b33
commit
83a5585035
@ -19,8 +19,8 @@ case class ScalarType private (name: String) extends DataType
|
||||
|
||||
object ScalarType {
|
||||
// TODO https://github.com/fluencelabs/interface-types/blob/master/crates/it-types/src/values.rs#L45-L49
|
||||
val i32 = ScalarType("i32")
|
||||
val i64 = ScalarType("i64")
|
||||
val u32 = ScalarType("u32")
|
||||
val u64 = ScalarType("u64")
|
||||
val s32 = ScalarType("s32")
|
||||
val s64 = ScalarType("s64")
|
||||
val f32 = ScalarType("f32")
|
||||
@ -30,25 +30,32 @@ object ScalarType {
|
||||
|
||||
val float = Set(f32, f64)
|
||||
val signed = float ++ Set(s32, s64)
|
||||
val number = signed ++ Set(i32, i64)
|
||||
val number = signed ++ Set(u32, u64)
|
||||
val all = number ++ Set(bool, string)
|
||||
|
||||
val boolSet = Set(bool)
|
||||
val stringSet = Set(string)
|
||||
|
||||
val scalarOrder: PartialOrder[ScalarType] =
|
||||
PartialOrder.from {
|
||||
case (a, b) if a == b => 0.0
|
||||
case (`i32`, `i64`) => -1.0
|
||||
case (`u32`, `u64`) => -1.0
|
||||
case (`s32`, `s64`) => -1.0
|
||||
case (`f32`, `f64`) => -1.0
|
||||
case (`i64`, `i32`) => 1.0
|
||||
case (`u64`, `u32`) => 1.0
|
||||
case (`s64`, `s32`) => 1.0
|
||||
case (`f64`, `f32`) => 1.0
|
||||
case _ => Double.NaN
|
||||
}
|
||||
}
|
||||
|
||||
case class LiteralType private (oneOf: Set[ScalarType]) extends Type
|
||||
|
||||
object LiteralType {
|
||||
val float = LiteralType(ScalarType.float)
|
||||
val signed = LiteralType(ScalarType.signed)
|
||||
val number = LiteralType(ScalarType.number)
|
||||
val bool = LiteralType(Set(ScalarType.bool))
|
||||
val string = LiteralType(Set(ScalarType.string))
|
||||
}
|
||||
|
||||
case class ArrayType(element: DataType) extends DataType
|
||||
case class ProductType(name: String, fields: NonEmptyMap[String, DataType]) extends DataType
|
||||
|
||||
@ -101,6 +108,10 @@ object Type {
|
||||
else
|
||||
(l, r) match {
|
||||
case (x: ScalarType, y: ScalarType) => ScalarType.scalarOrder.partialCompare(x, y)
|
||||
case (LiteralType(xs), y: ScalarType) if xs == Set(y) => 0.0
|
||||
case (LiteralType(xs), y: ScalarType) if xs(y) => -1.0
|
||||
case (x: ScalarType, LiteralType(ys)) if ys == Set(x) => 0.0
|
||||
case (x: ScalarType, LiteralType(ys)) if ys(x) => 1.0
|
||||
case (x: ArrayType, y: ArrayType) => cmp(x.element, y.element)
|
||||
case (ProductType(_, xFields), ProductType(_, yFields)) =>
|
||||
cmpProd(xFields, yFields)
|
||||
|
@ -1,6 +1,6 @@
|
||||
package aqua.parser.lexer
|
||||
|
||||
import aqua.interim.ScalarType
|
||||
import aqua.interim.LiteralType
|
||||
import aqua.parser.lexer.Token._
|
||||
import aqua.parser.lift.LiftParser
|
||||
import aqua.parser.lift.LiftParser._
|
||||
@ -15,7 +15,7 @@ case class VarLambda[F[_]](name: F[String], lambda: List[LambdaOp[F]] = Nil) ext
|
||||
override def as[T](v: T)(implicit F: Functor[F]): F[T] = name.as(v)
|
||||
}
|
||||
|
||||
case class Literal[F[_]](value: F[String], ts: Set[ScalarType]) extends Value[F] {
|
||||
case class Literal[F[_]](value: F[String], ts: LiteralType) extends Value[F] {
|
||||
override def as[T](v: T)(implicit F: Functor[F]): F[T] = value.as(v)
|
||||
}
|
||||
|
||||
@ -30,25 +30,25 @@ object Value {
|
||||
def bool[F[_]: LiftParser: Functor]: P[Literal[F]] =
|
||||
P.oneOf(
|
||||
("true" :: "false" :: Nil)
|
||||
.map(t ⇒ P.string(t).lift.map(fu => Literal(fu.as(t), Set(ScalarType.bool))))
|
||||
.map(t ⇒ P.string(t).lift.map(fu => Literal(fu.as(t), LiteralType.bool)))
|
||||
)
|
||||
|
||||
def num[F[_]: LiftParser: Comonad]: P[Literal[F]] =
|
||||
(P.char('-').?.with1 ~ Numbers.nonNegativeIntString).lift.map(fu =>
|
||||
fu.extract match {
|
||||
case (Some(_), n) ⇒ Literal(fu.as(s"-$n"), ScalarType.signed)
|
||||
case (None, n) ⇒ Literal(fu.as(n), ScalarType.number)
|
||||
case (Some(_), n) ⇒ Literal(fu.as(s"-$n"), LiteralType.signed)
|
||||
case (None, n) ⇒ Literal(fu.as(n), LiteralType.number)
|
||||
}
|
||||
)
|
||||
|
||||
def float[F[_]: LiftParser]: P[Literal[F]] =
|
||||
(P.char('-').?.with1 ~ (Numbers.nonNegativeIntString <* P.char('.')) ~ Numbers.nonNegativeIntString).string.lift
|
||||
.map(Literal(_, ScalarType.float))
|
||||
.map(Literal(_, LiteralType.float))
|
||||
|
||||
// TODO make more sophisticated escaping/unescaping
|
||||
def string[F[_]: LiftParser]: P[Literal[F]] =
|
||||
(`"` *> P.charsWhile0(_ != '"') <* `"`).string.lift
|
||||
.map(Literal(_, Set(ScalarType.string)))
|
||||
.map(Literal(_, LiteralType.string))
|
||||
|
||||
def literal[F[_]: LiftParser: Comonad]: P[Literal[F]] = P.oneOf(bool :: float.backtrack :: num :: string :: Nil)
|
||||
|
||||
|
@ -58,7 +58,7 @@ class AbilitiesResolveSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
| x <- First.arr()
|
||||
|
|
||||
|
|
||||
|func other(x: i32):
|
||||
|func other(x: u32):
|
||||
| Peer "smth"
|
||||
| y <- Second.arr2(x)
|
||||
| Peer.timestamp()
|
||||
@ -76,7 +76,7 @@ class AbilitiesResolveSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
| y <- Smth.foo()
|
||||
| x <- Ab.f(z, y)
|
||||
|
|
||||
|alias T: i32
|
||||
|alias T: u32
|
||||
|""".stripMargin)
|
||||
|
||||
res.isValid should be(false)
|
||||
|
@ -59,7 +59,7 @@ class ArgsAndVarsSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
| x <- arr()
|
||||
|
|
||||
|
|
||||
|func other(x: i32):
|
||||
|func other(x: u32):
|
||||
| y <- arr2(x)
|
||||
|
|
||||
|""".stripMargin)
|
||||
@ -77,7 +77,7 @@ class ArgsAndVarsSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
|func some():
|
||||
| x <- f(z, y)
|
||||
|
|
||||
|alias T: i32
|
||||
|alias T: u32
|
||||
|""".stripMargin)
|
||||
|
||||
res.isValid should be(false)
|
||||
|
@ -57,7 +57,7 @@ class ArrowsSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
| x <- First.arr()
|
||||
|
|
||||
|
|
||||
|func other(x: i32):
|
||||
|func other(x: u32):
|
||||
| Peer "smth"
|
||||
| y <- Second.arr2(x)
|
||||
| Peer.timestamp()
|
||||
@ -75,7 +75,7 @@ class ArrowsSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
| y <- Smth.foo()
|
||||
| x <- Ab.f(z, y)
|
||||
|
|
||||
|alias T: i32
|
||||
|alias T: u32
|
||||
|""".stripMargin)
|
||||
|
||||
res.isValid should be(false)
|
||||
|
@ -17,34 +17,41 @@ class TypeSpec extends AnyFlatSpec with Matchers {
|
||||
recv >= incoming
|
||||
|
||||
"scalar types" should "be variant" in {
|
||||
accepts(i64, i32) should be(true)
|
||||
(i32: Type) <= i32 should be(true)
|
||||
(i32: Type) >= i32 should be(true)
|
||||
(i32: Type) > i32 should be(false)
|
||||
(i32: Type) > i64 should be(false)
|
||||
(i32: Type) > f64 should be(false)
|
||||
(i64: Type) > i32 should be(true)
|
||||
(i64: Type) >= string should be(false)
|
||||
(i64: Type) <= string should be(false)
|
||||
accepts(u64, u32) should be(true)
|
||||
(u32: Type) <= u32 should be(true)
|
||||
(u32: Type) >= u32 should be(true)
|
||||
(u32: Type) > u32 should be(false)
|
||||
(u32: Type) > u64 should be(false)
|
||||
(u32: Type) > f64 should be(false)
|
||||
(u64: Type) > u32 should be(true)
|
||||
(u64: Type) >= string should be(false)
|
||||
(u64: Type) <= string should be(false)
|
||||
}
|
||||
|
||||
"literal types" should "be accepted by scalars" in {
|
||||
accepts(u64, LiteralType.number) should be(true)
|
||||
accepts(bool, LiteralType.bool) should be(true)
|
||||
accepts(u32, LiteralType.bool) should be(false)
|
||||
accepts(f32, LiteralType.number) should be(true)
|
||||
}
|
||||
|
||||
"arrays of scalars" should "be variant" in {
|
||||
(`[]`(i32): Type) <= i32 should be(false)
|
||||
(`[]`(i32): Type) >= i32 should be(false)
|
||||
(`[]`(i32): Type) <= `[]`(i32) should be(true)
|
||||
(`[]`(i32): Type) <= `[]`(i64) should be(true)
|
||||
(`[]`(i64): Type) <= `[]`(i32) should be(false)
|
||||
(`[]`(i64): Type) >= `[]`(i32) should be(true)
|
||||
(`[]`(i64): Type) > `[]`(i32) should be(true)
|
||||
(`[]`(i64): Type) >= `[]`(bool) should be(false)
|
||||
(`[]`(`[]`(i32)): Type) <= `[]`(i64) should be(false)
|
||||
(`[]`(`[]`(i32)): Type) <= `[]`(`[]`(i64)) should be(true)
|
||||
(`[]`(u32): Type) <= u32 should be(false)
|
||||
(`[]`(u32): Type) >= u32 should be(false)
|
||||
(`[]`(u32): Type) <= `[]`(u32) should be(true)
|
||||
(`[]`(u32): Type) <= `[]`(u64) should be(true)
|
||||
(`[]`(u64): Type) <= `[]`(u32) should be(false)
|
||||
(`[]`(u64): Type) >= `[]`(u32) should be(true)
|
||||
(`[]`(u64): Type) > `[]`(u32) should be(true)
|
||||
(`[]`(u64): Type) >= `[]`(bool) should be(false)
|
||||
(`[]`(`[]`(u32)): Type) <= `[]`(u64) should be(false)
|
||||
(`[]`(`[]`(u32)): Type) <= `[]`(`[]`(u64)) should be(true)
|
||||
}
|
||||
|
||||
"products of scalars" should "be variant" in {
|
||||
val one: Type = ProductType("one", NonEmptyMap.of("field" -> i32))
|
||||
val two: Type = ProductType("two", NonEmptyMap.of("field" -> i64, "other" -> string))
|
||||
val three: Type = ProductType("three", NonEmptyMap.of("field" -> i32))
|
||||
val one: Type = ProductType("one", NonEmptyMap.of("field" -> u32))
|
||||
val two: Type = ProductType("two", NonEmptyMap.of("field" -> u64, "other" -> string))
|
||||
val three: Type = ProductType("three", NonEmptyMap.of("field" -> u32))
|
||||
|
||||
one < two should be(true)
|
||||
two > one should be(true)
|
||||
@ -52,8 +59,8 @@ class TypeSpec extends AnyFlatSpec with Matchers {
|
||||
}
|
||||
|
||||
"arrows" should "be contravariant on arguments" in {
|
||||
val one: Type = ArrowType(i32 :: Nil, None)
|
||||
val two: Type = ArrowType(i64 :: Nil, None)
|
||||
val one: Type = ArrowType(u32 :: Nil, None)
|
||||
val two: Type = ArrowType(u64 :: Nil, None)
|
||||
|
||||
accepts(one, two) should be(true)
|
||||
|
||||
@ -62,8 +69,8 @@ class TypeSpec extends AnyFlatSpec with Matchers {
|
||||
}
|
||||
|
||||
"arrows" should "be variant on results" in {
|
||||
val one: Type = ArrowType(Nil, Some(i64))
|
||||
val two: Type = ArrowType(Nil, Some(i32))
|
||||
val one: Type = ArrowType(Nil, Some(u64))
|
||||
val two: Type = ArrowType(Nil, Some(u32))
|
||||
|
||||
accepts(one, two) should be(true)
|
||||
|
||||
@ -72,10 +79,10 @@ class TypeSpec extends AnyFlatSpec with Matchers {
|
||||
}
|
||||
|
||||
"arrows" should "respect both args and results" in {
|
||||
val one: Type = ArrowType(bool :: f64 :: Nil, Some(i64))
|
||||
val two: Type = ArrowType(bool :: Nil, Some(i64))
|
||||
val three: Type = ArrowType(bool :: f32 :: Nil, Some(i64))
|
||||
val four: Type = ArrowType(bool :: f32 :: Nil, Some(i32))
|
||||
val one: Type = ArrowType(bool :: f64 :: Nil, Some(u64))
|
||||
val two: Type = ArrowType(bool :: Nil, Some(u64))
|
||||
val three: Type = ArrowType(bool :: f32 :: Nil, Some(u64))
|
||||
val four: Type = ArrowType(bool :: f32 :: Nil, Some(u32))
|
||||
|
||||
accepts(one, two) should be(false)
|
||||
accepts(two, one) should be(false)
|
||||
|
@ -9,15 +9,15 @@ import cats.Id
|
||||
class BlockSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
|
||||
"blocks list" should "parse" in {
|
||||
Block.blocks[Id].parseAll("""func some(cb: -> i32):
|
||||
Block.blocks[Id].parseAll("""func some(cb: -> u32):
|
||||
| cb()
|
||||
|
|
||||
|func other(cb: -> i32):
|
||||
|func other(cb: -> u32):
|
||||
| on 22:
|
||||
| cb()
|
||||
| cb()
|
||||
|
|
||||
|func third(cb: -> i32):
|
||||
|func third(cb: -> u32):
|
||||
| on 23:
|
||||
| cb()
|
||||
|
|
||||
|
@ -1,7 +1,7 @@
|
||||
package aqua.parser
|
||||
|
||||
import aqua.interim.ScalarType
|
||||
import aqua.parser.lexer.{Ability, ArrowName, BasicTypeToken, IntoField, Literal, Var, VarLambda}
|
||||
import aqua.interim.{LiteralType, ScalarType}
|
||||
import aqua.parser.lexer.{Ability, ArrowName, IntoField, Literal, Var, VarLambda}
|
||||
import cats.data.NonEmptyList
|
||||
import org.scalatest.EitherValues
|
||||
import org.scalatest.flatspec.AnyFlatSpec
|
||||
@ -93,8 +93,8 @@ class FuncExprSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
"on" should "parse on x: y" in {
|
||||
val fCall = AbilityFuncCall[Id, HNil]("Ab", "func", "Ab.func", Nil, HNil)
|
||||
val extr = Extract[Id, HNil]("x", fCall, HNil)
|
||||
val resl = AbilityId[Id, HNil]("Peer", Literal[Id]("\"some id\"", ScalarType.stringSet), HNil)
|
||||
val call = FuncCall[Id, HNil]("call", Literal[Id]("true", ScalarType.boolSet) :: Nil, HNil)
|
||||
val resl = AbilityId[Id, HNil]("Peer", Literal[Id]("\"some id\"", LiteralType.string), HNil)
|
||||
val call = FuncCall[Id, HNil]("call", Literal[Id]("true", LiteralType.bool) :: Nil, HNil)
|
||||
|
||||
val script = """on peer.id:
|
||||
| x <- Ab.func()
|
||||
@ -127,7 +127,7 @@ class FuncExprSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
| Peer 3""".stripMargin) should be(
|
||||
NonEmptyList.of(
|
||||
Extract[Id, HNil]("x", FuncCall[Id, HNil]("func", Nil, HNil), HNil),
|
||||
AbilityId[Id, HNil]("Peer", Literal[Id]("3", ScalarType.number), HNil)
|
||||
AbilityId[Id, HNil]("Peer", Literal[Id]("3", LiteralType.number), HNil)
|
||||
)
|
||||
)
|
||||
parseBody(""" x <- func()
|
||||
@ -137,7 +137,7 @@ class FuncExprSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
Extract[Id, HNil]("x", FuncCall[Id, HNil]("func", Nil, HNil), HNil),
|
||||
On[Id, HNil](
|
||||
VarLambda[Id]("x"),
|
||||
NonEmptyList.of(AbilityId[Id, HNil]("Peer", Literal[Id]("3", ScalarType.number), HNil)),
|
||||
NonEmptyList.of(AbilityId[Id, HNil]("Peer", Literal[Id]("3", LiteralType.number), HNil)),
|
||||
HNil
|
||||
)
|
||||
)
|
||||
@ -147,7 +147,7 @@ class FuncExprSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
NonEmptyList.of(
|
||||
On[Id, HNil](
|
||||
VarLambda[Id]("x"),
|
||||
NonEmptyList.of(AbilityId[Id, HNil]("Peer", Literal[Id]("3", ScalarType.number), HNil)),
|
||||
NonEmptyList.of(AbilityId[Id, HNil]("Peer", Literal[Id]("3", LiteralType.number), HNil)),
|
||||
HNil
|
||||
)
|
||||
)
|
||||
|
@ -1,6 +1,6 @@
|
||||
package aqua.parser
|
||||
|
||||
import aqua.interim.ScalarType
|
||||
import aqua.interim.{LiteralType, ScalarType}
|
||||
import aqua.parser.lexer.{Ability, ArrowName, ArrowTypeToken, BasicTypeToken, CustomTypeToken, Literal, Var, VarLambda}
|
||||
import cats.data.NonEmptyList
|
||||
import org.scalatest.EitherValues
|
||||
@ -14,7 +14,7 @@ import scala.language.implicitConversions
|
||||
|
||||
class FuncSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
|
||||
import ScalarType.{i32, string}
|
||||
import ScalarType.{string, u32}
|
||||
|
||||
implicit def scToBt(sc: ScalarType): BasicTypeToken[Id] = BasicTypeToken[Id](sc)
|
||||
implicit def strToArrow(str: String): ArrowName[Id] = ArrowName[Id](str)
|
||||
@ -25,53 +25,53 @@ class FuncSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
"getTime",
|
||||
List(
|
||||
("peer", "peer", CustomTypeToken[Id]("PeerId")),
|
||||
("ret", "ret", ArrowTypeToken[Id]((), (i32: BasicTypeToken[Id]) :: Nil, None))
|
||||
("ret", "ret", ArrowTypeToken[Id]((), (u32: BasicTypeToken[Id]) :: Nil, None))
|
||||
),
|
||||
Some(string: BasicTypeToken[Id])
|
||||
)
|
||||
|
||||
"func header" should "parse" in {
|
||||
DefFunc.`funchead`.parseAll("func some()").right.value should be(FuncHead("some", Nil, None))
|
||||
DefFunc.`funchead`.parseAll("func some(peer: i32)").right.value should be(
|
||||
FuncHead[Id]("some", List(("peer", "peer", (i32: BasicTypeToken[Id]))), None)
|
||||
DefFunc.`funchead`.parseAll("func some(peer: u32)").right.value should be(
|
||||
FuncHead[Id]("some", List(("peer", "peer", (u32: BasicTypeToken[Id]))), None)
|
||||
)
|
||||
|
||||
DefFunc.`funchead`.parseAll("func some(peer: PeerId)").right.value should be(
|
||||
FuncHead[Id]("some", List(("peer", "peer", CustomTypeToken[Id]("PeerId"))), None)
|
||||
)
|
||||
DefFunc.`funchead`.parseAll("func some(peer: PeerId, other: i32)").right.value should be(
|
||||
DefFunc.`funchead`.parseAll("func some(peer: PeerId, other: u32)").right.value should be(
|
||||
FuncHead[Id](
|
||||
"some",
|
||||
List(("peer", "peer", CustomTypeToken[Id]("PeerId")), ("other", "other", (i32: BasicTypeToken[Id]))),
|
||||
List(("peer", "peer", CustomTypeToken[Id]("PeerId")), ("other", "other", (u32: BasicTypeToken[Id]))),
|
||||
None
|
||||
)
|
||||
)
|
||||
DefFunc.`funchead`.parseAll("func some(peer: PeerId, other: i32 -> i32)").right.value should be(
|
||||
DefFunc.`funchead`.parseAll("func some(peer: PeerId, other: u32 -> u32)").right.value should be(
|
||||
FuncHead[Id](
|
||||
"some",
|
||||
List(
|
||||
("peer", "peer", CustomTypeToken[Id]("PeerId")),
|
||||
("other", "other", ArrowTypeToken[Id]((), (i32: BasicTypeToken[Id]) :: Nil, Some(i32: BasicTypeToken[Id])))
|
||||
("other", "other", ArrowTypeToken[Id]((), (u32: BasicTypeToken[Id]) :: Nil, Some(u32: BasicTypeToken[Id])))
|
||||
),
|
||||
None
|
||||
)
|
||||
)
|
||||
|
||||
DefFunc.`funchead`.parseAll("func getTime(peer: PeerId, ret: i32 -> ()) -> string").right.value should be(
|
||||
DefFunc.`funchead`.parseAll("func getTime(peer: PeerId, ret: u32 -> ()) -> string").right.value should be(
|
||||
getTimeHead
|
||||
)
|
||||
}
|
||||
|
||||
"function" should "parse single line fn" in {
|
||||
val func =
|
||||
"""func getTime(peer: PeerId, ret: i32 -> ()) -> string:
|
||||
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
|
||||
| ret(43)""".stripMargin
|
||||
|
||||
DefFunc.`deffunc`.parseAll(func).right.value should be(
|
||||
DefFunc[Id, HNil](
|
||||
getTimeHead,
|
||||
NonEmptyList.of(
|
||||
FuncCall[Id, HNil]("ret", Literal[Id]("43", ScalarType.number) :: Nil, HNil)
|
||||
FuncCall[Id, HNil]("ret", Literal[Id]("43", LiteralType.number) :: Nil, HNil)
|
||||
),
|
||||
HNil
|
||||
)
|
||||
@ -80,7 +80,7 @@ class FuncSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
|
||||
"function" should "parse getTime as a whole" in {
|
||||
val func =
|
||||
"""func getTime(peer: PeerId, ret: i32 -> ()) -> string:
|
||||
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
|
||||
| on peer:
|
||||
| Peer "peer"
|
||||
| t <- Peer.timestamp()
|
||||
@ -93,7 +93,7 @@ class FuncSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
On[Id, HNil](
|
||||
VarLambda[Id]("peer", Nil),
|
||||
NonEmptyList.of(
|
||||
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", ScalarType.stringSet), HNil),
|
||||
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", LiteralType.string), HNil),
|
||||
Extract[Id, HNil]("t", AbilityFuncCall[Id, HNil]("Peer", "timestamp", "Peer.timestamp", Nil, HNil), HNil)
|
||||
),
|
||||
HNil
|
||||
@ -107,7 +107,7 @@ class FuncSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
|
||||
"function" should "parse getTime with no return" in {
|
||||
val func =
|
||||
"""func getTime(peer: PeerId, ret: i32 -> ()) -> string:
|
||||
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
|
||||
| on peer:
|
||||
| Peer "peer"
|
||||
| t <- Peer.timestamp()""".stripMargin
|
||||
@ -119,7 +119,7 @@ class FuncSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
On[Id, HNil](
|
||||
VarLambda[Id]("peer", Nil),
|
||||
NonEmptyList.of(
|
||||
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", ScalarType.stringSet), HNil),
|
||||
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", LiteralType.string), HNil),
|
||||
Extract[Id, HNil]("t", AbilityFuncCall[Id, HNil]("Peer", "timestamp", "Peer.timestamp", Nil, HNil), HNil)
|
||||
),
|
||||
HNil
|
||||
|
@ -11,12 +11,12 @@ import scala.language.implicitConversions
|
||||
|
||||
class TypeSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
|
||||
import aqua.interim.ScalarType.i32
|
||||
import aqua.interim.ScalarType.u32
|
||||
|
||||
implicit def strToBt(st: ScalarType): BasicTypeToken[Id] = BasicTypeToken[Id](st)
|
||||
|
||||
"Basic type" should "parse" in {
|
||||
BasicTypeToken.`basictypedef`.parseAll("i32").right.value should be(i32: BasicTypeToken[Id])
|
||||
BasicTypeToken.`basictypedef`.parseAll("u32").right.value should be(u32: BasicTypeToken[Id])
|
||||
BasicTypeToken.`basictypedef`.parseAll("()") should be('left)
|
||||
}
|
||||
|
||||
@ -28,23 +28,23 @@ class TypeSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
TypeToken.`arrowdef`.parseAll("A -> B").right.value should be(
|
||||
ArrowTypeToken[Id]((), CustomTypeToken[Id]("A") :: Nil, Some(CustomTypeToken[Id]("B")))
|
||||
)
|
||||
TypeToken.`arrowdef`.parseAll("i32 -> Boo").right.value should be(
|
||||
ArrowTypeToken[Id]((), (i32: BasicTypeToken[Id]) :: Nil, Some(CustomTypeToken[Id]("Boo")))
|
||||
TypeToken.`arrowdef`.parseAll("u32 -> Boo").right.value should be(
|
||||
ArrowTypeToken[Id]((), (u32: BasicTypeToken[Id]) :: Nil, Some(CustomTypeToken[Id]("Boo")))
|
||||
)
|
||||
TypeToken.`typedef`.parseAll("i32 -> ()").right.value should be(
|
||||
ArrowTypeToken[Id]((), (i32: BasicTypeToken[Id]) :: Nil, None)
|
||||
TypeToken.`typedef`.parseAll("u32 -> ()").right.value should be(
|
||||
ArrowTypeToken[Id]((), (u32: BasicTypeToken[Id]) :: Nil, None)
|
||||
)
|
||||
TypeToken.`arrowdef`.parseAll("A, i32 -> B").right.value should be(
|
||||
TypeToken.`arrowdef`.parseAll("A, u32 -> B").right.value should be(
|
||||
ArrowTypeToken[Id](
|
||||
(),
|
||||
CustomTypeToken[Id]("A") :: (i32: BasicTypeToken[Id]) :: Nil,
|
||||
CustomTypeToken[Id]("A") :: (u32: BasicTypeToken[Id]) :: Nil,
|
||||
Some(CustomTypeToken[Id]("B"))
|
||||
)
|
||||
)
|
||||
TypeToken.`arrowdef`.parseAll("[]Absolutely, i32 -> B").right.value should be(
|
||||
TypeToken.`arrowdef`.parseAll("[]Absolutely, u32 -> B").right.value should be(
|
||||
ArrowTypeToken[Id](
|
||||
(),
|
||||
ArrayTypeToken(CustomTypeToken[Id]("Absolutely")) :: (i32: BasicTypeToken[Id]) :: Nil,
|
||||
ArrayTypeToken(CustomTypeToken[Id]("Absolutely")) :: (u32: BasicTypeToken[Id]) :: Nil,
|
||||
Some(CustomTypeToken[Id]("B"))
|
||||
)
|
||||
)
|
||||
@ -53,9 +53,9 @@ class TypeSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
|
||||
"Array type" should "parse" in {
|
||||
TypeToken.`typedef`.parseAll("[]Something") should be(Right(ArrayTypeToken(CustomTypeToken[Id]("Something"))))
|
||||
TypeToken.`typedef`.parseAll("[]i32") should be(Right(ArrayTypeToken(i32: BasicTypeToken[Id])))
|
||||
TypeToken.`typedef`.parseAll("[][]i32") should be(
|
||||
Right(ArrayTypeToken[Id](ArrayTypeToken[Id](i32: BasicTypeToken[Id])))
|
||||
TypeToken.`typedef`.parseAll("[]u32") should be(Right(ArrayTypeToken(u32: BasicTypeToken[Id])))
|
||||
TypeToken.`typedef`.parseAll("[][]u32") should be(
|
||||
Right(ArrayTypeToken[Id](ArrayTypeToken[Id](u32: BasicTypeToken[Id])))
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package aqua.parser.lexer
|
||||
|
||||
import aqua.interim.ScalarType
|
||||
import aqua.interim.{LiteralType, ScalarType}
|
||||
import org.scalatest.EitherValues
|
||||
import org.scalatest.flatspec.AnyFlatSpec
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
@ -17,27 +17,27 @@ class ValueSpec extends AnyFlatSpec with Matchers with EitherValues {
|
||||
}
|
||||
|
||||
"literals" should "parse" in {
|
||||
Value.`value`.parseAll("true").right.value should be(Literal("true", ScalarType.boolSet))
|
||||
Value.`value`.parseAll("false").right.value should be(Literal("false", ScalarType.boolSet))
|
||||
Value.`value`.parseAll("true").right.value should be(Literal("true", LiteralType.bool))
|
||||
Value.`value`.parseAll("false").right.value should be(Literal("false", LiteralType.bool))
|
||||
|
||||
Value.`value`.parseAll("1").right.value should be(Literal("1", ScalarType.number))
|
||||
Value.`value`.parseAll("1111").right.value should be(Literal("1111", ScalarType.number))
|
||||
Value.`value`.parseAll("1").right.value should be(Literal("1", LiteralType.number))
|
||||
Value.`value`.parseAll("1111").right.value should be(Literal("1111", LiteralType.number))
|
||||
|
||||
Value.`value`.parseAll("-1543").right.value should be(Literal("-1543", ScalarType.signed))
|
||||
Value.`value`.parseAll("-1543").right.value should be(Literal("-1543", LiteralType.signed))
|
||||
|
||||
Value.`value`.parseAll("1.0").right.value should be(Literal("1.0", ScalarType.float))
|
||||
Value.`value`.parseAll("1.23").right.value should be(Literal("1.23", ScalarType.float))
|
||||
Value.`value`.parseAll("-1.23").right.value should be(Literal("-1.23", ScalarType.float))
|
||||
Value.`value`.parseAll("1.0").right.value should be(Literal("1.0", LiteralType.float))
|
||||
Value.`value`.parseAll("1.23").right.value should be(Literal("1.23", LiteralType.float))
|
||||
Value.`value`.parseAll("-1.23").right.value should be(Literal("-1.23", LiteralType.float))
|
||||
|
||||
Value.`value`.parseAll("\"some crazy string\"").right.value should be(
|
||||
Literal("\"some crazy string\"", Set(ScalarType.string))
|
||||
Literal("\"some crazy string\"", LiteralType.string)
|
||||
)
|
||||
// This does not work :(
|
||||
// Value.`value`.parseAll("\"some crazy string with escaped \\\" quote\"").right.value should be(
|
||||
// Literal("\"some crazy string with escaped \\\" quote\"", BasicType.string)
|
||||
// )
|
||||
Value.`value`.parse("\"just string\" ").right.value should be(
|
||||
(" ", Literal("\"just string\"", Set(ScalarType.string)))
|
||||
(" ", Literal("\"just string\"", LiteralType.string))
|
||||
)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user