report unbound identifier errors

This commit is contained in:
Sebastien Bourdeauducq 2020-03-29 12:20:40 +08:00
parent befd01b240
commit 10fd4d3c27

View File

@ -23,6 +23,7 @@ enum CompileErrorKind {
MissingTypeAnnotation, MissingTypeAnnotation,
UnknownTypeAnnotation, UnknownTypeAnnotation,
IncompatibleTypes, IncompatibleTypes,
UnboundIdentifier,
Internal(&'static str) Internal(&'static str)
} }
@ -37,6 +38,8 @@ impl fmt::Display for CompileErrorKind {
=> write!(f, "Unknown type annotation"), => write!(f, "Unknown type annotation"),
CompileErrorKind::IncompatibleTypes CompileErrorKind::IncompatibleTypes
=> write!(f, "Incompatible types"), => write!(f, "Incompatible types"),
CompileErrorKind::UnboundIdentifier
=> write!(f, "Unbound identifier"),
CompileErrorKind::Internal(details) CompileErrorKind::Internal(details)
=> write!(f, "Internal compiler error: {}", details), => write!(f, "Internal compiler error: {}", details),
} }
@ -171,12 +174,14 @@ impl<'ctx> CodeGen<'ctx> {
) -> CompileResult<values::BasicValueEnum<'ctx>> { ) -> CompileResult<values::BasicValueEnum<'ctx>> {
self.set_source_location(expression.location); self.set_source_location(expression.location);
use ast::ExpressionType::*;
match &expression.node { match &expression.node {
Identifier { name } => { ast::ExpressionType::Identifier { name } => {
Ok(*self.namespace.get(name).unwrap()) match self.namespace.get(name) {
Some(value) => Ok(*value),
None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier))
}
}, },
Binop { a, op, b } => { ast::ExpressionType::Binop { a, op, b } => {
let a = self.compile_expression(&a)?; let a = self.compile_expression(&a)?;
let b = self.compile_expression(&b)?; let b = self.compile_expression(&b)?;
if a.get_type() != b.get_type() { if a.get_type() != b.get_type() {