Lines
100 %
Functions
Branches
use super::*;
use crate::ast::{PairElement, StructId};
use crate::reader::Reader;
fn guess(source: &str, args: &[Option<WasmType>]) -> WasmType {
let body = Reader::parse(source)
.ok()
.and_then(|program| program.exprs.first().cloned())
.unwrap_or(Expr::Nil);
recursive_ret_guess(
"f",
&["p".to_string(), "n".to_string()],
&body,
args.iter().copied(),
)
}
#[test]
fn the_non_recursive_arm_decides_the_guess() {
let body = "(if (= n 0) 1/2 (+ 1/2 (f p (- n 1))))";
assert_eq!(guess(body, &[Some(WasmType::I32)]), WasmType::Ratio);
fn an_arm_of_a_cond_decides_the_guess() {
let body = "(cond ((= n 0) nil) (t (cons n (f p (- n 1)))))";
assert_eq!(guess(body, &[Some(WasmType::Ratio)]), WasmType::Bool);
fn a_non_numeric_first_argument_is_not_the_guess() {
let body = "(+ 1 (f p (- n 1)))";
let args = [Some(WasmType::StringRef), Some(WasmType::I32)];
assert_eq!(guess(body, &args), WasmType::Ratio);
fn an_integer_literal_exit_is_an_index_unless_a_scalar_is_accumulated() {
let count = "(if (= n 0) 0 (+ 1 (f p (- n 1))))";
assert_eq!(guess(count, &[Some(WasmType::StringRef)]), WasmType::I32);
let scalar = "(if (= n 0) 0 (+ 1/2 (f p (- n 1))))";
assert_eq!(guess(scalar, &[Some(WasmType::StringRef)]), WasmType::Ratio);
fn no_numeric_argument_defaults_to_ratio() {
assert_eq!(guess("(+ 1 (f p))", &[None]), WasmType::Ratio);
fn a_symbol_exit_is_not_known_so_the_arguments_decide() {
let body = "(if (= n 0) p (f p (- n 1)))";
assert_eq!(
guess(body, &[Some(WasmType::Commodity)]),
WasmType::Commodity
);
fn a_scalar_argument_makes_an_integer_literal_exit_a_scalar() {
let body = "(if (= n 0) 0 (+ (funcall g n) (f g (- n 1))))";
let args = [
Some(WasmType::Closure(crate::ast::ClosureSigId(0))),
Some(WasmType::Ratio),
];
fn a_bare_parameter_exit_has_its_arguments_type() {
let args = [Some(WasmType::StructRef(StructId(3))), Some(WasmType::I32)];
assert_eq!(guess(body, &args), WasmType::StructRef(StructId(3)));
fn a_list_of_literals_exit_is_a_pair() {
let body = "(if (= n 0) (list 0) (cons n (f p (- n 1))))";
assert_eq!(guess(body, &args), WasmType::PairRef(PairElement::I32));
fn a_let_rebinding_a_parameter_hides_its_argument_type() {
let body = "(if (> n 0) (let* ((q 1) (p (+ q (f p (- n 1))))) p) 0)";
assert_eq!(guess(body, &[Some(WasmType::StringRef)]), WasmType::I32);
let parallel = "(if (> n 0) (let ((p (* 2 (f p (- n 1))))) p) 0)";
assert_eq!(guess(parallel, &[Some(WasmType::StringRef)]), WasmType::I32);
fn a_let_binding_another_name_keeps_the_parameter_type() {
let body = "(if (> n 0) (let ((q (f p (- n 1)))) p) 0)";
fn a_list_of_a_parameter_is_a_pair_of_its_argument_type() {
let body = "(if (= n 0) (list n) (cons n (f p (- n 1))))";
guess(body, &[Some(WasmType::StringRef), Some(WasmType::I32)]),
WasmType::PairRef(PairElement::I32)
fn a_list_of_a_rebound_parameter_is_not_known() {
let body = "(if (= n 0) (let ((n 1/2)) (list n)) (cons n (f p (- n 1))))";
WasmType::Ratio