Lines
100 %
Functions
Branches
use super::cells::allocate_cells;
use crate::ast::{Expr, Number, WasmType};
use crate::compiler::CompileMode;
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::fold::stores::scan_stores;
use crate::reader::Reader;
use crate::runtime::{CellBinding, Symbol, SymbolKind, SymbolTable};
fn made(symbols: &mut SymbolTable, source: &str) -> Vec<String> {
let unit = Reader::parse(source).expect("parses").exprs;
let scan = scan_stores(symbols, &unit, CompileMode::Script).expect("scans");
symbols.snapshot_pre_unit(scan.written_names());
symbols.set_stored_globals(scan.stores());
let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
let mut emit = FunctionEmitter::new();
allocate_cells(&mut ctx, &mut emit, symbols, &scan).expect("allocates");
scan.cells().iter().cloned().collect()
}
fn type_of(symbols: &SymbolTable, name: &str) -> Option<WasmType> {
symbols.cell_global(name).map(|(_, ty)| ty)
fn with_constant(name: &str, value: i64) -> SymbolTable {
let mut symbols = SymbolTable::with_builtins_for_wasm();
symbols.define_special(
Symbol::new(name, SymbolKind::Variable).with_value(Expr::Number(Number::Index(value))),
);
symbols
#[test]
fn a_literal_writer_types_a_new_cell() {
assert_eq!(made(&mut symbols, "(setf g 1) (setf g 2)"), ["G"]);
assert_eq!(type_of(&symbols, "G"), Some(WasmType::I32));
assert_eq!(symbols.cell_binding("G"), CellBinding::Unbound);
assert!(symbols.cell_flag("G").is_some());
fn a_fractional_literal_types_a_scalar_cell() {
made(&mut symbols, "(setf g 1/2) (setf g 1)");
assert_eq!(type_of(&symbols, "G"), Some(WasmType::Ratio));
fn a_writer_without_a_literal_leaves_a_provisional_anyref_cell() {
made(&mut symbols, "(setf g (list 1)) (setf g (list 2))");
assert_eq!(type_of(&symbols, "G"), Some(WasmType::AnyRef));
fn an_earlier_constant_seeds_a_bound_cell() {
let mut symbols = with_constant("G", 7);
assert_eq!(made(&mut symbols, "(setf g 8)"), ["G"]);
assert_eq!(symbols.cell_binding("G"), CellBinding::Bound);
fn a_global_with_no_writer_or_one_definition_is_no_cell() {
assert!(made(&mut symbols, "(+ g 1)").is_empty());
assert!(made(&mut symbols, "(defparameter g 8)").is_empty());
assert!(made(&mut SymbolTable::with_builtins_for_wasm(), "(defvar h 1)").is_empty());
assert!(!symbols.is_cell("G"));
fn a_second_definition_makes_a_cell() {
assert_eq!(made(&mut symbols, "(defvar g 1) (defvar g 2)"), ["G"]);
fn a_cell_the_unit_defines_is_special_only_at_its_definition_and_a_stored_one_is_created() {
made(
&mut symbols,
"(defvar d 1) (setf d 2) (setf c 1) (setf c 2)",
assert!(!symbols.is_special("D"));
assert!(!symbols.is_created_global("D"));
assert!(symbols.is_special("C"));
assert!(symbols.is_created_global("C"));
fn a_built_in_name_is_never_a_cell() {
assert!(made(&mut symbols, "(setf list 1) (setf list 2)").is_empty());
fn a_seeded_cell_no_store_reaches_keeps_its_constant_for_a_macro() {
let mut symbols = with_constant("S", 7);
made(&mut symbols, "(defvar s 8) (defvar s 9)");
let expansion = symbols
.lookup("S")
.and_then(|s| s.expansion_value().cloned());
assert_eq!(expansion, Some(Expr::Number(Number::Index(7.into()))));
fn a_seeded_cell_a_store_reaches_keeps_no_constant_for_a_macro() {
made(&mut symbols, "(setf s 8)");
assert_eq!(expansion, None);