杀墨传奇

查看: 1420|回复: 1
打印 上一主题 下一主题

[科技] @神獸/大神:教我們像莎士比亞那樣編程(ZT)

[复制链接]

0

精华

37

主题

530

帖子

侠名远播

Rank: 3Rank: 3

银两
6942
贡献
0
主持积分
0
杀营积分
8
侠营积分
42
演员积分
38
游戏次数
13
注册时间
2013-11-17
跳转到指定楼层
楼主
发表于 2015-8-29 13:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
史上最优美最含蓄最富诗意最具文学性的编程语言:莎士比亚程序设计语言

译者:雨御 原作者:Karl Hasselström, Jon Åslund
http://article.yeeyan.org/view/19252/188118
两位计算机语言教授设计了一种非常富有诗意的编程语言,用该语言编程的时候,源代码看起来像莎士比亚的著作。

设计理念:

该语言最重要的设计目标是使源代码更加含蓄,更有文学性,并尽量使其看起来像莎士比亚的著作。该语言没有繁多的数据结构,复杂的逻辑判断,或花哨的流程控制——它只有最基本的算术能力、输入输出和跳转语句。
可以说,莎士比亚语言结合了BASIC 的运行速度和Assembly 的开发效率,是当今计算机编程领域中不可多得的一门独特的语言。
Hello World<o < o
我们不想打破传统,所以还是先依照惯例,用经典的Hello World 例子来介绍莎士比亚语言。尽管您可能会不相信,但是下面这段代码的唯一目的确实只是在屏幕上打出”Hello World!” 而已。
The Infamous Hello World Program.

Romeo, a young man with a remarkable patience.
Juliet, a likewise young woman of remarkable grace.
Ophelia, a remarkable woman much in dispute with Hamlet.
Hamlet, the flatterer of Andersen Insulting A/S.
<o </o
<o </o
                   Act I: Hamlet's insults and flattery.
<o </o
                   Scene I: The insulting of Romeo.
<o </o
[Enter Hamlet and Romeo]
<o </o
Hamlet:
You lying stupidfatherless big smelly half-witted coward!
You are as stupidas the difference between a handsome rich brave
hero and thyself!Speak your mind!
<o </o
You are as braveas the sum of your fat little stuffed misused dusty
old rotten codpiece and a beautiful fair warm peaceful sunny summer's
day. You are ashealthy as the difference between the sum of the
sweetest reddestrose and my father and yourself! Speak your mind!
<o </o
You are ascowardly as the sum of yourself and the difference
between a bigmighty proud kingdom and a horse. Speak your mind.
<o </o
Speak your mind!
<o </o
[Exit Romeo]
<o </o
                   Scene II: The praising of Juliet.
<o </o
[Enter Juliet]
<o </o
Hamlet:
Thou art as sweetas the sum of the sum of Romeo and his horse and his
black cat! Speakthy mind!
<o </o
[Exit Juliet]
<o </o
                   Scene III: The praising of Ophelia.
<o </o
[Enter Ophelia]
<o </o
Hamlet:
Thou art as lovelyas the product of a large rural town and my amazing
bottomless embroidered purse. Speak thy mind!
<o </o
Thou art as lovingas the product of the bluest clearest sweetest sky
and the sum of asquirrel and a white horse. Thou art as beautiful as
the difference between Juliet and thyself. Speak thy mind!
<o </o
[Exeunt Ophelia and Hamlet]
<o </o
<o </o
                   Act II: Behind Hamlet's back.
<o </o
                   Scene I: Romeo and Juliet's conversation.
<o </o
[Enter Romeo and Juliet]
<o </o
Romeo:
Speak your mind.You are as worried as the sum of yourself and the
difference betweenmy small smooth hamster and my nose. Speak your
mind!
<o </o
Juliet:
Speak YOUR mind!You are as bad as Hamlet! You are as small as the
difference between the square of the difference between my little pony
and your big hairyhound and the cube of your sorry little
codpiece. Speakyour mind!
<o </o
[Exit Romeo]
<o </o
                   Scene II: Juliet and Ophelia's conversation.
<o </o
[Enter Ophelia]
<o </o
Juliet:
Thou art as goodas the quotient between Romeo and the sum of a small
furry animal and aleech. Speak your mind!
<o </o
Ophelia:
Thou art asdisgusting as the quotient between Romeo and twice the
difference betweena mistletoe and an oozing infected blister! Speak
your mind!
<o </o
[Exeunt]
<o < o</o 
我们现在来看看上面这段代码是怎么打出Hello World的。
</o 
标题:

<o < o
莎士比亚语言里每个源码的第一句话都是标题。或者更精确的说,第一个句号之前的所有字符都是标题。在莎士比亚语言中,您可以随心所欲的缩进,空格,或是换行。不过,我们由衷地提醒您,适当的缩进将会显著提高源码的可读性。
当然,标题的存在完全由美学的角度出发的;在编译器看来,它只不过是一行注释语句。
</o 
演员表:

<o < o
标题下面的一段代码是演员表。您可以把演员当作变量理解,每个演员可以存一个整数。演员表里必须包含整个程序需要用到的所有演员,不然无法编译。
演员的声明包括:演员名字和演员概述(概述将直接被编译器跳过)。需要注意的是,您不可以给演员随便起名字;您必须使用莎士比亚著作中出现过的人物,比如Romeo, Juliet, 或者Ghost (哈姆雷特已故的父亲)。
</o 
(Act)、场(Scene)

<o < o
幕次和场次的主要作用是把一出戏划分为若干个小部分。通常来讲,一出戏里分若干幕,每一幕里分若干场,每一场里有若干句台词,以及控制登场和退场的描述性语句。
幕次和场次都需要用罗马数字标明;它们之后可以有描述性语句,但不影响编译。
除了具有文学修饰作用外,幕次和场次同时担负着标签的责任,在程序运行时可以配合跳转语句控制流程。上面的Hello World 程序没有用到跳转语句,所以我们稍后再说。
</o 
上场(enter)、下场(exit)、齐下(exeunt)

<o < o
演员必须登台亮相才能开始说台词。如果台词中提到了“你” 或者“你的”等第二人称代词(you, thou, thy, thee, etc),则必须有两人在场。若不足或超过两人在场,编译会出错。
您可以使用 Enter 关键字,之后加上一串需要登场的人名,使演员们登场。Exit 关键字之后必须只有一个人名;若有多人需要退场,请使用“齐下”。Exeunt 关键字后至少写两个人名;或不写人名,以表示所有演员退场。
要求已登台的人物上场,或要求未登台人物下场,会导致运行时刻错误。
</o 
台词:

<o < o
每句台词是由人名,冒号,以及一段话组成的。在Hello World 例子里,我们只用到了两种句型,即输出语句和赋值语句。输出语句负责把内容显示到屏幕上,而赋值语句则用来改变在场的第二个人物的值。
常量:<o< o</o
莎士比亚编程语言根据名词的褒贬以表示数字1 或者-1. 例如Flower可用作表示1 因为通常情况下,花儿是好的;反之Pig 则可被用作表示-1. 较为中立的名词,如Tree,也被纳入褒义类,可用来表示1.
名词之前的每个形容词都会将名词的值翻倍,如Pig 之前加三个形容词,就可用来表示-8. 在此基础上,配合基本运算,如“猪和花之和”等,即可轻易得到您需要的常数。
例如 the difference between the square of the difference between my little pony and your big hairy hound and the cube of your sorry little codpiece 这句话所表达的值为 -60.
正如您所见,这种表达常数的方法可以给予您无限诗意。
赋值:<o< o</o
我们来看两个例子:You lying stupid fatherless big smelly half-witted coward! 以及You are as stupid as the difference between a handsome rich brave hero and thyself!
第一个例子比较简单——第二人称代词后面接上一个常量,即可完成赋值。在这个例子中,舞台上没有说这句话的那位演员,将会被赋值-64.
第二个例子稍微复杂。我们首先要知道thyself (你自己)等于多少,然后再考虑比喻句as good as, 或者as bad as, 或者as + 形容词+ as 其实也是赋值的意思。
输出:<o< o</o
莎士比亚编程语言中包括两种输出语句:”Openyour heart” 和 “Speak your mind”. 第一种语句用作输出对方演员的值,比如 72;第二个语句则会以字母或符号形式输出对方演员的值。比如根据ASCII, 65 会被输出为 A。
</o 
稍微实用的例子(找质数):<o < o
现在我们来看一个稍微复杂的例子——找质数(源码见附录)。运行时,用户输入一个整数,程序循环打印出所有小于或等于它的质数。
这个程序用到了3个我们没见过的语句:输入、跳转、逻辑判断。
</o 
输入:

<o < o
输入语句和输出语句有些类似。若想读入一个数字,用”Listento your heart.”读入字符请用 ”Open your mind.” 显然,读入的数字或字符会被赋值于对方演员。
</o 
跳转:

<o < o
当您使用”Letus return to scene III”这样的语句,程序将跳转到本幕第三场。莎士比亚语言不支持跳转到其他幕的特定场次,但您可以跳转到其他幕(Act)的第一场。当然,除了”letus” 您还可以用”we shall” 或者 “we must”;除了”returnto” 您也可以用 “proceed to”。这些语句都是等效的。
</o 
逻辑判断:

<o < o
条件判断语句分两步。我们举例说明:
Juliet:
Am I better thanyou?
<o </o
Hamlet:
If so, let usproceed to scene III.
<o < o</o 
首先,其中一名演员提出问题。这个问题应该在一定程度上有些对比的成分,可被true或者false 作答。之后当有人提出 “if so” 表示肯定,或者 “if not” 表示否定,之后加上一个逗号的时候,则表示逗号后面的半句话将在条件满足时才被执行。
这和许多种assembly语言中的条件跳转操作相似。
</o 
对比:

<o < o
正如您所意料,对比语句分三种:”is X as good as Y” 表示相等,“is X better than Y” 表示X > Y (您可以用任何褒义形容词代替这里的good),而“worse” 等贬义形容词可用来表示X < Y. 当然,您也可以用 “not as good as” 或者 “not better than” 来翻转结果。
</o 
稍微复杂的例子(翻转字串):<o < o
至此,读者可能会认为莎士比亚编程语言跟其他编程语言相比,虽然更花哨并且更有诗意,但除了可用变量少了点儿(编译器可识别出约100个莎士比亚用过的人名)外,功能却一点儿都不缺。
为了解决变量数量上的局限,我们为莎士比亚语言引入了stack的概念。您可以先去文章附录看一个翻转字串程序的源码。该程序读入一个字符串,并翻转后输出。
</o 
(stack)

<o < o
莎士比亚语言中的每个角色都不简单——他们不但能记得当前发生的事情,并且能记得以前的事情(就像我们一样)!根据当今最流行的心理学研究成果,我们决定用stack来为演员们完成这项任务。
每个演员都可将任意整数进栈(push)至到他们的记忆里,以便稍后出栈(pop). 例如:
Lady Macbeth:
Remember me.
<o < o</o 
这句话显然是将LadyMacbeth 的值push到与她对话的演员的stack里。
Lady Macbeth:
Recall yourimminent death!
<o < o</o 
这句话即可将对方stack里的第一项pop出来,并赋值给对方自己。注意这里唯一重要的词是”recall”,后面的内容都是饱含艺术气息的修饰。
请注意,试图pop 一个空的stack是编故事能力欠佳的明显表现,系统会在运行时对您表示十分失望。
<o < o</o 
</o 
附录:

找质数源码:

<o < o
Prime Number Computation in Copenhagen.
<o </o
Romeo, a young man of Verona.
Juliet, a young woman.
Hamlet, a temporary variable from Denmark.
The Ghost, a limiting factor (and by a remarkable coincidencealso
       Hamlet'sfather).
<o </o
<o </o
                   Act I: Interview with the other side.
<o </o
                   Scene I: At the last hour before dawn.
<o </o
[Enter the Ghost and Juliet]
<o </o
The Ghost:
You pretty little warm thing! Thou art as prompt as the difference
between the square of thyself and your golden hair. Speak your mind.
<o </o
Juliet:
Listen to your heart!
<o </o
[Exit the Ghost]
<o </o
[Enter Romeo]
<o </o
Juliet:
Thou art as sweetas a sunny summer's day!
<o </o
<o </o
                   Act II: Determining divisibility.
<o </o
                   Scene I: A private conversation.
<o </o
Juliet:
Art thou more cunning than the Ghost?
<o </o
Romeo:
If so, let usproceed to scene V.
<o </o
[Exit Romeo]
<o </o
[Enter Hamlet]
<o </o
Juliet:
You are as villainous as the square root of Romeo!
<o </o
Hamlet:
You are as lovely as a red rose.
<o </o
                   Scene II: Questions and the consequences thereof.
<o </o
Juliet:
Am I better than you?
<o </o
Hamlet:
If so, let us proceed to scene III.
<o </o
Juliet:
Is the remainder of the quotient between Romeo and me as good as
nothing?
<o </o
Hamlet:
If so, let us proceed to scene IV.
Thou art as bold as the sum of thyself and a roman.
<o </o
Juliet:
Let us return to scene II.
<o </o
                   Scene III: Romeo must die!
<o </o
[Exit Hamlet]
<o </o
[Enter Romeo]
<o </o
Juliet:
Open your heart.
<o </o
[Exit Juliet]
<o </o
[Enter Hamlet]
<o </o
Romeo:
Thou art as rottenas the difference between nothing and the sum of a
snotty stinking half-witted hog and a small toad!
Speak your mind!
<o </o
[Exit Romeo]
<o </o
[Enter Juliet]
<o </o
                   Scene IV: One small dog at a time.
<o </o
[Exit Hamlet]
<o </o
[Enter Romeo]
<o </o
Juliet:
Thou art ashandsome as the sum of thyself and my chihuahua!
Let us return toscene I.
<o </o
                   Scene V: Fin.
<o </o
[Exeunt]
<o < o</o 
</o 
翻转字串源码:<o < o
<o < o</o 
Outputting Input Reversedly.
<o </o
Othello, a stacky man.
Lady Macbeth, who pushes him around till he pops.
<o </o
<o </o
                   Act I: The one and only.
<o </o
                   Scene I: In the beginning, there was nothing.
<o </o
[Enter Othello and Lady Macbeth]
<o </o
Othello:
You are nothing!
<o </o
                   Scene II: Pushing to the very end.
<o </o
Lady Macbeth:
Open your mind! Remember yourself.
<o </o
Othello:
You are as hard as the sum of yourself and a stone wall. Am I as
horrid as a flirt-gill?
<o </o
Lady Macbeth:
If not, let us return to scene II. Recall your imminent death!
<o </o
Othello:
You are as smallas the difference between yourself and a hair!
<o </o
                   Scene III: Once you pop, you can't stop!
<o </o
Lady Macbeth:
Recall your unhappy childhood. Speak your mind!
<o </o
Othello:
You are as vile asthe sum of yourself and a toad! Are you better
than nothing?
<o </o
Lady Macbeth:
If so, let us return to scene III.
<o </o
                   Scene IV: The end.
<o </o
[Exeunt]
</o 



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

1

精华

32

主题

509

帖子

天庭首脑(超版)

Rank: 8Rank: 8

银两
2382
贡献
0
主持积分
0
杀营积分
0
侠营积分
0
演员积分
0
游戏次数
0
注册时间
2013-6-10
沙发
发表于 2015-10-28 12:05 | 只看该作者
我突然觉得蛋有点儿疼。。。。。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

存档|手机|黑牢|杀墨传奇

GMT+8, 2026-2-1 19:34 , Processed in 0.020277 second(s), 10 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表