Factor似乎像任何基于c的语言一样有一个main方法:
#! /usr/bin/env factor -script
USE: io
IN: hello
: hello ( -- ) "Hello World!" print ;
MAIN: hello
但是Factor不会自动执行main函数;如果你在终端上运行./hello.factor
,什么也不会发生,因为main
没有被调用。
有人知道Factor是否有像Python一样的语法,所以hello
实际上是在./hello.py
上调用的吗?
def hello():
print "Hello World!"
if __name__=="__main__":
main()
Factor现在将执行main
函数,如果指定了一个。您仍然需要编辑~/.factor-rc
来添加INCLUDING
/IN
宏,以便Factor将在当前目录中搜索代码。
~/.factor-rc:
! Andrew Pennebaker
! 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 ;
scriptedmain.factor:
#! /usr/bin/env factor
USING: io math.parser ;
IN: scriptedmain
: meaning-of-life ( -- n ) 42 ;
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ;
MAIN: main
test.factor:
#! /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
的例子:
美元。/scriptedmain.factor人生的意义是42美元。/test.factor试题:人生的意义是42