From 9d3a01adb348b5c88010d4a99adb2fd309bb0e95 Mon Sep 17 00:00:00 2001 From: Huzaifa Raghav Date: Thu, 18 Apr 2024 22:47:24 +0800 Subject: [PATCH] Type chk sem_create --- example/concurrency-04.rst | 6 +++--- src/parser/src/structs.rs | 15 ++++----------- src/types/src/check_fn_call.rs | 7 ++++++- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/example/concurrency-04.rst b/example/concurrency-04.rst index 93ccaf1..4cdbc47 100644 --- a/example/concurrency-04.rst +++ b/example/concurrency-04.rst @@ -11,9 +11,9 @@ fn increment(times: int) { } } -let tid_1 = spawn increment(l00); -let tid_2 = spawn increment(100); -let tid_3 = spawn increment(100); +let tid_1 = spawn increment(1000); +let tid_2 = spawn increment(1000); +let tid_3 = spawn increment(1000); join tid_3; join tid_2; diff --git a/src/parser/src/structs.rs b/src/parser/src/structs.rs index 0930f98..1d1fc43 100644 --- a/src/parser/src/structs.rs +++ b/src/parser/src/structs.rs @@ -427,8 +427,9 @@ pub enum Type { Bool, String, UserFn(Box), - BuiltInFn, // type checking done separately since it can be polymorphic unlike user fn - ThreadId, // result of spawn + BuiltInFn, // type checking done separately since it can be polymorphic unlike user fn + ThreadId, // result of spawn + Semaphore, Unit, // void type like Rust Unitialised, // Type for variables that exist in a block but not yet declared - only used for TyEnv } @@ -443,15 +444,6 @@ impl Type { } } -#[test] -fn hi() { - let ty1 = dbg!(Type::Int.eq(&Type::UserFn(Box::new(FnTypeData { - params: vec![], - ret_type: Type::Bool - })))); - // dbg!(Type::Int.eq(&Type::Int)); -} - impl Type { /// Converts string to primitive type. pub fn from_string(input: &str) -> Result { @@ -479,6 +471,7 @@ impl Display for Type { Self::String => "string".to_string(), Self::UserFn(fn_ty) => fn_ty.to_string(), Self::ThreadId => "tid".to_string(), + Self::Semaphore => "sem".to_string(), }; write!(f, "{}", string) diff --git a/src/types/src/check_fn_call.rs b/src/types/src/check_fn_call.rs index eb39a28..eb853f3 100644 --- a/src/types/src/check_fn_call.rs +++ b/src/types/src/check_fn_call.rs @@ -319,9 +319,11 @@ impl<'prog> TypeChecker<'prog> { } } } + // () -> semaphore SEM_CREATE => { // Fill out this block - todo!() + TypeChecker::check_arg_params_len(name, arg_types.len(), 0)?; + Type::Semaphore } SEM_SET => { // Fill out this block @@ -503,5 +505,8 @@ mod tests { // Test int_to_float expect_pass("let x : float = int_to_float(3); x", Type::Float); + + // Test sem + expect_pass("let x = sem_create(); x", Type::Semaphore); } }