forked from M-Labs/nac3
1
0
Fork 0

test_case can be used, update symbol_resolver

This commit is contained in:
ychenfo 2021-07-27 10:24:53 +08:00
parent d14076fe7f
commit 512fc59281
2 changed files with 33 additions and 74 deletions

View File

@ -444,17 +444,14 @@ impl<'a> TypeInferencer<'a> {
} }
} }
#[cfg(test)]
pub mod test { pub mod test {
use std::vec;
use crate::typecheck::{symbol_resolver::SymbolResolver, symbol_resolver::*, location::*}; use crate::typecheck::{symbol_resolver::SymbolResolver, symbol_resolver::*, location::*};
use rustpython_parser::ast::Expr; use rustpython_parser::ast::Expr;
use super::*;
#[cfg(test)] #[cfg(test)]
use test_case::test_case; // FIXME use test_case::test_case;
use super::*;
pub fn new_ctx<'a>() -> TypeInferencer<'a> { pub fn new_ctx<'a>() -> TypeInferencer<'a> {
struct S; struct S;
@ -639,66 +636,33 @@ pub mod test {
); );
} }
#[test] #[test_case("False == [True or True, False][0]")]
fn test_mix() { #[test_case("1 < 2 < 3")]
#[test_case("1 + [123, 1232][0]")]
#[test_case("not True")]
#[test_case("[[1]][0][0]")]
#[test_case("[[1]][0]")]
#[test_case("[[(1, 2), (2, 3), (3, 4)], [(2, 4), (4, 6)]][0]")]
#[test_case("[1, 2, 3, 4, 5][1: 2]")]
#[test_case("4 if False and True else 8")]
#[test_case("(1, 2, 3, 4)[1]")]
#[test_case("(1, True, 3, False)[1]")]
fn test_mix(prog: &'static str) {
let mut inf = new_ctx(); let mut inf = new_ctx();
let ast1 = rustpython_parser::parser::parse_expression("False == [True or True, False][0]").unwrap(); let ast = rustpython_parser::parser::parse_expression(prog).unwrap();
let ast2 = rustpython_parser::parser::parse_expression("False == [True or True, False][0]").unwrap(); let folded = inf.fold_expr(ast).unwrap();
let ast3 = rustpython_parser::parser::parse_expression("1 < 2 < 3").unwrap(); // println!("{:?}\n", folded.custom);
let ast4 = rustpython_parser::parser::parse_expression("1 + [12312, 1231][0]").unwrap();
let ast5 = rustpython_parser::parser::parse_expression("not True").unwrap();
let ast6 = rustpython_parser::parser::parse_expression("[[1]][0][0]").unwrap();
let ast7 = rustpython_parser::parser::parse_expression("[[1]][0]").unwrap();
let ast8 = rustpython_parser::parser::parse_expression("[[(1, 2), (2, 3), (3, 4)], [(2, 4), (4, 6)]][0]").unwrap();
let ast9 = rustpython_parser::parser::parse_expression("[1, 2, 3, 4, 5][1: 2]").unwrap();
let ast10 = rustpython_parser::parser::parse_expression("4 if False and True else 8").unwrap();
let ast11 = rustpython_parser::parser::parse_expression("(1, 2, 3, 4)[1]").unwrap();
let ast12 = rustpython_parser::parser::parse_expression("(1, True, 3, False)[1]").unwrap();
// let ast13 = rustpython_parser::parser::parse_expression("[1, True, 2]").unwrap();
let folded = inf.fold_expr(ast1).unwrap();
let folded_2 = inf.fold_expr(ast2).unwrap();
let folded_3 = inf.fold_expr(ast3).unwrap();
let folded_4 = inf.fold_expr(ast4).unwrap();
let folded_5 = inf.fold_expr(ast5).unwrap();
let folded_6 = inf.fold_expr(ast6).unwrap();
let folded_7 = inf.fold_expr(ast7).unwrap();
let folded_8 = inf.fold_expr(ast8).unwrap();
let folded_9 = inf.fold_expr(ast9).unwrap();
let folded_10 = inf.fold_expr(ast10).unwrap();
let folded_11 = inf.fold_expr(ast11).unwrap();
let folded_12 = inf.fold_expr(ast12).unwrap();
println!("{:?}", folded.custom);
println!("{:?}", folded_2.custom);
println!("{:?}", folded_3.custom);
println!("{:?}", folded_4.custom);
println!("{:?}", folded_5.custom);
println!("{:?}", folded_6.custom);
println!("{:?}", folded_7.custom);
println!("{:?}", folded_8.custom);
println!("{:?}", folded_9.custom);
println!("{:?}", folded_10.custom);
println!("{:?}", folded_11.custom);
println!("{:?}", folded_12.custom);
// let folded_13 = inf.fold_expr(ast13);
} }
#[test] #[test_case("[1, True, 2]")]
fn test_err_msg() { #[test_case("True if 1 else False")]
#[test_case("1 if True else False")]
#[test_case("1 and 2")]
#[test_case("False or 1")]
fn test_err_msg(prog: &'static str) {
let mut inf = new_ctx(); let mut inf = new_ctx();
for prog in vec![ let ast = rustpython_parser::parser::parse_expression(prog).unwrap();
"[1, True, 2]", let _folded = inf.fold_expr(ast);
"True if 1 else False", println!("")
"1 if True else False",
] {
let ast = rustpython_parser::parser::parse_expression(prog).unwrap();
let _folded = inf.fold_expr(ast);
println!("");
}
// println!("{:?}", folded);
} }
} }

View File

@ -1,11 +1,6 @@
use super::typedef::Type; use super::typedef::Type;
use super::location::Location; use super::location::Location;
use rustpython_parser::ast::Expr;
pub enum SymbolType {
TypeName(Type),
Identifier(Type),
}
pub enum SymbolValue<'a> { pub enum SymbolValue<'a> {
I32(i32), I32(i32),
I64(i64), I64(i64),
@ -14,10 +9,10 @@ pub enum SymbolValue<'a> {
Tuple(&'a [SymbolValue<'a>]), Tuple(&'a [SymbolValue<'a>]),
Bytes(&'a [u8]), Bytes(&'a [u8]),
} }
pub trait SymbolResolver { pub trait SymbolResolver {
fn get_symbol_type(&self, str: &str) -> Option<SymbolType>; fn get_symbol_type(&mut self, str: &str) -> Option<Type>;
fn get_symbol_value(&self, str: &str) -> Option<SymbolValue>; fn parse_type_name(&mut self, expr: &Expr<()>) -> Option<Type>;
fn get_symbol_location(&self, str: &str) -> Option<Location>; fn get_symbol_value(&mut self, str: &str) -> Option<SymbolValue>;
fn get_symbol_location(&mut self, str: &str) -> Option<Location>;
// handle function call etc. // handle function call etc.
} }