feat: implements binary operators in asmGenerator
Co-authored-by: Gleb Koval <gleb@koval.net> Co-authored-by: Barf-Vader <Barf-Vader@users.noreply.github.com>
This commit is contained in:
@@ -69,13 +69,15 @@ object microWacc {
|
||||
// Statements
|
||||
sealed trait Stmt
|
||||
|
||||
case class Builtin(val name: String)(retTy: SemType) extends CallTarget(retTy) {
|
||||
override def toString(): String = name
|
||||
}
|
||||
object Builtin {
|
||||
case object ReadInt extends CallTarget(KnownType.Int)
|
||||
case object ReadChar extends CallTarget(KnownType.Char)
|
||||
case object Print extends CallTarget(?)
|
||||
case object Println extends CallTarget(?)
|
||||
case object Exit extends CallTarget(?)
|
||||
case object Free extends CallTarget(?)
|
||||
object Read extends Builtin("read")(?)
|
||||
object Printf extends Builtin("printf")(?)
|
||||
object Exit extends Builtin("exit")(?)
|
||||
object Free extends Builtin("free")(?)
|
||||
object Malloc extends Builtin("malloc")(?)
|
||||
}
|
||||
|
||||
case class Assign(lhs: LValue, rhs: Expr) extends Stmt
|
||||
|
||||
@@ -177,12 +177,14 @@ object typeChecker {
|
||||
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
|
||||
microWacc.Builtin.Read,
|
||||
List(
|
||||
destTy match {
|
||||
case KnownType.Int => "%d".toMicroWaccCharArray
|
||||
case KnownType.Char | _ => "%c".toMicroWaccCharArray
|
||||
},
|
||||
destTyped
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -213,10 +215,20 @@ object typeChecker {
|
||||
)
|
||||
case ast.Print(expr, newline) =>
|
||||
// This constraint should never fail, the scope-checker should have caught it already
|
||||
val exprTyped = checkValue(expr, Constraint.Unconstrained)
|
||||
val format = exprTyped.ty match {
|
||||
case KnownType.Bool | KnownType.String => "%s"
|
||||
case KnownType.Char => "%c"
|
||||
case KnownType.Int => "%d"
|
||||
case _ => "%p"
|
||||
}
|
||||
List(
|
||||
microWacc.Call(
|
||||
if newline then microWacc.Builtin.Println else microWacc.Builtin.Print,
|
||||
List(checkValue(expr, Constraint.Unconstrained))
|
||||
microWacc.Builtin.Printf,
|
||||
List(
|
||||
s"$format${if newline then "\n" else ""}".toMicroWaccCharArray,
|
||||
exprTyped
|
||||
)
|
||||
)
|
||||
)
|
||||
case ast.If(cond, thenStmt, elseStmt) =>
|
||||
@@ -262,7 +274,7 @@ object typeChecker {
|
||||
microWacc.CharLiter(v)
|
||||
case l @ ast.StrLiter(v) =>
|
||||
KnownType.String.satisfies(constraint, l.pos)
|
||||
microWacc.ArrayLiter(v.map(microWacc.CharLiter(_)).toList)(KnownType.String)
|
||||
v.toMicroWaccCharArray
|
||||
case l @ ast.PairLiter() =>
|
||||
microWacc.NullLiter()(KnownType.Pair(?, ?).satisfies(constraint, l.pos))
|
||||
case ast.Parens(expr) => checkValue(expr, constraint)
|
||||
@@ -441,4 +453,9 @@ object typeChecker {
|
||||
case _ => ctx.error(Error.InternalError(elem.pos, "snd must be applied to a pair"))
|
||||
})
|
||||
}
|
||||
|
||||
extension (s: String) {
|
||||
def toMicroWaccCharArray: microWacc.ArrayLiter =
|
||||
microWacc.ArrayLiter(s.map(microWacc.CharLiter(_)).toList)(KnownType.String)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user