feat: add Bytes.d.er/Obj.d.er

This commit is contained in:
Shunsuke Shibayama 2023-02-18 13:46:48 +09:00
parent 7389f68e3e
commit b552cad3b2
10 changed files with 192 additions and 55 deletions

View file

@ -34,23 +34,33 @@ impl Context {
let mut obj = Self::builtin_mono_class(OBJ, 2); let mut obj = Self::builtin_mono_class(OBJ, 2);
let Slf = mono_q(SELF, subtypeof(Obj)); let Slf = mono_q(SELF, subtypeof(Obj));
let t = fn0_met(Slf.clone(), Slf).quantify(); let t = fn0_met(Slf.clone(), Slf).quantify();
obj.register_builtin_erg_impl(FUNC_CLONE, t, Const, Public); obj.register_py_builtin(
obj.register_builtin_erg_impl(FUNDAMENTAL_MODULE, Str, Const, Public);
obj.register_builtin_erg_impl(FUNDAMENTAL_SIZEOF, fn0_met(Obj, Nat), Const, Public);
obj.register_builtin_erg_impl(FUNDAMENTAL_REPR, fn0_met(Obj, Str), Immutable, Public);
obj.register_builtin_erg_impl(FUNDAMENTAL_STR, fn0_met(Obj, Str), Immutable, Public);
obj.register_builtin_erg_impl(
FUNDAMENTAL_DICT, FUNDAMENTAL_DICT,
fn0_met(Obj, dict! {Str => Obj}.into()), dict! {Str => Obj}.into(),
Immutable, Some(FUNDAMENTAL_DICT),
Public, 3,
); );
obj.register_builtin_erg_impl( obj.register_py_builtin(FUNDAMENTAL_MODULE, Str, Some(FUNDAMENTAL_MODULE), 4);
obj.register_py_builtin(FUNC_CLONE, t, Some(FUNC_CLONE), 5);
obj.register_py_builtin(
FUNDAMENTAL_BYTES, FUNDAMENTAL_BYTES,
fn0_met(Obj, mono(BYTES)), fn0_met(Obj, mono(BYTES)),
Immutable, Some(FUNDAMENTAL_BYTES),
Public, 6,
); );
obj.register_py_builtin(
FUNDAMENTAL_REPR,
fn0_met(Obj, Str),
Some(FUNDAMENTAL_REPR),
7,
);
obj.register_py_builtin(
FUNDAMENTAL_SIZEOF,
fn0_met(Obj, Nat),
Some(FUNDAMENTAL_SIZEOF),
8,
);
obj.register_py_builtin(FUNDAMENTAL_STR, fn0_met(Obj, Str), Some(FUNDAMENTAL_STR), 9);
let mut obj_in = Self::builtin_methods(Some(poly(IN, vec![ty_tp(Type)])), 2); let mut obj_in = Self::builtin_methods(Some(poly(IN, vec![ty_tp(Type)])), 2);
obj_in.register_builtin_erg_impl(OP_IN, fn1_met(Obj, Type, Bool), Const, Public); obj_in.register_builtin_erg_impl(OP_IN, fn1_met(Obj, Type, Bool), Const, Public);
obj.register_trait(Obj, obj_in); obj.register_trait(Obj, obj_in);
@ -70,26 +80,30 @@ impl Context {
float.register_builtin_const(EPSILON, Public, ValueObj::Float(2.220446049250313e-16)); float.register_builtin_const(EPSILON, Public, ValueObj::Float(2.220446049250313e-16));
float.register_builtin_py_impl(REAL, Float, Const, Public, Some(FUNC_REAL)); float.register_builtin_py_impl(REAL, Float, Const, Public, Some(FUNC_REAL));
float.register_builtin_py_impl(IMAG, Float, Const, Public, Some(FUNC_IMAG)); float.register_builtin_py_impl(IMAG, Float, Const, Public, Some(FUNC_IMAG));
float.register_builtin_py_impl( float.register_py_builtin(
FUNC_AS_INTEGER_RATIO,
fn0_met(Float, tuple_t(vec![Int, Int])),
Some(FUNC_AS_INTEGER_RATIO),
38,
);
float.register_py_builtin(
FUNC_CONJUGATE, FUNC_CONJUGATE,
fn0_met(Float, Float), fn0_met(Float, Float),
Const,
Public,
Some(FUNC_CONJUGATE), Some(FUNC_CONJUGATE),
45,
); );
float.register_py_builtin(FUNC_HEX, fn0_met(Float, Str), Some(FUNC_HEX), 24); float.register_py_builtin(FUNC_HEX, fn0_met(Float, Str), Some(FUNC_HEX), 24);
float.register_py_builtin( float.register_py_builtin(
FUNC_IS_INTEGER, FUNC_IS_INTEGER,
fn0_met(Float, Bool), fn0_met(Float, Bool),
Some(FUNC_IS_INTEGER), Some(FUNC_IS_INTEGER),
32, 69,
); );
float.register_builtin_py_impl( float.register_py_builtin(
FUNC_FROMHEX, FUNC_FROMHEX,
nd_func(vec![kw(KW_S, Str)], None, Float), nd_func(vec![kw(KW_S, Str)], None, Float),
Const,
Public,
Some(FUNC_FROMHEX), Some(FUNC_FROMHEX),
53,
); );
float.register_marker_trait(mono(NUM)); float.register_marker_trait(mono(NUM));
float.register_marker_trait(mono(ORD)); float.register_marker_trait(mono(ORD));
@ -199,28 +213,16 @@ impl Context {
int.register_marker_trait(mono(NUM)); int.register_marker_trait(mono(NUM));
// class("Rational"), // class("Rational"),
// class("Integral"), // class("Integral"),
int.register_builtin_py_impl(FUNC_ABS, fn0_met(Int, Nat), Immutable, Public, Some(OP_ABS)); int.register_py_builtin(FUNC_ABS, fn0_met(Int, Nat), Some(OP_ABS), 11);
int.register_builtin_py_impl( int.register_py_builtin(FUNC_SUCC, fn0_met(Int, Int), Some(FUNC_SUCC), 54);
FUNC_SUCC, int.register_py_builtin(FUNC_PRED, fn0_met(Int, Int), Some(FUNC_PRED), 47);
fn0_met(Int, Int),
Immutable,
Public,
Some(FUNC_SUCC),
);
int.register_builtin_py_impl(
FUNC_PRED,
fn0_met(Int, Int),
Immutable,
Public,
Some(FUNC_PRED),
);
int.register_py_builtin( int.register_py_builtin(
FUNC_BIT_LENGTH, FUNC_BIT_LENGTH,
fn0_met(Int, Nat), fn0_met(Int, Nat),
Some(FUNC_BIT_LENGTH), Some(FUNC_BIT_LENGTH),
28, 38,
); );
int.register_py_builtin(FUNC_BIT_COUNT, fn0_met(Int, Nat), Some(FUNC_BIT_COUNT), 17); int.register_py_builtin(FUNC_BIT_COUNT, fn0_met(Int, Nat), Some(FUNC_BIT_COUNT), 27);
let t_from_bytes = func( let t_from_bytes = func(
vec![kw( vec![kw(
BYTES, BYTES,
@ -238,13 +240,7 @@ impl Context {
)], )],
Int, Int,
); );
int.register_builtin_py_impl( int.register_py_builtin(FUNC_FROM_BYTES, t_from_bytes, Some(FUNC_FROM_BYTES), 40);
FUNC_FROM_BYTES,
t_from_bytes,
Const,
Public,
Some(FUNC_FROM_BYTES),
);
let t_to_bytes = func( let t_to_bytes = func(
vec![kw(KW_SELF, Int)], vec![kw(KW_SELF, Int)],
None, None,
@ -259,13 +255,7 @@ impl Context {
], ],
mono(BYTES), mono(BYTES),
); );
int.register_builtin_py_impl( int.register_py_builtin(FUNC_TO_BYTES, t_to_bytes, Some(FUNC_TO_BYTES), 55);
FUNC_TO_BYTES,
t_to_bytes,
Immutable,
Public,
Some(FUNC_TO_BYTES),
);
let mut int_ord = Self::builtin_methods(Some(mono(ORD)), 2); let mut int_ord = Self::builtin_methods(Some(mono(ORD)), 2);
int_ord.register_builtin_erg_impl( int_ord.register_builtin_erg_impl(
OP_PARTIAL_CMP, OP_PARTIAL_CMP,
@ -909,7 +899,7 @@ impl Context {
vec![kw(KW_ENCODING, Str), kw(KW_ERRORS, Str)], vec![kw(KW_ENCODING, Str), kw(KW_ERRORS, Str)],
Str, Str,
); );
bytes.register_builtin_erg_impl(FUNC_DECODE, decode_t, Immutable, Public); bytes.register_py_builtin(FUNC_DECODE, decode_t, Some(FUNC_DECODE), 6);
/* GenericTuple */ /* GenericTuple */
let mut generic_tuple = Self::builtin_mono_class(GENERIC_TUPLE, 1); let mut generic_tuple = Self::builtin_mono_class(GENERIC_TUPLE, 1);
generic_tuple.register_superclass(Obj, &obj); generic_tuple.register_superclass(Obj, &obj);
@ -1439,9 +1429,19 @@ impl Context {
self.register_builtin_type(Never, never, vis, Const, Some(NEVER)); self.register_builtin_type(Never, never, vis, Const, Some(NEVER));
self.register_builtin_type(Obj, obj, vis, Const, Some(FUNC_OBJECT)); self.register_builtin_type(Obj, obj, vis, Const, Some(FUNC_OBJECT));
// self.register_type(mono(RECORD), vec![], record, Private, Const); // self.register_type(mono(RECORD), vec![], record, Private, Const);
self.register_builtin_type(Int, int, vis, Const, Some(FUNC_INT)); let name = if cfg!(feature = "py_compatible") {
FUNC_INT
} else {
INT
};
self.register_builtin_type(Int, int, vis, Const, Some(name));
self.register_builtin_type(Nat, nat, vis, Const, Some(NAT)); self.register_builtin_type(Nat, nat, vis, Const, Some(NAT));
self.register_builtin_type(Float, float, vis, Const, Some(FUNC_FLOAT)); let name = if cfg!(feature = "py_compatible") {
FUNC_FLOAT
} else {
FLOAT
};
self.register_builtin_type(Float, float, vis, Const, Some(name));
self.register_builtin_type(Ratio, ratio, vis, Const, Some(RATIO)); self.register_builtin_type(Ratio, ratio, vis, Const, Some(RATIO));
let name = if cfg!(feature = "py_compatible") { let name = if cfg!(feature = "py_compatible") {
FUNC_BOOL FUNC_BOOL

View file

@ -100,6 +100,7 @@ const REAL: &str = "Real";
const FUNC_REAL: &str = "real"; const FUNC_REAL: &str = "real";
const IMAG: &str = "Imag"; const IMAG: &str = "Imag";
const FUNC_IMAG: &str = "imag"; const FUNC_IMAG: &str = "imag";
const FUNC_AS_INTEGER_RATIO: &str = "as_integer_ratio";
const FUNC_CONJUGATE: &str = "conjugate"; const FUNC_CONJUGATE: &str = "conjugate";
const FUNC_IS_INTEGER: &str = "is_integer"; const FUNC_IS_INTEGER: &str = "is_integer";
const FUNC_HEX: &str = "hex"; const FUNC_HEX: &str = "hex";

View file

@ -0,0 +1,44 @@
.Bytes: ClassType
.Bytes.
capitalize: (self: .Bytes) -> .Bytes
center: (self: .Bytes, width: Nat, fillchar := .Bytes) -> .Bytes
count: (self: .Bytes, sub: .Bytes, start := Nat, end := Nat) -> Nat
decode: (self: .Bytes, encoding := Str, errors := Str) -> .Bytes
endswith: (self: .Bytes, suffix: .Bytes, start := Nat, end := Nat) -> Bool
expandtabs: (self: .Bytes, tabsize: Nat) -> .Bytes
find: (self: .Bytes, sub: .Bytes, start := Nat, end := Nat) -> Nat or {-1}
fromhex: (string: Str) -> .Bytes
hex: (self: .Bytes) -> Str
index: (self: .Bytes, sub: .Bytes, start := Nat, end := Nat) -> Nat
isalnum: (self: .Bytes) -> Bool
isalpha: (self: .Bytes) -> Bool
isascii: (self: .Bytes) -> Bool
isdigit: (self: .Bytes) -> Bool
islower: (self: .Bytes) -> Bool
isspace: (self: .Bytes) -> Bool
istitle: (self: .Bytes) -> Bool
isupper: (self: .Bytes) -> Bool
join: (self: .Bytes, iterable_of_bytes: Iterable .Bytes) -> .Bytes
ljust: (self: .Bytes, width: Nat, fillchar := .Bytes) -> .Bytes
lower: (self: .Bytes) -> .Bytes
lstrip: (self: .Bytes, bytes := .Bytes or NoneType) -> .Bytes
maketrans: (frm: .Bytes, to: .Bytes) -> .Bytes
partition: (self: .Bytes, sep: .Bytes) -> (.Bytes, .Bytes, .Bytes)
removeprefix: (self: .Bytes, prefix: .Bytes) -> .Bytes
removesuffix: (self: .Bytes, suffix: .Bytes) -> .Bytes
replace: (self: .Bytes, old: .Bytes, new: .Bytes, count := Nat or {-1}) -> .Bytes
rfind: (self: .Bytes, sub: .Bytes, start := Nat, end := Nat) -> Nat or {-1}
rindex: (self: .Bytes, sub: .Bytes, start := Nat, end := Nat) -> Nat
rjust: (self: .Bytes, width: Nat, fillchar := .Bytes) -> .Bytes
rpartition: (self: .Bytes, sep: .Bytes) -> (.Bytes, .Bytes, .Bytes)
rsplit: (self: .Bytes, sep := .Bytes or NoneType, maxsplit := Nat or {-1}) -> [.Bytes; _]
rstrip: (self: .Bytes, bytes := .Bytes or NoneType) -> .Bytes
split: (self: .Bytes, sep := .Bytes or NoneType, maxsplit := Nat or {-1}) -> [.Bytes; _]
splitlines: (self: .Bytes, keepends := Bool) -> [.Bytes; _]
startswith: (self: .Bytes, prefix: .Bytes, start := Nat, end := Nat) -> Bool
strip: (self: .Bytes, bytes := .Bytes or NoneType) -> .Bytes
swapcase: (self: .Bytes) -> .Bytes
title: (self: .Bytes) -> .Bytes
translate: (self: .Bytes, table: .Bytes, delete := .Bytes) -> .Bytes
upper: (self: .Bytes) -> .Bytes
zfill: (self: .Bytes, width: Nat) -> .Bytes

View file

@ -23,7 +23,44 @@
''' '''
hex: (self: .Float) -> Str hex: (self: .Float) -> Str
''' '''
Return True if the float is an integer. Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original float
and with a positive denominator.
Raise `OverflowError` on infinities and a `ValueError` on NaNs.
'''
'''erg
assert (10.0).as_integer_ratio() == (10, 1)
assert (0.0).as_integer_ratio() == (0, 1)
assert (-.25).as_integer_ratio() == (-1, 4)
'''
as_integer_ratio: (self: .Float) -> (Int, Int)
'''
Return `self`, the complex conjugate of any float.
'''
'''erg
assert (1.0).conjugate() == 1.0
'''
conjugate: (self: .Float) -> .Float
'''
Create a floating-point number from a hexadecimal string.
'''
'''erg
assert Float.fromhex("0x1.ffffp10") == 2047.984375
assert Float.fromhex("-0x1p-1074") == -5e-324
'''
fromhex: (string: Str) -> .Float
'''
Return a hexadecimal representation of a floating-point number.
'''
'''erg
assert (-0.1).hex() == "-0x1.999999999999ap-4"
assert 3.14159.hex() == "0x1.921f9f01b866ep+1"
'''
hex: (self: .Float) -> Str
'''
Return `True` if the float is an integer.
''' '''
'''erg '''erg
assert (1.0).is_integer() assert (1.0).is_integer()

View file

@ -1,5 +1,15 @@
.Int: ClassType .Int: ClassType
.Int. .Int.
Real: Float
Imag: Float
Denominator: Nat
Numerator: Int
'''erg
assert 1.abs() == 1
assert -1.abs() == 1
'''
abs: (self: .Int) -> Nat
as_integer_ratio: (self: .Int) -> (Int, Int)
''' '''
Number of ones in the binary representation of the absolute value of self. Number of ones in the binary representation of the absolute value of self.
@ -26,3 +36,20 @@
assert 37.bit_length() == 6 assert 37.bit_length() == 6
''' '''
bit_length: (self: .Int) -> Nat bit_length: (self: .Int) -> Nat
conjugate: (self: .Int) -> .Int
from_bytes: (bytes: Bytes, byteorder := {"big", "little"}, signed := Bool) -> .Int
'''
Predecessor of `self` (`self -1`).
'''
'''erg
assert 1.pred() == 0
'''
pred: (self: .Int) -> .Int
'''
Successor of `self` (`self + 1`).
'''
'''erg
assert 1.succ() == 2
'''
succ: (self: .Int) -> .Int
to_bytes: (self: .Int, length := Nat, byteorder := {"big", "little"}, signed := Bool) -> Bytes

View file

@ -0,0 +1,9 @@
.Obj: ClassType
.Obj.
__dict__: {Str: .Obj}
__module__: Str
clone: |T <: .Obj|(self: T) -> T
__bytes__: (self: .Obj) -> Bytes
__repr__: (self: .Obj) -> Str
__sizeof__: (self: .Obj) -> Nat
__str__: (self: .Obj) -> Str

View file

@ -30,4 +30,4 @@
assert "hello".center(10) == " hello " assert "hello".center(10) == " hello "
assert "hello".center(10, "-") == "--hello---" assert "hello".center(10, "-") == "--hello---"
''' '''
center: (self: .Str, width: Int, fillchar: .Str) -> .Str center: (self: .Str, width: Int, fillchar := .Str) -> .Str

View file

@ -2,6 +2,7 @@ from _erg_result import Error
from _erg_control import then__ from _erg_control import then__
class Float(float): class Float(float):
EPSILON = 2.220446049250313e-16
def try_new(i): # -> Result[Nat] def try_new(i): # -> Result[Nat]
if isinstance(i, float): if isinstance(i, float):
Float(i) Float(i)
@ -37,6 +38,7 @@ class Float(float):
class FloatMut(): # inherits Float class FloatMut(): # inherits Float
value: Float value: Float
EPSILON = 2.220446049250313e-16
def __init__(self, i): def __init__(self, i):
self.value = Float(i) self.value = Float(i)
def __repr__(self): def __repr__(self):

12
tests/should_ok/int.er Normal file
View file

@ -0,0 +1,12 @@
assert 1.abs() == 1
assert -1.abs() == 1
assert bin(13) == "0b1101"
assert 13.bit_count() == 3
assert bin(37) == "0b100101"
assert 37.bit_length() == 6
assert 1.pred() == 0
assert 1.succ() == 2

View file

@ -91,6 +91,11 @@ fn exec_infer_trait() -> Result<(), ()> {
expect_success("tests/should_ok/infer_trait.er") expect_success("tests/should_ok/infer_trait.er")
} }
#[test]
fn exec_int() -> Result<(), ()> {
expect_success("tests/should_ok/int.er")
}
#[test] #[test]
fn exec_interpolation() -> Result<(), ()> { fn exec_interpolation() -> Result<(), ()> {
expect_success("tests/should_ok/interpolation.er") expect_success("tests/should_ok/interpolation.er")