1 Beginning Student Language (BSL)
1.1 BSL Grammar
1.1.1 Literals
1.1.1.1 Numeric literals
1.1.1.2 String literals
1.2 Pre-defined variables
1.3 Template Variables

Pyret

William R. Turtle

Pyret is a new programming language designed to let learners use functional programming to build interesting and powerful programs with an economy of expression. It borrows liberally from many languages—as such, it should be instantly recognizable.

In the vein of the How to Design Programs languages, Pyret has been broken up into three different language levels.

Currently, only the first language level (Beginning Student Language) is available.

1 Beginning Student Language (BSL)

1.1 BSL Grammar

This is the full BSL grammar. For a gentler introduction to the language, please see the programs in the examples folder.

NOTE: braces are not part of the grammar – they are used to indicate groupings of tokens.

 

<program>                     ::= <def-expr-test-bang-newline>*

<def-expr-test-bang-newline>  ::= <def-expr-test-bang>

                               |  <newline>

<def-expr-test-bang>          ::= <definition>

                               |  <expr>

                               |  <test-case>

                               |  <big-bang>

 

 

<definition>                  ::= <def-style-definition>

                               |  <fun-style-definition>

                               |  struct: <id> (<id-list>?) <newline>

<def-style-definition>        ::= def <id>: <expr>

<fun-style-definition>        ::= fun <id> (<id-list>?): <test-expr> <newline>

                               |  fun <id> (<id-list>?): <newline> <local-defs>* <expr>

<id-list>                     ::= <id> {, <id>}*

<local-defs>                  ::= <def-style-definition>

                               |  <fun-style-definition>

 

 

<expr>                        ::= <test-expr> <newline>

                               |  <compound-expr>

<test-expr>                   ::= <simple-expr>

                               |  <boolean-comparison>

                               |  <test-expr> and <test-expr>

                               |  <test-expr> or <test-expr>

                               |  not <test-expr>

<boolean-comparison>          ::= <simple-expr> <boolean-comp-simple>+

<boolean-comp-simple>         ::= <boolean-comp-op> <simple-expr>

<boolean-comp-op>             ::= < | <= | = | != | >= | > | in

                               |  not in

<simple-expr>                 ::= <value-expr>

                               |  + <simple-expr>

                               |  - <simple-expr>

                               |  <simple-expr> + <simple-expr>

                               |  <simple-expr> - <simple-expr>

                               |  <simple-expr> * <simple-expr>

                               |  <simple-expr> / <simple-expr>

                               |  <simple-expr> % <simple-expr>

                               |  <simple-expr> ** <simple-expr>

                               |  <value-expr>[<index-expr>]

                               |  <template-expr>

<index-expr>          ::= <simple-expr>

                       |  <simple-expr>:<simple-expr>

                       |  <simple-expr>:<simple-expr>:<simple-expr>

<template-expr>               ::= ..

                               |  ...

                               |  ....

                               |  .....

                               |  ......

<value-expr>                  ::= empty

                               |  True

                               |  False

                               |  <id>

                               |  <number>

                               |  <image>

                               |  <string>

                               |  <list-expr>

                               |  (<test-expr>)

                               |  <id> (<id-list>?)

<list-expr>                   ::= [ ]

                               |  [<simple-expr>{, <simple-expr>}*]

<compound-expr>               ::= <conditional>

<conditional>                 ::= <if-expr> <else-expr>

                               |  <if-expr> <elif-expr>+ <if-end-marker>

<if-end-marker>               ::= <else-expr>

                               |  :done

<if-expr>                     ::= if <test-expr>: <newline>? <expr>

<elif-expr>                   ::= elif <test-expr>: <newline>? <expr>

<else-expr>                   ::= else: <newline>? <expr>

 

 

 

<test-case>                   ::= test: <test-expr> is: <test-expr>

 

 

<big-bang>                    ::= big_bang(<big-bang-clause>*) <newline>

<big-bang-clause>             ::= <id> = <test-expr> {, <big-bang-clause>}*

 

1.1.1 Literals
1.1.1.1 Numeric literals

There are four types of numeric literals: integers, floating point, imaginary, and inexact.

Integer literals come in four varieties: decimal integers, binary integers, octal integers, and hexadecimal integers. Binary integers start with the prefix 0b or 0B, octal integers start with the prefix 0o or 0O, and hexadecimal integers start with the prefix 0x or 0X.

Some examples of integer literals:

7
66
123456789123456789123456789
0o77
0xcafebabe

Floating point numbers are lexed just as they are in Racket.

3.14
2.718
10.
.001
1e3
1e+3
1E-3

Imaginary numbers are decimal integers or floating point numbers suffixed with the letter i. (Note that a number of the form a+bi is parsed as the addition of a real number and an imaginary number with a real part of 0.)

1i
66i
10.i
3.14i

Finally, a literal can be prefixed with 0nx, to indicate that it is inexact. (Here, a complex number such as 0nx2+3i parses as the inexact number 2+3i, NOT the inexact number 2 + 3i).

0nx1.0
0nx2+3i
0nx4i
1.1.1.2 String literals

String literals are read in exactly the same manner as they are in Racket. See the Racket reference for more information.

1.2 Pre-defined variables

empty : is_empty

The empty list.

True : is_true

The boolean value corresponding to truth.

False : is_false

The boolean value corresponding to falsehood.

1.3 Template Variables

..
A placeholder for indicating that a definition is a template
...
A placeholder for indicating that a definition is a template
....
A placeholder for indicating that a definition is a template
.....
A placeholder for indicating that a definition is a template
......
A placeholder for indicating that a definition is a template