1
0
mirror of https://github.com/KarolS/millfork.git synced 2025-02-08 15:30:50 +00:00

Early name check

This commit is contained in:
Karol Stasiak 2018-01-20 00:57:37 +01:00
parent f8f1af287f
commit cb5f025ea9
3 changed files with 32 additions and 2 deletions

View File

@ -45,6 +45,7 @@ object MlCompiler {
def nextLabel(prefix: String): String = "." + prefix + "__" + labelCounter.incrementAndGet().formatted("%05d")
def compile(ctx: CompilationContext): Chunk = {
ctx.env.nameCheck(ctx.function.code)
val chunk = compile(ctx, ctx.function.code)
val prefix = (if (ctx.function.interrupt) {
if (ctx.options.flag(CompilationFlag.EmitCmosOpcodes)) {

View File

@ -3,7 +3,7 @@ package millfork.env
import java.util.concurrent.atomic.AtomicLong
import millfork.{CompilationFlag, CompilationOptions}
import millfork.assembly.Opcode
import millfork.assembly.{Opcode, OpcodeClasses}
import millfork.compiler._
import millfork.error.ErrorReporting
import millfork.node._
@ -692,4 +692,33 @@ class Environment(val parent: Option[Environment], val prefix: String) {
case i: ImportStatement => ()
}
}
private def checkName[T <: Thing : Manifest](name: String, pos: Option[Position]): Unit = {
if (maybeGet[T](name).isEmpty) {
ErrorReporting.error(s"`$name` is not defined", pos)
}
}
def nameCheck(nodes: List[_ <:Node]): Unit = nodes.foreach(nameCheck)
def nameCheck(node: Node): Unit = node match {
case _:AssemblyStatement => ()
case _:DeclarationStatement => ()
case s:Statement => nameCheck(s.getAllExpressions)
case _:LiteralExpression => ()
case VariableExpression(name) => checkName[Thing](name, node.position)
case IndexedExpression(name, index) =>
checkName[Thing](name, node.position)
nameCheck(index)
case SeparateBytesExpression(h, l) =>
nameCheck(h)
nameCheck(l)
case SumExpression(params, _) =>
nameCheck(params.map(_._2))
case FunctionCallExpression(name, params) =>
if (name.exists(_.isLetter) && name != "not") {
checkName[MangledFunction](name, node.position)
}
nameCheck(params)
}
}

View File

@ -259,7 +259,7 @@ class AssemblyOptimizationSuite extends FunSuite with Matchers {
| }
| a.lo:a.hi=a
| output = a
| couput
| output
| }
| void barrier (){}
|