<commit_before># Language Reference

## Primitive Numeric Types:

zig          |        C equivalent    | Description
-------------|------------------------|-------------------------------
        bool |                   bool |  unsigned 1-bit integer
          i8 |                 int8_t |    signed 8-bit integer
          u8 |                uint8_t |  unsigned 8-bit integer
         i16 |                int16_t |   signed 16-bit integer
         u16 |               uint16_t | unsigned 16-bit integer
         i32 |                int32_t |   signed 32-bit integer
         u32 |               uint32_t | unsigned 32-bit integer
         i64 |                int64_t |   signed 64-bit integer
         u64 |               uint64_t | unsigned 64-bit integer
         f32 |                  float |  32-bit IEE754 floating point
         f64 |                 double |  64-bit IEE754 floating point
        f128 |            long double | 128-bit IEE754 floating point
       isize |               intptr_t |   signed pointer sized integer
       usize |              uintptr_t | unsigned pointer sized integer
     c_short |                  short | for API compatibility with C
    c_ushort |         unsigned short | for API compatibility with C
       c_int |                    int | for API compatibility with C
      c_uint |           unsigned int | for API compatibility with C
      c_long |                   long | for API compatibility with C
     c_ulong |          unsigned long | for API compatibility with C
  c_longlong |              long long | for API compatibility with C
 c_ulonglong |     unsigned long long | for API compatibility with C

## Grammar

```
Root : many(TopLevelDecl) token(EOF)

TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Import | ContainerDecl | VariableDeclaration

VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) PrefixOpExpression option(token(Eq) Expression))

ContainerDecl : many(Directive) option(FnVisibleMod) (token(Struct) | token(Enum)) token(Symbol) token(LBrace) many(StructMember) token(RBrace)

StructMember: StructField | FnDecl

StructField : token(Symbol) option(token(Colon) Expression) token(Comma))

Import : many(Directive) token(Import) token(String) token(Semicolon)

RootExportDecl : many(Directive) token(Export) token(Symbol) token(String) token(Semicolon)

ExternBlock : many(Directive) token(Extern) token(LBrace) many(FnDecl) token(RBrace)

FnProto : many(Directive) option(FnVisibleMod) token(Fn) token(Symbol) ParamDeclList option(PrefixOpExpression)

Directive : token(NumberSign) token(Symbol) token(LParen) token(String) token(RParen)

FnVisibleMod : token(Pub) | token(Export)

FnDecl : FnProto token(Semicolon)

FnDef : FnProto token(FatArrow) Block

ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen)

ParamDecl : option(token(NoAlias)) token(Symbol) token(Colon) PrefixOpExpression | token(Ellipsis)

Block : token(LBrace) list(option(Statement), token(Semicolon)) token(RBrace)

Statement : Label | VariableDeclaration token(Semicolon) | NonBlockExpression token(Semicolon) | BlockExpression

Label: token(Symbol) token(Colon)

Expression : BlockExpression | NonBlockExpression

NonBlockExpression : ReturnExpression | AssignmentExpression

AsmExpression : token(Asm) option(token(Volatile)) token(LParen) token(String) option(AsmOutput) token(RParen)

AsmOutput : token(Colon) list(AsmOutputItem, token(Comma)) option(AsmInput)

AsmInput : token(Colon) list(AsmInputItem, token(Comma)) option(AsmClobbers)

AsmOutputItem : token(LBracket) token(Symbol) token(RBracket) token(String) token(LParen) (token(Symbol) | token(Arrow) PrefixOpExpression) token(RParen)

AsmInputItem : token(LBracket) token(Symbol) token(RBracket) token(String) token(LParen) Expression token(RParen)

AsmClobbers: token(Colon) list(token(String), token(Comma))

UnwrapMaybeExpression : BoolOrExpression token(DoubleQuestionMark) BoolOrExpression | BoolOrExpression

AssignmentExpression : UnwrapMaybeExpression AssignmentOperator UnwrapMaybeExpression | UnwrapMaybeExpression

AssignmentOperator : token(Eq) | token(TimesEq) | token(DivEq) | token(ModEq) | token(PlusEq) | token(MinusEq) | token(BitShiftLeftEq) | token(BitShiftRightEq) | token(BitAndEq) | token(BitXorEq) | token(BitOrEq) | token(BoolAndEq) | token(BoolOrEq) 

BlockExpression : IfExpression | Block | WhileExpression | ForExpression

WhileExpression : token(While) token(LParen) Expression token(RParen) Expression

ForExpression : token(For) token(LParen) Symbol token(Comma) Expression option(token(Comma) token(Symbol)) token(RParen) Expression

BoolOrExpression : BoolAndExpression token(BoolOr) BoolOrExpression | BoolAndExpression

ReturnExpression : token(Return) option(Expression)

IfExpression : IfVarExpression | IfBoolExpression

IfBoolExpression : token(If) token(LParen) Expression token(RParen) Expression option(Else)

IfVarExpression : token(If) token(LParen) (token(Const) | token(Var)) token(Symbol) option(token(Colon) PrefixOpExpression) Token(MaybeAssign) Expression token(RParen) Expression Option(Else)

Else : token(Else) Expression

BoolAndExpression : ComparisonExpression token(BoolAnd) BoolAndExpression | ComparisonExpression

ComparisonExpression : BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression

ComparisonOperator : token(BoolEq) | token(BoolNotEq) | token(BoolLessThan) | token(BoolGreaterThan) | token(BoolLessEqual) | token(BoolGreaterEqual)

BinaryOrExpression : BinaryXorExpression token(BinOr) BinaryOrExpression | BinaryXorExpression

BinaryXorExpression : BinaryAndExpression token(BinXor) BinaryXorExpression | BinaryAndExpression

BinaryAndExpression : BitShiftExpression token(Ampersand) BinaryAndExpression | BitShiftExpression

BitShiftExpression : AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpression

BitShiftOperator : token(BitShiftLeft) | token(BitShiftRight)

AdditionExpression : MultiplyExpression AdditionOperator AdditionExpression | MultiplyExpression

AdditionOperator : token(Plus) | token(Minus)

MultiplyExpression : CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression

CurlySuffixExpression : PrefixOpExpression option(ContainerInitExpression)

MultiplyOperator : token(Star) | token(Slash) | token(Percent)

PrefixOpExpression : PrefixOp PrefixOpExpression | SuffixOpExpression

SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)

FieldAccessExpression : token(Dot) token(Symbol)

FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)

ArrayAccessExpression : token(LBracket) Expression token(RBracket)

SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression) token(RBracket) option(token(Const))

ContainerInitExpression : token(LBrace) ContainerInitBody token(RBrace)

ContainerInitBody : list(StructLiteralField, token(Comma)) | list(Expression, token(Comma))

StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression

PrefixOp : token(Not) | token(Dash) | token(Tilde) | token(Star) | (token(Ampersand) option(token(Const))) | token(QuestionMark)

PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | token(Symbol) | (token(AtSign) token(Symbol) FnCallExpression) | ArrayType | AsmExpression

ArrayType : token(LBracket) option(Expression) token(RBracket) option(token(Const)) PrefixOpExpression

GotoExpression: token(Goto) token(Symbol)

GroupedExpression : token(LParen) Expression token(RParen)

KeywordLiteral : token(True) | token(False) | token(Null) | token(Break) | token(Continue)
```

## Operator Precedence

```
x() x[] x.y
!x -x ~x *x &x ?x
x{}
* / %
+ -
<< >>
&
^
|
== != < > <= >=
&&
||
??
= *= /= %= += -= <<= >>= &= ^= |= &&= ||=
```

## Literals

### Characters and Strings

                | Example  | Characters  | Escapes        | Null Term | Type
----------------|----------|-------------|----------------|-----------|----------
 Byte           | 'H'      | All ASCII   | Byte           | No        | u8
 UTF-8 Bytes    | "hello"  | All Unicode | Byte & Unicode | No        | [5; u8]
 UTF-8 C string | c"hello" | All Unicode | Byte & Unicode | Yes       | *const u8

### Byte Escapes

      | Name
------|----------------------------------------
 \x7F | 8-bit character code (exactly 2 digits)
 \n   | Newline
 \r   | Carriage return
 \t   | Tab
 \\   | Backslash
 \0   | Null
 \'   | Single quote
 \"   | Double quote

### Unicode Escapes

          | Name
----------|-----------------------------------------------
 \u{7FFF} | 24-bit Unicode character code (up to 6 digits)

### Numbers

 Number literals    | Example     | Exponentiation
--------------------|-------------|---------------
 Decimal integer    | 98222       | N/A
 Hex integer        | 0xff        | N/A
 Octal integer      | 0o77        | N/A
 Binary integer     | 0b11110000  | N/A
 Floating-point     | 123.0E+77   | Optional
 Hex floating point | TODO        | TODO
<commit_msg>add switch statement to language reference<commit_after># Language Reference

## Primitive Numeric Types:

zig          |        C equivalent    | Description
-------------|------------------------|-------------------------------
        bool |                   bool |  unsigned 1-bit integer
          i8 |                 int8_t |    signed 8-bit integer
          u8 |                uint8_t |  unsigned 8-bit integer
         i16 |                int16_t |   signed 16-bit integer
         u16 |               uint16_t | unsigned 16-bit integer
         i32 |                int32_t |   signed 32-bit integer
         u32 |               uint32_t | unsigned 32-bit integer
         i64 |                int64_t |   signed 64-bit integer
         u64 |               uint64_t | unsigned 64-bit integer
         f32 |                  float |  32-bit IEE754 floating point
         f64 |                 double |  64-bit IEE754 floating point
        f128 |            long double | 128-bit IEE754 floating point
       isize |               intptr_t |   signed pointer sized integer
       usize |              uintptr_t | unsigned pointer sized integer
     c_short |                  short | for API compatibility with C
    c_ushort |         unsigned short | for API compatibility with C
       c_int |                    int | for API compatibility with C
      c_uint |           unsigned int | for API compatibility with C
      c_long |                   long | for API compatibility with C
     c_ulong |          unsigned long | for API compatibility with C
  c_longlong |              long long | for API compatibility with C
 c_ulonglong |     unsigned long long | for API compatibility with C

## Grammar

```
Root : many(TopLevelDecl) "EOF"

TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Import | ContainerDecl | VariableDeclaration

VariableDeclaration : option(FnVisibleMod) ("var" | "const") "symbol" ("=" Expression | ":" PrefixOpExpression option("=" Expression))

ContainerDecl : many(Directive) option(FnVisibleMod) ("struct" | "enum") "Symbol" "{" many(StructMember) "}"

StructMember: StructField | FnDecl

StructField : "Symbol" option(":" Expression) ",")

Import : many(Directive) "import" "String" ";"

RootExportDecl : many(Directive) "export" "Symbol" "String" ";"

ExternBlock : many(Directive) "extern" "{" many(FnDecl) "}"

FnProto : many(Directive) option(FnVisibleMod) "fn" "Symbol" ParamDeclList option(PrefixOpExpression)

Directive : "#" "Symbol" "(" "String" ")"

FnVisibleMod : "pub" | "export"

FnDecl : FnProto ";"

FnDef : FnProto "=>" Block

ParamDeclList : "(" list(ParamDecl, ",") ")"

ParamDecl : option("noalias") "Symbol" ":" PrefixOpExpression | "..."

Block : "{" list(option(Statement), ";") "}"

Statement : Label | VariableDeclaration ";" | NonBlockExpression ";" | BlockExpression

Label: "Symbol" ":"

Expression : BlockExpression | NonBlockExpression

NonBlockExpression : ReturnExpression | AssignmentExpression

AsmExpression : "asm" option("volatile") "(" "String" option(AsmOutput) ")"

AsmOutput : ":" list(AsmOutputItem, ",") option(AsmInput)

AsmInput : ":" list(AsmInputItem, ",") option(AsmClobbers)

AsmOutputItem : "[" "Symbol" "]" "String" "(" ("Symbol" | "->" PrefixOpExpression) ")"

AsmInputItem : "[" "Symbol" "]" "String" "(" Expression ")"

AsmClobbers: ":" list("String", ",")

UnwrapMaybeExpression : BoolOrExpression "??" BoolOrExpression | BoolOrExpression

AssignmentExpression : UnwrapMaybeExpression AssignmentOperator UnwrapMaybeExpression | UnwrapMaybeExpression

AssignmentOperator : "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" | "&&=" | "||="

BlockExpression : IfExpression | Block | WhileExpression | ForExpression | SwitchExpression

SwitchExpression : "switch" "(" Expression ")" "{" many(SwitchProng) "}"

SwitchProng : (list(SwitchItem, ",") | "else") option("(" "Symbol" ")") "=>" Expression ","

SwitchItem : Expression | (Expression "..." Expression)

WhileExpression : "while" "(" Expression ")" Expression

ForExpression : "for" "(" "Symbol" "," Expression option("," "Symbol") ")" Expression

BoolOrExpression : BoolAndExpression "||" BoolOrExpression | BoolAndExpression

ReturnExpression : "return" option(Expression)

IfExpression : IfVarExpression | IfBoolExpression

IfBoolExpression : "if" "(" Expression ")" Expression option(Else)

IfVarExpression : "if" "(" ("const" | "var") "Symbol" option(":" PrefixOpExpression) "?=" Expression ")" Expression Option(Else)

Else : "else" Expression

BoolAndExpression : ComparisonExpression "&&" BoolAndExpression | ComparisonExpression

ComparisonExpression : BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression

ComparisonOperator : "==" | "!=" | "<" | ">" | "<=" | ">="

BinaryOrExpression : BinaryXorExpression "|" BinaryOrExpression | BinaryXorExpression

BinaryXorExpression : BinaryAndExpression "^" BinaryXorExpression | BinaryAndExpression

BinaryAndExpression : BitShiftExpression "&" BinaryAndExpression | BitShiftExpression

BitShiftExpression : AdditionExpression BitShiftOperator BitShiftExpression | AdditionExpression

BitShiftOperator : "<<" | ">>"

AdditionExpression : MultiplyExpression AdditionOperator AdditionExpression | MultiplyExpression

AdditionOperator : "+" | "-"

MultiplyExpression : CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression

CurlySuffixExpression : PrefixOpExpression option(ContainerInitExpression)

MultiplyOperator : "*" | "/" | "%"

PrefixOpExpression : PrefixOp PrefixOpExpression | SuffixOpExpression

SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)

FieldAccessExpression : "." "Symbol"

FnCallExpression : "(" list(Expression, ",") ")"

ArrayAccessExpression : "[" Expression "]"

SliceExpression : "[" Expression "..." option(Expression) "]" option("const")

ContainerInitExpression : "{" ContainerInitBody "}"

ContainerInitBody : list(StructLiteralField, ",") | list(Expression, ",")

StructLiteralField : "." "Symbol" "=" Expression

PrefixOp : "!" | "-" | "~" | "*" | ("&" option("const")) | "?"

PrimaryExpression : "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | AsmExpression

ArrayType : "[" option(Expression) "]" option("const") PrefixOpExpression

GotoExpression: "goto" "Symbol"

GroupedExpression : "(" Expression ")"

KeywordLiteral : "true" | "false" | "null" | "break" | "continue"
```

## Operator Precedence

```
x() x[] x.y
!x -x ~x *x &x ?x
x{}
* / %
+ -
<< >>
&
^
|
== != < > <= >=
&&
||
??
= *= /= %= += -= <<= >>= &= ^= |= &&= ||=
```

## Literals

### Characters and Strings

                | Example  | Characters  | Escapes        | Null Term | Type
----------------|----------|-------------|----------------|-----------|----------
 Byte           | 'H'      | All ASCII   | Byte           | No        | u8
 UTF-8 Bytes    | "hello"  | All Unicode | Byte & Unicode | No        | [5; u8]
 UTF-8 C string | c"hello" | All Unicode | Byte & Unicode | Yes       | *const u8

### Byte Escapes

      | Name
------|----------------------------------------
 \x7F | 8-bit character code (exactly 2 digits)
 \n   | Newline
 \r   | Carriage return
 \t   | Tab
 \\   | Backslash
 \0   | Null
 \'   | Single quote
 \"   | Double quote

### Unicode Escapes

          | Name
----------|-----------------------------------------------
 \u{7FFF} | 24-bit Unicode character code (up to 6 digits)

### Numbers

 Number literals    | Example     | Exponentiation
--------------------|-------------|---------------
 Decimal integer    | 98222       | N/A
 Hex integer        | 0xff        | N/A
 Octal integer      | 0o77        | N/A
 Binary integer     | 0b11110000  | N/A
 Floating-point     | 123.0E+77   | Optional
 Hex floating point | TODO        | TODO

{'fraction_non_alphanumeric': 0.14605431868516144, 'fraction_numerical': 0.012122625014570463, 'mean_word_length': 3.1922795015880774, 'pattern_counts': {'":': 8, '<': 21, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7098698', 'n_tokens_mistral': 5063, 'n_tokens_neox': 4752, 'n_words': 1182}