Skip to content

Commit

Permalink
Merge pull request #35 from publicodes/optim-v2
Browse files Browse the repository at this point in the history
Feat(optim)!: rewrite of the constant folding pass
  • Loading branch information
EmileRolley authored Feb 12, 2024
2 parents f17f772 + a881cda commit 6e36c59
Show file tree
Hide file tree
Showing 12 changed files with 2,332 additions and 723 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn
- run: yarn install --immutable

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"format": "prettier --write .",
"format:check": "prettier --check ."
},
"engines": {
"node": ">=17"
},
"exports": {
".": {
"import": "./dist/index.js",
Expand Down Expand Up @@ -47,16 +50,15 @@
"license": "MIT",
"dependencies": {
"@types/node": "^18.11.18",
"publicodes": "1.0.0-beta.77"
"publicodes": "^1.0.1"
},
"devDependencies": {
"@types/jest": "^29.2.5",
"docdash": "^2.0.1",
"jest": "^29.4.1",
"mitata": "^0.1.6",
"prettier": "^3.0.0",
"ts-jest": "^29.0.4",
"ts-node": "^10.9.1",
"ts-node": "^10.9.2",
"tsup": "^6.5.0",
"typedoc": "^0.24.8",
"typedoc-plugin-export-functions": "^1.0.0",
Expand Down
40 changes: 3 additions & 37 deletions source/commons.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { basename } from 'path'
import {
Rule,
ParsedRules,
Logger,
ExprAST,
RuleNode,
reduceAST,
ASTNode,
} from 'publicodes'
import { Rule, Logger, ExprAST, reduceAST, ASTNode } from 'publicodes'
import yaml from 'yaml'

/**
Expand Down Expand Up @@ -67,39 +59,13 @@ export type ImportMacro = {
/**
* Represents a non-parsed NGC rule.
*/
export type RawRule = Omit<Rule, 'nom'> & ImportMacro
export type RawRule = Omit<Rule, 'nom'> | ImportMacro

/**
* Represents a non-parsed NGC model.
*/
export type RawRules = Record<RuleName, RawRule>

/**
* Returns the raw nodes of a parsed rules object.
*
* @param parsedRules - The parsed rules object.
*
* @returns The raw nodes of the parsed rules object.
*/
export function getRawNodes(parsedRules: ParsedRules<RuleName>): RawRules {
return Object.fromEntries(
Object.values(parsedRules).reduce((acc, rule) => {
const { nom, ...rawNode } = rule.rawNode
// We don't want to keep the `avec` attribute in the raw node
// as they are already resolved in the [parsedRules] object.
delete rawNode['avec']

acc.push([
rule.dottedName,
// If the rule only contained the 'nom' attribute, we don't want to
// keep an empty object in the raw node.
Object.keys(rawNode).length === 0 ? null : rawNode,
])
return acc
}, []),
) as RawRules
}

function consumeMsg(_: string): void {}

export const disabledLogger: Logger = {
Expand All @@ -115,7 +81,7 @@ export const disabledLogger: Logger = {
*
* @returns The references.
*/
export function getAllRefsInNode(node: RuleNode): RuleName[] {
export function getAllRefsInNode(node: ASTNode): RuleName[] {
return reduceAST<RuleName[]>(
(refs: RuleName[], node: ASTNode) => {
if (node === undefined) {
Expand Down
22 changes: 2 additions & 20 deletions source/compilation/resolveImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,6 @@ ${importedRule.description}`
}
}

/**
* @throws {Error} If the `nom` attribute is different from the `ruleNameToCheck`.
*/
function removeRawNodeNom(
rawNode: Rule,
ruleNameToCheck: string,
): Omit<Rule, 'nom'> {
const { nom, ...rest } = rawNode
if (nom !== ruleNameToCheck)
throw Error(
`Imported rule's publicode raw node "nom" attribute is different from the resolveImport script ruleName. Please investigate`,
)
return rest
}

function appearsMoreThanOnce(
rulesToImport: RuleToImport[],
ruleName: RuleName,
Expand Down Expand Up @@ -218,7 +203,7 @@ export function resolveImports(
let neededNamespaces = new Set<string>()
const resolvedRules = Object.entries(rules).reduce((acc, [name, value]) => {
if (name === IMPORT_KEYWORD) {
const importMacro: ImportMacro = value
const importMacro = value as ImportMacro
const engine = getEngine(filePath, importMacro, verbose)
const rulesToImport: RuleToImport[] =
importMacro['les règles']?.map(getRuleToImportInfos)
Expand Down Expand Up @@ -252,10 +237,7 @@ export function resolveImports(
utils
.ruleParents(ruleName)
.forEach((rule) => neededNamespaces.add(`${namespace} . ${rule}`))
return [
`${namespace} . ${ruleName}`,
removeRawNodeNom(ruleWithUpdatedDescription, ruleName),
]
return [`${namespace} . ${ruleName}`, ruleWithUpdatedDescription]
}

const ruleWithOverridenAttributes = { ...rule.rawNode, ...attrs }
Expand Down
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './commons'
export * from './serializeParsedRules'
Loading

0 comments on commit 6e36c59

Please sign in to comment.