Fine-tune int types

This commit is contained in:
Jeong YunWon 2023-05-10 19:26:53 +09:00
parent 455bcc01a0
commit 6fa3d0f90a
15 changed files with 27 additions and 42 deletions

View file

@ -24,6 +24,10 @@ builtin_type_mapping = {
} }
assert builtin_type_mapping.keys() == asdl.builtin_types assert builtin_type_mapping.keys() == asdl.builtin_types
builtin_int_mapping = {
"simple": "bool",
"is_async": "bool",
}
def rust_type_name(name): def rust_type_name(name):
"""Return a string for the C name of the type. """Return a string for the C name of the type.
@ -31,7 +35,8 @@ def rust_type_name(name):
This function special cases the default types provided by asdl. This function special cases the default types provided by asdl.
""" """
if name in asdl.builtin_types: if name in asdl.builtin_types:
return builtin_type_mapping[name] builtin = builtin_type_mapping[name]
return builtin
elif name.islower(): elif name.islower():
return "".join(part.capitalize() for part in name.split("_")) return "".join(part.capitalize() for part in name.split("_"))
else: else:
@ -355,6 +360,8 @@ class StructVisitor(EmitVisitor):
typ = f"Option<{typ}>" typ = f"Option<{typ}>"
if field.seq: if field.seq:
typ = f"Vec<{typ}>" typ = f"Vec<{typ}>"
if typ == "Int":
typ = builtin_int_mapping.get(field.name, typ)
name = rust_field(field.name) name = rust_field(field.name)
self.emit(f"{vis}{name}: {typ},", depth) self.emit(f"{vis}{name}: {typ},", depth)

View file

@ -154,7 +154,7 @@ pub struct StmtAnnAssign<U = ()> {
pub target: Box<Expr<U>>, pub target: Box<Expr<U>>,
pub annotation: Box<Expr<U>>, pub annotation: Box<Expr<U>>,
pub value: Option<Box<Expr<U>>>, pub value: Option<Box<Expr<U>>>,
pub simple: Int, pub simple: bool,
} }
impl<U> From<StmtAnnAssign<U>> for StmtKind<U> { impl<U> From<StmtAnnAssign<U>> for StmtKind<U> {
@ -815,7 +815,7 @@ pub struct Comprehension<U = ()> {
pub target: Expr<U>, pub target: Expr<U>,
pub iter: Expr<U>, pub iter: Expr<U>,
pub ifs: Vec<Expr<U>>, pub ifs: Vec<Expr<U>>,
pub is_async: Int, pub is_async: bool,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]

View file

@ -429,7 +429,7 @@ impl<'a> Unparser<'a> {
fn unparse_comp<U>(&mut self, generators: &[Comprehension<U>]) -> fmt::Result { fn unparse_comp<U>(&mut self, generators: &[Comprehension<U>]) -> fmt::Result {
for comp in generators { for comp in generators {
self.p(if comp.is_async.to_bool() { self.p(if comp.is_async {
" async for " " async for "
} else { } else {
" for " " for "

View file

@ -132,7 +132,7 @@ ExpressionStatement: ast::Stmt = {
target: Box::new(set_context(target, ast::ExprContext::Store)), target: Box::new(set_context(target, ast::ExprContext::Store)),
annotation: Box::new(annotation), annotation: Box::new(annotation),
value: rhs.map(Box::new), value: rhs.map(Box::new),
simple: ast::Int::new_bool(simple), simple,
}.into(), }.into(),
) )
}, },
@ -1707,7 +1707,7 @@ SingleForComprehension: ast::Comprehension = {
target: set_context(target, ast::ExprContext::Store), target: set_context(target, ast::ExprContext::Store),
iter, iter,
ifs, ifs,
is_async: ast::Int::new_bool(is_async), is_async,
} }
} }
}; };

6
parser/src/python.rs generated
View file

@ -1,5 +1,5 @@
// auto-generated: "lalrpop 0.20.0" // auto-generated: "lalrpop 0.20.0"
// sha3: 92f216faaf9f12c4ae180e3d5821a88e6bfa435ffe36293d4485c669f055b18e // sha3: 7764ff3f82c4bfb364f8e2c55698c0cf314d591d7aeacd8abf4c9dfe354d25e2
use crate::{ use crate::{
ast, ast,
lexer::{LexicalError, LexicalErrorType}, lexer::{LexicalError, LexicalErrorType},
@ -36926,7 +36926,7 @@ fn __action24<
target: Box::new(set_context(target, ast::ExprContext::Store)), target: Box::new(set_context(target, ast::ExprContext::Store)),
annotation: Box::new(annotation), annotation: Box::new(annotation),
value: rhs.map(Box::new), value: rhs.map(Box::new),
simple: ast::Int::new_bool(simple), simple,
}.into(), }.into(),
) )
} }
@ -39647,7 +39647,7 @@ fn __action206<
target: set_context(target, ast::ExprContext::Store), target: set_context(target, ast::ExprContext::Store),
iter, iter,
ifs, ifs,
is_async: ast::Int::new_bool(is_async), is_async,
} }
} }
} }

View file

@ -46,9 +46,7 @@ expression: parse_ast
), ),
}, },
), ),
simple: Int( simple: true,
1,
),
}, },
), ),
}, },

View file

@ -101,9 +101,7 @@ expression: parse_ast
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -101,9 +101,7 @@ expression: parse_ast
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -201,9 +201,7 @@ Attributed {
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -58,9 +58,7 @@ Attributed {
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -69,9 +69,7 @@ Attributed {
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
Comprehension { Comprehension {
target: Attributed { target: Attributed {
@ -174,9 +172,7 @@ Attributed {
), ),
}, },
], ],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -46,9 +46,7 @@ Attributed {
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -78,9 +78,7 @@ Attributed {
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -46,9 +46,7 @@ Attributed {
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },

View file

@ -87,9 +87,7 @@ Attributed {
), ),
}, },
ifs: [], ifs: [],
is_async: Int( is_async: false,
0,
),
}, },
], ],
}, },