nwnrs-nwscript 0.0.1

NWScript frontend, compiler, bytecode, and debug tooling for Neverwinter Nights
Documentation
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use serde::{Deserialize, Serialize};

use crate::source::Span;

/// One parsed `NWScript` translation unit.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Script {
    /// Top-level items in source order.
    pub items: Vec<TopLevelItem>,
}

/// One top-level `NWScript` item.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TopLevelItem {
    /// One `#include "..."` directive.
    Include(IncludeDirective),
    /// One global variable declaration statement.
    Global(Declaration),
    /// One function declaration or definition.
    Function(FunctionDecl),
    /// One user-defined `struct`.
    Struct(StructDecl),
}

/// One `#include "..."` directive.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IncludeDirective {
    /// Source span covering the directive.
    pub span: Span,
    /// Included script path payload.
    pub path: String,
}

/// One parsed function declaration or definition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FunctionDecl {
    /// Source span covering the whole declaration or definition.
    pub span:        Span,
    /// Function return type.
    pub return_type: TypeSpec,
    /// Function name.
    pub name:        String,
    /// Function parameters in source order.
    pub parameters:  Vec<Parameter>,
    /// Optional function body. `None` means this was only a declaration.
    pub body:        Option<BlockStmt>,
}

/// One user-defined structure declaration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StructDecl {
    /// Source span covering the whole declaration.
    pub span:   Span,
    /// Structure name.
    pub name:   String,
    /// Field declarations in source order.
    pub fields: Vec<StructFieldDecl>,
}

/// One structure field declaration statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StructFieldDecl {
    /// Source span covering the declaration.
    pub span:  Span,
    /// Field type.
    pub ty:    TypeSpec,
    /// Field names declared by this statement.
    pub names: Vec<NamedItem>,
}

/// One variable or field name plus span.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NamedItem {
    /// Source span covering the identifier.
    pub span: Span,
    /// Identifier text.
    pub name: String,
}

/// One parsed parameter declaration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Parameter {
    /// Source span covering the declaration.
    pub span:    Span,
    /// Parameter type.
    pub ty:      TypeSpec,
    /// Parameter name.
    pub name:    String,
    /// Optional default value expression.
    pub default: Option<Expr>,
}

/// One parsed type specifier.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TypeSpec {
    /// Source span covering the type specifier.
    pub span:     Span,
    /// Whether `const` was present.
    pub is_const: bool,
    /// Underlying type shape.
    pub kind:     TypeKind,
}

/// One `NWScript` type kind recognized syntactically by the parser.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TypeKind {
    /// `void`
    Void,
    /// `int`
    Int,
    /// `float`
    Float,
    /// `string`
    String,
    /// `object`
    Object,
    /// `vector`
    Vector,
    /// `struct name`
    Struct(String),
    /// One builtin engine structure such as `effect` or `json`.
    EngineStructure(String),
}

/// One variable declaration statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Declaration {
    /// Source span covering the whole declaration.
    pub span:        Span,
    /// Declared type.
    pub ty:          TypeSpec,
    /// Declared variables.
    pub declarators: Vec<VarDeclarator>,
}

/// One declared variable, optionally with an initializer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VarDeclarator {
    /// Source span covering the declarator.
    pub span:        Span,
    /// Variable name.
    pub name:        String,
    /// Optional initializer expression.
    pub initializer: Option<Expr>,
}

/// One compound block.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BlockStmt {
    /// Source span covering the block braces and contents.
    pub span:       Span,
    /// Statements inside the block.
    pub statements: Vec<Stmt>,
}

/// One statement in `NWScript` source.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Stmt {
    /// `{ ... }`
    Block(BlockStmt),
    /// One declaration statement.
    Declaration(Declaration),
    /// One expression statement.
    Expression(ExpressionStmt),
    /// `if (...) ... [else ...]`
    If(IfStmt),
    /// `switch (...) ...`
    Switch(SwitchStmt),
    /// `return [expr];`
    Return(ReturnStmt),
    /// `while (...) ...`
    While(WhileStmt),
    /// `do ... while (...);`
    DoWhile(DoWhileStmt),
    /// `for (...; ...; ...) ...`
    For(ForStmt),
    /// `case expr:`
    Case(CaseStmt),
    /// `default:`
    Default(DefaultStmt),
    /// `break;`
    Break(SimpleStmt),
    /// `continue;`
    Continue(SimpleStmt),
    /// `;`
    Empty(SimpleStmt),
}

/// One statement carrying only a span.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SimpleStmt {
    /// Source span covering the statement.
    pub span: Span,
}

/// One expression statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExpressionStmt {
    /// Source span covering the statement.
    pub span: Span,
    /// Parsed expression.
    pub expr: Expr,
}

/// One `if` statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IfStmt {
    /// Source span covering the whole statement.
    pub span:        Span,
    /// Condition expression.
    pub condition:   Expr,
    /// Statement executed when the condition is true.
    pub then_branch: Box<Stmt>,
    /// Optional `else` branch.
    pub else_branch: Option<Box<Stmt>>,
}

/// One `switch` statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SwitchStmt {
    /// Source span covering the whole statement.
    pub span:      Span,
    /// Condition expression.
    pub condition: Expr,
    /// Switch body.
    pub body:      Box<Stmt>,
}

/// One `return` statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReturnStmt {
    /// Source span covering the statement.
    pub span:  Span,
    /// Optional returned value.
    pub value: Option<Expr>,
}

/// One `while` statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WhileStmt {
    /// Source span covering the whole statement.
    pub span:      Span,
    /// Loop condition.
    pub condition: Expr,
    /// Loop body.
    pub body:      Box<Stmt>,
}

/// One `do ... while` statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DoWhileStmt {
    /// Source span covering the whole statement.
    pub span:      Span,
    /// Loop body.
    pub body:      Box<Stmt>,
    /// Loop condition.
    pub condition: Expr,
}

/// One `for` statement.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ForStmt {
    /// Source span covering the whole statement.
    pub span:        Span,
    /// Optional initializer expression.
    pub initializer: Option<Expr>,
    /// Optional loop condition.
    pub condition:   Option<Expr>,
    /// Optional update expression.
    pub update:      Option<Expr>,
    /// Loop body.
    pub body:        Box<Stmt>,
}

/// One `case` label.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CaseStmt {
    /// Source span covering the label.
    pub span:  Span,
    /// Case condition expression.
    pub value: Expr,
}

/// One `default` label.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DefaultStmt {
    /// Source span covering the label.
    pub span: Span,
}

/// One expression in `NWScript` source.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Expr {
    /// Source span covering the whole expression.
    pub span: Span,
    /// Expression shape.
    pub kind: ExprKind,
}

/// One expression shape.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExprKind {
    /// One literal constant.
    Literal(Literal),
    /// One variable or named constant reference.
    Identifier(String),
    /// One function call or action invocation.
    Call {
        /// Called expression.
        callee:    Box<Expr>,
        /// Call arguments in source order.
        arguments: Vec<Expr>,
    },
    /// One structure field access.
    FieldAccess {
        /// Expression on the left-hand side of `.`.
        base:  Box<Expr>,
        /// Field name on the right-hand side of `.`.
        field: String,
    },
    /// One unary or postfix operator.
    Unary {
        /// Applied operator.
        op:   UnaryOp,
        /// Operand expression.
        expr: Box<Expr>,
    },
    /// One binary operator.
    Binary {
        /// Applied operator.
        op:    BinaryOp,
        /// Left-hand operand.
        left:  Box<Expr>,
        /// Right-hand operand.
        right: Box<Expr>,
    },
    /// One conditional expression.
    Conditional {
        /// Condition before `?`.
        condition:  Box<Expr>,
        /// Expression between `?` and `:`.
        when_true:  Box<Expr>,
        /// Expression after `:`.
        when_false: Box<Expr>,
    },
    /// One assignment expression.
    Assignment {
        /// Applied assignment operator.
        op:    AssignmentOp,
        /// Assigned lvalue expression.
        left:  Box<Expr>,
        /// Right-hand expression.
        right: Box<Expr>,
    },
}

/// One unary operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnaryOp {
    /// Prefix `-`
    Negate,
    /// Prefix `~`
    OnesComplement,
    /// Prefix `!`
    BooleanNot,
    /// Prefix `++`
    PreIncrement,
    /// Prefix `--`
    PreDecrement,
    /// Postfix `++`
    PostIncrement,
    /// Postfix `--`
    PostDecrement,
}

/// One binary operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryOp {
    /// `*`
    Multiply,
    /// `/`
    Divide,
    /// `%`
    Modulus,
    /// `+`
    Add,
    /// `-`
    Subtract,
    /// `<<`
    ShiftLeft,
    /// `>>`
    ShiftRight,
    /// `>>>`
    UnsignedShiftRight,
    /// `>=`
    GreaterEqual,
    /// `>`
    GreaterThan,
    /// `<`
    LessThan,
    /// `<=`
    LessEqual,
    /// `!=`
    NotEqual,
    /// `==`
    EqualEqual,
    /// `&`
    BooleanAnd,
    /// `^`
    ExclusiveOr,
    /// `|`
    InclusiveOr,
    /// `&&`
    LogicalAnd,
    /// `||`
    LogicalOr,
}

/// One assignment operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AssignmentOp {
    /// `=`
    Assign,
    /// `-=`
    AssignMinus,
    /// `+=`
    AssignPlus,
    /// `*=`
    AssignMultiply,
    /// `/=`
    AssignDivide,
    /// `%=`
    AssignModulus,
    /// `&=`
    AssignAnd,
    /// `^=`
    AssignXor,
    /// `|=`
    AssignOr,
    /// `<<=`
    AssignShiftLeft,
    /// `>>=`
    AssignShiftRight,
    /// `>>>=`
    AssignUnsignedShiftRight,
}

/// One literal expression.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
    /// One integer literal.
    Integer(i32),
    /// One floating-point literal.
    Float(f32),
    /// One string literal.
    String(String),
    /// `OBJECT_SELF`
    ObjectSelf,
    /// `OBJECT_INVALID`
    ObjectInvalid,
    /// `LOCATION_INVALID`
    LocationInvalid,
    /// One JSON constructor keyword lowered to its textual payload.
    Json(String),
    /// One vector constant.
    Vector([f32; 3]),
    /// One magic macro token.
    Magic(MagicLiteral),
}

/// One magic macro literal preserved in syntax.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MagicLiteral {
    /// `__FUNCTION__`
    Function,
    /// `__FILE__`
    File,
    /// `__LINE__`
    Line,
    /// `__DATE__`
    Date,
    /// `__TIME__`
    Time,
}