Common Syntax

Example of common syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
###########################################################
# Example of Common Syntax suitable for testing Pygments.
# The program does not make much sense, it’s just a test.
# This comment block tests out end-of-line comments.
###########################################################

# This illustrates the keyword val, the := operator, 
# function calls, whitespace, symbols and punctuation.
val name1 := cgiGet( 'name1' );
val name2 := cgiGet( 'name2' );

# This is a function definition which involves the
# define/enddefine keywords and the => punctuation.
define output() =>>
    # Shows if/then/elseif/endif syntax and also
    # strings and environment variable syntax.
    if ${OUTPUT} = "html" then
        # This illustrates the embedded XML syntax.
        <html>
            # Here we have text with interpolated expressions
            # and an escaped HTML character entity.
####        Issue: string interpolation.
####        <title> "Welcome \(name1) \&amp; \(name2)" </title>
            <body>
        
            </body>
        </html>
    elseif ${OUTPUT}... = "text"... then  # Shows explodes.
        # Loops, lists and vectors with numeric literals.
        # Note use of parallel iteration operator.
        {
####        Issue: parallel iterators
            for i in [ 2, 3, 5, 7, 11 ]  #### // n from 1 
            do
                tmp := newVector( i, i );
                # Some arithmetic operators.
                if 2 * n > i + 1 then
                    # The other postfix function appl. syntax.
                    # Also the indexing operator.
                    n @factorise -> i[ 1 ]
               endif
            endfor
        } =: v;    # Reverse short declaration operator.
    else
        # Shows postfix function application.
        throw UnexpectedValue return _ with (
            "$OUTPUT has unexpected value",
            '$OUTPUT' -> ${OUTPUT}
        )
    endif
enddefine;

output().println; # And postfix function application.

Moar (test) examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#   This will be built-in at some point soon.
define print( x ) =>>
    _print( x )
enddefine;

#   So will this.
define println( x ) =>>
    _print( x );
    _putchar( '\n' )
enddefine;

define hello() =>>
    println( "hello, world!" )
enddefine;
1
2
3
4
5
6
7
define nfib( n ) =>>
    if n <= 1 then
        1
    else
        1 + nfib( n - 1 ) + nfib( n - 2 )
    endif
enddefine;
1
2
3
4
5
6
7
define fact( n ) =>>
    if n <= 1 then
        1
    else
        n * fact( n - 1 )
    endif
enddefine;

Formal description

Use http://railroad.my28msec.com/rr/ui to produce a railroad diagram.

Package ::=
    'package' PackageName ';' Import* TopLevelForms 'endpackage'

Import ::=
    'import' ImportQualifier* Tags? from PackageName ( 'alias' AliasName )? ( 'into' Tags )? ';'

ImportQualifier ::=
    'pervasive' |
    'nonpervasive' |
    'qualified' |
    'unqualified'

Tags ::= '(' ( Tag ( ',' Tag )* )? ')'

TopLevelForms ::= ( Declaration | Statement ) ( ';' TopLevelForms )*

Declaration ::=
    Definition |
    Query |
    RecordclassDeclaration

Definition ::=
    'define' ApplyPattern '=>>' Statements 'enddefine'

RecordclassDeclaration ::=
    'recordclass' Identifier
        ( 'slot' Identifier ';' )*
    'endrecordclass'

Statements ::=
    Statement ( ';' Statements )*

Statement ::=
    Query |
    Expr

Query ::=
    Pattern (
        ':=' Expr |
        'in' Expr |
        FromByTo
    )

Pattern ::=
    Literal |
    ( ( 'var' | 'val' )? Tags )? Identifier |
    Pattern ( (','|';') Pattern )+  |
    '(' Pattern? ')' |
    '[' Pattern? ']' |
    '{' Pattern? '}' |
    '[%' Pattern? '%]' |
    '\(' Expression ')' |
    ApplyPattern

ApplyPattern ::=
    Identifier '(' Pattern? ')' |
    Pattern ( '.'|'@') ( Identifier | '(' Expr ')' ) Pattern

FromByTo ::=
    'from' Expr ('by' Expr )? ('to' Expr)? |
    'by' Expr ('to' Expr)? |
    'to' Expr .

Expr ::=
    PrimaryExpr ( InfixOperator Expr )*


PrimaryExpr ::=
    AtomicExpr |
    AssignExpr |
    ApplyExpr |
    LambdaExpr |
    ListExpr |
    VectorExpr |
    ConditionalExpr |
    LoopExpr

AtomicExpr ::=
    Literal |
    Identifier |
    '(' Statements? ')'

Literal ::=
    'absent' |
    ( 'true' | 'false' ) |
    Number |
    CharacterConstant |
    String

AssignExpr ::=
    Expr '=::' TargetExpr |
    TargetExpr '::=' Expr

TargetExpr ::=
    Identifier |
    ApplyExpr |
    ConditionalTarget

ConditionalTarget ::=
    'if' CoreConditionalTarget 'endif' |
    'unless' CoreConditionalTarget 'endunless'

CoreConditionalTarget ::=
    Expr then TargetExpr MoreConditionalTarget* 'else' TargetExpr .

MoreConditionalTarget ::=
    ( 'elseif' | 'elseunless' ) Expr then TargetExpr .

ApplyExpr ::=
    AtomicExpr '(' Statements? ')' |
    PrimaryExpr ( '.' | '@' ) AtomicExpr PrimaryExpr .


LambdaExpr ::=
    'fn' AppExpr '=>>' Statements 'endfn'

ListExpr ::=
    '[' Expr* ( '|' Expr )? ']'

VectorExpr ::=
    '{' Expr '}'

ConditionalExpr ::=
    'if' CoreConditional 'endif' |
    'unless' CoreConditional 'endunless'

CoreConditional ::=
    Expr then Statements MoreConditional* ( 'else' Statements )?

MoreConditional ::=
    ( 'elseif' | 'elseunless' ) Expr then Statements

LoopExpr ::=
    'for' Query 'do' Statements 'endfor'


Number ::=
    digit+ ( '.' digit+ )

String ::=
    '"' QuotedCharacter* '"'

CharacterConstant ::=
    "'" QuotedCharacter "'"

QuotedCharacter ::=
    printing_character |
    '\\' ( 'n' | 'r' | 's' | 't' | 'v' ) |
    '\\&' HTMLEntity ';' |
    '\\(' Expr ')'

InfixOperator ::= ',' | '+' | '-' | '*' | '/'