Skip to content

Commit

Permalink
feat: enhance os, random type decls
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Mar 24, 2024
1 parent 54bd111 commit 35f55c6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/erg_compiler/context/instantiate_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,10 @@ impl Context {
if let Some(Greater) = self.try_cmp(&l, &r) {
panic!("{l}..{r} is not a valid interval type (should be lhs <= rhs)")
}
Ok(int_interval(op, l, r))
let l_t = self.get_tp_t(&l).unwrap_or(Obj).derefine();
let r_t = self.get_tp_t(&r).unwrap_or(Obj).derefine();
let t = self.union(&l_t, &r_t);
Ok(interval(op, t, l, r))
}
TypeSpec::Subr(subr) => {
let mut errs = TyCheckErrors::empty();
Expand Down
29 changes: 29 additions & 0 deletions crates/erg_compiler/lib/pystd/os.d/__init__.d.er
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ The name of the operating system dependent module imported. The following names
.O_SHLOCK: Nat
.O_EXLOCK: Nat

.POSIX_FADV_NORMAL: Nat
.POSIX_FADV_SEQUENTIAL: Nat
.POSIX_FADV_RANDOM: Nat
.POSIX_FADV_NOREUSE: Nat
.POSIX_FADV_WILLNEED: Nat
.POSIX_FADV_DONTNEED: Nat

.PRIO_PGRP: Nat
.PRIO_PROCESS: Nat
.PRIO_USER: Nat
Expand All @@ -89,12 +96,23 @@ The name of the operating system dependent module imported. The following names
.PRIO_DARWIN_BG: Nat
.PRIO_DARWIN_NONUI: Nat

.RWF_APPEND: Nat
.RWF_DSYNC: Nat
.RWF_HIPRI: Nat
.RWF_NOWAIT: Nat
.RWF_SYNC: Nat

.SEEK_SET: Nat
.SEEK_CUR: Nat
.SEEK_END: Nat
.SEEK_HOLE: Nat
.SEEK_DATA: Nat

.SF_MNOWAIT: Nat
.SF_NOCACHE: Nat
.SF_NODISKIO: Nat
.SF_SYNC: Nat

.close!: (fd: Nat or FileDescriptor) => NoneType
.closerange!: (fd_low: Nat, fd_high: Nat) => NoneType
.ctermid!: () => Str
Expand Down Expand Up @@ -150,14 +168,25 @@ The name of the operating system dependent module imported. The following names
.openpty!: () => (Nat, Nat)
.pipe!: () => (Nat, Nat)
.pipe2!: (flags: Nat) => (Nat, Nat)
# TODO: only on linux
.posix_fallocate!: (fd: Nat or FileDescriptor, offset: Nat, len: Nat) => NoneType
.posix_fadvise!: (fd: Nat or FileDescriptor, offset: Nat, len: Nat, advice: Nat) => NoneType
.pread!: (fd: Nat or FileDescriptor, n: Nat, offset: Nat) => Bytes
.preadv!: (fd: Nat or FileDescriptor, buffers: RefMut(ByteArray!), offset: Nat, flags := Nat) => Nat
.putenv!: (key: Str, value: Str) => NoneType
.pwrite!: (fd: Nat or FileDescriptor, str: Bytes, offset: Nat) => Nat
.pwritev!: (fd: Nat or FileDescriptor, buffers: Ref(ByteArray!), offset: Nat, flags := Nat) => Nat
.read!: (fd: Nat or FileDescriptor, n: Nat) => Bytes
.readlink!: (path: PathLike, dir_fd := Nat or FileDescriptor) => Str
.remove!: (path: PathLike,) => NoneType
.removedirs!: (path: PathLike,) => NoneType
.rename!: (src: PathLike, dst: PathLike) => NoneType
.renames!: (old: PathLike, new: PathLike) => NoneType
.replace!: (src: PathLike, dst: PathLike) => NoneType
.rmdir!: (path: PathLike,) => NoneType
.scandir!: (path := PathLike,) => Iterator DirEntry
.sendfile!: (out_fd: Nat or FileDescriptor, in_fd: Nat or FileDescriptor, offset := Nat, count := Nat) => Nat
.set_blocking!: (fd: Nat or FileDescriptor, blocking: Bool) => NoneType
.setegid!: (egid: Nat) => NoneType
.seteuid!: (euid: Nat) => NoneType
.setgid!: (gid: Nat) => NoneType
Expand Down
24 changes: 24 additions & 0 deletions crates/erg_compiler/lib/pystd/random.d.er
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
.Random!: ClassType
.Random! <: InheritableType
.Random!.
seed!: (self: RefMut(.Random!), a := Float, version := Nat) => NoneType
getstate!: (self: Ref(.Random!)) => (Nat, [Nat; _], Nat or NoneType)
setstate!: (self: RefMut(.Random!), state: (Nat, [Nat; _], Nat or NoneType)) => NoneType
random!: (self: RefMut(.Random!)) => Float
getrandbits!: (self: RefMut(.Random!), k: Nat) => Nat

.SystemRandom!: ClassType
.SystemRandom! <: InheritableType

.seed!: (a := Num, version := Int) => NoneType
.randbytes!: (n: Nat) => Bytes
.randrange!: (start: Int, stop := Int, step := Int) => Int
.randint!: (a: Int, b: Int) => Int
.getrandbits!: (k: Nat) => Nat
.getstate!: () => (Nat, [Nat; _], Nat or NoneType)
.setstate!: (state: (Nat, [Nat; _], Nat or NoneType)) => NoneType
.choice!: |T: Type|(seq: Sequence(T)) => T
# TODO: dependent length array type
.choices!: |T: Type|(population: Sequence(T), weights := [Nat; _] or NoneType, k := Nat) => [T; _]
Expand All @@ -12,3 +26,13 @@
.random!: () => 0.0..1.0 # TODO: <1.0
.dep_uniform! = 'uniform': |A: Int, B: Int|(a: {A}, b: {B}) => A..B
.uniform!: (a: Int, b: Int) => Int
.triangular!: (low: Float, high: Float, mode: Float) => Float
.betavariate!: (alpha: Float, beta: Float) => 0.0..1.0
.expovariate!: (lambd := Float) => Float
.gammavariate!: (alpha: Float, beta: Float) => Float
.gauss!: (mu := Float, sigma := Float) => Float
.lognormvariate!: (mu: Float, sigma: Float) => Float
.normalvariate!: (mu := Float, sigma := Float) => Float
.vonmisesvariate!: (mu: Float, kappa: Float) => Float
.paretovariate!: (alpha: Float) => Float
.weibullvariate!: (alpha: Float, beta: Float) => Float
6 changes: 6 additions & 0 deletions examples/move_check.er
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ v.push! 1
w = v
print! w
print! v # this should cause a MoveError

push! |T| a: RefMut(Array!(T, _)), value: T =
a.push! value

push! w, 2
print! w # OK

0 comments on commit 35f55c6

Please sign in to comment.