-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change plugin-wasm to expose outputVarIds in the generated module (
- Loading branch information
1 parent
1a77eed
commit 9c2f7d1
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2024 Climate Interactive / New Venture Fund | ||
|
||
/** | ||
* Helper function that converts a Vensim variable or subscript name | ||
* into a valid C identifier as used by SDE. | ||
* TODO: Import helper function from `compile` package instead | ||
*/ | ||
function sdeNameForVensimName(name: string): string { | ||
return ( | ||
'_' + | ||
name | ||
.trim() | ||
.replace(/"/g, '_') | ||
.replace(/\s+!$/g, '!') | ||
.replace(/\s/g, '_') | ||
.replace(/,/g, '_') | ||
.replace(/-/g, '_') | ||
.replace(/\./g, '_') | ||
.replace(/\$/g, '_') | ||
.replace(/'/g, '_') | ||
.replace(/&/g, '_') | ||
.replace(/%/g, '_') | ||
.replace(/\//g, '_') | ||
.replace(/\|/g, '_') | ||
.toLowerCase() | ||
) | ||
} | ||
|
||
/** | ||
* Helper function that converts a Vensim variable name (possibly containing | ||
* subscripts) into a valid C identifier as used by SDE. | ||
* TODO: Import helper function from `compile` package instead | ||
*/ | ||
export function sdeNameForVensimVarName(varName: string): string { | ||
const m = varName.match(/([^[]+)(?:\[([^\]]+)\])?/) | ||
if (!m) { | ||
throw new Error(`Invalid Vensim name: ${varName}`) | ||
} | ||
let id = sdeNameForVensimName(m[1]) | ||
if (m[2]) { | ||
const subscripts = m[2].split(',').map(x => sdeNameForVensimName(x)) | ||
id += `[${subscripts.join('][')}]` | ||
} | ||
|
||
return id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters