如果__name __ ==“__ main__”:main(),因子是否等效于Python习语?

时间:2011-08-18 02:59:36

标签: main factor-lang scriptedmain

因子似乎有一个像任何基于C语言的主要方法:

#! /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()

1 个答案:

答案 0 :(得分:1)

如果指定了一个,则因子将立即执行main函数。您仍然需要修改~/.factor-rc以添加INCLUDING / IN宏,以便因子搜索当前目录中的代码。

〜/ .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

发布于RosettaCode