Search code examples
importincluderequirefactor-lang

Importing Factor code without putting it in the work directory?


I like version controlling my code. It would be a pain to copy every Factor file from my git over to Factor's work directory and back.

Does Factor have an import command that works like Ruby's require, one that looks in the current directory for source code?


Solution

  • It's easy enough to write a macro that does this. Add the following to ~/.factor-rc:

    ! INCLUDING macro that imports source code files in the current directory
    
    USING: kernel vocabs.loader parser sequences lexer vocabs.parser ;
    IN: syntax
    
    : include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ;
    
    SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;
    

    Which you can use like this (see Rosetta Code).

    #! /usr/bin/env factor
    
    INCLUDING: scriptedmain ;
    USING: io math.parser ;
    IN: test
    
    : main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ;
    
    MAIN: main