Delete prelude.er (-> _prelude.er)

This commit is contained in:
Shunsuke Shibayama 2022-10-17 11:28:08 +09:00
parent 1154a54c39
commit cc2b3c4b11
6 changed files with 47 additions and 41 deletions

View file

@ -301,6 +301,7 @@ fn convert_to_python_name(name: Str) -> Str {
} }
} }
/// This method obviously does not scale, so in the future all Python APIs will be replaced by declarations in d.er, and renaming will be done in `HIRDesugarer`.
fn escape_name(ident: Identifier) -> Str { fn escape_name(ident: Identifier) -> Str {
let vis = ident.vis(); let vis = ident.vis();
let mut name = convert_to_python_name(ident.name.into_token().content).to_string(); let mut name = convert_to_python_name(ident.name.into_token().content).to_string();

View file

@ -13,8 +13,8 @@ PartialOrd(R := Self) = Trait {
} }
Ord = Subsume PartialOrd() Ord = Subsume PartialOrd()
EqForOrd R = Patch Ord, Impl := Eq() EqForOrd R = Patch Ord
EqForOrd(R). EqForOrd(R)|<: Eq()|.
`==`(self, other: R): Bool = self.cmp(other) == Ordering.Equal `==`(self, other: R): Bool = self.cmp(other) == Ordering.Equal
LeForOrd = Patch Ord LeForOrd = Patch Ord
@ -36,9 +36,9 @@ Add(R := Self) = Trait {
} }
Sub(R := Self) = Trait { Sub(R := Self) = Trait {
.Output = Type .Output = Type
.`_-_` = (self: Self, R) -> Self.Output .`-` = (self: Self, R) -> Self.Output
} }
Mul(R := Self()) = Trait { Mul(R := Self) = Trait {
.Output = Type .Output = Type
.`*` = (self: Self, R) -> Self.Output .`*` = (self: Self, R) -> Self.Output
} }
@ -46,7 +46,6 @@ Div(R := Self) = Trait {
.Output = Type .Output = Type
.`/` = (self: Self, R) -> Self.Output or Panic .`/` = (self: Self, R) -> Self.Output or Panic
} }
Num: (R := Type) -> Type
Num = Add and Sub and Mul Num = Add and Sub and Mul
Seq T = Trait { Seq T = Trait {
@ -54,29 +53,40 @@ Seq T = Trait {
.get = (self: Ref(Self), Nat) -> T .get = (self: Ref(Self), Nat) -> T
} }
`_+_`: |R: Type, A <: Add(R)| (A, R) -> A.AddO AddForInt = Patch Int
`_-_`: |R: Type, S <: Add(R)| (S, R) -> S.SubO AddForInt|<: Add(Int)|.
`*`: |R, O: Type, M <: Add(R)| (M, R) -> M.MulO AddO = Int
`/`: |R, O: Type, D <: Add(R)| (D, R) -> D.DivO `_+_`(self: Self, other: Int): Int = magic("Add.`_+_`")
AddForInt = Patch Int, Impl := Add()
AddForInt.AddO = Int
AddForInt.
`_+_`: (self: Self, other: Int) -> Int = magic("Add.`_+_`")
# TODO: Mul and Div # TODO: Mul and Div
NumForInterval M, N, O, P: Int = NumForInterval M, N, O, P: Int = Patch M..N
Patch M..N, Impl := Add(R := O..P) and Sub(R := O..P) NumForInterval(M, N, O, P)|<: Add(O..P)|.
NumForInterval(M, N, O, P). Output = M+O..N+P
`_+_`: (self: Self, other: O..P) -> M+O..N+P = magic("NumForInterval.`_+_`") __add__(self: Self, other: O..P) = magic("NumForInterval.`_+_`")
`_-_`: (self: Self, other: O..P) -> M-P..N-O = magic("NumForInterval.`_-_`") NumForInterval(M, N, O, P)|<: Sub(O..P)|.
Output = M-P..N-O
__sub__(self: Self, other: O..P) = magic("NumForInterval.`_-_`")
Read = Trait { Read = Trait {
.read = (self: Ref(Self)) -> Str .read = (self: Ref(Self),) -> Str
} }
Read! = Trait { Read! = Trait {
.read! = (self: Ref!(Self)) => Str .read! = (self: Ref!(Self),) => Str
} }
Write! = Trait { Write! = Trait {
.write! = (self: Ref!(Self), Str) => () .write! = (self: Ref!(Self), Str) => ()
} }
discard _x = None
discard 1
# if: |T, U|(Bool, T, U) -> T or U
cond|T: Type|(c: Bool, then: T, else: T): T =
if c:
do then
do else
assert cond(False, 1, 2) == 2
# assert cond(True, 1, 3) == "a"
# assert "a" == cond(True, 1, 3)

View file

@ -1,13 +0,0 @@
discard _x = None
discard 1
# if: |T, U|(Bool, T, U) -> T or U
cond|T: Type|(c: Bool, then: T, else: T): T =
if c:
do then
do else
assert cond(False, 1, 2) == 2
# assert cond(True, 1, 3) == "a"
# assert "a" == cond(True, 1, 3)

View file

@ -186,7 +186,8 @@ impl Lexer /*<'a>*/ {
fn is_definable_operator(s: &str) -> bool { fn is_definable_operator(s: &str) -> bool {
matches!( matches!(
s, s,
"+" | "-" "+_" | "_+_"
| "-"
| "*" | "*"
| "/" | "/"
| "//" | "//"
@ -1127,6 +1128,18 @@ impl Iterator for Lexer /*<'a>*/ {
return self.accept(Symbol, &op); return self.accept(Symbol, &op);
} else { } else {
let token = self.emit_token(Illegal, &op); let token = self.emit_token(Illegal, &op);
let hint = if op.contains('+') {
Some(
switch_lang!(
"japanese" => "二項演算子の+は`_+_`、単項演算子の+は`+_`です",
"simplified_chinese" => "二元运算符+是`_+_`,一元运算符+是`+_`",
"traditional_chinese" => "二元運算符+是`_+_`,一元運算符+是`+_`",
"english" => "the binary operator + is `_+_`, the unary operator + is `+_`",
).into(),
)
} else {
None
};
return Some(Err(LexError::syntax_error( return Some(Err(LexError::syntax_error(
0, 0,
token.loc(), token.loc(),
@ -1136,7 +1149,7 @@ impl Iterator for Lexer /*<'a>*/ {
"traditional_chinese" => format!("`{}`不能由用戶定義", &token.content), "traditional_chinese" => format!("`{}`不能由用戶定義", &token.content),
"english" => format!("`{}` cannot be defined by user", &token.content), "english" => format!("`{}` cannot be defined by user", &token.content),
), ),
None, hint,
))); )));
} }
} }

View file

@ -66,11 +66,6 @@ fn exec_move_check() -> Result<(), ()> {
expect_failure("examples/move_check.er") expect_failure("examples/move_check.er")
} }
#[test]
fn exec_prelude() -> Result<(), ()> {
expect_success("compiler/erg_compiler/lib/std/prelude.er")
}
#[test] #[test]
fn exec_pyimport() -> Result<(), ()> { fn exec_pyimport() -> Result<(), ()> {
expect_end_with("examples/pyimport.er", 111) expect_end_with("examples/pyimport.er", 111)