feat: add some builtin python modules decls

This commit is contained in:
Shunsuke Shibayama 2023-03-15 13:51:11 +09:00
parent e0d5cd2748
commit f45c165499
19 changed files with 161 additions and 4 deletions

View file

@ -1,5 +1,5 @@
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::hash::{Hash, Hasher};
use std::iter::FromIterator;
@ -237,4 +237,8 @@ impl<K: Hash + Eq, V> Dict<K, V> {
}
self
}
pub fn entry(&mut self, k: K) -> Entry<K, V> {
self.dict.entry(k)
}
}

View file

@ -16,12 +16,12 @@ pub const BUILTIN_PYTHON_MODS: [&str; 176] = [
"base64",
"bdb",
"binascii",
"binhex",
"bisect",
"builtins",
"bz2",
"calendar",
"cmath",
"cmd",
"code",
"codecs",
"codeop",
@ -194,12 +194,12 @@ pub const BUILTIN_PYTHON_MODS: [&str; 170] = [
"base64",
"bdb",
"binascii",
"binhex",
"bisect",
"builtins",
"bz2",
"calendar",
"cmath",
"cmd",
"code",
"codecs",
"codeop",
@ -368,12 +368,12 @@ pub const BUILTIN_PYTHON_MODS: [&str; 165] = [
"base64",
"bdb",
"binascii",
"binhex",
"bisect",
"builtins",
"bz2",
"calendar",
"cmath",
"cmd",
"code",
"codecs",
"codeop",

View 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

View 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

View file

@ -0,0 +1,4 @@
# TODO: return value
.run!: (coro: GenericCallable, debug := Bool) => NoneType
.sleep!: (delay: Float) => NoneType
.gather!: (*coros: GenericCallable) => NoneType

View file

@ -0,0 +1,2 @@
.register!: (func: GenericCallable, *args: Obj) => NoneType
.unregister!: (func: GenericCallable) => NoneType

View 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

View file

@ -0,0 +1,4 @@
.BreakPoint: ClassType
.Bdb: ClassType
.set_trace!: () => NoneType

View file

@ -0,0 +1,4 @@
.a2b_uu: (string: Str or Bytes) -> Bytes
.hexlify: (string: Bytes) -> Bytes
.unhexlify: (string: Bytes or Str) -> Bytes

View 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

View 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

View 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

View file

View file

@ -0,0 +1 @@
.Cmd: ClassType

View file

@ -0,0 +1 @@
.compile_command: (source: Str, filename := Str, symbol := Str) -> Code or NoneType

View file

@ -1,5 +1,9 @@
use erg::DummyVM;
use erg_common::config::ErgConfig;
use erg_common::error::MultiErrorDisplay;
use erg_compiler::artifact::Buildable;
use erg_compiler::module::SharedCompilerResource;
use erg_compiler::HIRBuilder;
use erg_compiler::Transpiler;
#[test]
@ -24,3 +28,16 @@ fn test_transpiler_embedding() -> Result<(), ()> {
assert!(res.object.code.ends_with("(print)(Str(\"\"),)\n"));
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(())
}