fixed errors

This commit is contained in:
pca006132 2020-12-29 11:42:16 +08:00
parent 1990486cc2
commit 06af1623a8

View File

@ -14,45 +14,41 @@ pub fn parse_expr(ctx: &GlobalContext, sym_table: &SymTable, expr: &Expression)
} }
fn parse_constant( fn parse_constant(
ctx: &GlobalContext, _: &GlobalContext,
sym_table: &SymTable, _: &SymTable,
value: &rustpython_parser::ast::Number, value: &rustpython_parser::ast::Number,
) -> ParserResult { ) -> ParserResult {
use rustpython_parser::ast::Number; use rustpython_parser::ast::Number;
match value { match value {
Number::Integer { value } => { Number::Integer { .. } => {
if i32::try_from(&value).is_ok() { // not check the range now
Ok(PrimitiveType(INT32_TYPE).into()) Ok(PrimitiveType(INT32_TYPE).into())
} else if i64::try_from(&value).is_ok() { // if i32::try_from(&value).is_ok() {
Ok(PrimitiveType(INT64_TYPE).into()) // Ok(PrimitiveType(INT32_TYPE).into())
} else { // } else if i64::try_from(&value).is_ok() {
Err("integer out of range".to_string()) // Ok(PrimitiveType(INT64_TYPE).into())
} // } else {
// Err("integer out of range".to_string())
// }
} }
Number::Float { .. } => Ok(PrimitiveType(FLOAT_TYPE).into()), Number::Float { .. } => Ok(PrimitiveType(FLOAT_TYPE).into()),
_ => Err("not supported".to_string()), _ => Err("not supported".to_string()),
} }
} }
fn parse_identifier(ctx: &GlobalContext, sym_table: &SymTable, name: &str) -> ParserResult { fn parse_identifier(_: &GlobalContext, sym_table: &SymTable, name: &str) -> ParserResult {
match sym_table.get(name) { match sym_table.get(name) {
Some(v) => Ok(v.clone()), Some(v) => Ok(v.clone()),
None => Err("unbounded variable".to_string()), None => Err("unbounded variable".to_string()),
} }
} }
fn parse_list( fn parse_list(ctx: &GlobalContext, sym_table: &SymTable, elements: &[Expression]) -> ParserResult {
ctx: &GlobalContext,
sym_table: &SymTable,
elements: &[Expression],
) -> ParserResult {
if elements.len() == 0 { if elements.len() == 0 {
return Ok(ParametricType(LIST_TYPE, vec![BotType.into()]).into()); return Ok(ParametricType(LIST_TYPE, vec![BotType.into()]).into());
} }
let mut types = elements let mut types = elements.iter().map(|v| parse_expr(&ctx, sym_table, v));
.iter()
.map(|v| parse_expr(&ctx, sym_table, v));
let head = types.next().unwrap()?; let head = types.next().unwrap()?;
for v in types { for v in types {
@ -63,4 +59,12 @@ fn parse_list(
Ok(ParametricType(LIST_TYPE, vec![head]).into()) Ok(ParametricType(LIST_TYPE, vec![head]).into())
} }
fn parse_tuple(ctx: &GlobalContext, sym_table: &SymTable, elements: &[Expression]) -> ParserResult {
let types: Result<Vec<_>, String> = elements
.iter()
.map(|v| parse_expr(&ctx, sym_table, v))
.collect();
Ok(ParametricType(TUPLE_TYPE, types?).into())
}