From c8d16fc77fbe1f3306861b978f4463083c314a7b Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 4 May 2024 19:23:53 -0500 Subject: [PATCH] Minimal typechecking for Binding --- CHANGELOG.md | 6 +++--- Cargo.lock | 2 +- Cargo.toml | 2 +- src/semantic/types/expression.rs | 12 ++++++++++-- src/semantic/types/mod.rs | 3 --- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3825ded..7e56fa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,9 +35,9 @@ - [x] Parse binding of form `val Type variable = value` - [x] Parse binding of form `Type variable = value` -- [ ] Infer datatype of `value` in the above for a simple expression -- [ ] Ensure that the anotated datatype matches the datatype of `value` in the above -- [ ] Infer datatype of a `val variable = value` in the AST: Use the infered datatype +- [x] Infer datatype of `value` in the above for a simple expression (minimal) +- [x] Ensure that the anotated datatype matches the datatype of `value` in the above (binding) +- [x] Infer datatype of a `val variable = value` in the AST: Use the infered datatype (binding) - [x] Formally define the top level constructs - [x] Parse bindings and function declarations as top level constructs - [x] Parse function declaration arguments (`Type id`) diff --git a/Cargo.lock b/Cargo.lock index 2be0314..03bf7a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,7 +20,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "thp" -version = "0.0.10" +version = "0.0.11" dependencies = [ "colored", ] diff --git a/Cargo.toml b/Cargo.toml index c4de58b..f2ec7d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "thp" -version = "0.0.10" +version = "0.0.11" edition = "2021" diff --git a/src/semantic/types/expression.rs b/src/semantic/types/expression.rs index 770fc6d..8042173 100644 --- a/src/semantic/types/expression.rs +++ b/src/semantic/types/expression.rs @@ -2,9 +2,17 @@ use crate::syntax::ast::Expression; use super::Typed; - impl Typed for Expression<'_> { fn get_type(&self) -> String { - todo!() + match self { + // TODO: Distinguish between Int & Float + Expression::Number(_) => "Int".into(), + Expression::String(_) => "String".into(), + Expression::Boolean(_) => "Bool".into(), + Expression::Identifier(_) => todo!(), + Expression::FunctionCall(_) => todo!(), + Expression::UnaryOperator(_, _) => todo!(), + Expression::BinaryOperator(_, _, _) => todo!(), + } } } diff --git a/src/semantic/types/mod.rs b/src/semantic/types/mod.rs index f85ac29..7ce9580 100644 --- a/src/semantic/types/mod.rs +++ b/src/semantic/types/mod.rs @@ -1,11 +1,8 @@ // This crate provides an interface and implementations // for determining the datatypes of the language constructs. -use crate::lexic::token::Token; - mod expression; - pub trait Typed { fn get_type(&self) -> String; }