mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-27 19:59:07 +00:00
feat: add some builtin python modules decls
This commit is contained in:
parent
e0d5cd2748
commit
f45c165499
19 changed files with 161 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
use std::collections::hash_map::{IntoValues, Iter, IterMut, Keys, Values, ValuesMut};
|
use std::collections::hash_map::{Entry, IntoValues, Iter, IterMut, Keys, Values, ValuesMut};
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
|
@ -237,4 +237,8 @@ impl<K: Hash + Eq, V> Dict<K, V> {
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn entry(&mut self, k: K) -> Entry<K, V> {
|
||||||
|
self.dict.entry(k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,12 +16,12 @@ pub const BUILTIN_PYTHON_MODS: [&str; 176] = [
|
||||||
"base64",
|
"base64",
|
||||||
"bdb",
|
"bdb",
|
||||||
"binascii",
|
"binascii",
|
||||||
"binhex",
|
|
||||||
"bisect",
|
"bisect",
|
||||||
"builtins",
|
"builtins",
|
||||||
"bz2",
|
"bz2",
|
||||||
"calendar",
|
"calendar",
|
||||||
"cmath",
|
"cmath",
|
||||||
|
"cmd",
|
||||||
"code",
|
"code",
|
||||||
"codecs",
|
"codecs",
|
||||||
"codeop",
|
"codeop",
|
||||||
|
@ -194,12 +194,12 @@ pub const BUILTIN_PYTHON_MODS: [&str; 170] = [
|
||||||
"base64",
|
"base64",
|
||||||
"bdb",
|
"bdb",
|
||||||
"binascii",
|
"binascii",
|
||||||
"binhex",
|
|
||||||
"bisect",
|
"bisect",
|
||||||
"builtins",
|
"builtins",
|
||||||
"bz2",
|
"bz2",
|
||||||
"calendar",
|
"calendar",
|
||||||
"cmath",
|
"cmath",
|
||||||
|
"cmd",
|
||||||
"code",
|
"code",
|
||||||
"codecs",
|
"codecs",
|
||||||
"codeop",
|
"codeop",
|
||||||
|
@ -368,12 +368,12 @@ pub const BUILTIN_PYTHON_MODS: [&str; 165] = [
|
||||||
"base64",
|
"base64",
|
||||||
"bdb",
|
"bdb",
|
||||||
"binascii",
|
"binascii",
|
||||||
"binhex",
|
|
||||||
"bisect",
|
"bisect",
|
||||||
"builtins",
|
"builtins",
|
||||||
"bz2",
|
"bz2",
|
||||||
"calendar",
|
"calendar",
|
||||||
"cmath",
|
"cmath",
|
||||||
|
"cmd",
|
||||||
"code",
|
"code",
|
||||||
"codecs",
|
"codecs",
|
||||||
"codeop",
|
"codeop",
|
||||||
|
|
23
crates/erg_compiler/lib/pystd/array.d.er
Normal file
23
crates/erg_compiler/lib/pystd/array.d.er
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
.Array! = 'array': ClassType
|
||||||
|
.Array!.
|
||||||
|
__call__: (typecode: Str, initializer := [Obj; _] or Iterable(Obj)) -> .Array
|
||||||
|
typecode: Str
|
||||||
|
itemsize: Nat
|
||||||
|
append: (self: RefMut(.Array!), x: Obj) => NoneType
|
||||||
|
buffer_info: (self: Ref(.Array!)) -> (Nat, Nat)
|
||||||
|
byteswap: (self: RefMut(.Array!)) => NoneType
|
||||||
|
count: (self: Ref(.Array!), x: Obj) -> Nat
|
||||||
|
extend: (self: RefMut(.Array!), iterable: Iterable(Obj)) => NoneType
|
||||||
|
frombytes: (self: RefMut(.Array!), bytes: Bytes) => NoneType
|
||||||
|
fromfile: (self: RefMut(.Array!), f: File!, n: Nat) => NoneType
|
||||||
|
fromlist: (self: RefMut(.Array!), list: [Obj; _]) => NoneType
|
||||||
|
fromunicode: (self: RefMut(.Array!), s: Str) => NoneType
|
||||||
|
index: (self: Ref(.Array!), x: Obj) -> Nat
|
||||||
|
insert: (self: RefMut(.Array!), i: Nat, x: Obj) => NoneType
|
||||||
|
pop: (self: RefMut(.Array!), i := Nat) -> Obj
|
||||||
|
remove: (self: RefMut(.Array!), x: Obj) => NoneType
|
||||||
|
reverse: (self: RefMut(.Array!)) => NoneType
|
||||||
|
tobytes: (self: Ref(.Array!)) -> Bytes
|
||||||
|
tofile: (self: Ref(.Array!), f: File!) => NoneType
|
||||||
|
tolist: (self: Ref(.Array!)) -> [Obj; _]
|
||||||
|
tounicode: (self: Ref(.Array!)) -> Str
|
66
crates/erg_compiler/lib/pystd/ast.d.er
Normal file
66
crates/erg_compiler/lib/pystd/ast.d.er
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
.AST: ClassType
|
||||||
|
|
||||||
|
.Constant: ClassType
|
||||||
|
.FormattedValue: ClassType
|
||||||
|
.JoinedStr: ClassType
|
||||||
|
.List: ClassType
|
||||||
|
.Tuple: ClassType
|
||||||
|
.Set: ClassType
|
||||||
|
.Dict: ClassType
|
||||||
|
.Name: ClassType
|
||||||
|
.Load: ClassType
|
||||||
|
.Store: ClassType
|
||||||
|
.Del: ClassType
|
||||||
|
.Starred: ClassType
|
||||||
|
.Expr: ClassType
|
||||||
|
.UnaryOp: ClassType
|
||||||
|
.UAdd: ClassType
|
||||||
|
.USub: ClassType
|
||||||
|
.Not: ClassType
|
||||||
|
.Invert: ClassType
|
||||||
|
.BinOp: ClassType
|
||||||
|
.Add: ClassType
|
||||||
|
.Sub: ClassType
|
||||||
|
.Mult: ClassType
|
||||||
|
.Div: ClassType
|
||||||
|
.FloorDiv: ClassType
|
||||||
|
.Mod: ClassType
|
||||||
|
.Pow: ClassType
|
||||||
|
.LShift: ClassType
|
||||||
|
.RShift: ClassType
|
||||||
|
.BitOr: ClassType
|
||||||
|
.BitXor: ClassType
|
||||||
|
.BitAnd: ClassType
|
||||||
|
.MatMult: ClassType
|
||||||
|
.BoolOp: ClassType
|
||||||
|
.And: ClassType
|
||||||
|
.Or: ClassType
|
||||||
|
.Compare: ClassType
|
||||||
|
.Eq: ClassType
|
||||||
|
.NotEq: ClassType
|
||||||
|
.Lt: ClassType
|
||||||
|
.LtE: ClassType
|
||||||
|
.Gt: ClassType
|
||||||
|
.GtE: ClassType
|
||||||
|
.Is: ClassType
|
||||||
|
.IsNot: ClassType
|
||||||
|
.In: ClassType
|
||||||
|
.NotIn: ClassType
|
||||||
|
.Call: ClassType
|
||||||
|
.Keyword = 'keyword': ClassType
|
||||||
|
.IfExp: ClassType
|
||||||
|
.Attribute: ClassType
|
||||||
|
.NamedExpr: ClassType
|
||||||
|
.Subscript: ClassType
|
||||||
|
.Slice: ClassType
|
||||||
|
.ListComp: ClassType
|
||||||
|
.SetComp: ClassType
|
||||||
|
.GeneratorExp: ClassType
|
||||||
|
.DictComp: ClassType
|
||||||
|
.Comprehension = 'comprehension': ClassType
|
||||||
|
.Assign: ClassType
|
||||||
|
.AnnAssign: ClassType
|
||||||
|
|
||||||
|
.parse: (source: Str, filename := Str, mode := Str) -> .AST
|
||||||
|
.unparse: (ast_obj: .AST) -> Str
|
||||||
|
.literal_eval: (node_or_string: .AST or Str) -> Obj
|
4
crates/erg_compiler/lib/pystd/asyncio.d.er
Normal file
4
crates/erg_compiler/lib/pystd/asyncio.d.er
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# TODO: return value
|
||||||
|
.run!: (coro: GenericCallable, debug := Bool) => NoneType
|
||||||
|
.sleep!: (delay: Float) => NoneType
|
||||||
|
.gather!: (*coros: GenericCallable) => NoneType
|
2
crates/erg_compiler/lib/pystd/atexit.d.er
Normal file
2
crates/erg_compiler/lib/pystd/atexit.d.er
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.register!: (func: GenericCallable, *args: Obj) => NoneType
|
||||||
|
.unregister!: (func: GenericCallable) => NoneType
|
6
crates/erg_compiler/lib/pystd/base64.d.er
Normal file
6
crates/erg_compiler/lib/pystd/base64.d.er
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
.b64encode: (s: Bytes, altchars := Bytes) -> Bytes
|
||||||
|
.b64decode: (s: Bytes, altchars := Bytes, validate := Bool) -> Bytes
|
||||||
|
.b32encode: (s: Bytes) -> Bytes
|
||||||
|
.b32decode: (s: Bytes, casefold := Bool, map01 := { Str : Str }) -> Bytes
|
||||||
|
.b16encode: (s: Bytes) -> Bytes
|
||||||
|
.b16decode: (s: Bytes, casefold := Bool) -> Bytes
|
4
crates/erg_compiler/lib/pystd/bdb.d.er
Normal file
4
crates/erg_compiler/lib/pystd/bdb.d.er
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
.BreakPoint: ClassType
|
||||||
|
.Bdb: ClassType
|
||||||
|
|
||||||
|
.set_trace!: () => NoneType
|
4
crates/erg_compiler/lib/pystd/binascii.d.er
Normal file
4
crates/erg_compiler/lib/pystd/binascii.d.er
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
.a2b_uu: (string: Str or Bytes) -> Bytes
|
||||||
|
|
||||||
|
.hexlify: (string: Bytes) -> Bytes
|
||||||
|
.unhexlify: (string: Bytes or Str) -> Bytes
|
3
crates/erg_compiler/lib/pystd/bisect.d.er
Normal file
3
crates/erg_compiler/lib/pystd/bisect.d.er
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
.bisect: |T|(a: [T; _], x: T) -> Nat
|
||||||
|
.bisect_left: |T|(a: [T; _], x: T) -> Nat
|
||||||
|
.bisect_right: |T|(a: [T; _], x: T) -> Nat
|
14
crates/erg_compiler/lib/pystd/bz2.d.er
Normal file
14
crates/erg_compiler/lib/pystd/bz2.d.er
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
.BZ2File: ClassType
|
||||||
|
.BZ2File.
|
||||||
|
# TODO: should be a procedure
|
||||||
|
__call__: (filename: Str, mode := Str) -> .BZ2File
|
||||||
|
peek: (self: Ref(.BZ2File), n := Nat) -> Str
|
||||||
|
read!: (self: RefMut(.BZ2File), size := Nat or {-1}) => Str
|
||||||
|
readline!: (self: RefMut(.BZ2File), size := Nat or {-1}) => Str
|
||||||
|
close!: (self: RefMut(.BZ2File)) => NoneType
|
||||||
|
closed: (self: Ref(.BZ2File)) -> Bool
|
||||||
|
write!: (self: RefMut(.BZ2File), data: Str) => Nat
|
||||||
|
flush!: (self: RefMut(.BZ2File)) => NoneType
|
||||||
|
seek!: (self: RefMut(.BZ2File), offset: Nat, whence := Nat) => Nat
|
||||||
|
|
||||||
|
.open!: (filename: Str, mode := Str, compresslevel := Nat, encoding := Str) => .BZ2File
|
8
crates/erg_compiler/lib/pystd/calendar.d.er
Normal file
8
crates/erg_compiler/lib/pystd/calendar.d.er
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
.Calendar: ClassType
|
||||||
|
.TextCalendar: ClassType
|
||||||
|
.HTMLCalendar: ClassType
|
||||||
|
.LocaleTextCalendar: ClassType
|
||||||
|
.LocaleHTMLCalendar: ClassType
|
||||||
|
|
||||||
|
.month: (theyear: Nat, themonth: Nat, w := Nat, l := Nat) -> Str
|
||||||
|
.calendar: (year: Nat, w := Nat, l := Nat, c := Nat, m := Nat) -> Str
|
0
crates/erg_compiler/lib/pystd/cmath.d.er
Normal file
0
crates/erg_compiler/lib/pystd/cmath.d.er
Normal file
1
crates/erg_compiler/lib/pystd/cmd.d.er
Normal file
1
crates/erg_compiler/lib/pystd/cmd.d.er
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.Cmd: ClassType
|
1
crates/erg_compiler/lib/pystd/code.d.er
Normal file
1
crates/erg_compiler/lib/pystd/code.d.er
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.compile_command: (source: Str, filename := Str, symbol := Str) -> Code or NoneType
|
0
crates/erg_compiler/lib/pystd/codecs.d.er
Normal file
0
crates/erg_compiler/lib/pystd/codecs.d.er
Normal file
0
crates/erg_compiler/lib/pystd/codeop.d.er
Normal file
0
crates/erg_compiler/lib/pystd/codeop.d.er
Normal file
0
crates/erg_compiler/lib/pystd/colorsys.d.er
Normal file
0
crates/erg_compiler/lib/pystd/colorsys.d.er
Normal file
|
@ -1,5 +1,9 @@
|
||||||
use erg::DummyVM;
|
use erg::DummyVM;
|
||||||
|
use erg_common::config::ErgConfig;
|
||||||
use erg_common::error::MultiErrorDisplay;
|
use erg_common::error::MultiErrorDisplay;
|
||||||
|
use erg_compiler::artifact::Buildable;
|
||||||
|
use erg_compiler::module::SharedCompilerResource;
|
||||||
|
use erg_compiler::HIRBuilder;
|
||||||
use erg_compiler::Transpiler;
|
use erg_compiler::Transpiler;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -24,3 +28,16 @@ fn test_transpiler_embedding() -> Result<(), ()> {
|
||||||
assert!(res.object.code.ends_with("(print)(Str(\"\"),)\n"));
|
assert!(res.object.code.ends_with("(print)(Str(\"\"),)\n"));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_builder() -> Result<(), ()> {
|
||||||
|
let mods = ["math", "time"];
|
||||||
|
let src = mods.into_iter().fold("".to_string(), |acc, module| {
|
||||||
|
acc + &format!("_ = pyimport \"{module}\"\n")
|
||||||
|
});
|
||||||
|
let cfg = ErgConfig::string(src.clone());
|
||||||
|
let shared = SharedCompilerResource::new(cfg.clone());
|
||||||
|
let mut checker = HIRBuilder::inherit(cfg, shared);
|
||||||
|
let _res = checker.build(src, "exec");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue