Factor 是否有与 Python 习惯用法 if __name__=="__main__": main() 等效的语言?

发布于 2024-11-30 04:28:20 字数 474 浏览 4 评论 0 原文

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 appears to have a main method like any C-based language:

#! /usr/bin/env factor -script

USE: io
IN: hello

: hello ( -- ) "Hello World!" print ;

MAIN: hello

But Factor does not execute the main function automatically; if you run ./hello.factor in a terminal, nothing happens because main isn't called.

Does anyone know if Factor has syntax like Python, so that hello is actually called on ./hello.py?

def hello():
   print "Hello World!"

if __name__=="__main__":
   main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-12-07 04:28:20

如果指定了,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
$ ./测试因子
测试:生命的意义是 42

正如 RosettaCode 上发布的那样。

Factor will now execute a main function if one is specified. You'll still have to edit ~/.factor-rc to add the INCLUDING/IN macros so that Factor will search for code in the current directory.

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

Example:

$ ./scriptedmain.factor
Main: The meaning of life is 42
$ ./test.factor
Test: The meaning of life is 42

As posted on RosettaCode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文