pub fn import_keyword() -> impl BindingMonad<Binding=SyntaxCompiler> {
    LocateImportFile.map_result(|(filename, file_path)| {
        // Read the file content
        let file_content = match file_path {
            ImportFile::NotFound                => "".to_string(),
            ImportFile::FromPath(path)          => fs::read_to_string(path.as_path()).map_err(|_io| BindError::IOError)?,
            ImportFile::BuiltIn(_name, data)    => data
        };

        // Parse the file content
        let parsed = parse_safas(&mut TokenReadBuffer::new(file_content.chars()), FileLocation::new(&filename))?;

        // Pass through the parsed content
        Ok((filename, parsed))
    }).and_then(|(_filename, parsed_input)| {

        // Bind the result
        BindingFn::from_binding_fn(move |bindings| {
            // The input is a list of statements
            let mut bindings    = bindings;
            let mut pos         = &*parsed_input;

            // Pre-bind each of the statements
            while let SafasCell::List(statement, next) = pos {
                let (next_bindings, _result)    = pre_bind_statement(statement.clone(), bindings);
                bindings                        = next_bindings;

                pos                             = &*next;
            }

            // Bind each statement in turn
            let mut bound_statements    = vec![];
            let mut pos                 = &*parsed_input;

            while let SafasCell::List(statement, next) = pos {
                match bind_statement(statement.clone(), bindings) {
                    Ok((result, new_bindings))  => { bindings = new_bindings; bound_statements.push(result); }
                    Err((err, new_bindings))    => { return (new_bindings, Err(err)); }
                }

                pos                             = &*next;
            }

            // Result is the list of bound cells
            (bindings, Ok(SafasCell::list_with_cells(bound_statements).into()))
        })

    }).map(|binding: CellRef| {
        // Create a copy of binidng for ourselves
        let binding = binding.clone();

        let compile = |binding: CellRef| {
            let binding = binding.clone();

            // Start with some empty actions for the import
            let mut actions = CompiledActions::empty();

            // The binding is the list of statements from the import
            let mut pos = &*binding;

            while let SafasCell::List(statement, next) = pos {
                // Compile the next statement
                let compiled = compile_statement(statement.clone())?;

                // Add the actions to the compiled result
                actions.extend(compiled);

                // Move to the next statement
                pos = &*next;
            }

            // Return the final result
            Ok(actions)
        };

        SyntaxCompiler::with_compiler(compile, binding)
    })
}
///
/// Creates the compiler for the import keyword
/// 
/// `(import "foo")` attempts to import the file `foo.sf` from the current set of search paths.
///
Creates the compiler for the import keyword
`(import "foo")` attempts to import the file `foo.sf` from the current set of search paths.
Creates the compiler for the import keyword ` ( import " foo " ) ` attempts to import the file ` foo . sf ` from the current set of search paths .
pub fn import_keyword() -> impl BindingMonad<Binding=SyntaxCompiler> {
    LocateImportFile.map_result(|(filename, file_path)| {
        // Read the file content
        let file_content = match file_path {
            ImportFile::NotFound                => "".to_string(),
            ImportFile::FromPath(path)          => fs::read_to_string(path.as_path()).map_err(|_io| BindError::IOError)?,
            ImportFile::BuiltIn(_name, data)    => data
        };
        // Parse the file content
        let parsed = parse_safas(&mut TokenReadBuffer::new(file_content.chars()), FileLocation::new(&filename))?;
        // Pass through the parsed content
        Ok((filename, parsed))
    }).and_then(|(_filename, parsed_input)| {
        // Bind the result
        BindingFn::from_binding_fn(move |bindings| {
            // The input is a list of statements
            let mut bindings    = bindings;
            let mut pos         = &*parsed_input;
            // Pre-bind each of the statements
            while let SafasCell::List(statement, next) = pos {
                let (next_bindings, _result)    = pre_bind_statement(statement.clone(), bindings);
                bindings                        = next_bindings;
                pos                             = &*next;
            }
            // Bind each statement in turn
            let mut bound_statements    = vec![];
            let mut pos                 = &*parsed_input;
            while let SafasCell::List(statement, next) = pos {
                match bind_statement(statement.clone(), bindings) {
                    Ok((result, new_bindings))  => { bindings = new_bindings; bound_statements.push(result); }
                    Err((err, new_bindings))    => { return (new_bindings, Err(err)); }
                }
                pos                             = &*next;
            }
            // Result is the list of bound cells
            (bindings, Ok(SafasCell::list_with_cells(bound_statements).into()))
        })
    }).map(|binding: CellRef| {
        // Create a copy of binidng for ourselves
        let binding = binding.clone();
        let compile = |binding: CellRef| {
            let binding = binding.clone();
            // Start with some empty actions for the import
            let mut actions = CompiledActions::empty();
            // The binding is the list of statements from the import
            let mut pos = &*binding;
            while let SafasCell::List(statement, next) = pos {
                // Compile the next statement
                let compiled = compile_statement(statement.clone())?;
                // Add the actions to the compiled result
                actions.extend(compiled);
                // Move to the next statement
                pos = &*next;
            }
            // Return the final result
            Ok(actions)
        };
        SyntaxCompiler::with_compiler(compile, binding)
    })
}
pub fn import_keyword ( ) -> impl BindingMonad < Binding = SyntaxCompiler > { LocateImportFile . map_result ( | ( filename , file_path ) | { // Read the file content let file_content = match file_path { ImportFile :: NotFound => " " . to_string ( ) , ImportFile :: FromPath ( path ) => fs :: read_to_string ( path . as_path ( ) ) . map_err ( | _io | BindError :: IOError ) ? , ImportFile :: BuiltIn ( _name , data ) => data } ; // Parse the file content let parsed = parse_safas ( & mut TokenReadBuffer :: new ( file_content . chars ( ) ) , FileLocation :: new ( & filename ) ) ? ; // Pass through the parsed content Ok ( ( filename , parsed ) ) } ) . and_then ( | ( _filename , parsed_input ) | { // Bind the result BindingFn :: from_binding_fn ( move | bindings | { // The input is a list of statements let mut bindings = bindings ; let mut pos = & * parsed_input ; // Pre-bind each of the statements while let SafasCell :: List ( statement , next ) = pos { let ( next_bindings , _result ) = pre_bind_statement ( statement . clone ( ) , bindings ) ; bindings = next_bindings ; pos = & * next ; } // Bind each statement in turn let mut bound_statements = vec ! [ ] ; let mut pos = & * parsed_input ; while let SafasCell :: List ( statement , next ) = pos { match bind_statement ( statement . clone ( ) , bindings ) { Ok ( ( result , new_bindings ) ) => { bindings = new_bindings ; bound_statements . push ( result ) ; } Err ( ( err , new_bindings ) ) => { return ( new_bindings , Err ( err ) ) ; } } pos = & * next ; } // Result is the list of bound cells ( bindings , Ok ( SafasCell :: list_with_cells ( bound_statements ) . into ( ) ) ) } ) } ) . map ( | binding : CellRef | { // Create a copy of binidng for ourselves let binding = binding . clone ( ) ; let compile = | binding : CellRef | { let binding = binding . clone ( ) ; // Start with some empty actions for the import let mut actions = CompiledActions :: empty ( ) ; // The binding is the list of statements from the import let mut pos = & * binding ; while let SafasCell :: List ( statement , next ) = pos { // Compile the next statement let compiled = compile_statement ( statement . clone ( ) ) ? ; // Add the actions to the compiled result actions . extend ( compiled ) ; // Move to the next statement pos = & * next ; } // Return the final result Ok ( actions ) } ; SyntaxCompiler :: with_compiler ( compile , binding ) } ) }
Creates the compiler for the import keyword
`(import "foo")` attempts to import the file `foo.sf` from the current set of search paths.
Creates the compiler for the import keyword ` ( import " foo " ) ` attempts to import the file ` foo . sf ` from the current set of search paths .