hophop

HopHop Language Specification

This document defines the HopHop language contract for independent compiler implementations. It is written to be implementation-oriented and evolution-friendly.

Primary goals:

See also:

1. Conformance Model

1.1 Normative terms

1.2 Stability tags

1.3 Conformance profiles

2. Lexical Grammar

2.1 Source, whitespace, comments

2.2 Identifiers and reserved names

2.3 Keywords

2.4 Literals

2.5 Semicolon insertion

2.6 Directives

3. Concrete Syntax

3.1 File structure

3.2 EBNF

SourceFile      = { ImportDecl ";" } { DirectiveRun TopDecl ";" } .
StringLit       = /* lexical string literal; see [LEX-LIT-006] through [LEX-LIT-009] */ .
RuneLit         = /* lexical rune literal; see [LEX-LIT-010] through [LEX-LIT-012] */ .
Literal         = IntLit | FloatLit | StringLit | RuneLit | BoolLit | "null" .
DirectiveRun    = { Directive ";" } .
Directive       = "@" Ident [ "(" [ Literal { "," Literal } [ "," ] ] ")" ] .

ImportDecl      = "import" StringLit [ ImportAlias ] [ ImportSymbols ] .
ImportAlias     = "as" ( Ident | "_" ) .
ImportSymbols   = "{" [ ImportSymbolList ] "}" .
ImportSymbolList = ImportSymbol { ImportSep ImportSymbol } [ ImportSep ] .
ImportSymbol    = Ident [ "as" Ident ] .
ImportSep       = "," | ";" .

TopDecl         = [ "pub" ] ( StructDecl | UnionDecl | EnumDecl | TypeAliasDecl | FnDeclOrDef | TopConstDecl ) .
DeclName        = Ident | "_" .
DeclNameList    = DeclName { "," DeclName } .
TopDeclNameList = Ident { "," Ident } .

TypeParamList   = "[" Ident { "," Ident } "]" .

StructDecl      = "struct" Ident [ TypeParamList ] "{" [ StructFieldDeclList ] "}" .
UnionDecl       = "union" Ident [ TypeParamList ] "{" [ FieldDeclList ] "}" .
EnumDecl        = "enum" Ident [ TypeParamList ] Type "{" [ EnumItemList ] "}" .
TypeAliasDecl   = "type" Ident [ TypeParamList ] Type .
FieldSep        = "," | ";" .
AnonFieldSep    = ";" .

StructFieldDeclList = StructFieldDecl { FieldSep StructFieldDecl } [ FieldSep ] .
FieldDeclList   = FieldDecl { FieldSep FieldDecl } [ FieldSep ] .
AnonFieldDeclList = FieldDecl { AnonFieldSep FieldDecl } [ AnonFieldSep ] .
EnumItemList    = EnumItem { FieldSep EnumItem } [ FieldSep ] .
StructFieldDecl = ( FieldDecl | EmbeddedFieldDecl ) [ FieldDefault ] .
FieldDecl       = Ident { "," Ident } Type .
EmbeddedFieldDecl = TypeName .
FieldDefault    = "=" Expr .
EnumItem        = Ident [ EnumPayload ] [ "=" Expr ] .
EnumPayload     = "{" [ FieldDeclList ] "}" .

FnDeclOrDef     = "fn" FnName [ TypeParamList ] "(" [ ParamList ] ")" [ FnResultClause ]
                [ Block ] .
FnName          = Ident | "sizeof" .
ParamList       = ParamGroup { "," ParamGroup } .
ParamGroup      = "const" ( ( Ident | "_" ) Type | ( Ident | "_" ) "..." Type )
                | ( Ident | "_" ) { "," ( Ident | "_" ) } Type
                | ( Ident | "_" ) "..." Type .
FnResultClause  = Type | "(" FnResultGroup { "," FnResultGroup } ")" .
FnResultGroup   = Type | ( Ident { "," Ident } Type ) .

TopConstDecl    = "const" TopDeclNameList ( [ Type ] "=" ExprList ) .
LocalConstDecl  = "const" DeclNameList ( [ Type ] "=" ExprList ) .

Type            = OptionalType | PtrType | RefType | SliceType | ArrayType | VarArrayType
                | FnType | TupleType | AnonStructType | AnonUnionType | TypeName .
OptionalType    = "?" Type .
PtrType         = "*" Type .
RefType         = "&" Type .
SliceType       = "[" Type "]" .
ArrayType       = "[" Type Expr "]" .
VarArrayType    = "[" Type "." Ident "]" .
FnType          = "fn" "(" [ FnTypeParamList ] ")" [ Type ] .
TupleType       = "(" Type "," Type { "," Type } ")" .
FnTypeParamList = FnTypeParam { "," FnTypeParam } .
FnTypeParam     = "const" ( Type | Ident Type | "..." Type )
                | Type | ( Ident { "," Ident } Type ) | "..." Type .
AnonStructType  = [ "struct" ] "{" [ AnonFieldDeclList ] "}" .
AnonUnionType   = "union" "{" [ AnonFieldDeclList ] "}" .
TypeArgList     = "[" Type { "," Type } "]" .
TypeName        = Ident { "." Ident } [ TypeArgList ] .

Block           = "{" [ StmtList ] "}" .
StmtList        = Stmt { ";" Stmt } [ ";" ] .
Stmt            = Block | VarDeclStmt | LocalConstDecl | IfStmt | ForStmt | SwitchStmt
                | ReturnStmt | BreakStmt | ContinueStmt | DeferStmt | AssertStmt
                | ConstBlockStmt | DelStmt | ShortAssignStmt | MultiAssignStmt | ExprStmt .

VarDeclStmt     = "var" DeclNameList ( Type [ "=" ExprList ] | "=" ExprList ) .
ShortAssignStmt = DeclNameList ":=" ExprList .
MultiAssignStmt = ExprList "=" ExprList .
IfStmt          = "if" Expr Block [ "else" ( IfStmt | Block ) ] .
ForStmt         = "for" ( Block | Expr Block | ForClause Block | ForInClause Block ) .
ForClause       = [ ForInit ] ";" [ Expr ] ";" [ Expr ] .
ForInit         = VarDeclStmt | ShortAssignStmt | Expr .
ForInClause     = ForInValueBinding "in" Expr
                | ForInKeyBinding "," ForInValueBinding "in" Expr .
ForInKeyBinding = [ "&" ] Ident .
ForInValueBinding = "_" | [ "&" ] Ident .
SwitchStmt      = "switch" [ Expr ] "{" { CaseClause } [ DefaultClause ] "}" .
CaseClause      = "case" CasePattern { "," CasePattern } Block .
CasePattern     = Expr [ "as" Ident ] .
DefaultClause   = "default" Block .
ReturnStmt      = "return" [ ExprList ] .
BreakStmt       = "break" .
ContinueStmt    = "continue" .
DeferStmt       = "defer" ( Block | Stmt ) .
AssertStmt      = "assert" Expr [ "," Expr { "," Expr } ] .
ConstBlockStmt  = "const" Block .
DelStmt         = "del" Expr { "," Expr } [ "in" Expr ] .
ExprStmt        = Expr .

Expr            = AssignExpr .
AssignExpr      = LogicalOrExpr [ AssignOp AssignExpr ] .
AssignOp        = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" .
LogicalOrExpr   = LogicalAndExpr { "||" LogicalAndExpr } .
LogicalAndExpr  = BitOrExpr { "&&" BitOrExpr } .
BitOrExpr       = BitXorExpr { "|" BitXorExpr } .
BitXorExpr      = BitAndExpr { "^" BitAndExpr } .
BitAndExpr      = EqualityExpr { "&" EqualityExpr } .
EqualityExpr    = RelExpr { ( "==" | "!=" ) RelExpr } .
RelExpr         = ShiftExpr { ( "<" | ">" | "<=" | ">=" ) ShiftExpr } .
ShiftExpr       = AddExpr { ( "<<" | ">>" ) AddExpr } .
AddExpr         = MulExpr { ( "+" | "-" ) MulExpr } .
MulExpr         = UnaryExpr { ( "*" | "/" | "%" ) UnaryExpr } .
UnaryExpr       = ( ( "+" | "-" | "!" | "*" | "&" ) UnaryExpr ) | PostfixExpr .
PostfixExpr     = PrimaryExpr { PostfixSuffix } .
PostfixSuffix   = CallSuffix | IndexSuffix | SelectorSuffix | CastSuffix | UnwrapSuffix .
CallSuffix      = "(" [ CallArgList ] ")" .
CallArgList     = CallArg { "," CallArg } .
CallArg         = [ Ident ":" ] Expr [ "..." ] .
ExprList        = Expr { "," Expr } .
IndexSuffix     = "[" Expr "]" | "[" [ Expr ] ":" [ Expr ] "]" .
SelectorSuffix  = "." Ident .
CastSuffix      = "as" Type .
UnwrapSuffix    = "!" .

PrimaryExpr     = Ident | IntLit | FloatLit | StringLit | RuneLit | BoolLit | "null"
                | TypeValueExpr
                | CompoundLit | NewExpr | "sizeof" "(" ( Type | Expr ) ")" | "(" Expr ")"
                | TupleExpr .
TypeValueExpr   = "type" Type .
TupleExpr       = "(" Expr "," Expr { "," Expr } ")" .
NewExpr         = "new" ( "[" Type Expr "]" | Type [ "{" [ FieldInitList ] "}" ] ) [ "in" Expr ] .
CompoundLit     = [ TypeName ] "{" [ FieldInitList ] "}" .
FieldInitList   = FieldInit { "," FieldInit } [ "," ] .
FieldInit       = Ident { "." Ident } ":" Expr .

3.3 Parsing disambiguation rules

Canonical examples:

fn f() {
    { x = 1 }      // block with one statement: assignment to x
}
fn f() {
    ({ x: 1 })    // expression statement: compound literal value discarded
}
fn f() {
    defer { x = 1 }    // deferred block
}

4. Declarations, Scope, and Binding

4.1 Function declarations and overloading

4.2 Struct composition and enum member scope

4.3 Namespace model

5. Type System

5.1 Built-in and constructed types

5.1.1 Textual types (str and rune)

5.2 Mutability model

5.3 Optional types

5.4 Assignability and coercion

5.5 Inference and zero values

6. Expressions and Operators

6.1 Comparable and ordered types

6.2 Selector call sugar and overload resolution

6.3 Indexing and slicing

6.3.1 Type values in expression context

6.4 Compound literals

6.5 Array literals

7. Statements and Control Flow

8. Contexts and Capabilities

9. Built-in Forms and Functions

9.1 len(x)

9.2 cstr(s)

9.3 copy(dst, src)

9.4 new

9.5 concat(a, b) and del

9.6 panic(msg)

9.7 sizeof

9.8 print(msg)

9.9 format(format, args...) and format_str(format, args...)

9.10 typeof(x)

9.11 kind(...) and base(...)

9.12 compiler.error* / compiler.warn*

10. Variable-Size Structs (VSS)

11. Packages, Imports, and Exports

11.1 Package model

11.2 Imports

11.3 Exports and API closure

12. Entrypoint and Program Rules

13. Diagnostics Contract

14. Reference-hop Profile (Implementation-Defined)

This section is non-core and documents current reference behavior.

15. Evolution Policy

16. Draft (Non-Normative)