C Numerical & Embedded Engineering Integration (.c, .h)
@modelscript/c (languages/c) provides native WebAssembly GLR parsing, procedural simulation step compilation, automated Modelica external "C" header binding, continuous DAE lowering, and static analysis diagnostics for ANSI C / C99 engineering blocks, Simulink C MEX S-functions, and PSCAD custom C components.
Supported Extensions & Standards
| Extension | Standard / Format | Description |
|---|---|---|
.c | C99 / ANSI C | Control algorithms, filter routines, ODE integration steps, and S-functions |
.h | C Headers | Function prototypes, typedefs, and struct layouts for Modelica external bindings |
Key Capabilities
1. WebAssembly GLR C Parser
- Ahead-of-time compiled WebAssembly GLR parser generated by
@modelscript/dslwith zero grammar conflicts. - Parses full C99 translation units, nested functions,
struct/union/enumdeclarations,typedefnames, control flow (if,while,for,do-while), pointer/array operators, and bitwise/arithmetic expressions.
2. Automated Modelica External "C" Bridge
- Header Scraper (
scrapeCHeader) extracts C prototypes, argument names, causalities, and struct fields from.hfiles. - Modelica Binder (
bindCHeaderToModelica) auto-generates strongly typed Modelica functions with:modelicafunction motor_torque input Real current; input MotorParams p; output Real y; external "C" y = motor_torque(current, p) annotation(Include = "#include \"motor.h\""); end motor_torque;
3. Zero-Allocation Simulation Step Compiler
- Compiles procedural C control blocks and discrete filters into low-overhead execution closures executing 100,000 steps in < 30ms without heap allocations.
- Direct mapping to pre-allocated typed arrays:
statesReal(Float64Array) andstatesInt(Int32Array). - Built-in support for standard
<math.h>mathematical functions (sin,cos,tan,asin,acos,atan,atan2,exp,log,log10,sqrt,fabs,pow,fmin,fmax,floor,ceil).
4. Direct DAE Subroutine Lowering
- Translates ODE continuous state derivatives (
dx[i] = .../der_x = ...) directly into continuous statesder(s_i)in@modelscript/runtime'sIDaeBuilderlinear memory arena. - Enables Simulink continuous S-functions and PSCAD C segments to integrate directly into continuous DAE and hybrid ODE solvers.
Static Analysis & Safety Linter Rules
The C analyzer surfaces common numeric defects, memory safety concerns, and algorithmic cycles (M8401–M8410):
10 rules3 warning6 error1 info Run with msx lint
| Code | Rule | Severity | Message |
|---|---|---|---|
M8401 | uninitialized-variable | warning | Local variable '<varName>' may be read before being initialized. |
M8402 | constant-division-by-zero | error | Division or modulo by constant zero detected in arithmetic expression. |
M8403 | array-bounds-overflow | error | Static array subscript '<arrName>[<index>]' exceeds declared dimension bound of <bound>. |
M8404 | unreachable-code | warning | Unreachable statements detected following unconditional 'return', 'break', or 'goto'. |
M8405 | assignment-in-conditional | warning | Assignment operator '=' used inside conditional expression; did you mean '=='? |
M8406 | algebraic-dependency-cycle | error | Procedural algebraic dependency cycle detected without state lag: '<cycle>'. |
M8407 | null-pointer-dereference | error | Dereference of statically known NULL pointer in expression '<expr>'. |
M8408 | function-prototype-mismatch | error | Function '<fnName>' signature conflicts with declaration: expected '<expected>', got '<actual>'. |
M8409 | missing-return-value | error | Non-void function '<fnName>' with return type '<returnType>' reaches end of body without returning a value. |
M8410 | implicit-lossy-conversion | info | Implicit floating-point to integer conversion from '<fromType>' to '<toType>' may cause lossy truncation. |
Generated from languages/c/src/errors.ts. All languages →
CLI Usage
bash
# Lint C source files or headers
msx lint Controller.c
# Generate Modelica external "C" bindings from header
msx c to-modelica SensorDriver.h -o SensorDriver.mo
