mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-03 14:04:33 +00:00
feat: add std.d
This commit is contained in:
parent
51cae591a3
commit
677ced0fcd
7 changed files with 131 additions and 14 deletions
|
@ -60,8 +60,6 @@ impl<Checker: BuildRunnable> Server<Checker> {
|
||||||
}
|
}
|
||||||
_ => format!("this {kind} cannot be renamed"),
|
_ => 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);
|
let edit = WorkspaceEdit::new(changes);
|
||||||
Self::send(
|
Self::send(
|
||||||
&json!({ "jsonrpc": "2.0", "id": msg["id"].as_i64().unwrap(), "result": edit }),
|
&json!({ "jsonrpc": "2.0", "id": msg["id"].as_i64().unwrap(), "result": edit }),
|
||||||
|
|
|
@ -16,6 +16,13 @@ fn _erg_std_path() -> PathBuf {
|
||||||
.canonicalize()
|
.canonicalize()
|
||||||
.expect("ERG_PATH/lib/std not found")
|
.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 {
|
fn _erg_pystd_path() -> PathBuf {
|
||||||
_erg_path()
|
_erg_path()
|
||||||
.join("lib")
|
.join("lib")
|
||||||
|
@ -34,6 +41,7 @@ fn _erg_external_lib_path() -> PathBuf {
|
||||||
thread_local! {
|
thread_local! {
|
||||||
pub static ERG_PATH: PathBuf = normalize_path(_erg_path());
|
pub static ERG_PATH: PathBuf = normalize_path(_erg_path());
|
||||||
pub static ERG_STD_PATH: PathBuf = normalize_path(_erg_std_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_PYSTD_PATH: PathBuf = normalize_path(_erg_pystd_path());
|
||||||
pub static ERG_EXTERNAL_LIB_PATH: PathBuf = normalize_path(_erg_external_lib_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())
|
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 {
|
pub fn erg_pystd_path() -> PathBuf {
|
||||||
ERG_PYSTD_PATH.with(|s| s.clone())
|
ERG_PYSTD_PATH.with(|s| s.clone())
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,19 +77,17 @@ impl Context {
|
||||||
Public,
|
Public,
|
||||||
Some(FUNC_CONJUGATE),
|
Some(FUNC_CONJUGATE),
|
||||||
);
|
);
|
||||||
float.register_builtin_py_impl(
|
float.register_py_builtin(
|
||||||
FUNC_IS_INTEGER,
|
|
||||||
fn0_met(Float, Bool),
|
|
||||||
Const,
|
|
||||||
Public,
|
|
||||||
Some(FUNC_IS_INTEGER),
|
|
||||||
);
|
|
||||||
float.register_builtin_py_impl(
|
|
||||||
FUNC_HEX,
|
FUNC_HEX,
|
||||||
fn0_met(Float, Str),
|
fn0_met(Float, Str),
|
||||||
Const,
|
|
||||||
Public,
|
|
||||||
Some(FUNC_HEX),
|
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(
|
float.register_builtin_py_impl(
|
||||||
FUNC_FROMHEX,
|
FUNC_FROMHEX,
|
||||||
|
@ -562,6 +560,7 @@ impl Context {
|
||||||
Public,
|
Public,
|
||||||
Some(FUNC_COUNT),
|
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);
|
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);
|
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);
|
str_.register_builtin_erg_impl(FUNDAMENTAL_GETITEM, str_getitem_t, Immutable, Public);
|
||||||
|
|
|
@ -134,6 +134,7 @@ const FUNC_UPPER: &str = "upper";
|
||||||
const FUNC_TO_INT: &str = "to_int";
|
const FUNC_TO_INT: &str = "to_int";
|
||||||
const FUNC_STARTSWITH: &str = "startswith";
|
const FUNC_STARTSWITH: &str = "startswith";
|
||||||
const FUNC_ENDSWITH: &str = "endswith";
|
const FUNC_ENDSWITH: &str = "endswith";
|
||||||
|
const FUNC_CAPITALIZE: &str = "capitalize";
|
||||||
const FUNC_CONTAINS: &str = "contains";
|
const FUNC_CONTAINS: &str = "contains";
|
||||||
const FUNC_SPLIT: &str = "split";
|
const FUNC_SPLIT: &str = "split";
|
||||||
const FUNC_SPLITLINES: &str = "splitlines";
|
const FUNC_SPLITLINES: &str = "splitlines";
|
||||||
|
@ -438,6 +439,15 @@ pub fn builtins_path() -> PathBuf {
|
||||||
PathBuf::from("lib/pystd/builtins.d.er")
|
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 {
|
impl Context {
|
||||||
fn register_builtin_decl(
|
fn register_builtin_decl(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
@ -578,14 +588,19 @@ impl Context {
|
||||||
} else {
|
} else {
|
||||||
VarName::from_static(name)
|
VarName::from_static(name)
|
||||||
};
|
};
|
||||||
let vis = if cfg!(feature = "py_compatible") {
|
let vis = if cfg!(feature = "py_compatible") || &self.name[..] != "<builtins>" {
|
||||||
Public
|
Public
|
||||||
} else {
|
} else {
|
||||||
Private
|
Private
|
||||||
};
|
};
|
||||||
let muty = Immutable;
|
let muty = Immutable;
|
||||||
let loc = Location::range(lineno, 0, lineno, name.inspect().len() as u32);
|
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);
|
self.register_builtin_impl(name, t, muty, vis, py_name, abs_loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
32
crates/erg_compiler/lib/std.d/Float.d.er
Normal file
32
crates/erg_compiler/lib/std.d/Float.d.er
Normal 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
|
28
crates/erg_compiler/lib/std.d/Int.d.er
Normal file
28
crates/erg_compiler/lib/std.d/Int.d.er
Normal 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
|
33
crates/erg_compiler/lib/std.d/Str.d.er
Normal file
33
crates/erg_compiler/lib/std.d/Str.d.er
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue