Add raw identifier

This commit is contained in:
Shunsuke Shibayama 2022-10-20 18:53:35 +09:00
parent 48eb3c5920
commit 03a36f48a3
14 changed files with 137 additions and 26 deletions

View file

@ -1648,11 +1648,6 @@ impl CodeGenerator {
self.emit_expr(*tasc.expr);
}
Expr::Import(acc) => self.emit_import(acc),
other => {
CompileError::feature_error(self.cfg.input.clone(), other.loc(), "??", "".into())
.write_to_stderr();
self.crash("cannot compile this expression at this time");
}
}
}

View file

@ -1641,6 +1641,11 @@ impl Context {
NoneType,
);
let t_bin = nd_func(vec![kw("n", Int)], None, Str);
let t_bytes = nd_func(
vec![kw("str", Str), kw("encoding", Str)],
None,
mono("Bytes"),
);
let t_chr = nd_func(
vec![kw("i", Type::from(value(0usize)..=value(1_114_111usize)))],
None,
@ -1742,6 +1747,7 @@ impl Context {
self.register_builtin_py_impl("ascii", t_ascii, Immutable, Private, Some("ascii"));
self.register_builtin_impl("assert", t_assert, Const, Private); // assert casting に悪影響が出る可能性があるため、Constとしておく
self.register_builtin_py_impl("bin", t_bin, Immutable, Private, Some("bin"));
self.register_builtin_py_impl("bytes", t_bytes, Immutable, Private, Some("bytes"));
self.register_builtin_py_impl("chr", t_chr, Immutable, Private, Some("chr"));
self.register_builtin_py_impl("classof", t_classof, Immutable, Private, Some("type"));
self.register_builtin_py_impl("compile", t_compile, Immutable, Private, Some("compile"));

View file

@ -183,7 +183,7 @@ impl Context {
self.impl_of(),
py_name,
);
log!(info "Registered {}::{}: {} {:?}", self.name, ident.name, vi.t, vi.impl_of);
log!(info "Registered {}::{}: {}", self.name, ident.name, vi);
self.locals.insert(ident.name.clone(), vi);
Ok(())
}

View file

@ -841,10 +841,6 @@ impl Context {
}
Ok(())
}
hir::Expr::Decl(decl) => {
decl.t = self.deref_tyvar(mem::take(&mut decl.t), Covariant, decl.loc())?;
Ok(())
}
hir::Expr::Def(def) => {
// It is not possible to further dereference the quantified type.
// TODO: However, it is possible that there are external type variables within the quantified type.

View file

@ -3,6 +3,8 @@ use std::fmt;
use erg_common::dict::Dict as HashMap;
use erg_common::error::Location;
#[allow(unused_imports)]
use erg_common::log;
use erg_common::traits::{Locational, NestedDisplay, Stream};
use erg_common::vis::{Field, Visibility};
use erg_common::Str;
@ -547,10 +549,22 @@ impl Accessor {
pub fn local_name(&self) -> Option<&str> {
match self {
Self::Ident(ident) => ident.qual_name.as_ref().map(|s| {
let mut seps = s.split_with(&[".", "::"]);
seps.remove(seps.len() - 1)
}),
Self::Ident(ident) => ident
.qual_name
.as_ref()
.map(|s| {
let mut seps = s.split_with(&[".", "::"]);
seps.remove(seps.len() - 1)
})
.or_else(|| {
let mut raw_parts = ident.name.inspect().split_with(&["'"]);
// "'aaa'".split_with(&["'"]) == ["", "aaa", ""]
if raw_parts.len() == 3 || raw_parts.len() == 4 {
Some(raw_parts.remove(1))
} else {
Some(ident.name.inspect())
}
}),
_ => None,
}
}
@ -1810,7 +1824,6 @@ pub enum Expr {
UnaryOp(UnaryOp),
Call(Call),
Lambda(Lambda),
Decl(Decl),
Def(Def),
ClassDef(ClassDef),
AttrDef(AttrDef),
@ -1820,10 +1833,10 @@ pub enum Expr {
Import(Accessor),
}
impl_nested_display_for_chunk_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Decl, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
impl_nested_display_for_chunk_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
impl_display_from_nested!(Expr);
impl_locational_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Decl, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
impl_t_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Decl, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
impl_locational_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
impl_t_for_enum!(Expr; Lit, Accessor, Array, Tuple, Dict, Record, BinOp, UnaryOp, Call, Lambda, Def, ClassDef, AttrDef, Code, Compound, TypeAsc, Set, Import);
impl Default for Expr {
fn default() -> Self {

View file

@ -1,4 +1,13 @@
.StringIO!: ClassType
.StringIO!.getvalue!: (self: Ref(StringIO!),) => Str
.StringIO! <: FileLike!
.StringIO!.read!: (self: RefMut(.StringIO!), ) => Str
.StringIO!.write!: (self: RefMut(.StringIO!), s: Str) => NoneType
.StringIO!.getvalue!: (self: Ref(.StringIO!),) => Str
.TextIOWrapper!: ClassType
.BytesIO!: ClassType
.BytesIO! <: FileLike!
.BytesIO!.read!: (self: RefMut(.BytesIO!), ) => Bytes
.BytesIO!.write!: (self: RefMut(.BytesIO!), b: Bytes) => NoneType
.newBytesIO = 'BytesIO': (bytes: Bytes,) -> .BytesIO!

View file

@ -1,7 +1,7 @@
.ZipFile!: ClassType
.ZipFile! <: FileLike!
.ZipFile!.open!: (path: PathLike or NoneType := NoneType, mode := Str) => .ZipFile!
.ZipFile!.open!: (path: PathLike or FileLike!, mode := Str) => .ZipFile!
.ZipFile!.add!: (self: RefMut(.ZipFile!), name: PathLike, arcname: PathLike or NoneType := NoneType, recursive := Bool) => NoneType
.ZipFile!.close!: (self: .ZipFile!,) => NoneType
.ZipFile!.extractall!: (self: RefMut(.ZipFile!), path := PathLike, members: [Str; _] or NoneType := NoneType, numeric_owner := Bool) => NoneType

View file

@ -121,7 +121,6 @@ impl<'a> Linker<'a> {
self.resolve_pymod_path(&mut arg.expr);
}
}
Expr::Decl(_decl) => {}
Expr::Def(def) => {
for chunk in def.body.block.iter_mut() {
self.resolve_pymod_path(chunk);
@ -238,7 +237,6 @@ impl<'a> Linker<'a> {
}
}
},
Expr::Decl(_decl) => {}
Expr::Def(def) => {
for chunk in def.body.block.iter_mut() {
self.replace_import(chunk);

View file

@ -1515,9 +1515,11 @@ impl ASTLowerer {
_ => unreachable!(),
};
let id = body.id;
self.ctx.assign_var_sig(&sig, found_body_t, id, py_name)?;
self.ctx
.assign_var_sig(&sig, found_body_t, id, py_name.clone())?;
let mut ident = hir::Identifier::bare(ident.dot.clone(), ident.name.clone());
ident.vi.t = found_body_t.clone();
ident.vi.py_name = py_name;
let sig = hir::VarSignature::new(ident);
let body = hir::DefBody::new(body.op, block, body.id);
Ok(hir::Def::new(hir::Signature::Var(sig), body))
@ -1648,6 +1650,10 @@ impl ASTLowerer {
t: &Type,
py_name: Str,
) -> LowerResult<()> {
// .X = 'x': Type
if ident.is_raw() {
return Ok(());
}
if ident.is_const() {
let vi = VarInfo::new(
t.clone(),
@ -1693,6 +1699,9 @@ impl ASTLowerer {
}
fn declare_subtype(&mut self, ident: &ast::Identifier, trait_: &Type) -> LowerResult<()> {
if ident.is_raw() {
return Ok(());
}
if let Some((_, ctx)) = self.ctx.get_mut_type(ident.inspect()) {
ctx.register_marker_trait(trait_.clone());
Ok(())

View file

@ -130,8 +130,8 @@ impl fmt::Display for VarInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"VarInfo{{t: {}, muty: {:?}, vis: {:?}, kind: {:?}}}",
self.t, self.muty, self.vis, self.kind
"VarInfo{{t: {}, muty: {:?}, vis: {:?}, kind: {:?}, py_name: {:?}}}",
self.t, self.muty, self.vis, self.kind, self.py_name,
)
}
}