diff --git a/crates/els/semantic.rs b/crates/els/semantic.rs index 4a48d648c..58bb5920e 100644 --- a/crates/els/semantic.rs +++ b/crates/els/semantic.rs @@ -141,9 +141,11 @@ impl ASTSemanticState { Expr::Literal(lit) => { let typ = match lit.token.kind { TokenKind::StrLit => SemanticTokenType::STRING, - TokenKind::NatLit | TokenKind::IntLit | TokenKind::RatioLit => { - SemanticTokenType::NUMBER - } + TokenKind::NatLit + | TokenKind::IntLit + | TokenKind::RatioLit + | TokenKind::ImLit + | TokenKind::ComplexLit => SemanticTokenType::NUMBER, _ => SemanticTokenType::VARIABLE, }; let token = self.gen_token(lit.loc(), typ); diff --git a/crates/erg_compiler/codegen.rs b/crates/erg_compiler/codegen.rs index 9085c9dfd..2607aa7ac 100644 --- a/crates/erg_compiler/codegen.rs +++ b/crates/erg_compiler/codegen.rs @@ -3145,6 +3145,14 @@ impl PyCodeGenerator { self.emit_push_null(); self.emit_load_name_instr(Identifier::public("Ratio")); } + Imag => { + self.emit_push_null(); + self.emit_load_name_instr(Identifier::public("Imag")); + } + Complex => { + self.emit_push_null(); + self.emit_load_name_instr(Identifier::public("Complex")); + } Float => { self.emit_push_null(); self.emit_load_name_instr(Identifier::public("Float")); diff --git a/crates/erg_compiler/context/compare.rs b/crates/erg_compiler/context/compare.rs index 1e2bf249f..649dea0a4 100644 --- a/crates/erg_compiler/context/compare.rs +++ b/crates/erg_compiler/context/compare.rs @@ -1661,7 +1661,7 @@ impl Context { // sup/inf({±∞}) = ±∞ではあるが、Inf/NegInfにはOrdを実装しない fn sup(&self, t: &Type) -> Option { match t { - Int | Nat | Ratio | Float => Some(TyParam::value(Inf)), + Int | Nat | Ratio | Imag | Complex | Float => Some(TyParam::value(Inf)), Refinement(refine) => { let mut maybe_max = None; for pred in refine.pred.ands() { @@ -1688,7 +1688,7 @@ impl Context { fn inf(&self, t: &Type) -> Option { match t { - Int | Ratio | Float => Some(TyParam::value(-Inf)), + Int | Ratio | Imag | Complex | Float => Some(TyParam::value(-Inf)), Nat => Some(TyParam::value(0usize)), Refinement(refine) => { let mut maybe_min = None; diff --git a/crates/erg_compiler/context/eval.rs b/crates/erg_compiler/context/eval.rs index 6d38c4dc3..4538fd20e 100644 --- a/crates/erg_compiler/context/eval.rs +++ b/crates/erg_compiler/context/eval.rs @@ -10,6 +10,7 @@ use erg_common::ratio::Ratio; use erg_common::set::Set; use erg_common::shared::Shared; use erg_common::traits::{Locational, Stream}; +use erg_common::{complex::Complex, imag::Imag}; use erg_common::{dict, fmt_vec, fn_name, option_enum_unwrap, set, Triple}; use erg_common::{ArcArray, Str}; use OpKind::*; @@ -1394,7 +1395,9 @@ impl Context { ValueObj::Bool(b) => Ok(ValueObj::Nat(b as u64 + 1)), ValueObj::Nat(n) => Ok(ValueObj::Nat(n + 1)), ValueObj::Int(n) => Ok(ValueObj::Int(n + 1)), - ValueObj::Ratio(r) => Ok(ValueObj::Ratio(r + Ratio::new(1, 1))), + ValueObj::Ratio(r) => Ok(ValueObj::Ratio(r + Ratio::int_new(1))), + ValueObj::Imag(i) => Ok(ValueObj::Imag(i + Imag::int_new(1))), + ValueObj::Complex(c) => Ok(ValueObj::Complex(c + Complex::int_new(1, 0))), ValueObj::Float(n) => Ok(ValueObj::Float(n + f64::EPSILON)), ValueObj::Inf | ValueObj::NegInf => Ok(val), _ => Err(EvalErrors::from(EvalError::unreachable( @@ -1410,7 +1413,9 @@ impl Context { ValueObj::Bool(_) => Ok(ValueObj::Nat(0)), ValueObj::Nat(n) => Ok(ValueObj::Nat(n.saturating_sub(1))), ValueObj::Int(n) => Ok(ValueObj::Int(n - 1)), - ValueObj::Ratio(r) => Ok(ValueObj::Ratio(r - Ratio::new(1, 1))), + ValueObj::Ratio(r) => Ok(ValueObj::Ratio(r - Ratio::int_new(1))), + ValueObj::Imag(i) => Ok(ValueObj::Imag(i - Imag::int_new(1))), + ValueObj::Complex(c) => Ok(ValueObj::Complex(c - Complex::int_new(1, 0))), ValueObj::Float(n) => Ok(ValueObj::Float(n - f64::EPSILON)), ValueObj::Inf | ValueObj::NegInf => Ok(val), _ => Err(EvalErrors::from(EvalError::unreachable( diff --git a/crates/erg_compiler/context/instantiate_spec.rs b/crates/erg_compiler/context/instantiate_spec.rs index c2a6ab550..b5cd2cc0b 100644 --- a/crates/erg_compiler/context/instantiate_spec.rs +++ b/crates/erg_compiler/context/instantiate_spec.rs @@ -530,6 +530,8 @@ impl Context { "Nat" => Ok(Type::Nat), "Int" => Ok(Type::Int), "Ratio" => Ok(Type::Ratio), + "Imag" => Ok(Type::Imag), + "Complex" => Ok(Type::Complex), "Float" => Ok(Type::Float), "Str" => Ok(Type::Str), "Bool" => Ok(Type::Bool), diff --git a/crates/erg_compiler/ty/value.rs b/crates/erg_compiler/ty/value.rs index 55b685ced..a25bfef24 100644 --- a/crates/erg_compiler/ty/value.rs +++ b/crates/erg_compiler/ty/value.rs @@ -7,17 +7,18 @@ use std::hash::{Hash, Hasher}; use std::ops::Neg; use std::sync::Arc; -use erg_common::dict::Dict; -use erg_common::error::{ErrorCore, ErrorKind, Location}; use erg_common::fresh::FRESH_GEN; -use erg_common::io::Input; use erg_common::python_util::PythonVersion; use erg_common::ratio::Ratio; use erg_common::serialize::*; use erg_common::set::Set; use erg_common::traits::LimitedDisplay; -use erg_common::{complex::Complex, config::Input, dict::Dict, imag::Imag}; +use erg_common::{complex::Complex, dict::Dict, imag::Imag}; use erg_common::{dict, fmt_iter, impl_display_from_debug, log, switch_lang}; +use erg_common::{ + error::{ErrorCore, ErrorKind, Location}, + io::Input, +}; use erg_common::{ArcArray, Str}; use erg_parser::ast::{ConstArgs, ConstExpr}; @@ -1009,8 +1010,8 @@ impl ValueObj { matches!( self, Self::Float(_) - | Self::Ratio(_) | Self::Complex(_) + | Self::Ratio(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_) @@ -1021,8 +1022,8 @@ impl ValueObj { matches!( self, Self::Float(_) - | Self::Ratio(_) | Self::Complex(_) + | Self::Ratio(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_) @@ -1032,7 +1033,7 @@ impl ValueObj { pub const fn is_ratio(&self) -> bool { matches!( self, - Self::Ratio(_) | Self::Complex(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_) + Self::Ratio(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_) ) } @@ -1347,15 +1348,15 @@ impl ValueObj { Some(Self::Complex(Complex::new(Ratio::new(r as i64, 1), l))) } (Self::Imag(l), Self::Nat(r)) => { - Some(Self::Complex(Complex::new(Ratio::new(r as i64, 1), l))) + Some(Self::Complex(Complex::new(Ratio::int_new(r as i64), l))) } (Self::Ratio(l), Self::Imag(r)) => Some(Self::Complex(Complex::new(l, r))), (Self::Int(l), Self::Imag(r)) => { - Some(Self::Complex(Complex::new(Ratio::new(l as i64, 1), r))) + Some(Self::Complex(Complex::new(Ratio::int_new(l as i64), r))) } (Self::Nat(l), Self::Imag(r)) => { // TODO: check if l is greater then the max value of i64 - Some(Self::Complex(Complex::new(Ratio::new(l as i64, 1), r))) + Some(Self::Complex(Complex::new(Ratio::int_new(l as i64), r))) } /* Complex */ (Self::Complex(l), Self::Complex(r)) => Some(Self::Complex(l + r)),