VarTypes WIP

This commit is contained in:
alari 2021-03-05 18:40:17 +03:00
parent 83a5585035
commit 2276f27389
2 changed files with 36 additions and 7 deletions

View File

@ -2,7 +2,7 @@ package aqua.context
import aqua.context.walker.Walker
import aqua.context.walker.Walker.UnresolvedError
import aqua.interim.ScalarType
import aqua.interim.{ScalarType, Type}
import aqua.parser.lexer.{ArrowDef, ArrowTypeToken, BasicTypeToken, DataTypeToken, Literal, TypeToken, VarLambda}
import aqua.parser.{Block, Extract, FuncExpr}
import cats.{Comonad, Functor}
@ -14,14 +14,15 @@ import cats.syntax.comonad._
import scala.collection.immutable.Queue
case class VarTypes[F[_]](
derived: Map[String, DataTypeToken[F]] = Map.empty[String, DataTypeToken[F]],
vars: Map[String, Type] = Map.empty[String, Type],
errorsQ: Queue[VarTypes.Err[F]] = Queue.empty
) {
def error(err: VarTypes.Err[F]): VarTypes[F] = copy(errorsQ = errorsQ.appended(err))
def errors: List[VarTypes.Err[F]] = errorsQ.toList
def derive(varname: String, dt: DataTypeToken[F]): VarTypes[F] = copy(derived + (varname -> dt))
def resolve(varname: String, t: Type): VarTypes[F] = copy(vars + (varname -> t))
def get(varname: String): Option[Type] = vars.get(varname)
}
object VarTypes {
@ -90,8 +91,19 @@ object VarTypes {
.fold(prev.head.error(ArrowUntyped(c.arrow.unit, c.arrow.name.extract))) { arrowDef =>
val types = getTypes(ectx)
val varTypes =
arrowDef.resType.fold(prev.head)(prev.head.derive(vr.name.extract, _))
val withResultType =
arrowDef.resType.flatMap(types.resolveTypeToken).fold(prev.head)(prev.head.resolve(vr.name.extract, _))
val valueTypes = c.args.map {
case Literal(_, ts) => ts // We want to collect errors with pointers!
case VarLambda(name, Nil) =>
// variable
// or argument
// or arrow
case VarLambda(name, lambda) =>
// variable
// or argument
}
val args = arrowDef.argTypes
@ -132,6 +144,7 @@ object VarTypes {
prev.head
}) :: extend.funcOpCtx(op, prev.tail)
// TODO fetch argument types
override def blockCtx(block: Block[F, I]): Ctx =
VarTypes[F]() :: extend.blockCtx(block)

View File

@ -59,9 +59,19 @@ object LiteralType {
case class ArrayType(element: DataType) extends DataType
case class ProductType(name: String, fields: NonEmptyMap[String, DataType]) extends DataType
case class ArrowType(args: List[DataType], res: Option[DataType]) extends Type
sealed trait CallableType extends Type {
def acceptsValuesOf(valueTypes: List[Type]): Boolean
}
case class FuncArrowType(args: List[(String, Either[ArrowType, DataType])], res: Option[DataType]) extends Type {
case class ArrowType(args: List[DataType], res: Option[DataType]) extends CallableType {
override def acceptsValuesOf(valueTypes: List[Type]): Boolean =
(args.length == valueTypes.length) && args.zip(valueTypes).forall(av => av._1.acceptsValueOf(av._2))
}
case class FuncArrowType(args: List[(String, Either[ArrowType, DataType])], res: Option[DataType])
extends CallableType {
def toArrowType: Option[ArrowType] = {
val dataArgs = args.map(_._2).collect {
@ -69,6 +79,12 @@ case class FuncArrowType(args: List[(String, Either[ArrowType, DataType])], res:
}
Option.when(dataArgs.length == args.length)(ArrowType(dataArgs, res))
}
override def acceptsValuesOf(valueTypes: List[Type]): Boolean =
(args.length == valueTypes.length) && args
.map(_._2)
.zip(valueTypes)
.forall(av => av._1.fold(identity, identity).acceptsValueOf(av._2))
}
object Type {