aboutsummaryrefslogtreecommitdiff
path: root/grammar.md
blob: b51adb4e99a778b53c3b323d1202aa3a4bd69f68 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
    program        = statement* EOF ;

    statement      = expr ";"
                   | definition
                   | block
                   | "if" expr block ( "else" block )? 
                   | "for" ( expr? | IDENTIFIER "in" expr ) block
                   | "return" "!?"? expr? ";"
                   | "continue" ";"
                   | "break" ";"
                   | declaration ;

    definition     = "def" IDENTIFIER struct_block
                   | "def" IDENTIFIER "("  params? ")" TYPE? block

    params         = param ( "," param )* ;
    param          = IDENTIFIER TYPE ;

    block          = "{" statement* "}" ;

    struct_block   = "{" ( definitions | declaration )+ "}" ;

    expr           = assignment ;

    declaration    = "const" IDENTIFIER TYPE = expr_list ";"
                   | "var" IDENTIFIER TYPE ( "=" expr_list )? ";" ;

    expr_list      = expr
                   | "[" expr ( "," expr )? "]"

    assignment     = INDENTIFIER ( "[" expr "]" )* "=" expr
                   | logic_or ;

    logic_or       = logic_and ( "||" logic_and )* ;
    logic_and      = equality ( "&&" equality )* ;
    equality       = comparison ( ( "!=" | "==" ) comparison )* ;
    comparison     = bitterm ( ( ">" | ">=" | "<" | "<=" ) bitterm )* ;
    bitterm        = term ( ( "<<" | ">>" | "|" | "^" ) term )* ;
    term           = bitfactor ( ( "-" | "+" ) bitfactor )* ;
    bitfactor      = factor ( "&" factor )* ;
    factor         = unary ( ( "/" | "*" | "%" ) unary )* ;

    unary          = ( "?" | "!" | "-" | "~" ) unary | call ;
    call           = primary ( "(" arguments? ")" | "." IDENTIFIER )*
                   | primary ( "[" expr "]" )+ ;
    primary        = NUMBER | STRING | FUNC | "true" | "false"
                   | "(" expr ")"
                   | IDENTIFIER ;

    arguments      = expr ( "," expr )* ;

    NUMBER         =  ( "0x" HEX_DIGIT+ | "0b" BIN_DIGIT+ | DIGIT+ ) 
                   | "'" CHAR "'";
    STRING         = "\"" ( "\\\"" | <not "\""> )* "\"" ;
    FUNC           = IDENTIFIER ;
    IDENTIFIER     = ALPHA ( ALPHA | DIGIT )* ;
    TYPE           = NON_ARRAY_TYPE | ARRAY_TYPE
    NON_ARRAY_TYPE = "number" | "bool" | "string" | FUNC_TYPE
    FUNC_TYPE      = "func" "(" ( TYPE ( "," TYPE )* )? ")" TYPE?
    ARRAY_TYPE     = "[" expr "]" NON_ARRAY_TYPE
    ALPHA          = "a" ... "z" | "A" ... "Z" | "_" ;
    ALPHA_DIGIT    = "0" ... "9" | "a" ... "f" | "A" ... "F" ;
    BIN_DIGIT      = "0" | "1" ;
    DIGIT          = "0" ... "9" ;