From f1c44175f73c53295cc0bf657fdf1eea98143a31 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Fri, 18 Aug 2023 13:03:38 +0900 Subject: [PATCH] fix: eliminate `todo!`s --- crates/erg_compiler/context/inquire.rs | 24 +++++++- crates/erg_compiler/error/mod.rs | 76 +++++++++++++------------- 2 files changed, 59 insertions(+), 41 deletions(-) diff --git a/crates/erg_compiler/context/inquire.rs b/crates/erg_compiler/context/inquire.rs index 10923afd..639f5a35 100644 --- a/crates/erg_compiler/context/inquire.rs +++ b/crates/erg_compiler/context/inquire.rs @@ -1199,9 +1199,18 @@ impl Context { namespace: &Context, ) -> TyCheckResult { erg_common::debug_power_assert!(args.len() == 2); - let cont = Str::rc(binop_to_dname(op.inspect())); + let Some(cont) = binop_to_dname(op.inspect()) else { + return Err(TyCheckError::no_var_error( + self.cfg.input.clone(), + line!() as usize, + op.loc(), + namespace.caused_by(), + op.inspect(), + None, + ).into()); + }; // not a `Token::from_str(op.kind, cont)` because ops are defined as symbols - let symbol = Token::symbol_with_loc(cont, Location::concat(&args[0], &args[1])); + let symbol = Token::symbol_with_loc(Str::rc(cont), Location::concat(&args[0], &args[1])); let ident = Identifier::private_from_token(symbol.clone()); let t = self .rec_get_var_info(&ident, AccessKind::Name, input, namespace) @@ -1244,7 +1253,16 @@ impl Context { namespace: &Context, ) -> TyCheckResult { erg_common::debug_power_assert!(args.len() == 1); - let cont = unaryop_to_dname(op.inspect()); + let Some(cont) = unaryop_to_dname(op.inspect()) else { + return Err(TyCheckError::no_var_error( + self.cfg.input.clone(), + line!() as usize, + op.loc(), + namespace.caused_by(), + op.inspect(), + None, + ).into()); + }; let symbol = Token::symbol(cont); let ident = Identifier::private_from_token(symbol.clone()); let vi = self diff --git a/crates/erg_compiler/error/mod.rs b/crates/erg_compiler/error/mod.rs index 1ba560f0..8a13543c 100644 --- a/crates/erg_compiler/error/mod.rs +++ b/crates/erg_compiler/error/mod.rs @@ -78,50 +78,50 @@ pub fn ordinal_num(n: usize) -> String { } /// dname is for "double under name" -pub fn binop_to_dname(op: &str) -> &str { +pub fn binop_to_dname(op: &str) -> Option<&str> { match op { - "+" => "__add__", - "-" => "__sub__", - "*" => "__mul__", - "/" => "__div__", - "//" => "__floordiv__", - "**" => "__pow__", - "%" => "__mod__", - "@" => "__matmul__", - ".." => "__rng__", - "<.." => "__lorng__", - "..<" => "__rorng__", - "<..<" => "__orng__", - "&&" | "&" | "and" => "__and__", - "||" | "|" | "or" => "__or__", - "^^" | "^" => "__xor__", - "as" => "__as__", + "+" => Some("__add__"), + "-" => Some("__sub__"), + "*" => Some("__mul__"), + "/" => Some("__div__"), + "//" => Some("__floordiv__"), + "**" => Some("__pow__"), + "%" => Some("__mod__"), + "@" => Some("__matmul__"), + ".." => Some("__rng__"), + "<.." => Some("__lorng__"), + "..<" => Some("__rorng__"), + "<..<" => Some("__orng__"), + "&&" | "&" | "and" => Some("__and__"), + "||" | "|" | "or" => Some("__or__"), + "^^" | "^" => Some("__xor__"), + "as" => Some("__as__"), // l in r, l notin r -> r contains l, not(r contains l) - "contains" => "__contains__", - "subof" => "__subof__", - "supof" => "__supof__", - "is!" => "__is__!", - "isnot!" => "__isnot__!", - "==" => "__eq__", - "!=" => "__ne__", - "<" => "__lt__", - "<=" => "__le__", - ">" => "__gt__", - ">=" => "__ge__", - "<<" => "__lshift__", - ">>" => "__rshift__", - other => todo!("no such binary operator: {other}"), + "contains" => Some("__contains__"), + "subof" => Some("__subof__"), + "supof" => Some("__supof__"), + "is!" => Some("__is__!"), + "isnot!" => Some("__isnot__!"), + "==" => Some("__eq__"), + "!=" => Some("__ne__"), + "<" => Some("__lt__"), + "<=" => Some("__le__"), + ">" => Some("__gt__"), + ">=" => Some("__ge__"), + "<<" => Some("__lshift__"), + ">>" => Some("__rshift__"), + _ => None, } } -pub fn unaryop_to_dname(op: &str) -> &str { +pub fn unaryop_to_dname(op: &str) -> Option<&str> { match op { - "+" => "__pos__", - "-" => "__neg__", - "~" => "__invert__", - "!" => "__mutate__", - "..." => "__spread__", - other => todo!("no such unary operator: {other}"), + "+" => Some("__pos__"), + "-" => Some("__neg__"), + "~" => Some("__invert__"), + "!" => Some("__mutate__"), + "..." => Some("__spread__"), + _ => None, } }