Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Feb 19, 2024
1 parent 8a511ad commit ebd98c1
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions ext/js/language/language-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export class LanguageTransformer {
const rules2 = [];
for (let j = 0, jj = rules.length; j < jj; ++j) {
const {suffixIn, suffixOut, conditionsIn, conditionsOut} = rules[j];
const conditionFlagsIn = this._getConditionFlags(conditionFlagsMap, conditionsIn);
const conditionFlagsIn = this._getConditionFlagsStrict(conditionFlagsMap, conditionsIn);
if (conditionFlagsIn === null) { throw new Error(`Invalid conditionsIn for transform[${i}].rules[${j}]`); }
const conditionFlagsOut = this._getConditionFlags(conditionFlagsMap, conditionsOut);
const conditionFlagsOut = this._getConditionFlagsStrict(conditionFlagsMap, conditionsOut);
if (conditionFlagsOut === null) { throw new Error(`Invalid conditionsOut for transform[${i}].rules[${j}]`); }
rules2.push({
suffixIn,
Expand Down Expand Up @@ -88,45 +88,27 @@ export class LanguageTransformer {
}

/**
* @param {string} conditionType
* @returns {number}
*/
getConditionFlagsFromConditionType(conditionType) {
const conditionFlags = this._conditionTypeToConditionFlagsMap.get(conditionType);
return typeof conditionFlags !== 'undefined' ? conditionFlags : 0;
}

/**
* @param {string} partOfSpeech
* @param {string[]} partsOfSpeech
* @returns {number}
*/
getConditionFlagsFromPartOfSpeech(partOfSpeech) {
const conditionFlags = this._partOfSpeechToConditionFlagsMap.get(partOfSpeech);
return typeof conditionFlags !== 'undefined' ? conditionFlags : 0;
getConditionFlagsFromPartsOfSpeech(partsOfSpeech) {
return this._getConditionFlags(this._partOfSpeechToConditionFlagsMap, partsOfSpeech);
}

/**
* @param {string[]} partsOfSpeech
* @param {string[]} conditionTypes
* @returns {number}
*/
getConditionFlagsFromPartsOfSpeech(partsOfSpeech) {
let result = 0;
for (const partOfSpeech of partsOfSpeech) {
result |= this.getConditionFlagsFromPartOfSpeech(partOfSpeech);
}
return result;
getConditionFlagsFromConditionTypes(conditionTypes) {
return this._getConditionFlags(this._conditionTypeToConditionFlagsMap, conditionTypes);
}

/**
* @param {string[]} conditionTypes
* @param {string} conditionType
* @returns {number}
*/
getConditionFlagsFromConditionTypes(conditionTypes) {
let result = 0;
for (const conditionType of conditionTypes) {
result |= this.getConditionFlagsFromConditionType(conditionType);
}
return result;
getConditionFlagsFromConditionType(conditionType) {
return this._getConditionFlags(this._conditionTypeToConditionFlagsMap, [conditionType]);
}

/**
Expand Down Expand Up @@ -182,7 +164,7 @@ export class LanguageTransformer {
flags = 1 << nextFlagIndex;
++nextFlagIndex;
} else {
const multiFlags = this._getConditionFlags(conditionFlagsMap, subConditions);
const multiFlags = this._getConditionFlagsStrict(conditionFlagsMap, subConditions);
if (multiFlags === null) {
nextTargets.push(target);
continue;
Expand All @@ -206,11 +188,30 @@ export class LanguageTransformer {
* @param {string[]} conditionTypes
* @returns {?number}
*/
_getConditionFlags(conditionFlagsMap, conditionTypes) {
_getConditionFlagsStrict(conditionFlagsMap, conditionTypes) {
let flags = 0;
for (const conditionType of conditionTypes) {
const flags2 = conditionFlagsMap.get(conditionType);
if (typeof flags2 === 'undefined') { return null; }
if (typeof flags2 === 'undefined') {
return null;
}
flags |= flags2;
}
return flags;
}

/**
* @param {Map<string, number>} conditionFlagsMap
* @param {string[]} conditionTypes
* @returns {number}
*/
_getConditionFlags(conditionFlagsMap, conditionTypes) {
let flags = 0;
for (const conditionType of conditionTypes) {
let flags2 = conditionFlagsMap.get(conditionType);
if (typeof flags2 === 'undefined') {
flags2 = 0;
}
flags |= flags2;
}
return flags;
Expand Down

0 comments on commit ebd98c1

Please sign in to comment.