feat: add std.d

This commit is contained in:
Shunsuke Shibayama 2023-02-17 21:09:23 +09:00
parent 51cae591a3
commit 677ced0fcd
7 changed files with 131 additions and 14 deletions

View file

@ -60,8 +60,6 @@ impl<Checker: BuildRunnable> Server<Checker> {
}
_ => format!("this {kind} cannot be renamed"),
};
// identical change (to avoid displaying "no result")
Self::commit_change(&mut changes, &vi.def_loc, tok.content.to_string());
let edit = WorkspaceEdit::new(changes);
Self::send(
&json!({ "jsonrpc": "2.0", "id": msg["id"].as_i64().unwrap(), "result": edit }),

View file

@ -16,6 +16,13 @@ fn _erg_std_path() -> PathBuf {
.canonicalize()
.expect("ERG_PATH/lib/std not found")
}
fn _erg_std_decl_path() -> PathBuf {
_erg_path()
.join("lib")
.join("std.d")
.canonicalize()
.expect("ERG_PATH/lib/std.d not found")
}
fn _erg_pystd_path() -> PathBuf {
_erg_path()
.join("lib")
@ -34,6 +41,7 @@ fn _erg_external_lib_path() -> PathBuf {
thread_local! {
pub static ERG_PATH: PathBuf = normalize_path(_erg_path());
pub static ERG_STD_PATH: PathBuf = normalize_path(_erg_std_path());
pub static ERG_STD_DECL_PATH: PathBuf = normalize_path(_erg_std_decl_path());
pub static ERG_PYSTD_PATH: PathBuf = normalize_path(_erg_pystd_path());
pub static ERG_EXTERNAL_LIB_PATH: PathBuf = normalize_path(_erg_external_lib_path());
}
@ -46,6 +54,10 @@ pub fn erg_std_path() -> PathBuf {
ERG_STD_PATH.with(|s| s.clone())
}
pub fn erg_std_decl_path() -> PathBuf {
ERG_STD_DECL_PATH.with(|s| s.clone())
}
pub fn erg_pystd_path() -> PathBuf {
ERG_PYSTD_PATH.with(|s| s.clone())
}

View file

@ -77,19 +77,17 @@ impl Context {
Public,
Some(FUNC_CONJUGATE),
);
float.register_builtin_py_impl(
FUNC_IS_INTEGER,
fn0_met(Float, Bool),
Const,
Public,
Some(FUNC_IS_INTEGER),
);
float.register_builtin_py_impl(
float.register_py_builtin(
FUNC_HEX,
fn0_met(Float, Str),
Const,
Public,
Some(FUNC_HEX),
24,
);
float.register_py_builtin(
FUNC_IS_INTEGER,
fn0_met(Float, Bool),
Some(FUNC_IS_INTEGER),
32,
);
float.register_builtin_py_impl(
FUNC_FROMHEX,
@ -562,6 +560,7 @@ impl Context {
Public,
Some(FUNC_COUNT),
);
str_.register_py_builtin(FUNC_CAPITALIZE, fn0_met(Str, Str), Some(FUNC_CAPITALIZE), 13);
str_.register_builtin_erg_impl(FUNC_CONTAINS, fn1_met(Str, Str, Bool), Immutable, Public);
let str_getitem_t = fn1_kw_met(Str, kw(KW_IDX, Nat), Str);
str_.register_builtin_erg_impl(FUNDAMENTAL_GETITEM, str_getitem_t, Immutable, Public);

View file

@ -134,6 +134,7 @@ const FUNC_UPPER: &str = "upper";
const FUNC_TO_INT: &str = "to_int";
const FUNC_STARTSWITH: &str = "startswith";
const FUNC_ENDSWITH: &str = "endswith";
const FUNC_CAPITALIZE: &str = "capitalize";
const FUNC_CONTAINS: &str = "contains";
const FUNC_SPLIT: &str = "split";
const FUNC_SPLITLINES: &str = "splitlines";
@ -438,6 +439,15 @@ pub fn builtins_path() -> PathBuf {
PathBuf::from("lib/pystd/builtins.d.er")
}
#[cfg(not(feature = "no_std"))]
pub fn std_decl_path() -> PathBuf {
erg_common::env::erg_std_decl_path()
}
#[cfg(feature = "no_std")]
pub fn builtins_path() -> PathBuf {
PathBuf::from("lib/std.d")
}
impl Context {
fn register_builtin_decl(
&mut self,
@ -578,14 +588,19 @@ impl Context {
} else {
VarName::from_static(name)
};
let vis = if cfg!(feature = "py_compatible") {
let vis = if cfg!(feature = "py_compatible") || &self.name[..] != "<builtins>" {
Public
} else {
Private
};
let muty = Immutable;
let loc = Location::range(lineno, 0, lineno, name.inspect().len() as u32);
let abs_loc = AbsLocation::new(Some(builtins_path()), loc);
let module = if &self.name[..] == "<builtins>" {
builtins_path()
} else {
std_decl_path().join(format!("{}.d.er", self.name))
};
let abs_loc = AbsLocation::new(Some(module), loc);
self.register_builtin_impl(name, t, muty, vis, py_name, abs_loc);
}

View file

@ -0,0 +1,32 @@
.Float: ClassType
.Float.
'''
the real part of a complex number
'''
'''japanese
複素数の実部
'''
Real: Float
'''
the imaginary part of a complex number
'''
'''japanese
複素数の虚部
'''
Imag: Float
'''
Return a hexadecimal representation of a floating-point number.
'''
'''erg
assert (100.0).hex() == "0x1.9000000000000p+6"
assert (12.34).hex() == "0x1.8ae147ae147aep+3"
'''
hex: (self: .Float) -> Str
'''
Return True if the float is an integer.
'''
'''erg
assert (1.0).is_integer()
assert not (1.1).is_integer()
'''
is_integer: (self: .Float) -> Bool

View file

@ -0,0 +1,28 @@
.Int: ClassType
.Int.
'''
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
'''
'''japanese
2進数表現の絶対値の中の1の数
ハミング重みとも呼ばれる
'''
'''erg
assert bin(13) == "0b1101"
assert 13.bit_count() == 3
'''
bit_count: (self: .Int) -> Nat
'''
Number of bits necessary to represent self in binary.
'''
'''japanese
2進数表現においてselfを表すのに必要なビット数
'''
'''erg
assert bin(37) == "0b100101"
assert 37.bit_length() == 6
'''
bit_length: (self: .Int) -> Nat

View file

@ -0,0 +1,33 @@
.Str: ClassType
.Str.
'''
Return a capitalized version of the string.
More specifically, make the first character have upper case and the rest lower
case.
'''
'''erg
assert "hello".capitalize() == "Hello"
assert "HELLO".capitalize() == "Hello"
'''
capitalize: (self: .Str) -> .Str
'''
Return a version of the string suitable for caseless comparisons.
'''
'''erg
assert "camelCase".casefold() == "camelcase"
assert "CamelCase".casefold() == "camelcase"
assert "FULLCAPS".casefold() == "fullcaps"
assert "snake_case".casefold() == "snake_case"
'''
casefold: (self: .Str) -> .Str
'''
Return a centered string of length width.
Padding is done using the specified fill character (default is a space).
'''
'''erg
assert "hello".center(10) == " hello "
assert "hello".center(10, "-") == "--hello---"
'''
center: (self: .Str, width: Int, fillchar: .Str) -> .Str