|
| 1 | +---- |
| 2 | +Base Grammar |
| 3 | +---- |
| 4 | +This will be the grammar that we start out with and gradually add features to. We need a base level that works because it's hard to enumerate all features. |
| 5 | +Here are the features that will be included: |
| 6 | +- functions |
| 7 | + - functions are first-class |
| 8 | +- pointers (this will make certain things easier, later revisions will put restrictions on these) |
| 9 | +- integers (floating-point numbers will be added later) |
| 10 | + - all typical sizes (8,16,32,64) |
| 11 | +- arrays |
| 12 | +- base types (ADTs) |
| 13 | + - type expressions: | (union), & (intersection) |
| 14 | + - generics |
| 15 | + - product types (tuples, tagged tuples) |
| 16 | +- classes/structs (structs are raw and have different inheritance rules than classes) |
| 17 | + - constructors |
| 18 | + - methods |
| 19 | + - fields |
| 20 | + - inheritance |
| 21 | + - static functions |
| 22 | +- pure functions (functions that are just expressions) |
| 23 | + - function calls |
| 24 | + - operators (infix functions) |
| 25 | + - let-in expressions |
| 26 | + - where clauses |
| 27 | + - if-else expressions |
| 28 | + - tail recursion optimization |
| 29 | + - match expressions |
| 30 | + - FOR NOW: pure functions can call imperative functions, as long as they aren't void |
| 31 | +- imperative functions (functions that are sequences of instructions) |
| 32 | + - can do everything that pure functions can, plus... |
| 33 | + - blocks/statements |
| 34 | + - loops: for, foreach, while, do-while |
| 35 | + - return |
| 36 | + - throw, try-catch, try-catch-finally (EXCEPTIONS, only imperative functions can deal with exceptions for now) |
| 37 | +- quasi-pure functions (this is more complex but it is a central part of the language) |
| 38 | + - pure functions that look like imperative functions |
| 39 | + - assignments create new allocations, don't break old references |
| 40 | + - loops not allowed |
| 41 | + - any parameters modified will be implicitly returned, which leads to... |
| 42 | +- function contexts (again, complex but central) |
| 43 | + - implicit parameters for quasi-pure functions, pure and imperative can access them explicitly |
| 44 | + - examples: 'this' for classes/structs, 'global' for mutable state |
| 45 | + - functions using contexts must explicitly declare them (except for 'this' and 'global'), callers will implicitly pass them |
| 46 | + - modified contexts will be implicitly returned so the caller will hold a new reference to the modified context |
| 47 | + - unmodified contexts |
| 48 | +- 'this' |
| 49 | + - built-in context that makes it easier to mutate instances from methods in a pure way |
| 50 | + - always refers to an instance of the class containing the method |
| 51 | + - '<inst>.<blah>()' will pass 'inst' as 'this' into 'blah' |
| 52 | +- 'global' |
| 53 | + - built-in context that provides access to impure code/state from pure code |
| 54 | + - anytime 'global' is used to access impure code/state, it returns a modified instance of itself |
| 55 | + - 'global' is passed implicitly into 'main', so it can be accessed anywhere in the application |
| 56 | +- modules |
| 57 | + - modules' named exports can be imported individually |
| 58 | + - default export is used when only the module name is imported |
| 59 | + - modules can export named exports or default export |
| 60 | + - each imported module is evaluated according to node-esque rules |
| 61 | +- 'main' |
| 62 | + - for now, this is the top-level of all applications |
| 63 | + - the specified module passed into the command line must contain 'main' so that the program can start |
| 64 | +---- |
| 65 | + |
| 66 | +# Breaks are implicit in this grammar to improve clarity. |
| 67 | +# A break is a new line or a semicolon, so semicolons are only required when putting multiple incompatible constructions on the same line |
| 68 | + |
| 69 | +# A Program is a list of top-level components, which can be: |
| 70 | +# - functions |
| 71 | +# - types |
| 72 | +# - classes |
| 73 | +# - structs |
| 74 | +# - import declarations |
| 75 | +# - export declarations |
| 76 | +# A stipulation is that import declarations must come before any other declaration |
| 77 | +Program ::= ImportDeclaration* FreeDeclaration* |
| 78 | +FreeDeclaration ::= ComponentDeclaration |
| 79 | + | ExportDeclaration |
| 80 | + |
| 81 | +ExportDeclaration ::= EXPORT DEFAULT Expression -- default export |
| 82 | + | EXPORT IDENT EQUALS Expression -- named inline export |
| 83 | + | EXPORT IDENT -- named export of already declared name |
| 84 | + |
| 85 | +ComponentDeclaration ::= FunctionDeclaration |
| 86 | + | TypeDeclaration |
| 87 | + | ClassDeclaration |
| 88 | + | StructDeclaration |
| 89 | + |
| 90 | +# pure <returnType> <name>\<<typeParameters>\>(<parameters>) => <expression> |
| 91 | +# proc <returnType> <name>\<<typeParameters>\>(<parameters>) => <block> |
| 92 | +# func <returnType> <name>\<<typeParameters>\>[<contextParameters>](<parameters>) => <quasiBlock> |
| 93 | +FunctionDeclaration ::= PURE Type IDENT TypeParameterList? ParameterList FAT_ARROW Expression |
| 94 | + | PROC Type IDENT TypeParameterList? ParameterList FAT_ARROW Block |
| 95 | + | FUNC Type IDENT TypeParameterList? ContextParameterList? ParameterList FAT_ARROW QuasiBlock |
| 96 | + |
| 97 | +TypeParameterList ::= LT GenericParam (COMMA GenericParam)* GT |
| 98 | +ContextParameterList ::= LBRACK Param (COMMA Param)* RBRACK |
| 99 | +ParameterList ::= LPAREN (Param (COMMA Param)*)? RPAREN |
| 100 | + |
| 101 | +Param ::= Type IDENT |
| 102 | + |
| 103 | +GenericParam ::= IDENT |
| 104 | + | IDENT COLON Type |
| 105 | + |
| 106 | +# type <name> |
| 107 | +# type <name> = <typeExpression> |
| 108 | +# type <name> { <typeComponentDeclarations> } |
| 109 | +# type <name>(<types>) |
| 110 | +# type <name>(<type> <fieldName>...) |
| 111 | +TypeDeclaration ::= TYPE IDENT |
| 112 | + | TYPE IDENT EQUALS Type |
| 113 | + | TYPE IDENT LBRACE TypeComponentDeclaration+ RBRACE |
| 114 | + | TYPE IDENT LPAREN Type+ RPAREN |
| 115 | + | TYPE IDENT LPAREN (Type IDENT)+ RPAREN |
| 116 | + |
| 117 | +TypeComponentDeclaration ::= IDENT |
| 118 | + | IDENT EQUALS |
0 commit comments