Expand description
§nwnrs-nwscript
nwnrs-nwscript is the workspace’s pure Rust NWScript frontend and compiler
toolchain. It depends on nwnrs-types for shared IO and resource vocabulary,
but owns the language pipeline itself.
§Scope
- source loading and preprocessing
- lexing, parsing, semantic analysis, and optimization
- emission of
NCSandNDBartifacts NCSasm and disassembly through the upstream-stylenwasmtext layer- a lightweight
NCSVM and test-runner layer for exercising compiled scripts - typed vocabulary for NWScript-related binary formats
§Internal Structure
sourceandpreprocess: source loading and include handlinglexer,token, andparser: tokenization and syntax constructionast,hir,sema, andir: progressively richer semantic and lowered program formsoptandcodegen: optimization and NCS emissionncs,ndb, andnwasm: binary artifact support plus text asm and disasmvm: lightweight bytecode execution and action-command test harnessdiag,langspec, andhash: diagnostics, builtin language specification, and NWScript-specific string hashing
§Public Surface by Stage
§Source loading and preprocessing
ScriptResolverSourceBundleSourceFileSourceIdSourceMapSourceLocationSourceLoadOptionsSourceErrorPreprocessedSourcePreprocessErrorIncludeDirectiveload_source_bundlepreprocess_source_bundle
§Lexing and parsing
LexerLexerErrorTokenTokenKindKeywordMAX_TOKEN_LENGTHParseErrorParserErrorResolvedParseErrorparse_bytesparse_textparse_tokensparse_sourceparse_source_bundlelex_byteslex_textlex_source
§Syntax-layer vocabulary
ScriptTopLevelItemDeclarationFunctionDeclStructDeclStructFieldDeclParameterTypeSpecTypeKindStmtSimpleStmtBlockStmtIfStmtForStmtWhileStmtDoWhileStmtSwitchStmtCaseStmtDefaultStmtReturnStmtExpressionStmtExprExprKindUnaryOpBinaryOpAssignmentOpVarDeclaratorMagicLiteralNamedItem
§Semantic and lowered representations
SemanticModelSemanticFunctionSemanticGlobalSemanticStructSemanticFieldSemanticParameterSemanticTypeSemanticOptionsSemanticErrorHirModuleHirFunctionHirStmtHirExprHirExprKindHirStructHirFieldHirGlobalHirLocalHirLocalIdHirLocalKindHirValueRefHirBlockHirParameterHirCallTargetHirDeclaratorHirDeclareStmtHirIfStmtHirForStmtHirDoWhileStmtHirReturnStmtHirSwitchStmtHirLowerErrorIrModuleIrFunctionIrBlockIrBlockIdIrInstructionIrTerminatorIrValueIdIrLocalIdIrGlobalIrLowerErroranalyze_scriptanalyze_script_with_optionslower_to_hirlower_hir_to_ir
§Builtins and language specification
LangSpecLangSpecErrorBuiltinTypeBuiltinFunctionBuiltinParameterBuiltinConstantBuiltinValueDEFAULT_LANGSPEC_SCRIPT_NAMENW_SCRIPT_SOURCE_RES_TYPEload_langspecparse_langspecparse_langspec_bytesparse_langspec_from_source_map
§Compile and optimization
CompileOptionsCompileArtifactsCompileErrorCodegenErrorCompilerErrorCodeOptimizationLevelcompile_scriptcompile_script_with_source_mapcompile_source_bundlecompile_hir_to_ncs
§Artifacts: NCS, NDB, and asm
NCS_HEADERNCS_BINARY_HEADER_SIZENCS_OPERATION_BASE_SIZENcsHeaderNcsHeaderErrorNcsInstructionNcsOpcodeNcsAuxCodeNcsReadErrorNcsAsmErrorNcsAsmLineNcsDisassemblyOptionsNdbNdbFileNdbFunctionNdbLineNdbStructNdbStructFieldNdbTypeNdbVariableNdbErrordecode_ncs_headerdecode_ncs_instructionsencode_ncs_instructionsdisassemble_ncsrender_disassembly_linesrender_ncs_disassemblyrender_ncs_disassembly_with_ndbassemble_ncs_bytesassemble_ncs_textread_ndbparse_ndb_strwrite_ndb
§Compiler sessions
CompilerSessionCompilerSessionErrorCompilerSessionOptions
§Compiler driver
CompilerHostCompilerHostErrorCompilerDriverOptionsCompilerDriverErrorCompileFileOutcomecompile_file_with_hostFileSystemScriptResolverDirectoryCompilerHostBatchCompileOptionsBatchCompileEntryBatchCompileStatusBatchCompileReportBatchCompileErrorcompile_paths
§Graphviz
render_script_graphviz
§VM and test execution
VmVmRunOptionsVmScriptVmSituationVmStepOutcomeVmTraceEventVmTraceHookVmValueVmObjectIdVmEngineStructureComparerVmEngineStructureFactoryVmEngineStructureValueVmFunctionInfoVmCommandHandlerVmErrorVmSourceLocation
For compiled scripts that include user-function calls, prefer the NDB-backed
execution path (VmScript::from_bytes_with_ndb, Vm::run_bytes_with_ndb, or
Vm::run_function_bytes) so the VM can recover callee stack layouts. The
direct named-function runner also boots global initializers automatically for
main() and StartingConditional() entry loaders. The debugger-oriented VM
surface also supports single-step execution plus step_over, step_out,
run_until_offset, run_until_function, and run_until_line when attached
NDB metadata is available.
§Miscellaneous semantic helpers
Spannwscript_string_hashnwscript_string_hash_bytes
§Logical Edges
- the crate intentionally exposes multiple representations instead of pretending parsing and compilation are one stage
LangSpecis part of normal compilation, not an optional afterthought- HIR and IR are real public control points
nwasmis both an artifact boundary and a debugging surface- source loading and include handling are operationally significant
§See also
nwnrs_types::resman, which provides the resource layer through which source and compiled script files are typically loadednwnrs_types::install, which resolves the install root and language root needed to locatenwscript.nssfor compilation
§Why This Crate Exists
The point of nwnrs-types is to make the language subsystem inspectable and
operable at every meaningful stage, from raw source bundle through lowered IR
and into bytecode and debug artifacts.
Modules§
- prelude
- Common imports for consumers of this crate.
Structs§
- Batch
Compile Entry - One per-input result from a batch compile run.
- Batch
Compile Options - Options controlling multi-file directory and file compilation.
- Batch
Compile Report - Summary of one batch compile run.
- Block
Stmt - One compound block.
- Builtin
Constant - One builtin constant declaration.
- Builtin
Function - One builtin function declaration.
- Builtin
Parameter - One builtin function parameter.
- Case
Stmt - One
caselabel. - Codegen
Error - One pure-Rust code generation failure.
- Compile
Artifacts - Compiler outputs produced in memory.
- Compile
Options - One compilation request.
- Compiler
Driver Options - Options controlling one callback-driven compiler invocation.
- Compiler
Host Error - Errors returned while resolving or writing through a callback-driven compiler host.
- Compiler
Session - One reusable pure-Rust compiler session backed by a script resolver.
- Compiler
Session Options - Configuration for one reusable NWScript compiler session.
- Declaration
- One variable declaration statement.
- Default
Stmt - One
defaultlabel. - Directory
Compiler Host - One directory-backed compiler host that reads source files from filesystem roots and writes outputs back to disk.
- DoWhile
Stmt - One
do ... whilestatement. - Expr
- One expression in
NWScriptsource. - Expression
Stmt - One expression statement.
- File
System Script Resolver - One filesystem-backed source resolver that searches one or more root directories.
- ForStmt
- One
forstatement. - Function
Decl - One parsed function declaration or definition.
- HirBlock
- One lowered block.
- HirDeclarator
- One lowered declared local.
- HirDeclare
Stmt - One lowered declaration statement.
- HirDo
While Stmt - One lowered
do/while. - HirExpr
- One lowered expression.
- HirField
- One lowered structure field.
- HirFor
Stmt - One lowered
for. - HirFunction
- One lowered function.
- HirGlobal
- One lowered global variable.
- HirIf
Stmt - One lowered
if. - HirLocal
- One lowered local slot.
- HirLocal
Id - One function-local identifier.
- HirLower
Error - One HIR lowering failure.
- HirModule
- One lowered HIR module ready for further compiler passes.
- HirParameter
- One lowered function parameter.
- HirReturn
Stmt - One lowered
return. - HirStruct
- One lowered structure definition.
- HirSwitch
Stmt - One lowered
switch. - HirWhile
Stmt - One lowered
while. - IfStmt
- One
ifstatement. - InMemory
Script Resolver - An in-memory script resolver used for tests and fixture loading.
- Include
Directive - One
#include "..."directive. - Include
Edge - One include relationship discovered while traversing source files.
- IrBlock
- One basic block.
- IrBlock
Id - One block id.
- IrFunction
- One lowered function.
- IrGlobal
- One lowered global.
- IrLocal
Id - One local slot id.
- IrLower
Error - One HIR-to-IR lowering failure.
- IrModule
- One lowered IR module ready for code generation.
- IrValue
Id - One SSA-like transient value id.
- Lang
Spec - Parsed builtin declarations from
nwscript.nss. - Lexer
- Lexes
NWScriptsource using the upstream compiler’s token vocabulary. - Lexer
Error - A lexical error returned while scanning
NWScriptsource text. - Macro
Definition - One object-like
#definecaptured during preprocessing. - Named
Item - One variable or field name plus span.
- NcsAsm
Line - One decoded disassembly line.
- NcsDisassembly
Options - Options controlling NCS disassembly rendering.
- NcsHeader
- Fixed metadata extracted from the leading bytes of an
NCSfile. - NcsInstruction
- One decoded
NCSinstruction. - Ndb
- Parsed contents of an
NDB V1.0file. - NdbFile
- One file entry in an
NDBfile table. - NdbFunction
- One function entry in an
NDBfile. - NdbLine
- One line mapping entry in an
NDBfile. - NdbStruct
- One struct entry in an
NDBfile. - NdbStruct
Field - One struct field entry in an
NDBfile. - NdbVariable
- One variable entry in an
NDBfile. - Parameter
- One parsed parameter declaration.
- Parser
Error - One parser error aligned to the upstream compiler’s diagnostic space.
- Preprocessed
Source - One preprocessed token stream plus the macros captured while producing it.
- Return
Stmt - One
returnstatement. - Script
- One parsed
NWScripttranslation unit. - Semantic
Error - One semantic-analysis error aligned to the upstream compiler’s diagnostic space.
- Semantic
Field - One resolved structure field.
- Semantic
Function - One resolved function signature.
- Semantic
Global - One resolved global variable.
- Semantic
Model - Semantic facts collected from one script.
- Semantic
Options - Options controlling semantic analysis checks.
- Semantic
Parameter - One resolved function parameter.
- Semantic
Struct - One resolved user-defined structure.
- Simple
Stmt - One statement carrying only a span.
- Source
Bundle - One loaded root script plus all transitively discovered include files.
- Source
File - One loaded source file plus its line-start table.
- Source
Id - Identifies one loaded
NWScriptsource file within a compilation session. - Source
Load Options - Options controlling source loading and include traversal.
- Source
Location - A one-based line and column location inside a source file.
- Source
Map - A collection of loaded source files indexed by both id and normalized name.
- Span
- A half-open byte span within one source file.
- Struct
Decl - One user-defined structure declaration.
- Struct
Field Decl - One structure field declaration statement.
- Switch
Stmt - One
switchstatement. - Token
- One token plus its source span and normalized payload.
- Type
Spec - One parsed type specifier.
- Unknown
Compiler Error Code - An error returned when a numeric compiler error code is not recognized.
- Unknown
NcsAux Code - An error returned when converting an auxcode byte to
NcsAuxCode. - Unknown
NcsOpcode - An error returned when converting an opcode byte to
NcsOpcode. - VarDeclarator
- One declared variable, optionally with an initializer.
- Vm
- One command-dispatch table used to execute
ACTIONopcodes. - VmFunction
Info - One debugger-visible function range derived from attached
NDBmetadata. - VmRun
Options - One set of optional execution controls for a VM run.
- VmScript
- One executable
NCSscript plus its VM runtime state. - VmSituation
- One deferred NWScript action captured by
STORESTATE. - VmSource
Location - One debugger-visible source location derived from attached
NDBmetadata. - VmTrace
Event - One instruction-dispatch trace event emitted by the VM.
- While
Stmt - One
whilestatement.
Enums§
- Assignment
Op - One assignment operator.
- Batch
Compile Error - Errors returned before or outside individual compile attempts in batch mode.
- Batch
Compile Status - One status emitted for a batch compile input.
- Binary
Op - One binary operator.
- Builtin
Type - One builtin
NWScripttype defined bynwscript.nss. - Builtin
Value - One literal builtin value extracted from the language spec.
- Compile
Error - One compilation failure across analysis, lowering, or code generation.
- Compile
File Outcome - Result of one callback-driven compile request.
- Compiler
Driver Error - Errors returned while executing one callback-driven compile request.
- Compiler
Error Code - Stable compiler and VM error codes used by the upstream
NWScriptcompiler. - Compiler
Session Error - Errors returned while using one reusable compiler session.
- Expr
Kind - One expression shape.
- HirCall
Target - One resolved call target.
- HirExpr
Kind - One lowered expression kind.
- HirLocal
Kind - One lowered local kind.
- HirStmt
- One lowered statement.
- HirValue
Ref - One resolved value reference.
- IrCall
Argument - One lowered call argument.
- IrInstruction
- One stack-oriented IR instruction.
- IrTerminator
- One control-flow terminator.
- Keyword
- One
NWScriptkeyword or builtin token recognized during lexing. - Lang
Spec Error - Errors returned while bootstrapping the builtin language spec.
- Literal
- One literal expression.
- Magic
Literal - One magic macro literal preserved in syntax.
- NcsAsm
Error - Errors returned while rendering or parsing NCS asm text.
- NcsAux
Code - One
NWScriptVM aux code. - NcsHeader
Error - Errors returned while decoding the fixed
NCSheader. - NcsOpcode
- One
NWScriptVM opcode. - NcsRead
Error - Errors returned while decoding a full
NCSbytecode stream. - NdbError
- Errors returned while parsing or writing
NDB V1.0. - NdbType
- A type abbreviation used in an
NDB V1.0file. - Optimization
Level - Optimization levels accepted by the pure-Rust compiler pipeline.
- Parse
Error - One parser failure.
- Preprocess
Error - Errors returned while scanning include directives across multiple files.
- Resolved
Parse Error - One parser failure after source resolution and preprocessing.
- Semantic
Type - One resolved semantic type.
- Source
Error - Errors returned while resolving or loading source files.
- Stmt
- One statement in
NWScriptsource. - Token
Kind - One lexical token in
NWScriptsource. - TopLevel
Item - One top-level
NWScriptitem. - Type
Kind - One
NWScripttype kind recognized syntactically by the parser. - UnaryOp
- One unary operator.
- VmEngine
Structure Value - One engine-structure payload carried by a stack value.
- VmError
- Errors returned while executing
NCSbytecode. - VmStep
Outcome - One result returned after executing exactly one instruction.
- VmValue
- One runtime stack value.
Constants§
- DEFAULT_
LANGSPEC_ SCRIPT_ NAME - Default logical script name for the builtin
NWScriptlanguage definition. - DEFAULT_
MAX_ INCLUDE_ DEPTH - The upstream default include-depth limit.
- MAX_
TOKEN_ LENGTH - Maximum token payload length used by the upstream compiler.
- NCS_
AUXCODE_ OFFSET - Byte offset of the auxcode field inside an instruction.
- NCS_
BINARY_ HEADER_ SIZE - Total size of the fixed binary header, including the encoded bytecode size.
- NCS_
EXTRA_ DATA_ OFFSET - Byte offset of the extra-data section inside an instruction.
- NCS_
HEADER - Text prefix of an
NCS V1.0bytecode stream. - NCS_
OPCODE_ OFFSET - Byte offset of the opcode field inside an instruction.
- NCS_
OPERATION_ BASE_ SIZE - Size of the opcode-plus-auxcode instruction prefix.
- NW_
SCRIPT_ SOURCE_ RES_ TYPE - The built-in NWN resource type used for
NWScriptsource files.
Traits§
- Compiler
Host - One callback-driven host for loading NWScript source and receiving compiler outputs.
- Script
Resolver - Loads script source text by logical name and resource type.
Functions§
- analyze_
script - Performs semantic analysis on one parsed script.
- analyze_
script_ with_ options - Performs semantic analysis on one parsed script with explicit options.
- assemble_
ncs_ bytes - Parses assembleable NCS asm text and encodes it into bytecode without the fixed NCS file header.
- assemble_
ncs_ text - Parses assembleable NCS asm text into decoded instructions.
- compile_
file_ with_ host - Compiles one logical script through a callback-driven host.
- compile_
hir_ to_ ncs - Compiles one lowered HIR module to
NCS. - compile_
paths - Collects and compiles a set of script files and directories.
- compile_
script - Compiles one parsed script through semantic analysis, HIR lowering, and
O0NCS emission. - compile_
script_ with_ source_ map - Compiles one parsed script and emits
NDBwhen a source map is available. - compile_
source_ bundle - Parses and compiles one already-loaded source bundle with
NDBoutput. - decode_
ncs_ header - Decodes the fixed binary header of an
NCS V1.0file. - decode_
ncs_ instructions - Decodes a full
NCS V1.0bytecode stream into individual instructions. - disassemble_
ncs - Decodes a full
NCSstream into instruction-shaped disassembly lines. - encode_
ncs_ instructions - Encodes one
NCS V1.0instruction stream with the fixed binary header. - lex_
bytes - Lexes a byte buffer associated with
source_id. - lex_
source - Lexes the contents of one source file.
- lex_
text - Lexes a string slice associated with
source_id. - load_
langspec - Loads
nwscript.nssthrough a source resolver and parses the builtin declarations. - load_
source_ bundle - Loads a root script and recursively discovers its
#includedependencies. - lower_
hir_ to_ ir - Lowers HIR into the compiler IR used by later codegen work.
- lower_
to_ hir - Lowers one semantically-valid script into typed HIR.
- nwscript_
string_ hash - Returns the exact
NWScriptruntime hash for a UTF-8 string slice. - nwscript_
string_ hash_ bytes - Returns the exact
NWScriptruntime hash for a cooked byte string. - parse_
bytes - Lexes and parses a byte buffer associated with
source_id. - parse_
langspec - Parses builtin declarations from one already-loaded source file.
- parse_
langspec_ bytes - Parses builtin declarations from one already-loaded byte buffer.
- parse_
langspec_ from_ source_ map - Parses builtin declarations from
root_idinsource_map. - parse_
ndb_ str - Parses
NDB V1.0from a string slice. - parse_
resolved_ script - Resolves, preprocesses, and parses one named root script.
- parse_
source - Lexes and parses one source file.
- parse_
source_ bundle - Parses one already-loaded source bundle after include traversal and macro expansion.
- parse_
text - Lexes and parses a text buffer associated with
source_id. - parse_
tokens - Parses one already-tokenized
NWScripttranslation unit. - preprocess_
source_ bundle - Preprocesses one already-loaded source bundle into a token stream.
- read_
ndb - Parses
NDB V1.0from a buffered reader. - render_
disassembly_ lines - Renders already-decoded disassembly lines into plain text.
- render_
ncs_ disassembly - Renders a full
NCSstream into stable human-readable disassembly text. - render_
ncs_ disassembly_ with_ ndb - Renders a full
NCSstream into NDB-aware disassembly text. - render_
script_ graphviz - Renders one parsed script as Graphviz DOT.
- write_
ndb - Writes an
NDB V1.0file in upstream-compatible textual form.
Type Aliases§
- VmCommand
Handler - One immutable
ACTIONhandler. - VmEngine
Structure Comparer - One engine-structure equality hook used by
EQandNEQ. - VmEngine
Structure Factory - One engine-structure default factory used by
RSADD. - VmObject
Id - One opaque object id visible to the VM runtime.
- VmTrace
Hook - One instruction trace hook invoked before the VM executes each opcode.