现在的位置: 首页 > 综合 > 正文

有趣的“h 语言”

2013年12月13日 ⁄ 综合 ⁄ 共 2958字 ⁄ 字号 评论关闭

从微软学生社区找到的:http://www.wildnoodle.com/ic2006/UI/Compete/Tutorial.aspx

The "h" language

Herbert is programmed in a simple but powerful language called "h". "h" contains elements of traditional high-level languages: statements, procedures, parameters, arguments, and recursion. However, "h" is syntactically simpler, and contains some concepts (procedural arguments) that are not found in traditional languages.

Here are the basic elements of "h":

Element   Syntax   Examples  
Statement s(go straight), r (turn right), or l (turn left), a procedure call, or a procedural parameter. s
Procedure definition x[(P1,P2,...,Pk)]:y1y2y3...yn where x is any lowercase letter (except s, r, or l), Pi is a parameter, yi is any statement, 0 <= k <=15, and >= 0. If k = 0 (no parameters) then the parentheses are not used. a:ssssr
b(C,D):sra(B)C
Parameter X where X is any uppercase letter. A
Expression [-] X [(+ or -) X [(+ or -) X [(+ or -) X ... ]]] where each X is either a numeric parameter or a number. 5
A-5
-A-B-C+1
Argument Any number, expression, or sequence of zero or more statements. 4
rsr
A-1
Procedure call x[(a1,a2,...,ak)] where x is the name of a procedure and ai is an argument (one for each parameter). If k = 0 (no parameters) then the parentheses are not used. No call is made if any numeric argument is 0 or less. a(1,B-1,srs)
b
Recursion x[(P1,P2,...,Pk)]:y1y2y3...ymx[(a1,a2,a3,..,ak)]ym+1ym+2ym+3...yn where x is any lowercase letter (except s, r, or l), Pi is a parameter, ai is an argument, yi is any statement, 0<= k <= 15, m >= 0, and >= 0. a(A):sa(A-1)
b:sssrb
Main Procedure y1y2y3...ynwhere yi is any statement and n >= 1. sa(1,rsr)r
Program

<procedure definition> 
     :
     :
<main procedure>

a:ssssra
sssa
Byte Any lowercase or uppercase letter, or any number s
A
123

a(A,B,C):f(B)Ca(A-1,B,C)
b(A):a(4,5,r)lb(A-1)
f(A):sf(A-1)
b(4)

a(A,B,C):f(B)Ca(A-1,B,C)
b(A):a(4,A,r)b(A-1)
f(A):sf(A-1)
b(10)

a(A,B,C):f(B)Ca(A-1,B,C)
b(A):a(2,11-A,r)b(A-1)
f(A):sf(A-1)
b(10)

a(A,B,C):f(B)Ca(A-1,B,C)
f(A):sf(A-1)
a(4,5,rslsr)

Limitations and Advanced Techniques
Herbert is a pretty capable robot, but he does have his limits:

  • "h" programs can be a maximum of 999 characters (including special characters and digits). Your program can have a maximum of 15 lines, and each line can have a maximum of 127 characters (excluding the function prototype; that is, including everything after the ":").
     
  • Numbers are limited to the range between -256 and 255. You will receive a runtime error if a numeric argument evaluates to a number outside this range.
     
  • Herbert's stack is circular and of size 64K. For a procedure call, the size of each stack frame is 4 * (1 + number of arguments). For evaluating procedural arguments, a stack frame of size 8 is used. What this means in practice is that programs that recurse infinitely, without returning, and without nested procedural arguments, can run forever. Other programs are limited to a maximum call depth of approximately 2,000 calls (depending on the number of arguments being passed). You will receive a runtime error if you attempt to return from a call stack which exceeds this size.
     
  • There is no inherent time limit to running Herbert programs. Keep in mind, however, that your score will be based on the number of buttons pressed (and score uploaded) by the end of the round.
     
  • Level 0, in both the tutorial and full version of Herbert, is a practice level. Programs left on this level are not saved and restored across program sessions.

抱歉!评论已关闭.