From 758766b491bb5d4bc0fa28ed5b2f4f1493155c4d Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Sat, 17 Jun 2023 17:28:55 -0700 Subject: [PATCH] Add a validator check that the types named in casts are reasonable. --- src/syntax/validate.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/syntax/validate.rs b/src/syntax/validate.rs index 676ff88..1c933da 100644 --- a/src/syntax/validate.rs +++ b/src/syntax/validate.rs @@ -1,6 +1,8 @@ -use crate::syntax::{Expression, Location, Program, Statement}; +use crate::{syntax::{Expression, Location, Program, Statement}, eval::PrimitiveType}; use codespan_reporting::diagnostic::Diagnostic; -use std::collections::HashMap; +use std::{collections::HashMap, str::FromStr}; + +use super::location; /// An error we found while validating the input program. /// @@ -11,6 +13,7 @@ use std::collections::HashMap; /// and using [`codespan_reporting`] to present them to the user. pub enum Error { UnboundVariable(Location, String), + UnknownType(Location, String), } impl From for Diagnostic { @@ -19,6 +22,10 @@ impl From for Diagnostic { Error::UnboundVariable(location, name) => location .labelled_error("unbound here") .with_message(format!("Unbound variable '{}'", name)), + + Error::UnknownType(location, name) => location + .labelled_error("type referenced here") + .with_message(format!("Unknown type '{}'", name)), } } } @@ -127,7 +134,15 @@ impl Expression { vec![Error::UnboundVariable(loc.clone(), var.clone())], vec![], ), - Expression::Cast(_, _, expr) => expr.validate(variable_map), + Expression::Cast(location, t, expr) => { + let (mut errs, warns) = expr.validate(variable_map); + + if PrimitiveType::from_str(t).is_err() { + errs.push(Error::UnknownType(location.clone(), t.clone())) + } + + (errs, warns) + } Expression::Primitive(_, _, args) => { let mut errors = vec![]; let mut warnings = vec![];