feat: renamer maybe maybe maybe maybe

This commit is contained in:
2025-02-04 22:26:38 +00:00
committed by Barf-Vader
parent e9ed197782
commit 3fbb90322f
5 changed files with 181 additions and 9 deletions

119
src/main/wacc/renamer.scala Normal file
View File

@@ -0,0 +1,119 @@
package wacc
import scala.collection.mutable
object renamer {
import ast._
import types._
private case class Scope(
current: mutable.Map[String, Ident],
parent: Map[String, Ident]
) {
def subscope: Scope =
Scope(mutable.Map.empty, Map.empty.withDefault(current.withDefault(parent)))
def add(semType: SemType, name: Ident)(using
globalNames: mutable.Map[Ident, SemType],
globalNumbering: mutable.Map[String, Int],
errors: mutable.Builder[Error, List[Error]]
) = {
if (current.contains(name.v)) {
errors += Error.DuplicateDeclaration(name)
} else {
val uid = globalNumbering.getOrElse(name.v, 0)
name.uid = uid
current(name.v) = name
globalNames(name) = semType
globalNumbering(name.v) = uid + 1
}
}
}
def rename(prog: Program)(using
errors: mutable.Builder[Error, List[Error]]
): Map[Ident, SemType] =
given globalNames: mutable.Map[Ident, SemType] = mutable.Map.empty
given globalNumbering: mutable.Map[String, Int] = mutable.Map.empty
rename(Scope(mutable.Map.empty, Map.empty))(prog)
globalNames.toMap
private def rename(scope: Scope)(
node: Program | FuncDecl | Ident | Stmt | LValue | RValue
)(using
globalNames: mutable.Map[Ident, SemType],
globalNumbering: mutable.Map[String, Int],
errors: mutable.Builder[Error, List[Error]]
): Unit = node match {
case Program(funcs, main) => {
funcs.foreach(rename(scope))
main.toList.foreach(rename(scope))
}
case FuncDecl(retType, name, params, body) => {
val functionScope = scope.subscope
val paramTypes = params.map { param =>
val paramType = SemType(param.paramType)
functionScope.add(paramType, param.name)
paramType
}
scope.add(KnownType.Func(SemType(retType), paramTypes), name)
body.toList.foreach(rename(functionScope))
}
case VarDecl(synType, name, value) => {
// Order matters here. Variable isn't declared until after the value is evaluated.
rename(scope)(value)
scope.add(SemType(synType), name)
}
case Assign(lhs, value) => {
rename(scope)(lhs)
rename(scope)(value)
}
case Read(lhs) => rename(scope)(lhs)
case Free(expr) => rename(scope)(expr)
case Return(expr) => rename(scope)(expr)
case Exit(expr) => rename(scope)(expr)
case Print(expr, _) => rename(scope)(expr)
case If(cond, thenStmt, elseStmt) => {
rename(scope)(cond)
thenStmt.toList.foreach(rename(scope.subscope))
elseStmt.toList.foreach(rename(scope.subscope))
}
case While(cond, body) => {
rename(scope)(cond)
body.toList.foreach(rename(scope.subscope))
}
case Block(body) => body.toList.foreach(rename(scope.subscope))
case NewPair(fst, snd) => {
rename(scope)(fst)
rename(scope)(snd)
}
case Call(name, args) => {
rename(scope)(name)
args.foreach(rename(scope))
}
case Fst(elem) => rename(scope)(elem)
case Snd(elem) => rename(scope)(elem)
case ArrayLiter(elems) => elems.foreach(rename(scope))
case ArrayElem(name, indices) => {
rename(scope)(name)
indices.toList.foreach(rename(scope))
}
case Parens(expr) => rename(scope)(expr)
case op: UnaryOp => rename(scope)(op.x)
case op: BinaryOp => {
rename(scope)(op.x)
rename(scope)(op.y)
}
case id: Ident => {
scope.current.withDefault(scope.parent).get(id.v) match {
case Some(Ident(_, uid)) => id.uid = uid
case None => {
errors += Error.UndefinedIdentifier(id)
scope.add(?, id)
}
}
}
case IntLiter(_) | BoolLiter(_) | CharLiter(_) | StrLiter(_) | PairLiter | Skip => ()
}
}