proper constant handling

This commit is contained in:
pca006132 2020-12-30 10:45:35 +08:00
parent 03712e3762
commit 95789ec303
1 changed files with 14 additions and 10 deletions

View File

@ -7,6 +7,7 @@ use rustpython_parser::ast::{
UnaryOperator, UnaryOperator,
}; };
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto;
use std::rc::Rc; use std::rc::Rc;
type SymTable<'a> = HashMap<&'a str, Rc<Type>>; type SymTable<'a> = HashMap<&'a str, Rc<Type>>;
@ -48,6 +49,7 @@ pub fn parse_expr(ctx: &GlobalContext, sym_table: &SymTable, expr: &Expression)
} }
_ => Err("only list comprehension is supported".into()), _ => Err("only list comprehension is supported".into()),
}, },
ExpressionType::True | ExpressionType::False => Ok(Some(PrimitiveType(BOOL_TYPE).into())),
_ => Err("not supported".into()), _ => Err("not supported".into()),
} }
} }
@ -59,16 +61,18 @@ fn parse_constant(
) -> ParserResult { ) -> ParserResult {
use rustpython_parser::ast::Number; use rustpython_parser::ast::Number;
match value { match value {
Number::Integer { .. } => { Number::Integer { value } => {
// not check the range now let int32: Result<i32, _> = value.try_into();
Ok(Some(PrimitiveType(INT32_TYPE).into())) if int32.is_ok() {
// if i32::try_from(&value).is_ok() { Ok(Some(PrimitiveType(INT32_TYPE).into()))
// Ok(PrimitiveType(INT32_TYPE).into()) } else {
// } else if i64::try_from(&value).is_ok() { let int64: Result<i64, _> = value.try_into();
// Ok(PrimitiveType(INT64_TYPE).into()) if int64.is_ok() {
// } else { Ok(Some(PrimitiveType(INT64_TYPE).into()))
// Err("integer out of range".into()) } else {
// } Err("integer out of range".into())
}
}
} }
Number::Float { .. } => Ok(Some(PrimitiveType(FLOAT_TYPE).into())), Number::Float { .. } => Ok(Some(PrimitiveType(FLOAT_TYPE).into())),
_ => Err("not supported".into()), _ => Err("not supported".into()),