Fix builtin types methods

This commit is contained in:
Shunsuke Shibayama 2023-02-03 02:17:44 +09:00
parent 8cdc735486
commit d5e9649172
5 changed files with 107 additions and 101 deletions

View file

@ -10,29 +10,29 @@ class Float(float):
def mutate(self): def mutate(self):
return FloatMut(self) return FloatMut(self)
def __add__(self, other): def __add__(self, other):
return then__(super().__add__(other), Float) return then__(float.__add__(self, other), Float)
def __radd__(self, other): def __radd__(self, other):
return then__(super().__radd__(other), Float) return then__(float.__add__(other, self), Float)
def __sub__(self, other): def __sub__(self, other):
return then__(super().__sub__(other), Float) return then__(float.__sub__(self, other), Float)
def __rsub__(self, other): def __rsub__(self, other):
return then__(super().__rsub__(other), Float) return then__(float.__sub__(other, self), Float)
def __mul__(self, other): def __mul__(self, other):
return then__(super().__mul__(other), Float) return then__(float.__mul__(self, other), Float)
def __rmul__(self, other): def __rmul__(self, other):
return then__(super().__rmul__(other), Float) return then__(float.__mul__(other, self), Float)
def __div__(self, other): def __div__(self, other):
return then__(super().__div__(other), Float) return then__(float.__div__(self, other), Float)
def __rdiv__(self, other): def __rdiv__(self, other):
return then__(super().__rdiv__(other), Float) return then__(float.__div__(other, self), Float)
def __floordiv__(self, other): def __floordiv__(self, other):
return then__(super().__floordiv__(other), Float) return then__(float.__floordiv__(self, other), Float)
def __rfloordiv__(self, other): def __rfloordiv__(self, other):
return then__(super().__rfloordiv__(other), Float) return then__(float.__floordiv__(other, self), Float)
def __pow__(self, other): def __pow__(self, other):
return then__(super().__pow__(other), Float) return then__(float.__pow__(self, other), Float)
def __rpow__(self, other): def __rpow__(self, other):
return then__(super().__rpow__(other), Float) return then__(float.__pow__(other, self), Float)
class FloatMut(): # inherits Float class FloatMut(): # inherits Float
value: Float value: Float

View file

@ -14,29 +14,29 @@ class Int(int):
def mutate(self): def mutate(self):
return IntMut(self) return IntMut(self)
def __add__(self, other): def __add__(self, other):
return then__(super().__add__(other), Int) return then__(int.__add__(self, other), Int)
def __radd__(self, other): def __radd__(self, other):
return then__(super().__radd__(other), Int) return then__(int.__add__(other, self), Int)
def __sub__(self, other): def __sub__(self, other):
return then__(super().__sub__(other), Int) return then__(int.__sub__(self, other), Int)
def __rsub__(self, other): def __rsub__(self, other):
return then__(super().__rsub__(other), Int) return then__(int.__sub__(other, self), Int)
def __mul__(self, other): def __mul__(self, other):
return then__(super().__mul__(other), Int) return then__(int.__mul__(self, other), Int)
def __rmul__(self, other): def __rmul__(self, other):
return then__(super().__rmul__(other), Int) return then__(int.__mul__(other, self), Int)
def __div__(self, other): def __div__(self, other):
return then__(super().__div__(other), Int) return then__(int.__div__(self, other), Int)
def __rdiv__(self, other): def __rdiv__(self, other):
return then__(super().__rdiv__(other), Int) return then__(int.__div__(other, self), Int)
def __floordiv__(self, other): def __floordiv__(self, other):
return then__(super().__floordiv__(other), Int) return then__(int.__floordiv__(self, other), Int)
def __rfloordiv__(self, other): def __rfloordiv__(self, other):
return then__(super().__rfloordiv__(other), Int) return then__(int.__floordiv__(other, self), Int)
def __pow__(self, other): def __pow__(self, other):
return then__(super().__pow__(other), Int) return then__(int.__pow__(self, other), Int)
def __rpow__(self, other): def __rpow__(self, other):
return then__(super().__rpow__(other), Int) return then__(int.__pow__(other, self), Int)
class IntMut(): # inherits Int class IntMut(): # inherits Int
value: Int value: Int

View file

@ -1,5 +1,6 @@
from _erg_result import Error from _erg_result import Error
from _erg_int import Int, IntMut from _erg_int import Int
from _erg_int import IntMut # don't unify with the above line
from _erg_control import then__ from _erg_control import then__
class Nat(Int): class Nat(Int):

View file

@ -20,13 +20,13 @@ class Str(str):
def to_int(self): def to_int(self):
return Int(self) if self.isdigit() else None return Int(self) if self.isdigit() else None
def __add__(self, other): def __add__(self, other):
return then__(super().__add__(other), Str) return then__(str.__add__(self, other), Str)
def __radd__(self, other): def __radd__(self, other):
return then__(super().__radd__(other), Str) return then__(str.__add__(other, self), Str)
def __mul__(self, other): def __mul__(self, other):
return then__(super().__mul__(other), Str) return then__(str.__mul__(self, other), Str)
def __mod__(self, other): def __mod__(self, other):
return then__(super().__mod__(other), Str) return then__(str.__mod__(other, self), Str)
class StrMut(): # Inherits Str class StrMut(): # Inherits Str
value: Str value: Str

View file

@ -305,32 +305,47 @@ impl ScriptGenerator {
.replace("from _erg_range import Range", "") .replace("from _erg_range import Range", "")
.replace("from _erg_result import Error", "") .replace("from _erg_result import Error", "")
.replace("from _erg_result import is_ok", "") .replace("from _erg_result import is_ok", "")
.replace("from _erg_control import then__", "")
} }
fn load_namedtuple(&mut self) { fn load_namedtuple_if_not(&mut self) {
if !self.namedtuple_loaded {
self.prelude += "from collections import namedtuple as NamedTuple__\n"; self.prelude += "from collections import namedtuple as NamedTuple__\n";
self.namedtuple_loaded = true;
}
} }
// TODO: name escaping // TODO: name escaping
fn load_range_ops(&mut self) { fn load_range_ops_if_not(&mut self) {
if !self.range_ops_loaded {
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_result.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_result.py"));
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_int.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_int.py"));
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_nat.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_nat.py"));
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_str.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_str.py"));
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_range.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_range.py"));
self.range_ops_loaded = true;
}
} }
fn load_in_op(&mut self) { fn load_in_op_if_not(&mut self) {
if !self.in_op_loaded {
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_result.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_result.py"));
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_range.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_range.py"));
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_in_operator.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_in_operator.py"));
self.in_op_loaded = true;
}
} }
fn load_mutate_op(&mut self) { fn load_mutate_op_if_not(&mut self) {
if !self.mutate_op_loaded {
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_mutate_operator.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_mutate_operator.py"));
self.mutate_op_loaded = true;
}
} }
fn load_builtin_types(&mut self) { fn load_builtin_types_if_not(&mut self) {
if !self.builtin_types_loaded {
self.load_builtin_controls_if_not();
if self.range_ops_loaded { if self.range_ops_loaded {
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_array.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_array.py"));
} else if self.in_op_loaded { } else if self.in_op_loaded {
@ -347,14 +362,22 @@ impl ScriptGenerator {
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_str.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_str.py"));
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_array.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_array.py"));
} }
self.builtin_types_loaded = true;
}
} }
fn load_builtin_controls(&mut self) { fn load_builtin_controls_if_not(&mut self) {
if !self.builtin_control_loaded {
self.prelude += include_str!("lib/std/_erg_control.py"); self.prelude += include_str!("lib/std/_erg_control.py");
self.builtin_control_loaded = true;
}
} }
fn load_convertors(&mut self) { fn load_convertors_if_not(&mut self) {
if !self.convertors_loaded {
self.prelude += &Self::replace_import(include_str!("lib/std/_erg_convertors.py")); self.prelude += &Self::replace_import(include_str!("lib/std/_erg_convertors.py"));
self.convertors_loaded = true;
}
} }
fn escape_str(s: &str) -> String { fn escape_str(s: &str) -> String {
@ -455,10 +478,7 @@ impl ScriptGenerator {
&lit.value, &lit.value,
ValueObj::Bool(_) | ValueObj::Int(_) | ValueObj::Nat(_) | ValueObj::Str(_) ValueObj::Bool(_) | ValueObj::Int(_) | ValueObj::Nat(_) | ValueObj::Str(_)
) { ) {
if !self.builtin_types_loaded { self.load_builtin_types_if_not();
self.load_builtin_types();
self.builtin_types_loaded = true;
}
format!("{}({escaped})", lit.value.class()) format!("{}({escaped})", lit.value.class())
} else { } else {
escaped escaped
@ -466,10 +486,7 @@ impl ScriptGenerator {
} }
fn transpile_record(&mut self, rec: Record) -> String { fn transpile_record(&mut self, rec: Record) -> String {
if !self.namedtuple_loaded { self.load_namedtuple_if_not();
self.load_namedtuple();
self.namedtuple_loaded = true;
}
let mut attrs = "[".to_string(); let mut attrs = "[".to_string();
let mut values = "(".to_string(); let mut values = "(".to_string();
for mut attr in rec.attrs.into_iter() { for mut attr in rec.attrs.into_iter() {
@ -494,10 +511,7 @@ impl ScriptGenerator {
fn transpile_binop(&mut self, bin: BinOp) -> String { fn transpile_binop(&mut self, bin: BinOp) -> String {
match bin.op.kind { match bin.op.kind {
TokenKind::Closed | TokenKind::LeftOpen | TokenKind::RightOpen | TokenKind::Open => { TokenKind::Closed | TokenKind::LeftOpen | TokenKind::RightOpen | TokenKind::Open => {
if !self.range_ops_loaded { self.load_range_ops_if_not();
self.load_range_ops();
self.range_ops_loaded = true;
}
let mut code = match bin.op.kind { let mut code = match bin.op.kind {
TokenKind::Closed => "ClosedRange(", TokenKind::Closed => "ClosedRange(",
TokenKind::LeftOpen => "LeftOpenRange(", TokenKind::LeftOpen => "LeftOpenRange(",
@ -513,10 +527,7 @@ impl ScriptGenerator {
code code
} }
TokenKind::InOp => { TokenKind::InOp => {
if !self.in_op_loaded { self.load_in_op_if_not();
self.load_in_op();
self.in_op_loaded = true;
}
let mut code = "in_operator(".to_string(); let mut code = "in_operator(".to_string();
code += &self.transpile_expr(*bin.lhs); code += &self.transpile_expr(*bin.lhs);
code.push(','); code.push(',');
@ -540,10 +551,7 @@ impl ScriptGenerator {
fn transpile_unaryop(&mut self, unary: UnaryOp) -> String { fn transpile_unaryop(&mut self, unary: UnaryOp) -> String {
let mut code = "".to_string(); let mut code = "".to_string();
if unary.op.kind == TokenKind::Mutate { if unary.op.kind == TokenKind::Mutate {
if !self.mutate_op_loaded { self.load_mutate_op_if_not();
self.load_mutate_op();
self.mutate_op_loaded = true;
}
code += "mutate_operator("; code += "mutate_operator(";
} else { } else {
code += "("; code += "(";
@ -558,17 +566,14 @@ impl ScriptGenerator {
match acc { match acc {
Accessor::Ident(ident) => { Accessor::Ident(ident) => {
match &ident.inspect()[..] { match &ident.inspect()[..] {
"Str" | "Bool" | "Nat" | "Array" if !self.builtin_types_loaded => { "Str" | "Bool" | "Nat" | "Array" => {
self.load_builtin_types(); self.load_builtin_types_if_not();
self.builtin_types_loaded = true;
} }
"if" | "if!" | "for!" | "while" | "discard" if !self.builtin_control_loaded => { "if" | "if!" | "for!" | "while" | "discard" => {
self.load_builtin_controls(); self.load_builtin_controls_if_not();
self.builtin_control_loaded = true;
} }
"int" | "nat" if !self.convertors_loaded => { "int" | "nat" | "float" | "str" => {
self.load_convertors(); self.load_convertors_if_not();
self.convertors_loaded = true;
} }
_ => {} _ => {}
} }