diff --git a/packages/compile/src/Helpers.js b/packages/compile/src/_shared/helpers.js similarity index 100% rename from packages/compile/src/Helpers.js rename to packages/compile/src/_shared/helpers.js index 21047d4e..1f1688ba 100644 --- a/packages/compile/src/Helpers.js +++ b/packages/compile/src/_shared/helpers.js @@ -1,9 +1,9 @@ import util from 'util' +import B from 'bufx' +import parseCsv from 'csv-parse/lib/sync.js' import R from 'ramda' import split from 'split-string' import XLSX from 'xlsx' -import parseCsv from 'csv-parse/lib/sync.js' -import B from 'bufx' // Set true to print a stack trace in vlog export const PRINT_VLOG_TRACE = false diff --git a/packages/compile/src/ReadDat.js b/packages/compile/src/_shared/read-dat.js similarity index 97% rename from packages/compile/src/ReadDat.js rename to packages/compile/src/_shared/read-dat.js index 7b3586f4..bccf04b6 100644 --- a/packages/compile/src/ReadDat.js +++ b/packages/compile/src/_shared/read-dat.js @@ -1,10 +1,9 @@ import fs from 'fs' - import B from 'bufx' import byline from 'byline' import R from 'ramda' -import { canonicalVensimName } from './Helpers.js' +import { canonicalVensimName } from './helpers.js' /** * Read a Vensim `dat` file with static data and return a Map. diff --git a/packages/compile/src/Subscript.js b/packages/compile/src/_shared/subscript.js similarity index 99% rename from packages/compile/src/Subscript.js rename to packages/compile/src/_shared/subscript.js index 03ae4c7e..8a1f6c20 100644 --- a/packages/compile/src/Subscript.js +++ b/packages/compile/src/_shared/subscript.js @@ -1,8 +1,8 @@ import util from 'util' -import R from 'ramda' import B from 'bufx' import yaml from 'js-yaml' -import { canonicalName, asort, vlog } from './Helpers.js' +import R from 'ramda' +import { canonicalName, asort, vlog } from './helpers.js' // A subscript is a dimension or an index. // Both have the same properties: model name, canonical name, family, values. diff --git a/packages/compile/src/CodeGen.js b/packages/compile/src/generate/code-gen.js similarity index 97% rename from packages/compile/src/CodeGen.js rename to packages/compile/src/generate/code-gen.js index 15100251..b12e4edb 100644 --- a/packages/compile/src/CodeGen.js +++ b/packages/compile/src/generate/code-gen.js @@ -1,9 +1,11 @@ import R from 'ramda' -import ModelLHSReader from './ModelLHSReader.js' -import EquationGen from './EquationGen.js' -import Model from './Model.js' -import { sub, allDimensions, allMappings, subscriptFamilies } from './Subscript.js' -import { asort, lines, strlist, abend, mapIndexed } from './Helpers.js' + +import { asort, lines, strlist, abend, mapIndexed } from '../_shared/helpers.js' +import { sub, allDimensions, allMappings, subscriptFamilies } from '../_shared/subscript.js' +import Model from '../model/model.js' + +import EquationGen from './equation-gen.js' +import ModelLHSReader from './model-lhs-reader.js' export function generateCode(parseTree, opts) { return codeGenerator(parseTree, opts).generate() diff --git a/packages/compile/src/EquationGen.js b/packages/compile/src/generate/equation-gen.js similarity index 99% rename from packages/compile/src/EquationGen.js rename to packages/compile/src/generate/equation-gen.js index c715c29f..24c2afa7 100644 --- a/packages/compile/src/EquationGen.js +++ b/packages/compile/src/generate/equation-gen.js @@ -1,23 +1,8 @@ import path from 'path' +import { ModelLexer, ModelParser } from 'antlr4-vensim' import R from 'ramda' import XLSX from 'xlsx' -import { ModelLexer, ModelParser } from 'antlr4-vensim' -import ModelReader from './ModelReader.js' -import ModelLHSReader from './ModelLHSReader.js' -import LoopIndexVars from './LoopIndexVars.js' -import Model from './Model.js' -import { - dimensionNames, - extractMarkedDims, - hasMapping, - isDimension, - isIndex, - isTrivialDimension, - indexInSepDim, - normalizeSubscripts, - separatedVariableIndex, - sub -} from './Subscript.js' + import { canonicalName, cartesianProductOf, @@ -34,7 +19,24 @@ import { readCsv, strToConst, vlog -} from './Helpers.js' +} from '../_shared/helpers.js' +import { + dimensionNames, + extractMarkedDims, + hasMapping, + isDimension, + isIndex, + isTrivialDimension, + indexInSepDim, + normalizeSubscripts, + separatedVariableIndex, + sub +} from '../_shared/subscript.js' +import ModelReader from '../parse/model-reader.js' +import Model from '../model/model.js' + +import LoopIndexVars from './loop-index-vars.js' +import ModelLHSReader from './model-lhs-reader.js' export default class EquationGen extends ModelReader { constructor(variable, extData, directData, mode, modelDirname) { diff --git a/packages/compile/src/LoopIndexVars.js b/packages/compile/src/generate/loop-index-vars.js similarity index 92% rename from packages/compile/src/LoopIndexVars.js rename to packages/compile/src/generate/loop-index-vars.js index a9c5fd17..df6c9373 100644 --- a/packages/compile/src/LoopIndexVars.js +++ b/packages/compile/src/generate/loop-index-vars.js @@ -1,4 +1,4 @@ -import { isDimension } from './Subscript.js' +import { isDimension } from '../_shared/subscript.js' export default class LoopIndexVars { constructor(indexVars) { diff --git a/packages/compile/src/ModelLHSReader.js b/packages/compile/src/generate/model-lhs-reader.js similarity index 93% rename from packages/compile/src/ModelLHSReader.js rename to packages/compile/src/generate/model-lhs-reader.js index 84e5cb1c..229c22f9 100644 --- a/packages/compile/src/ModelLHSReader.js +++ b/packages/compile/src/generate/model-lhs-reader.js @@ -1,8 +1,9 @@ import R from 'ramda' -import ModelReader from './ModelReader.js' -import { createParser } from './Parser.js' -import { sub, isDimension } from './Subscript.js' -import { canonicalName } from './Helpers.js' + +import { canonicalName } from '../_shared/helpers.js' +import { sub, isDimension } from '../_shared/subscript.js' +import ModelReader from '../parse/model-reader.js' +import { createParser } from '../parse/parser.js' // // ModelLHSReader parses the LHS of a var in Vensim format and diff --git a/packages/compile/src/index.js b/packages/compile/src/index.js index f4e8489a..14afd496 100644 --- a/packages/compile/src/index.js +++ b/packages/compile/src/index.js @@ -1,8 +1,8 @@ // Copyright (c) 2022 Climate Interactive / New Venture Fund -export { generateCode } from './CodeGen.js' -export { parseAndGenerate, printNames } from './Entry.js' -export { canonicalName } from './Helpers.js' -export { parseModel } from './Parser.js' -export { preprocessModel } from './Preprocessor.js' -export { readDat } from './ReadDat.js' +export { canonicalName } from './_shared/helpers.js' +export { readDat } from './_shared/read-dat.js' +export { preprocessModel } from './preprocess/preprocessor.js' +export { parseModel } from './parse/parser.js' +export { generateCode } from './generate/code-gen.js' +export { parseAndGenerate, printNames } from './parse-and-generate.js' diff --git a/packages/compile/src/EquationReader.js b/packages/compile/src/model/equation-reader.js similarity index 99% rename from packages/compile/src/EquationReader.js rename to packages/compile/src/model/equation-reader.js index f05c9b2f..2f20e002 100644 --- a/packages/compile/src/EquationReader.js +++ b/packages/compile/src/model/equation-reader.js @@ -1,18 +1,6 @@ import { ModelParser } from 'antlr4-vensim' import R from 'ramda' -import ExprReader from './ExprReader.js' -import Model from './Model.js' -import ModelReader from './ModelReader.js' -import { createParser } from './Parser.js' -import VariableReader from './VariableReader.js' -import { - extractMarkedDims, - indexNamesForSubscript, - normalizeSubscripts, - separatedVariableIndex, - sub, - isDimension -} from './Subscript.js' + import { canonicalName, canonicalVensimName, @@ -29,7 +17,21 @@ import { newLookupVarName, newFixedDelayVarName, cartesianProductOf -} from './Helpers.js' +} from '../_shared/helpers.js' +import { + extractMarkedDims, + indexNamesForSubscript, + normalizeSubscripts, + separatedVariableIndex, + sub, + isDimension +} from '../_shared/subscript.js' +import ModelReader from '../parse/model-reader.js' +import { createParser } from '../parse/parser.js' + +import ExprReader from './expr-reader.js' +import Model from './model.js' +import VariableReader from './variable-reader.js' // Set this true to get a list of functions used in the model. This may include lookups. const PRINT_FUNCTION_NAMES = false diff --git a/packages/compile/src/ExprReader.js b/packages/compile/src/model/expr-reader.js similarity index 97% rename from packages/compile/src/ExprReader.js rename to packages/compile/src/model/expr-reader.js index 069ed492..ed08d47c 100644 --- a/packages/compile/src/ExprReader.js +++ b/packages/compile/src/model/expr-reader.js @@ -1,7 +1,9 @@ import { ModelLexer, ModelVisitor } from 'antlr4-vensim' -import { canonicalName } from './Helpers.js' -import Model from './Model.js' -import { createParser } from './Parser.js' + +import { canonicalName } from '../_shared/helpers.js' +import { createParser } from '../parse/parser.js' + +import Model from './model.js' /** * Reads an expression and determines if it resolves to a constant numeric value. diff --git a/packages/compile/src/Model.js b/packages/compile/src/model/model.js similarity index 98% rename from packages/compile/src/Model.js rename to packages/compile/src/model/model.js index 5ce495e3..3f067b40 100644 --- a/packages/compile/src/Model.js +++ b/packages/compile/src/model/model.js @@ -1,13 +1,8 @@ -import R from 'ramda' import B from 'bufx' import yaml from 'js-yaml' -import toposort from './toposort.js' -import VariableReader from './VariableReader.js' -import VarNameReader from './VarNameReader.js' -import SubscriptRangeReader from './SubscriptRangeReader.js' -import EquationReader from './EquationReader.js' -import Variable from './Variable.js' -import { createParser } from './Parser.js' +import R from 'ramda' + +import { decanonicalize, isIterable, listConcat, strlist, vlog, vsort } from '../_shared/helpers.js' import { addIndex, allAliases, @@ -18,8 +13,15 @@ import { normalizeSubscripts, sub, subscriptFamilies -} from './Subscript.js' -import { decanonicalize, isIterable, listConcat, strlist, vlog, vsort } from './Helpers.js' +} from '../_shared/subscript.js' +import { createParser } from '../parse/parser.js' + +import EquationReader from './equation-reader.js' +import SubscriptRangeReader from './subscript-range-reader.js' +import toposort from './toposort.js' +import VarNameReader from './var-name-reader.js' +import Variable from './variable.js' +import VariableReader from './variable-reader.js' let variables = [] let inputVars = [] diff --git a/packages/compile/src/SubscriptRangeReader.js b/packages/compile/src/model/subscript-range-reader.js similarity index 96% rename from packages/compile/src/SubscriptRangeReader.js rename to packages/compile/src/model/subscript-range-reader.js index 2d3f030f..3ee42580 100644 --- a/packages/compile/src/SubscriptRangeReader.js +++ b/packages/compile/src/model/subscript-range-reader.js @@ -2,9 +2,10 @@ import path from 'path' import { ModelParser } from 'antlr4-vensim' import R from 'ramda' import XLSX from 'xlsx' -import ModelReader from './ModelReader.js' -import { Subscript } from './Subscript.js' -import { cFunctionName, matchRegex, readCsv } from './Helpers.js' + +import { cFunctionName, matchRegex, readCsv } from '../_shared/helpers.js' +import { Subscript } from '../_shared/subscript.js' +import ModelReader from '../parse/model-reader.js' export default class SubscriptRangeReader extends ModelReader { constructor(modelDirname) { diff --git a/packages/compile/src/toposort.js b/packages/compile/src/model/toposort.js similarity index 100% rename from packages/compile/src/toposort.js rename to packages/compile/src/model/toposort.js diff --git a/packages/compile/src/VarNameReader.js b/packages/compile/src/model/var-name-reader.js similarity index 84% rename from packages/compile/src/VarNameReader.js rename to packages/compile/src/model/var-name-reader.js index adb093de..cdb43ef0 100644 --- a/packages/compile/src/VarNameReader.js +++ b/packages/compile/src/model/var-name-reader.js @@ -1,8 +1,10 @@ -import ModelReader from './ModelReader.js' import R from 'ramda' -import { createParser } from './Parser.js' -import { sub, isIndex, normalizeSubscripts } from './Subscript.js' -import { canonicalName } from './Helpers.js' + +import { canonicalName } from '../_shared/helpers.js' +import { sub, isIndex, normalizeSubscripts } from '../_shared/subscript.js' +import ModelReader from '../parse/model-reader.js' +import { createParser } from '../parse/parser.js' + // // VarNameReader reads a model var name using the parser to get the var name in C format. // This is used to generate a variable output in the output section. diff --git a/packages/compile/src/VariableReader.js b/packages/compile/src/model/variable-reader.js similarity index 95% rename from packages/compile/src/VariableReader.js rename to packages/compile/src/model/variable-reader.js index 5fd86f1f..c34cff04 100644 --- a/packages/compile/src/VariableReader.js +++ b/packages/compile/src/model/variable-reader.js @@ -1,10 +1,19 @@ import { ModelParser } from 'antlr4-vensim' import R from 'ramda' -import ModelReader from './ModelReader.js' -import Model from './Model.js' -import Variable from './Variable.js' -import { sub, isDimension, isIndex, normalizeSubscripts, subscriptsMatch, isSubdimension } from './Subscript.js' -import { canonicalName, vlog, strlist, cartesianProductOf } from './Helpers.js' + +import { canonicalName, vlog, strlist, cartesianProductOf } from '../_shared/helpers.js' +import { + sub, + isDimension, + isIndex, + normalizeSubscripts, + subscriptsMatch, + isSubdimension +} from '../_shared/subscript.js' +import ModelReader from '../parse/model-reader.js' + +import Model from './model.js' +import Variable from './variable.js' // Set true to print extra debugging information to stderr. const DEBUG_LOG = false diff --git a/packages/compile/src/Variable.js b/packages/compile/src/model/variable.js similarity index 100% rename from packages/compile/src/Variable.js rename to packages/compile/src/model/variable.js diff --git a/packages/compile/src/Entry.js b/packages/compile/src/parse-and-generate.js similarity index 92% rename from packages/compile/src/Entry.js rename to packages/compile/src/parse-and-generate.js index 97785e38..3b220318 100644 --- a/packages/compile/src/Entry.js +++ b/packages/compile/src/parse-and-generate.js @@ -3,12 +3,12 @@ import path from 'path' import B from 'bufx' -import { generateCode } from './CodeGen.js' -import { readXlsx } from './Helpers.js' -import Model from './Model.js' -import { parseModel } from './Parser.js' -import { readDat } from './ReadDat.js' -import { printSubscripts, yamlSubsList } from './Subscript.js' +import { readXlsx } from './_shared/helpers.js' +import { readDat } from './_shared/read-dat.js' +import { printSubscripts, yamlSubsList } from './_shared/subscript.js' +import { parseModel } from './parse/parser.js' +import Model from './model/model.js' +import { generateCode } from './generate/code-gen.js' /** * Parse a Vensim model and generate C code. diff --git a/packages/compile/src/ModelReader.js b/packages/compile/src/parse/model-reader.js similarity index 100% rename from packages/compile/src/ModelReader.js rename to packages/compile/src/parse/model-reader.js diff --git a/packages/compile/src/Parser.js b/packages/compile/src/parse/parser.js similarity index 100% rename from packages/compile/src/Parser.js rename to packages/compile/src/parse/parser.js diff --git a/packages/compile/src/Preprocessor.js b/packages/compile/src/preprocess/preprocessor.js similarity index 98% rename from packages/compile/src/Preprocessor.js rename to packages/compile/src/preprocess/preprocessor.js index 839cb8de..64c720ea 100644 --- a/packages/compile/src/Preprocessor.js +++ b/packages/compile/src/preprocess/preprocessor.js @@ -1,7 +1,7 @@ import path from 'path' -import R from 'ramda' import B from 'bufx' -import { splitEquations, replaceDelimitedStrings } from './Helpers.js' +import R from 'ramda' +import { splitEquations, replaceDelimitedStrings } from '../_shared/helpers.js' export let preprocessModel = (mdlFilename, spec, profile = 'genc', writeFiles = false, outDecls = []) => { const MACROS_FILENAME = 'macros.txt'