Skip to content

Commit

Permalink
chore: add Imag and Complex
Browse files Browse the repository at this point in the history
  • Loading branch information
GreasySlug committed Oct 16, 2023
1 parent 09d0a8e commit 36b9f51
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
8 changes: 5 additions & 3 deletions crates/els/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions crates/erg_compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
4 changes: 2 additions & 2 deletions crates/erg_compiler/context/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ impl Context {
// sup/inf({±∞}) = ±∞ではあるが、Inf/NegInfにはOrdを実装しない
fn sup(&self, t: &Type) -> Option<TyParam> {
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() {
Expand All @@ -1688,7 +1688,7 @@ impl Context {

fn inf(&self, t: &Type) -> Option<TyParam> {
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;
Expand Down
9 changes: 7 additions & 2 deletions crates/erg_compiler/context/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions crates/erg_compiler/context/instantiate_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 11 additions & 10 deletions crates/erg_compiler/ty/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -1009,8 +1010,8 @@ impl ValueObj {
matches!(
self,
Self::Float(_)
| Self::Ratio(_)
| Self::Complex(_)
| Self::Ratio(_)
| Self::Int(_)
| Self::Nat(_)
| Self::Bool(_)
Expand All @@ -1021,8 +1022,8 @@ impl ValueObj {
matches!(
self,
Self::Float(_)
| Self::Ratio(_)
| Self::Complex(_)
| Self::Ratio(_)
| Self::Int(_)
| Self::Nat(_)
| Self::Bool(_)
Expand All @@ -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(_)
)
}

Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 36b9f51

Please sign in to comment.