feat: type-checker returns micro wacc

This commit is contained in:
Gleb Koval 2025-02-17 15:26:32 +00:00
parent b7e442b269
commit 27cc25cc0d
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
3 changed files with 259 additions and 115 deletions

View File

@ -19,7 +19,7 @@ object microWacc {
extends Expr(identTy) extends Expr(identTy)
with CallTarget(identTy) with CallTarget(identTy)
with LValue with LValue
case class ArrayElem(name: Ident, indices: NonEmptyList[Expr])(ty: SemType) case class ArrayElem(value: LValue, indices: NonEmptyList[Expr])(ty: SemType)
extends Expr(ty) extends Expr(ty)
with LValue with LValue
@ -48,6 +48,23 @@ object microWacc {
case And case And
case Or case Or
} }
object BinaryOperator {
def fromAst(op: ast.BinaryOp): BinaryOperator = op match {
case _: ast.Add => Add
case _: ast.Sub => Sub
case _: ast.Mul => Mul
case _: ast.Div => Div
case _: ast.Mod => Mod
case _: ast.Greater => Greater
case _: ast.GreaterEq => GreaterEq
case _: ast.Less => Less
case _: ast.LessEq => LessEq
case _: ast.Eq => Eq
case _: ast.Neq => Neq
case _: ast.And => And
case _: ast.Or => Or
}
}
// Statements // Statements
sealed trait Stmt sealed trait Stmt

View File

@ -2,6 +2,7 @@ package wacc
import cats.syntax.all._ import cats.syntax.all._
import scala.collection.mutable import scala.collection.mutable
import cats.data.NonEmptyList
object typeChecker { object typeChecker {
import wacc.types._ import wacc.types._
@ -100,17 +101,26 @@ object typeChecker {
*/ */
def check(prog: ast.Program)(using def check(prog: ast.Program)(using
ctx: TypeCheckerCtx ctx: TypeCheckerCtx
): Unit = { ): microWacc.Program =
// Ignore function syntax types for return value and params, since those have been converted microWacc.Program(
// to SemTypes by the renamer. // Ignore function syntax types for return value and params, since those have been converted
prog.funcs.foreach { case ast.FuncDecl(_, name, _, stmts) => // to SemTypes by the renamer.
val FuncType(retType, _) = ctx.funcType(name) prog.funcs.map { case ast.FuncDecl(_, name, params, stmts) =>
stmts.toList.foreach( val FuncType(retType, paramTypes) = ctx.funcType(name)
checkStmt(_, Constraint.Is(retType, s"function ${name.v} must return $retType")) microWacc.FuncDecl(
) microWacc.Ident(name.v, name.uid)(retType),
} params.zip(paramTypes).map { case (ast.Param(_, ident), ty) =>
prog.main.toList.foreach(checkStmt(_, Constraint.Never("main function must not return"))) microWacc.Ident(ident.v, name.uid)(ty)
} },
stmts.toList
.flatMap(
checkStmt(_, Constraint.Is(retType, s"function ${name.v} must return $retType"))
)
)
},
prog.main.toList
.flatMap(checkStmt(_, Constraint.Never("main function must not return")))
)
/** Type-check an AST statement node. /** Type-check an AST statement node.
* *
@ -121,32 +131,38 @@ object typeChecker {
*/ */
private def checkStmt(stmt: ast.Stmt, returnConstraint: Constraint)(using private def checkStmt(stmt: ast.Stmt, returnConstraint: Constraint)(using
ctx: TypeCheckerCtx ctx: TypeCheckerCtx
): Unit = stmt match { ): List[microWacc.Stmt] = stmt match {
// Ignore the type of the variable, since it has been converted to a SemType by the renamer. // Ignore the type of the variable, since it has been converted to a SemType by the renamer.
case ast.VarDecl(_, name, value) => case ast.VarDecl(_, name, value) =>
val expectedTy = ctx.typeOf(name) val expectedTy = ctx.typeOf(name)
checkValue( val typedValue = checkValue(
value, value,
Constraint.Is( Constraint.Is(
expectedTy, expectedTy,
s"variable ${name.v} must be assigned a value of type $expectedTy" s"variable ${name.v} must be assigned a value of type $expectedTy"
) )
) )
List(microWacc.Assign(microWacc.Ident(name.v, name.uid)(expectedTy), typedValue))
case ast.Assign(lhs, rhs) => case ast.Assign(lhs, rhs) =>
val lhsTy = checkValue(lhs, Constraint.Unconstrained) val lhsTyped = checkLValue(lhs, Constraint.Unconstrained)
(lhsTy, checkValue(rhs, Constraint.Is(lhsTy, s"assignment must have type $lhsTy"))) match { val rhsTyped =
checkValue(rhs, Constraint.Is(lhsTyped.ty, s"assignment must have type ${lhsTyped.ty}"))
(lhsTyped.ty, rhsTyped.ty) match {
case (?, ?) => case (?, ?) =>
ctx.error( ctx.error(
Error.SemanticError(lhs.pos, "assignment with both sides of unknown type is illegal") Error.SemanticError(lhs.pos, "assignment with both sides of unknown type is illegal")
) )
case _ => () case _ => ()
} }
List(microWacc.Assign(lhsTyped, rhsTyped))
case ast.Read(dest) => case ast.Read(dest) =>
checkValue(dest, Constraint.Unconstrained) match { val destTyped = checkLValue(dest, Constraint.Unconstrained)
val destTy = destTyped.ty match {
case ? => case ? =>
ctx.error( ctx.error(
Error.SemanticError(dest.pos, "cannot read into a destination with an unknown type") Error.SemanticError(dest.pos, "cannot read into a destination with an unknown type")
) )
?
case destTy => case destTy =>
destTy.satisfies( destTy.satisfies(
Constraint.IsEither( Constraint.IsEither(
@ -157,32 +173,69 @@ object typeChecker {
dest.pos dest.pos
) )
} }
List(
microWacc.Assign(
destTyped,
microWacc.Call(
destTy match {
case KnownType.Int => microWacc.Builtin.ReadInt
case KnownType.Char => microWacc.Builtin.ReadChar
case _ => microWacc.Builtin.ReadInt // we'll stop due to error anyway
},
Nil
)
)
)
case ast.Free(lhs) => case ast.Free(lhs) =>
checkValue( List(
lhs, microWacc.Call(
Constraint.IsEither( microWacc.Builtin.Free,
KnownType.Array(?), List(
KnownType.Pair(?, ?), checkValue(
"free must be applied to an array or pair" lhs,
Constraint.IsEither(
KnownType.Array(?),
KnownType.Pair(?, ?),
"free must be applied to an array or pair"
)
)
)
) )
) )
case ast.Return(expr) => case ast.Return(expr) =>
checkValue(expr, returnConstraint) List(microWacc.Return(checkValue(expr, returnConstraint)))
case ast.Exit(expr) => case ast.Exit(expr) =>
checkValue(expr, Constraint.Is(KnownType.Int, "exit value must be int")) List(
case ast.Print(expr, _) => microWacc.Call(
microWacc.Builtin.Exit,
List(checkValue(expr, Constraint.Is(KnownType.Int, "exit value must be int")))
)
)
case ast.Print(expr, newline) =>
// This constraint should never fail, the scope-checker should have caught it already // This constraint should never fail, the scope-checker should have caught it already
checkValue(expr, Constraint.Unconstrained) List(
microWacc.Call(
if newline then microWacc.Builtin.Println else microWacc.Builtin.Print,
List(checkValue(expr, Constraint.Unconstrained))
)
)
case ast.If(cond, thenStmt, elseStmt) => case ast.If(cond, thenStmt, elseStmt) =>
checkValue(cond, Constraint.Is(KnownType.Bool, "if condition must be a bool")) List(
thenStmt.toList.foreach(checkStmt(_, returnConstraint)) microWacc.If(
elseStmt.toList.foreach(checkStmt(_, returnConstraint)) checkValue(cond, Constraint.Is(KnownType.Bool, "if condition must be a bool")),
thenStmt.toList.flatMap(checkStmt(_, returnConstraint)),
elseStmt.toList.flatMap(checkStmt(_, returnConstraint))
)
)
case ast.While(cond, body) => case ast.While(cond, body) =>
checkValue(cond, Constraint.Is(KnownType.Bool, "while condition must be a bool")) List(
body.toList.foreach(checkStmt(_, returnConstraint)) microWacc.While(
case ast.Block(body) => checkValue(cond, Constraint.Is(KnownType.Bool, "while condition must be a bool")),
body.toList.foreach(checkStmt(_, returnConstraint)) body.toList.flatMap(checkStmt(_, returnConstraint))
case ast.Skip() => () )
)
case ast.Block(body) => body.toList.flatMap(checkStmt(_, returnConstraint))
case skip @ ast.Skip() => List.empty
} }
/** Type-check an AST LValue, RValue or Expr node. This function does all 3 since these traits /** Type-check an AST LValue, RValue or Expr node. This function does all 3 since these traits
@ -197,47 +250,42 @@ object typeChecker {
*/ */
private def checkValue(value: ast.LValue | ast.RValue | ast.Expr, constraint: Constraint)(using private def checkValue(value: ast.LValue | ast.RValue | ast.Expr, constraint: Constraint)(using
ctx: TypeCheckerCtx ctx: TypeCheckerCtx
): SemType = value match { ): microWacc.Expr = value match {
case l @ ast.IntLiter(_) => KnownType.Int.satisfies(constraint, l.pos) case l @ ast.IntLiter(v) =>
case l @ ast.BoolLiter(_) => KnownType.Bool.satisfies(constraint, l.pos) KnownType.Int.satisfies(constraint, l.pos)
case l @ ast.CharLiter(_) => KnownType.Char.satisfies(constraint, l.pos) microWacc.IntLiter(v)
case l @ ast.StrLiter(_) => KnownType.String.satisfies(constraint, l.pos) case l @ ast.BoolLiter(v) =>
case l @ ast.PairLiter() => KnownType.Pair(?, ?).satisfies(constraint, l.pos) KnownType.Bool.satisfies(constraint, l.pos)
case id: ast.Ident => microWacc.BoolLiter(v)
ctx.typeOf(id).satisfies(constraint, id.pos) case l @ ast.CharLiter(v) =>
case ast.ArrayElem(id, indices) => KnownType.Char.satisfies(constraint, l.pos)
val arrayTy = ctx.typeOf(id) microWacc.CharLiter(v)
val elemTy = indices.foldLeftM(arrayTy) { (acc, elem) => case l @ ast.StrLiter(v) =>
checkValue(elem, Constraint.Is(KnownType.Int, "array index must be an int")) KnownType.String.satisfies(constraint, l.pos)
acc match { microWacc.ArrayLiter(v.map(microWacc.CharLiter(_)).toList)(KnownType.String)
case KnownType.Array(innerTy) => Some(innerTy) case l @ ast.PairLiter() =>
case ? => Some(?) // we can keep indexing an unknown type microWacc.NullLiter()(KnownType.Pair(?, ?).satisfies(constraint, l.pos))
case nonArrayTy =>
ctx.error(
Error.TypeMismatch(elem.pos, KnownType.Array(?), acc, "cannot index into a non-array")
)
None
}
}
elemTy.getOrElse(?).satisfies(constraint, id.pos)
case ast.Parens(expr) => checkValue(expr, constraint) case ast.Parens(expr) => checkValue(expr, constraint)
case l @ ast.ArrayLiter(elems) => case l @ ast.ArrayLiter(elems) =>
KnownType val (elemTy, elemsTyped) = elems.mapAccumulate[SemType, microWacc.Expr](?) {
// Start with an unknown param type, make it more specific while checking the elements. case (acc, elem) =>
.Array(elems.foldLeft[SemType](?) { case (acc, elem) => val elemTyped = checkValue(
checkValue(
elem, elem,
Constraint.IsSymmetricCompatible(acc, s"array elements must have the same type") Constraint.IsSymmetricCompatible(acc, s"array elements must have the same type")
) )
}) (elemTyped.ty, elemTyped)
}
val arrayTy = KnownType
// Start with an unknown param type, make it more specific while checking the elements.
.Array(elemTy)
.satisfies(constraint, l.pos) .satisfies(constraint, l.pos)
microWacc.ArrayLiter(elemsTyped)(arrayTy)
case l @ ast.NewPair(fst, snd) => case l @ ast.NewPair(fst, snd) =>
KnownType val fstTyped = checkValue(fst, Constraint.Unconstrained)
.Pair( val sndTyped = checkValue(snd, Constraint.Unconstrained)
checkValue(fst, Constraint.Unconstrained), microWacc.ArrayLiter(List(fstTyped, sndTyped))(
checkValue(snd, Constraint.Unconstrained) KnownType.Pair(fstTyped.ty, sndTyped.ty).satisfies(constraint, l.pos)
) )
.satisfies(constraint, l.pos)
case ast.Call(id, args) => case ast.Call(id, args) =>
val funcTy @ FuncType(retTy, paramTys) = ctx.funcType(id) val funcTy @ FuncType(retTy, paramTys) = ctx.funcType(id)
if (args.length != paramTys.length) { if (args.length != paramTys.length) {
@ -245,76 +293,152 @@ object typeChecker {
} }
// Even if the number of arguments is wrong, we still check the types of the arguments // Even if the number of arguments is wrong, we still check the types of the arguments
// in the best way we can (by taking a zip). // in the best way we can (by taking a zip).
args.zip(paramTys).foreach { case (arg, paramTy) => val argsTyped = args.zip(paramTys).map { case (arg, paramTy) =>
checkValue(arg, Constraint.Is(paramTy, s"argument type mismatch in function ${id.v}")) checkValue(arg, Constraint.Is(paramTy, s"argument type mismatch in function ${id.v}"))
} }
retTy.satisfies(constraint, id.pos) microWacc.Call(microWacc.Ident(id.v, id.uid)(retTy.satisfies(constraint, id.pos)), argsTyped)
case ast.Fst(elem) =>
checkValue(
elem,
Constraint.Is(KnownType.Pair(?, ?), "fst must be applied to a pair")
) match {
case what @ KnownType.Pair(left, _) =>
left.satisfies(constraint, elem.pos)
case _ => ctx.error(Error.InternalError(elem.pos, "fst must be applied to a pair"))
}
case ast.Snd(elem) =>
checkValue(
elem,
Constraint.Is(KnownType.Pair(?, ?), "snd must be applied to a pair")
) match {
case KnownType.Pair(_, right) => right.satisfies(constraint, elem.pos)
case _ => ctx.error(Error.InternalError(elem.pos, "snd must be applied to a pair"))
}
// Unary operators // Unary operators
case ast.Negate(x) => case ast.Negate(x) =>
checkValue(x, Constraint.Is(KnownType.Int, "negation must be applied to an int")) microWacc.UnaryOp(
KnownType.Int.satisfies(constraint, x.pos) checkValue(x, Constraint.Is(KnownType.Int, "negation must be applied to an int")),
microWacc.UnaryOperator.Negate
)(KnownType.Int.satisfies(constraint, x.pos))
case ast.Not(x) => case ast.Not(x) =>
checkValue(x, Constraint.Is(KnownType.Bool, "logical not must be applied to a bool")) microWacc.UnaryOp(
KnownType.Bool.satisfies(constraint, x.pos) checkValue(x, Constraint.Is(KnownType.Bool, "logical not must be applied to a bool")),
microWacc.UnaryOperator.Not
)(KnownType.Bool.satisfies(constraint, x.pos))
case ast.Len(x) => case ast.Len(x) =>
checkValue(x, Constraint.Is(KnownType.Array(?), "len must be applied to an array")) microWacc.UnaryOp(
KnownType.Int.satisfies(constraint, x.pos) checkValue(x, Constraint.Is(KnownType.Array(?), "len must be applied to an array")),
microWacc.UnaryOperator.Len
)(KnownType.Int.satisfies(constraint, x.pos))
case ast.Ord(x) => case ast.Ord(x) =>
checkValue(x, Constraint.Is(KnownType.Char, "ord must be applied to a char")) microWacc.UnaryOp(
KnownType.Int.satisfies(constraint, x.pos) checkValue(x, Constraint.Is(KnownType.Char, "ord must be applied to a char")),
microWacc.UnaryOperator.Ord
)(KnownType.Int.satisfies(constraint, x.pos))
case ast.Chr(x) => case ast.Chr(x) =>
checkValue(x, Constraint.Is(KnownType.Int, "chr must be applied to an int")) microWacc.UnaryOp(
KnownType.Char.satisfies(constraint, x.pos) checkValue(x, Constraint.Is(KnownType.Int, "chr must be applied to an int")),
microWacc.UnaryOperator.Chr
)(KnownType.Char.satisfies(constraint, x.pos))
// Binary operators // Binary operators
case op: (ast.Add | ast.Sub | ast.Mul | ast.Div | ast.Mod) => case op: (ast.Add | ast.Sub | ast.Mul | ast.Div | ast.Mod) =>
val operand = Constraint.Is(KnownType.Int, s"${op.name} operator must be applied to an int") val operand = Constraint.Is(KnownType.Int, s"${op.name} operator must be applied to an int")
checkValue(op.x, operand) microWacc.BinaryOp(
checkValue(op.y, operand) checkValue(op.x, operand),
KnownType.Int.satisfies(constraint, op.pos) checkValue(op.y, operand),
microWacc.BinaryOperator.fromAst(op)
)(KnownType.Int.satisfies(constraint, op.pos))
case op: (ast.Eq | ast.Neq) => case op: (ast.Eq | ast.Neq) =>
val xTy = checkValue(op.x, Constraint.Unconstrained) val xTyped = checkValue(op.x, Constraint.Unconstrained)
checkValue( microWacc.BinaryOp(
op.y, xTyped,
Constraint.Is(xTy, s"${op.name} operator must be applied to values of the same type") checkValue(
) op.y,
KnownType.Bool.satisfies(constraint, op.pos) Constraint
.Is(xTyped.ty, s"${op.name} operator must be applied to values of the same type")
),
microWacc.BinaryOperator.fromAst(op)
)(KnownType.Bool.satisfies(constraint, op.pos))
case op: (ast.Less | ast.LessEq | ast.Greater | ast.GreaterEq) => case op: (ast.Less | ast.LessEq | ast.Greater | ast.GreaterEq) =>
val xConstraint = Constraint.IsEither( val xConstraint = Constraint.IsEither(
KnownType.Int, KnownType.Int,
KnownType.Char, KnownType.Char,
s"${op.name} operator must be applied to an int or char" s"${op.name} operator must be applied to an int or char"
) )
val xTyped = checkValue(op.x, xConstraint)
// If x type-check failed, we still want to check y is an Int or Char (rather than ?) // If x type-check failed, we still want to check y is an Int or Char (rather than ?)
val yConstraint = checkValue(op.x, xConstraint) match { val yConstraint = xTyped.ty match {
case ? => xConstraint case ? => xConstraint
case xTy => case xTy =>
Constraint.Is(xTy, s"${op.name} operator must be applied to values of the same type") Constraint.Is(xTy, s"${op.name} operator must be applied to values of the same type")
} }
checkValue(op.y, yConstraint) microWacc.BinaryOp(
KnownType.Bool.satisfies(constraint, op.pos) xTyped,
checkValue(op.y, yConstraint),
microWacc.BinaryOperator.fromAst(op)
)(KnownType.Bool.satisfies(constraint, op.pos))
case op: (ast.And | ast.Or) => case op: (ast.And | ast.Or) =>
val operand = Constraint.Is(KnownType.Bool, s"${op.name} operator must be applied to a bool") val operand = Constraint.Is(KnownType.Bool, s"${op.name} operator must be applied to a bool")
checkValue(op.x, operand) microWacc.BinaryOp(
checkValue(op.y, operand) checkValue(op.x, operand),
KnownType.Bool.satisfies(constraint, op.pos) checkValue(op.y, operand),
microWacc.BinaryOperator.fromAst(op)
)(KnownType.Bool.satisfies(constraint, op.pos))
case lvalue: ast.LValue => checkLValue(lvalue, constraint)
}
/** Type-check an AST LValue node. Separate because microWacc keeps LValues
*
* @param value
* The value to type-check.
* @param constraint
* The type constraint that the value must satisfy.
* @param ctx
* The type checker context which includes the global names and functions, and an errors
* builder.
* @return
* The most specific type of the value if it could be determined, or ? if it could not.
*/
private def checkLValue(value: ast.LValue, constraint: Constraint)(using
ctx: TypeCheckerCtx
): microWacc.LValue = value match {
case id @ ast.Ident(name, uid) =>
microWacc.Ident(name, uid)(ctx.typeOf(id).satisfies(constraint, id.pos))
case ast.ArrayElem(id, indices) =>
val arrayTy = ctx.typeOf(id)
val (elemTy, indicesTyped) = indices.mapAccumulate(arrayTy) { (acc, elem) =>
val idxTyped = checkValue(elem, Constraint.Is(KnownType.Int, "array index must be an int"))
val next = acc match {
case KnownType.Array(innerTy) => innerTy
case ? => ? // we can keep indexing an unknown type
case nonArrayTy =>
ctx.error(
Error.TypeMismatch(
elem.pos,
KnownType.Array(?),
acc,
"cannot index into a non-array"
)
)
?
}
(next, idxTyped)
}
microWacc.ArrayElem(
microWacc.Ident(id.v, id.uid)(arrayTy),
indicesTyped
)(elemTy.satisfies(constraint, value.pos))
case ast.Fst(elem) =>
val elemTyped = checkLValue(
elem,
Constraint.Is(KnownType.Pair(?, ?), "fst must be applied to a pair")
)
microWacc.ArrayElem(
elemTyped,
NonEmptyList.of(microWacc.IntLiter(0))
)(elemTyped.ty match {
case KnownType.Pair(left, _) =>
left.satisfies(constraint, elem.pos)
case _ => ctx.error(Error.InternalError(elem.pos, "fst must be applied to a pair"))
})
case ast.Snd(elem) =>
val elemTyped = checkLValue(
elem,
Constraint.Is(KnownType.Pair(?, ?), "snd must be applied to a pair")
)
microWacc.ArrayElem(
elemTyped,
NonEmptyList.of(microWacc.IntLiter(1))
)(elemTyped.ty match {
case KnownType.Pair(_, right) =>
right.satisfies(constraint, elem.pos)
case _ => ctx.error(Error.InternalError(elem.pos, "snd must be applied to a pair"))
})
} }
} }

View File

@ -36,7 +36,7 @@ def compile(contents: String): Int = {
given errors: mutable.Builder[Error, List[Error]] = List.newBuilder given errors: mutable.Builder[Error, List[Error]] = List.newBuilder
val (names, funcs) = renamer.rename(prog) val (names, funcs) = renamer.rename(prog)
given ctx: typeChecker.TypeCheckerCtx = typeChecker.TypeCheckerCtx(names, funcs, errors) given ctx: typeChecker.TypeCheckerCtx = typeChecker.TypeCheckerCtx(names, funcs, errors)
typeChecker.check(prog) val typedProg = typeChecker.check(prog)
if (errors.result.nonEmpty) { if (errors.result.nonEmpty) {
given errorContent: String = contents given errorContent: String = contents
errors.result errors.result
@ -48,7 +48,10 @@ def compile(contents: String): Int = {
} }
} }
.max() .max()
} else 0 } else {
println(typedProg)
0
}
case Failure(msg) => case Failure(msg) =>
println(msg) println(msg)
100 100