parser-lr 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/parser-lr.js +110 -823
- package/bin/parser-lr.js.map +1 -1
- package/dist/lib/grammar/grammar-from-cst.js +4 -1
- package/dist/lib/grammar/grammar-from-cst.js.map +1 -1
- package/dist/lib/grammar/grammar.json +86 -820
- package/dist/lib/grammar/table-validator.d.ts.map +1 -1
- package/dist/lib/grammar/table-validator.js +19 -1
- package/dist/lib/grammar/table-validator.js.map +1 -1
- package/docs/The Ferrite Programming Language.md +568 -0
- package/docs/grammar.md +55 -3
- package/grammars/6502.grammar +2 -2
- package/grammars/ferrite.grammar +107 -97
- package/grammars/grammar.grammar +19 -140
- package/grammars/lisp.grammar +3 -2
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table-validator.d.ts","sourceRoot":"","sources":["../../../src/lib/grammar/table-validator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAO5C;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG,OAAO,GAAG,SAAS,CAAC;AAE1D;;GAEG;AACH,MAAM,WAAW,oBAAoB;IAEjC,QAAQ,CAAC,QAAQ,EAAE,uBAAuB,CAAC;IAC3C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO,GAAG,oBAAoB,EAAE,CAiC7E;
|
|
1
|
+
{"version":3,"file":"table-validator.d.ts","sourceRoot":"","sources":["../../../src/lib/grammar/table-validator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAO5C;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG,OAAO,GAAG,SAAS,CAAC;AAE1D;;GAEG;AACH,MAAM,WAAW,oBAAoB;IAEjC,QAAQ,CAAC,QAAQ,EAAE,uBAAuB,CAAC;IAC3C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO,GAAG,oBAAoB,EAAE,CAiC7E;AAsXD;;;;GAIG;AACH,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,SAAS,oBAAoB,EAAE,GAAG,MAAM,EAAE,CAM7F"}
|
|
@@ -75,13 +75,31 @@ function reportDuplicateNames(names, section, kind, issues) {
|
|
|
75
75
|
* @param issues - Issue list to append to.
|
|
76
76
|
*/
|
|
77
77
|
function validateTransformProductionExists(rule, bnfProductionNames, issues) {
|
|
78
|
-
|
|
78
|
+
// Accept direct production names.
|
|
79
|
+
if (bnfProductionNames.has(rule.production)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// Match authored `$repeat_0` aliases to globally numbered repeats.
|
|
83
|
+
const repeatPrefix = syntheticRepeatPrefix(rule.production);
|
|
84
|
+
const matchesRepeat = repeatPrefix !== null
|
|
85
|
+
&& [...bnfProductionNames].some((productionName) => syntheticRepeatPrefix(productionName) === repeatPrefix);
|
|
86
|
+
if (!matchesRepeat) {
|
|
79
87
|
issues.push({
|
|
80
88
|
severity: 'error',
|
|
81
89
|
message: `transform rule for unknown production ${JSON.stringify(rule.production)}`,
|
|
82
90
|
});
|
|
83
91
|
}
|
|
84
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Returns the stable prefix of a numbered synthetic repeat production.
|
|
95
|
+
*
|
|
96
|
+
* @param name - Production or transform-rule name.
|
|
97
|
+
* @returns Prefix ending in `$repeat`, or null for a regular production.
|
|
98
|
+
*/
|
|
99
|
+
function syntheticRepeatPrefix(name) {
|
|
100
|
+
const match = /^(.+\$repeat)_\d+$/.exec(name);
|
|
101
|
+
return match?.[1] ?? null;
|
|
102
|
+
}
|
|
85
103
|
/**
|
|
86
104
|
* Validates one transform expression against the grammar AST schema.
|
|
87
105
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table-validator.js","sourceRoot":"","sources":["../../../src/lib/grammar/table-validator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AAkBnE;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAgB;IAEjD,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,MAAM,GAAG,GAAG,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;IACjD,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;IAEzF,kCAAkC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAEpD,IAAI,OAAO,CAAC,eAAe,KAAK,IAAI,EACpC,CAAC;QACG,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,eAAe,CAAC,KAAK,EAChD,CAAC;YACG,iCAAiC,CAAC,IAAI,EAAE,kBAAkB,EAAE,MAAM,CAAC,CAAC;YAEpE,KAAK,MAAM,WAAW,IAAI,IAAI,CAAC,YAAY,EAC3C,CAAC;gBACG,2BAA2B,CACvB,OAAO,EACP,WAAW,CAAC,UAAU,EACtB,MAAM,CACT,CAAC;gBACF,2BAA2B,CACvB,OAAO,EACP,IAAI,EACJ,WAAW,CAAC,UAAU,EACtB,GAAG,CAAC,WAAW,EACf,MAAM,CACT,CAAC;YACN,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAS,kCAAkC,CACvC,OAAgB,EAChB,MAA8B;IAG9B,yDAAyD;IACzD,oBAAoB,CAChB,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC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;AACH,SAAS,iCAAiC,CACtC,IAAmB,EACnB,kBAAuC,EACvC,MAA8B;IAG9B,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"table-validator.js","sourceRoot":"","sources":["../../../src/lib/grammar/table-validator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AAkBnE;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAgB;IAEjD,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,MAAM,GAAG,GAAG,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;IACjD,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;IAEzF,kCAAkC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAEpD,IAAI,OAAO,CAAC,eAAe,KAAK,IAAI,EACpC,CAAC;QACG,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,eAAe,CAAC,KAAK,EAChD,CAAC;YACG,iCAAiC,CAAC,IAAI,EAAE,kBAAkB,EAAE,MAAM,CAAC,CAAC;YAEpE,KAAK,MAAM,WAAW,IAAI,IAAI,CAAC,YAAY,EAC3C,CAAC;gBACG,2BAA2B,CACvB,OAAO,EACP,WAAW,CAAC,UAAU,EACtB,MAAM,CACT,CAAC;gBACF,2BAA2B,CACvB,OAAO,EACP,IAAI,EACJ,WAAW,CAAC,UAAU,EACtB,GAAG,CAAC,WAAW,EACf,MAAM,CACT,CAAC;YACN,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAS,kCAAkC,CACvC,OAAgB,EAChB,MAA8B;IAG9B,yDAAyD;IACzD,oBAAoB,CAChB,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,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AAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC7D,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACH,SAAS,4BAA4B,CACjC,cAAsB,EACtB,cAAwC;IAGxC,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE,CACtC,UAAU,CAAC,IAAI,KAAK,cAAc;WAC/B,UAAU,CAAC,GAAG,CAAC,MAAM,KAAK,CAAC;WAC3B,gBAAgB,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAChD,CAAC;AAED;;;;GAIG;AACH,SAAS,gBAAgB,CAAC,MAA6B;IAEnD,OAAO,MAAM,EAAE,IAAI,KAAK,UAAU,IAAI,MAAM,EAAE,IAAI,KAAK,OAAO,CAAC;AACnE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,2BAA2B,CAAC,MAAuC;IAE/E,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACxB,KAAK,CAAC,QAAQ,KAAK,OAAO;QACtB,CAAC,CAAC,UAAU,KAAK,CAAC,OAAO,EAAE;QAC3B,CAAC,CAAC,YAAY,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
# The Ferrite Programming Language
|
|
2
|
+
|
|
3
|
+
Ferrite is a statically typed, C-like language with C#-style structs, file-scoped namespaces, explicit pointer semantics, and no preprocessor. A source file is a single compilation unit: an optional namespace header, `using` directives, and top-level declarations compose the program.
|
|
4
|
+
|
|
5
|
+
This guide describes the language as defined by [`grammars/ferrite.grammar`](../grammars/ferrite.grammar). Ferrite is included with parser-lr as a reference grammar; parsing and AST generation are supported today. Code generation and runtime semantics are outside the scope of this document unless noted.
|
|
6
|
+
|
|
7
|
+
## Quick start
|
|
8
|
+
|
|
9
|
+
A minimal Ferrite program declares a `main` function and returns an integer:
|
|
10
|
+
|
|
11
|
+
```ferrite
|
|
12
|
+
public Int32 main()
|
|
13
|
+
{
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
A more typical program places types in a file-scoped namespace and imports other namespaces with `using`:
|
|
19
|
+
|
|
20
|
+
```ferrite
|
|
21
|
+
namespace Ferrite.Math;
|
|
22
|
+
|
|
23
|
+
using Ferrite.Core;
|
|
24
|
+
|
|
25
|
+
public struct Vec2
|
|
26
|
+
{
|
|
27
|
+
Float32 x;
|
|
28
|
+
Float32 y;
|
|
29
|
+
Float32 dot(Vec2* other)
|
|
30
|
+
{
|
|
31
|
+
return x * other->x + y * other->y;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
public Int32 main()
|
|
36
|
+
{
|
|
37
|
+
Vec2 a = { 1.0, 2.0 };
|
|
38
|
+
Vec2* p = &a;
|
|
39
|
+
return (Int32)p->dot(&a);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Comments use C-style syntax:
|
|
44
|
+
|
|
45
|
+
```ferrite
|
|
46
|
+
// line comment
|
|
47
|
+
|
|
48
|
+
/* block
|
|
49
|
+
comment */
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Naming conventions
|
|
53
|
+
|
|
54
|
+
These casing rules are **conventions**, not enforced by the parser. Prefer them in Ferrite source so programs stay consistent:
|
|
55
|
+
|
|
56
|
+
| Kind | Convention | Examples |
|
|
57
|
+
|------|------------|----------|
|
|
58
|
+
| Built-in types | PascalCase keywords | `Int32`, `Float64`, `Bool`, `Void` |
|
|
59
|
+
| Struct and other type names | PascalCase | `Vec2`, `Node`, `HttpServer` |
|
|
60
|
+
| Functions and methods | camelCase | `main`, `dot`, `isSet` |
|
|
61
|
+
| Variables, fields, and parameters | camelCase | `count`, `other`, `sum` |
|
|
62
|
+
| Constants | camelCase (same as variables) | `maxRetries` — no `UPPER_CASE` style |
|
|
63
|
+
|
|
64
|
+
Identifiers may contain letters, digits, and underscores; the grammar does not reject names that break these conventions.
|
|
65
|
+
|
|
66
|
+
## Program structure
|
|
67
|
+
|
|
68
|
+
A Ferrite file has a fixed order. Nothing may appear before the optional namespace, and every `using` directive must appear before any declaration:
|
|
69
|
+
|
|
70
|
+
1. Optional file-scoped namespace: `namespace Qualified.Name;`
|
|
71
|
+
2. Zero or more using directives: `using Qualified.Name;`
|
|
72
|
+
3. Zero or more top-level declarations (structs and functions)
|
|
73
|
+
|
|
74
|
+
| Form | Example |
|
|
75
|
+
|------|---------|
|
|
76
|
+
| File-scoped namespace | `namespace Ferrite.Math;` |
|
|
77
|
+
| Using directive | `using Ferrite.Core;` |
|
|
78
|
+
| Struct | `public struct Point { … };` |
|
|
79
|
+
| Function | `public Int32 add(Int32 a, Int32 b) { … }` or `private Int32 add(Int32 a, Int32 b);` |
|
|
80
|
+
|
|
81
|
+
There is no `#include` or macro layer. Share code across files by using namespaces and `using` directives. Block namespaces (`namespace Name { … }`) and nested namespace bodies are not part of the language.
|
|
82
|
+
|
|
83
|
+
### File-scoped namespace
|
|
84
|
+
|
|
85
|
+
At most one namespace declaration may appear, and only as the first construct in the file:
|
|
86
|
+
|
|
87
|
+
```ferrite
|
|
88
|
+
namespace Ferrite.Math;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Qualified names use dot notation: `Ferrite.Math.Vec2`.
|
|
92
|
+
|
|
93
|
+
Omitting the namespace leaves declarations in the global namespace for that file.
|
|
94
|
+
|
|
95
|
+
### Using directives
|
|
96
|
+
|
|
97
|
+
All `using` directives must follow the namespace (if present) and precede every struct or function declaration:
|
|
98
|
+
|
|
99
|
+
```ferrite
|
|
100
|
+
namespace Ferrite.App;
|
|
101
|
+
|
|
102
|
+
using Ferrite.Math;
|
|
103
|
+
using Ferrite.Math.Vec;
|
|
104
|
+
|
|
105
|
+
public Int32 main()
|
|
106
|
+
{
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
A file may begin with usings and no namespace:
|
|
112
|
+
|
|
113
|
+
```ferrite
|
|
114
|
+
using Ferrite.Math;
|
|
115
|
+
|
|
116
|
+
public Int32 main()
|
|
117
|
+
{
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Access modifiers
|
|
123
|
+
|
|
124
|
+
Every **top-level** struct, function definition, and function prototype requires `public` or `private`:
|
|
125
|
+
|
|
126
|
+
```ferrite
|
|
127
|
+
public struct Point
|
|
128
|
+
{
|
|
129
|
+
Int32 x;
|
|
130
|
+
Int32 y;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
public Int32 twice(Int32 x)
|
|
134
|
+
{
|
|
135
|
+
return x + x;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private Void helper();
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Access modifiers are **not** required on:
|
|
142
|
+
|
|
143
|
+
- local variables inside functions
|
|
144
|
+
- parameters
|
|
145
|
+
- struct fields
|
|
146
|
+
- struct methods
|
|
147
|
+
|
|
148
|
+
```ferrite
|
|
149
|
+
public Int32 sum(Int32 a, Int32 b)
|
|
150
|
+
{
|
|
151
|
+
Int32 total = a + b;
|
|
152
|
+
return total;
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Types
|
|
157
|
+
|
|
158
|
+
### Primitive types
|
|
159
|
+
|
|
160
|
+
| Type | Description |
|
|
161
|
+
|------|-------------|
|
|
162
|
+
| `Void` | No value; used for functions with no return value |
|
|
163
|
+
| `Bool` | Boolean |
|
|
164
|
+
| `Int8`, `Int16`, `Int32`, `Int64` | Signed integers |
|
|
165
|
+
| `UInt8`, `UInt16`, `UInt32`, `UInt64` | Unsigned integers |
|
|
166
|
+
| `Float32`, `Float64` | Floating point |
|
|
167
|
+
| `Char` | Character |
|
|
168
|
+
|
|
169
|
+
### Named types
|
|
170
|
+
|
|
171
|
+
Struct names are ordinary type names. After a struct is declared, its identifier can appear wherever a type is expected:
|
|
172
|
+
|
|
173
|
+
```ferrite
|
|
174
|
+
public struct Point
|
|
175
|
+
{
|
|
176
|
+
Int32 x;
|
|
177
|
+
Int32 y;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
public Point* origin(Point* cursor)
|
|
181
|
+
{
|
|
182
|
+
return cursor;
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Pointers
|
|
187
|
+
|
|
188
|
+
Append one or more `*` after a type to form a pointer type:
|
|
189
|
+
|
|
190
|
+
```ferrite
|
|
191
|
+
Int32 value;
|
|
192
|
+
Int32* p;
|
|
193
|
+
Int32** pp;
|
|
194
|
+
Vec2* origin;
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
The address-of operator `&` takes a pointer to an lvalue; unary `*` dereferences a pointer:
|
|
198
|
+
|
|
199
|
+
```ferrite
|
|
200
|
+
Int32 x = 10;
|
|
201
|
+
Int32* p = &x;
|
|
202
|
+
Int32 y = *p;
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Member access on pointers uses the arrow operator `->`:
|
|
206
|
+
|
|
207
|
+
```ferrite
|
|
208
|
+
Vec2* p = &a;
|
|
209
|
+
Float32 x = p->x;
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Arrays
|
|
213
|
+
|
|
214
|
+
Fixed-size and unsized array types are written with brackets after the element type:
|
|
215
|
+
|
|
216
|
+
```ferrite
|
|
217
|
+
Int32 buffer[10];
|
|
218
|
+
Int32[] unsized;
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Pointer suffixes apply after the array form:
|
|
222
|
+
|
|
223
|
+
```ferrite
|
|
224
|
+
Int32*[4] row;
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Declarations
|
|
228
|
+
|
|
229
|
+
### Functions
|
|
230
|
+
|
|
231
|
+
Functions have an access modifier, return type, name, parameter list, and either a body or a terminating semicolon for a prototype:
|
|
232
|
+
|
|
233
|
+
```ferrite
|
|
234
|
+
public Int32 abs(Int32 x); // prototype
|
|
235
|
+
|
|
236
|
+
public Int32 twice(Int32 x) // definition
|
|
237
|
+
{
|
|
238
|
+
return x + x;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private Void noop() // no parameters
|
|
242
|
+
{
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Parameters are declared as `type name`, comma-separated, without access modifiers:
|
|
248
|
+
|
|
249
|
+
```ferrite
|
|
250
|
+
public Bool both(Bool a, Bool b)
|
|
251
|
+
{
|
|
252
|
+
return a && b;
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Structs
|
|
257
|
+
|
|
258
|
+
A struct declares fields and methods inside braces, then ends with a semicolon. The struct itself needs an access modifier; members do not:
|
|
259
|
+
|
|
260
|
+
```ferrite
|
|
261
|
+
public struct Counter
|
|
262
|
+
{
|
|
263
|
+
Int32 n;
|
|
264
|
+
|
|
265
|
+
Int32 next()
|
|
266
|
+
{
|
|
267
|
+
return n + 1;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
Int32 add(Int32 delta)
|
|
271
|
+
{
|
|
272
|
+
return n + delta;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Fields are type-name pairs terminated by semicolons. Methods use the same body syntax as free functions but are defined inside the struct body without `public` or `private`.
|
|
278
|
+
|
|
279
|
+
An empty struct is valid:
|
|
280
|
+
|
|
281
|
+
```ferrite
|
|
282
|
+
public struct Empty
|
|
283
|
+
{
|
|
284
|
+
};
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Local variables
|
|
288
|
+
|
|
289
|
+
Inside a block, declare variables with a type, name, and optional initializer. Locals do not take access modifiers:
|
|
290
|
+
|
|
291
|
+
```ferrite
|
|
292
|
+
Int32 sum = 0;
|
|
293
|
+
Vec2 v = { 1, 2 };
|
|
294
|
+
Int32* p;
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Brace initialization supplies a comma-separated list of expressions:
|
|
298
|
+
|
|
299
|
+
```ferrite
|
|
300
|
+
Vec2 a = { 1.0, 2.0 };
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Statements
|
|
304
|
+
|
|
305
|
+
Statements appear in function bodies, struct methods, and blocks.
|
|
306
|
+
|
|
307
|
+
| Statement | Form |
|
|
308
|
+
|-----------|------|
|
|
309
|
+
| Block | `{ … }` |
|
|
310
|
+
| Variable declaration | `type name [= init];` |
|
|
311
|
+
| Expression | `expr;` |
|
|
312
|
+
| `if` | `if (cond) stmt [else stmt]` |
|
|
313
|
+
| `while` | `while (cond) stmt` |
|
|
314
|
+
| `for` | `for ([init;] [test;] [step]) stmt` |
|
|
315
|
+
| `return` | `return [expr];` |
|
|
316
|
+
| `break` | `break;` |
|
|
317
|
+
| `continue` | `continue;` |
|
|
318
|
+
|
|
319
|
+
### `if` and `else`
|
|
320
|
+
|
|
321
|
+
```ferrite
|
|
322
|
+
if (flag)
|
|
323
|
+
{
|
|
324
|
+
x = 1;
|
|
325
|
+
}
|
|
326
|
+
else
|
|
327
|
+
{
|
|
328
|
+
x = 2;
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
The `else` branch is optional. Each branch is a single statement, often a block.
|
|
333
|
+
|
|
334
|
+
### `while`
|
|
335
|
+
|
|
336
|
+
```ferrite
|
|
337
|
+
while (running)
|
|
338
|
+
{
|
|
339
|
+
step = step + 1;
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### `for`
|
|
344
|
+
|
|
345
|
+
Ferrite uses C-style `for` loops. The init, test, and step clauses are each optional:
|
|
346
|
+
|
|
347
|
+
```ferrite
|
|
348
|
+
for (Int32 i = 0; i < 10; i = i + 1)
|
|
349
|
+
{
|
|
350
|
+
sum = sum + i;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
for (; i < 10; i = i + 1)
|
|
354
|
+
{
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
for (Int32 i = 0;; i = i + 1)
|
|
358
|
+
{
|
|
359
|
+
if (i >= 10)
|
|
360
|
+
{
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
The init clause may be a variable declaration or an expression.
|
|
367
|
+
|
|
368
|
+
### `return`, `break`, and `continue`
|
|
369
|
+
|
|
370
|
+
```ferrite
|
|
371
|
+
public Int32 zero()
|
|
372
|
+
{
|
|
373
|
+
return 0;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
public Void finish()
|
|
377
|
+
{
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
while (true)
|
|
382
|
+
{
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
while (true)
|
|
387
|
+
{
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
## Expressions
|
|
393
|
+
|
|
394
|
+
### Literals
|
|
395
|
+
|
|
396
|
+
| Kind | Examples |
|
|
397
|
+
|------|----------|
|
|
398
|
+
| Integer | `42`, `0xFF`, `0xDeadBeef`, `0b1010` |
|
|
399
|
+
| Floating | `3.14`, `1.5e10`, `3.14e-2` |
|
|
400
|
+
| Character | `'x'`, `'\n'` |
|
|
401
|
+
| String | `"hello"`, `"line\n"` |
|
|
402
|
+
| Boolean | `true`, `false` |
|
|
403
|
+
| Null pointer | `null` |
|
|
404
|
+
|
|
405
|
+
Integer literals accept hexadecimal (`0x` / `0X`) and binary (`0b` / `0B`) prefixes. Float literals may use a decimal exponent (`e` / `E`).
|
|
406
|
+
|
|
407
|
+
### Primary expressions
|
|
408
|
+
|
|
409
|
+
Identifiers, literals, parenthesized expressions, and `new`:
|
|
410
|
+
|
|
411
|
+
```ferrite
|
|
412
|
+
x
|
|
413
|
+
0xFF
|
|
414
|
+
(obj.field)
|
|
415
|
+
new Node()
|
|
416
|
+
new Node(1, 2)
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### Postfix operations
|
|
420
|
+
|
|
421
|
+
Postfix operators bind to the expression on their left:
|
|
422
|
+
|
|
423
|
+
| Operator | Meaning |
|
|
424
|
+
|----------|---------|
|
|
425
|
+
| `[expr]` | Index |
|
|
426
|
+
| `( )`, `(args…)` | Call |
|
|
427
|
+
| `.name` | Member access |
|
|
428
|
+
| `->name` | Pointer member access |
|
|
429
|
+
| `++`, `--` | Postfix increment / decrement |
|
|
430
|
+
|
|
431
|
+
```ferrite
|
|
432
|
+
arr[i]
|
|
433
|
+
f(a, b)
|
|
434
|
+
main()
|
|
435
|
+
obj.field
|
|
436
|
+
ptr->field
|
|
437
|
+
i++
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### Unary operators
|
|
441
|
+
|
|
442
|
+
| Operator | Meaning |
|
|
443
|
+
|----------|---------|
|
|
444
|
+
| `(type) expr` | Cast |
|
|
445
|
+
| `++`, `--` | Prefix increment / decrement |
|
|
446
|
+
| `&` | Address of |
|
|
447
|
+
| `*` | Dereference |
|
|
448
|
+
| `!` | Logical not |
|
|
449
|
+
| `~` | Bitwise not |
|
|
450
|
+
| `-`, `+` | Unary minus / plus |
|
|
451
|
+
|
|
452
|
+
```ferrite
|
|
453
|
+
(Int32)value
|
|
454
|
+
++i
|
|
455
|
+
&x
|
|
456
|
+
*p
|
|
457
|
+
!flag
|
|
458
|
+
~mask
|
|
459
|
+
-n
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### Binary operators and precedence
|
|
463
|
+
|
|
464
|
+
From highest to lowest binding strength:
|
|
465
|
+
|
|
466
|
+
1. Postfix: `[]`, `()`, `.`, `->`, `++`, `--`
|
|
467
|
+
2. Unary: cast, prefix `++`/`--`, `&`, `*`, `!`, `~`, unary `-`/`+`
|
|
468
|
+
3. Multiplicative: `*`, `/`, `%`
|
|
469
|
+
4. Additive: `+`, `-`
|
|
470
|
+
5. Shift: `<<`, `>>`
|
|
471
|
+
6. Relational: `<`, `>`, `<=`, `>=`
|
|
472
|
+
7. Equality: `==`, `!=`
|
|
473
|
+
8. Bitwise AND: `&`
|
|
474
|
+
9. Bitwise XOR: `^`
|
|
475
|
+
10. Bitwise OR: `|`
|
|
476
|
+
11. Logical AND: `&&`
|
|
477
|
+
12. Logical OR: `||`
|
|
478
|
+
13. Assignment: `=`, `+=`, `-=`, `*=`, `/=`, `&=`, `|=`, `^=`
|
|
479
|
+
|
|
480
|
+
Assignment is right-associative. All other binary operators listed here are left-associative.
|
|
481
|
+
|
|
482
|
+
```ferrite
|
|
483
|
+
a + b * c
|
|
484
|
+
a + b * c < d && e || f
|
|
485
|
+
x = y = 0
|
|
486
|
+
x += 1
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
## Memory and construction
|
|
490
|
+
|
|
491
|
+
`new` allocates or constructs a value of the given type:
|
|
492
|
+
|
|
493
|
+
```ferrite
|
|
494
|
+
Node* n = new Node();
|
|
495
|
+
Node* m = new Node(1, 2);
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
The result type is typically stored in a pointer. `null` represents a null pointer constant:
|
|
499
|
+
|
|
500
|
+
```ferrite
|
|
501
|
+
Int32* p = null;
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
Exact allocation semantics (stack vs heap, constructor dispatch) depend on a future implementation. The syntax above is what the Ferrite grammar accepts.
|
|
505
|
+
|
|
506
|
+
## Worked example
|
|
507
|
+
|
|
508
|
+
The following program combines a file-scoped namespace, ordered usings, visibility, structs with methods, pointers, and brace initialization:
|
|
509
|
+
|
|
510
|
+
```ferrite
|
|
511
|
+
namespace Ferrite.Math;
|
|
512
|
+
|
|
513
|
+
using Ferrite.Core;
|
|
514
|
+
|
|
515
|
+
public struct Vec2
|
|
516
|
+
{
|
|
517
|
+
Float32 x;
|
|
518
|
+
Float32 y;
|
|
519
|
+
|
|
520
|
+
Float32 dot(Vec2* other)
|
|
521
|
+
{
|
|
522
|
+
return x * other->x + y * other->y;
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
public Int32 main()
|
|
527
|
+
{
|
|
528
|
+
Vec2 a = { 1.0, 2.0 };
|
|
529
|
+
Vec2* p = &a;
|
|
530
|
+
return (Int32)p->dot(&a);
|
|
531
|
+
}
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
Reading it top to bottom:
|
|
535
|
+
|
|
536
|
+
1. The file declares namespace `Ferrite.Math`.
|
|
537
|
+
2. `using Ferrite.Core;` imports another namespace before any declarations.
|
|
538
|
+
3. `Vec2` is a public struct with two fields and a method that reads another vector through a pointer.
|
|
539
|
+
4. `main` creates a vector, takes its address, and calls `dot` through a pointer, casting the result to `Int32`.
|
|
540
|
+
|
|
541
|
+
## Parsing Ferrite with parser-lr
|
|
542
|
+
|
|
543
|
+
Ferrite ships as a `.grammar` file in this repository. Generate a parse table, validate transforms, or parse source with the CLI:
|
|
544
|
+
|
|
545
|
+
```bash
|
|
546
|
+
parser-lr table generate -g grammars/ferrite.grammar -o ferrite.json
|
|
547
|
+
parser-lr table validate -g grammars/ferrite.grammar
|
|
548
|
+
parser-lr parse -i program.fe -g grammars/ferrite.grammar
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
Output is JSON containing an AST when parsing succeeds. See the [project README](../README.md) for install instructions and library usage.
|
|
552
|
+
|
|
553
|
+
Integration tests in [`src/lib/ferrite.test.ts`](../src/lib/ferrite.test.ts) exercise the grammar across types, control flow, operators, and full programs.
|
|
554
|
+
|
|
555
|
+
## Summary
|
|
556
|
+
|
|
557
|
+
| Topic | Ferrite approach |
|
|
558
|
+
|-------|------------------|
|
|
559
|
+
| Modules | Optional file-scoped `namespace Name;`, then `using` directives; no preprocessor |
|
|
560
|
+
| Visibility | Required `public` / `private` on top-level structs and functions |
|
|
561
|
+
| User-defined types | `struct` with fields and methods |
|
|
562
|
+
| Naming | PascalCase types, camelCase functions and variables (convention) |
|
|
563
|
+
| Pointers | Explicit `*`, `&`, `->`, and `null` |
|
|
564
|
+
| Control flow | `if`, `while`, C-style `for`, `break`, `continue` |
|
|
565
|
+
| Expressions | C-family operators and precedence |
|
|
566
|
+
| Initialization | Assignment and brace lists |
|
|
567
|
+
|
|
568
|
+
For grammar-file mechanics (tokens, AST shapes, transforms), see [`.grammar` file syntax](grammar.md).
|