diff --git a/src/lib/codegen/codegen_context.rs b/src/lib/codegen/codegen_context.rs index a94e22b8..54ce3cbd 100644 --- a/src/lib/codegen/codegen_context.rs +++ b/src/lib/codegen/codegen_context.rs @@ -104,7 +104,10 @@ impl<'a> CodegenContext<'a> { pub fn lower_type(&mut self, t: &Type, builder: &'a Builder) -> Result, ()> { Ok(match t { Type::Primitive(PrimitiveType::Int8) => self.context.i8_type().into(), + Type::Primitive(PrimitiveType::Int16) => self.context.i16_type().into(), + Type::Primitive(PrimitiveType::Int32) => self.context.i32_type().into(), Type::Primitive(PrimitiveType::Int64) => self.context.i64_type().into(), + Type::Primitive(PrimitiveType::Int) => self.context.i64_type().into(), Type::Primitive(PrimitiveType::Float64) => self.context.f64_type().into(), Type::Primitive(PrimitiveType::Bool) => self.context.bool_type().into(), Type::Primitive(PrimitiveType::Char) => self.context.i8_type().into(), @@ -767,7 +770,8 @@ impl<'a> CodegenContext<'a> { ) -> Result, ()> { Ok(match &lit.kind { LiteralKind::Number(mut n) => { - let i64_type = self.context.i64_type(); + let ty = self.hir.node_types.get(&lit.get_hir_id()).unwrap(); + let ty = self.lower_type(ty, builder)?.into_int_type(); let mut negative = false; if n < 0 { @@ -775,7 +779,7 @@ impl<'a> CodegenContext<'a> { n = -n; } - i64_type.const_int((n).try_into().unwrap(), negative).into() + ty.const_int((n).try_into().unwrap(), negative).into() } LiteralKind::Float(n) => { let f64_type = self.context.f64_type(); diff --git a/src/lib/diagnostics/diagnostic.rs b/src/lib/diagnostics/diagnostic.rs index 29612527..8811ce18 100644 --- a/src/lib/diagnostics/diagnostic.rs +++ b/src/lib/diagnostics/diagnostic.rs @@ -241,7 +241,7 @@ impl Display for DiagnosticKind { Self::TypeConflict(t1, t2, _in1, _in2) => { format!( "Type conflict:\n{:<8}Expected {:?}\n{:<8}But got {:?}", - "", t1, "", t2 + "", t2, "", t1 ) } Self::UnresolvedType(t) => { diff --git a/src/lib/infer/constraint.rs b/src/lib/infer/constraint.rs index f7acfa8a..e36fefff 100644 --- a/src/lib/infer/constraint.rs +++ b/src/lib/infer/constraint.rs @@ -313,6 +313,7 @@ impl<'a> ConstraintContext<'a> { // Fix the current sig if some types were still unknown self.envs.amend_current_sig(&new_f_sig); + println!("NEW F SIG {:#?}", new_f_sig); // We restore the scope here if !self.envs.set_current_fn(old_f) { @@ -421,6 +422,7 @@ impl<'a, 'ar> Visitor<'a> for ConstraintContext<'ar> { fn visit_function_decl(&mut self, f: &'a FunctionDecl) { self.envs.apply_args_type(f); + println!("VISITING FUNCTION DECL {:#?}", f); walk_list!(self, visit_argument_decl, &f.arguments); @@ -442,6 +444,8 @@ impl<'a, 'ar> Visitor<'a> for ConstraintContext<'ar> { )), ); + println!("FN DECL {:#?}", self.envs.get_type(&f.hir_id)); + self.envs.set_type_eq(&f.name.hir_id, &f.hir_id); self.add_tmp_resolution_to_current_fn(&f.name.hir_id, &f.hir_id); @@ -724,7 +728,7 @@ impl<'a, 'ar> Visitor<'a> for ConstraintContext<'ar> { fn visit_literal(&mut self, lit: &Literal) { let t = match &lit.kind { - LiteralKind::Number(_n) => Type::Primitive(PrimitiveType::Int64), + LiteralKind::Number(_n) => Type::Primitive(PrimitiveType::Int), // not a real type LiteralKind::Float(_f) => Type::Primitive(PrimitiveType::Float64), LiteralKind::String(_s) => Type::Primitive(PrimitiveType::String), LiteralKind::Bool(_b) => Type::Primitive(PrimitiveType::Bool), diff --git a/src/lib/infer/mod.rs b/src/lib/infer/mod.rs index 1392f10b..a354f3dd 100644 --- a/src/lib/infer/mod.rs +++ b/src/lib/infer/mod.rs @@ -13,6 +13,10 @@ pub fn infer( parsing_ctx: &mut ParsingCtx, config: &Config, ) -> Result { + if config.show_state { + super::hir::hir_printer::print(root); + } + let (tmp_resolutions, diags) = constraint::solve(root); parsing_ctx.diagnostics.append(diags); diff --git a/src/lib/infer/state.rs b/src/lib/infer/state.rs index e5573637..97435e54 100644 --- a/src/lib/infer/state.rs +++ b/src/lib/infer/state.rs @@ -80,6 +80,15 @@ impl Envs { )); } } + // generic Int subtyping + (Type::Primitive(src_prim), Some(Type::Primitive(PrimitiveType::Int))) + if src_prim.is_concrete_int() => + { + error!( + "Replacing hir_id {:?} type {:?} with {:?}", + dest, previous, src + ); + } (src, Some(previous)) if !src.eq(&previous) => { if previous.is_solved() && src.is_solved() { self.diagnostics.push_error(Diagnostic::new_type_conflict( @@ -107,33 +116,8 @@ impl Envs { } let src_t = src_t_opt?.clone(); - let previous = self.set_type_alone(dest, &src_t); - match (src_t.clone(), previous.clone()) { - (Type::Func(src_f), Some(Type::Func(prev_f))) if !src_f.eq(&prev_f) => { - if prev_f.is_solved() && src_f.is_solved() { - self.diagnostics.push_error(Diagnostic::new_type_conflict( - self.spans.get(src).unwrap().clone().into(), - previous.clone().unwrap(), - src_t.clone(), - previous.unwrap(), - src_t.clone(), - )); - } - } - (src_t, Some(previous)) if !src_t.eq(&previous) => { - if previous.is_solved() && src_t.is_solved() { - self.diagnostics.push_error(Diagnostic::new_type_conflict( - self.spans.get(src).unwrap().clone().into(), - previous.clone(), - src_t.clone(), - previous, - src_t, - )); - } - } - _ => (), - } + self.set_type(dest, &src_t); Some(()) } diff --git a/src/lib/parser/mod.rs b/src/lib/parser/mod.rs index 22e0b935..cdf8b66b 100644 --- a/src/lib/parser/mod.rs +++ b/src/lib/parser/mod.rs @@ -108,6 +108,10 @@ lazy_static! { "/std/src/vec.rk".into(), include_str!("../../../std/src/vec.rk"), ); + m.insert( + "/std/src/socket.rk".into(), + include_str!("../../../std/src/socket.rk"), + ); m }; } @@ -242,7 +246,7 @@ pub fn parse_root(input: Parser) -> Res { pub fn parse_mod(input: Parser) -> Res { map( - many1(terminated(parse_top_level, many1(line_ending))), + terminated(many1(terminated(parse_top_level, many1(line_ending))), eof), Mod::new, )(input) } @@ -289,7 +293,9 @@ pub fn parse_mod_decl(input: Parser) -> Res { .files .insert(new_ctx.current_file_path().clone(), file.clone()); - let new_parser = Parser::new_extra(&file.content, new_ctx); + let content = file.content.clone(); + + let new_parser = Parser::new_extra(&content, new_ctx); use nom::Finish; @@ -305,6 +311,8 @@ pub fn parse_mod_decl(input: Parser) -> Res { input.extra.identities.extend(new_parser.extra.identities); input.extra.files.extend(new_parser.extra.files); + println!("ERROR: {:#?}", err); + return Err(nom::Err::Error(VerboseError::from_external_error( input, ErrorKind::Fail, @@ -1178,6 +1186,9 @@ pub fn parse_type(input: Parser) -> Res { alt(( map(tag("Bool"), |_| PrimitiveType::Bool), map(tag("Int64"), |_| PrimitiveType::Int64), + map(tag("Int32"), |_| PrimitiveType::Int32), + map(tag("Int16"), |_| PrimitiveType::Int16), + map(tag("Int8"), |_| PrimitiveType::Int8), map(tag("Float64"), |_| PrimitiveType::Float64), map(tag("String"), |_| PrimitiveType::String), map(tag("Char"), |_| PrimitiveType::Char), diff --git a/src/lib/ty/func_type.rs b/src/lib/ty/func_type.rs index e42a10f3..83a5b000 100644 --- a/src/lib/ty/func_type.rs +++ b/src/lib/ty/func_type.rs @@ -133,7 +133,7 @@ impl FuncType { .unzip(); if !ret.is_forall() { - warn!("Trying to apply type to a not forall"); + warn!("Trying to apply type to a not forall: {:#?}", ret); } // FIXME: must remplace all occurences of ret @@ -154,7 +154,7 @@ impl FuncType { .enumerate() .filter_map(|(i, arg_t)| -> Option<(Type, Type)> { if !arg_t.is_forall() { - warn!("Trying to apply type to a not forall"); + warn!("Trying to apply type to a not forall: {:#?}", arg_t); return None; } @@ -168,7 +168,7 @@ impl FuncType { if let Some(t) = ret { if !t.is_forall() { - warn!("Trying to apply type to a not forall"); + warn!("Trying to apply type to a not forall: {:#?}", t); } // FIXME: must remplace all occurences of ret diff --git a/src/lib/ty/primitive_type.rs b/src/lib/ty/primitive_type.rs index 56fefd51..28198076 100644 --- a/src/lib/ty/primitive_type.rs +++ b/src/lib/ty/primitive_type.rs @@ -8,6 +8,7 @@ pub enum PrimitiveType { Int16, Int32, Int64, + Int, // not a real type, default to Int64 Float64, String, Array(Box, usize), @@ -31,6 +32,7 @@ impl PrimitiveType { Self::Int16 => "Int16".to_string(), Self::Int32 => "Int32".to_string(), Self::Int64 => "Int64".to_string(), + Self::Int => "Int64".to_string(), Self::Float64 => "Float64".to_string(), Self::String => "String".to_string(), Self::Array(t, _size) => format!("[{}]", t.get_name()), @@ -46,6 +48,7 @@ impl PrimitiveType { "Int16" => Some(Self::Int16), "Int32" => Some(Self::Int32), "Int64" => Some(Self::Int64), + "Int" => Some(Self::Int64), "Float64" => Some(Self::Float64), "String" => Some(Self::String), "Char" => Some(Self::Char), @@ -73,6 +76,14 @@ impl PrimitiveType { matches!(self, PrimitiveType::Int64) } + pub fn is_int(&self) -> bool { + matches!(self, PrimitiveType::Int) + } + + pub fn is_concrete_int(&self) -> bool { + self.is_int8() || self.is_int16() || self.is_int32() || self.is_int64() + } + pub fn is_float64(&self) -> bool { matches!(self, PrimitiveType::Float64) } diff --git a/std/src/functor.rk b/std/src/functor.rk index 1c7f039b..b1d50605 100644 --- a/std/src/functor.rk +++ b/std/src/functor.rk @@ -9,7 +9,7 @@ map: f, arr -> i = i + 1 arr2 -foreach: f arr -> +foreach: f, arr -> let i = 0 while i < (~Len arr arr) f arr[i] diff --git a/std/src/helpers.rk b/std/src/helpers.rk index cee9d6fa..ccc7773f 100644 --- a/std/src/helpers.rk +++ b/std/src/helpers.rk @@ -2,9 +2,9 @@ infix >> 2 infix << 2 infix |> 1 ->>: x y -> y -<<: x y -> x -|>: k l -> l k +>>: x, y -> y +<<: x, y -> x +|>: k, l -> l k _shutup_warnings: x -> 1 >> 1 diff --git a/std/src/lib.rk b/std/src/lib.rk index c4db0e03..4ed5ddef 100644 --- a/std/src/lib.rk +++ b/std/src/lib.rk @@ -9,5 +9,6 @@ mod functor mod clone mod vec mod helpers +mod socket mod prelude diff --git a/std/src/prelude.rk b/std/src/prelude.rk index 3ef293b4..7d43e8d9 100644 --- a/std/src/prelude.rk +++ b/std/src/prelude.rk @@ -5,3 +5,4 @@ use super::clone::(*) use super::fs::(*) use super::vec::(*) use super::helpers::(*) +use super::socket::(*)