Skip to content

Commit

Permalink
refactor: remove unused unitless subquery code from javascript code
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreminger committed Jan 7, 2025
1 parent 2f053c2 commit 9d69544
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 70 deletions.
51 changes: 2 additions & 49 deletions src/parser/LatexToSympy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import LatexParserVisitor from "./LatexParserVisitor";
import type { FieldTypes, Statement, QueryStatement, RangeQueryStatement, UserFunctionRange,
AssignmentStatement, ImplicitParameter, UserFunction, FunctionArgumentQuery,
FunctionArgumentAssignment, LocalSubstitution, LocalSubstitutionRange,
UnitlessSubExpression, GuessAssignmentStatement, FunctionUnitsQuery,
GuessAssignmentStatement, FunctionUnitsQuery,
SolveParametersWithGuesses, ErrorStatement, EqualityStatement,
EqualityUnitsQueryStatement,
SolveParameters, AssignmentList, InsertMatrix,
Expand Down Expand Up @@ -63,7 +63,7 @@ type ParsingResult = {
}

export function getBlankStatement(): BlankStatement {
return { type: "blank", params: [], implicitParams: [], unitlessSubExpressions: [], isFromPlotCell: false};
return { type: "blank", params: [], implicitParams: [], isFromPlotCell: false};
}

export function parseLatex(latex: string, id: number, type: FieldTypes,
Expand Down Expand Up @@ -181,14 +181,11 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

dimNeedsValuesIndex = 0;

unitlessSubExpressionIndex = 0;
unitlessSubExpressionPrefix = "unitless__";
implicitParams: ImplicitParameter[] = [];

params: string[] = [];
parsingError = false;
private parsingErrorMessages = new Set<string>();
unitlessSubExpressions: UnitlessSubExpression[] = [];
subQueries: SubQueryStatement[] = [];
subQueryReplacements: [string, Replacement][] = [];
inQueryStatement = false;
Expand Down Expand Up @@ -270,11 +267,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
return `${this.paramPrefix}${this.equationIndex}_${this.paramIndex++}`;
}

getNextUnitlessSubExpressionName() {
return `${this.unitlessSubExpressionPrefix}${this.equationIndex}_${this
.unitlessSubExpressionIndex++}`;
}

getNextFunctionName() {
return `${this.functionPrefix}${this.equationIndex}_${this
.functionIndex++}`;
Expand Down Expand Up @@ -402,11 +394,9 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
sympy: sympyExpression,
implicitParams: this.implicitParams,
params: this.params,
unitlessSubExpressions: this.unitlessSubExpressions,
functions: this.functions,
arguments: this.arguments,
localSubs: this.localSubs,
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isFromPlotCell: false,
Expand Down Expand Up @@ -599,15 +589,13 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

const initialQuery: QueryStatement = {
type: "query",
unitlessSubExpressions: this.unitlessSubExpressions,
implicitParams: this.implicitParams,
params: this.params,
functions: this.functions,
arguments: this.arguments,
localSubs: this.localSubs,
units: units,
unitsLatex: unitsLatex,
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
Expand Down Expand Up @@ -683,15 +671,13 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

const codeFunctionRawQuery: CodeFunctionRawQuery = {
type: "query",
unitlessSubExpressions: [],
implicitParams: [],
params: [codeFunctionName,],
functions: [],
arguments: [],
localSubs: [],
units: units,
unitsLatex: unitsLatex,
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
Expand Down Expand Up @@ -738,7 +724,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

let implicitParamsCursor = this.implicitParams.length;
let paramsCursor = this.params.length;
let exponentsCursor = this.unitlessSubExpressions.length;
let functionsCursor = this.functions.length;
let argumentsCursor = this.arguments.length;
let localSubsCursor = this.localSubs.length;
Expand All @@ -754,15 +739,13 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
const xValuesQuery: ScatterXValuesQueryStatement = {
type: "query",
equationIndex: this.equationIndex,
unitlessSubExpressions: this.unitlessSubExpressions.slice(exponentsCursor),
implicitParams: this.implicitParams.slice(implicitParamsCursor),
params: this.params.slice(paramsCursor),
functions: this.functions.slice(functionsCursor),
arguments: this.arguments.slice(argumentsCursor),
localSubs: this.localSubs.slice(localSubsCursor),
units: "",
unitsLatex: "",
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
Expand All @@ -780,7 +763,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

implicitParamsCursor = this.implicitParams.length;
paramsCursor = this.params.length;
exponentsCursor = this.unitlessSubExpressions.length;
functionsCursor = this.functions.length;
argumentsCursor = this.arguments.length;
localSubsCursor = this.localSubs.length;
Expand All @@ -796,15 +778,13 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
const yValuesQuery: ScatterYValuesQueryStatement = {
type: "query",
equationIndex: this.equationIndex,
unitlessSubExpressions: this.unitlessSubExpressions.slice(exponentsCursor),
implicitParams: this.implicitParams.slice(implicitParamsCursor),
params: this.params.slice(paramsCursor),
functions: this.functions.slice(functionsCursor),
arguments: this.arguments.slice(argumentsCursor),
localSubs: this.localSubs.slice(localSubsCursor),
units: "",
unitsLatex: "",
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
Expand Down Expand Up @@ -840,7 +820,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
arguments: this.arguments,
localSubs: this.localSubs,
implicitParams: this.implicitParams,
unitlessSubExpressions: this.unitlessSubExpressions,
equationIndex: this.equationIndex,
cellNum: -1,
isFromPlotCell: this.type === "plot",
Expand Down Expand Up @@ -869,7 +848,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

const implicitParamsCursor = this.implicitParams.length;
const paramsCursor = this.params.length;
const exponentsCursor = this.unitlessSubExpressions.length;
const functionsCursor = this.functions.length;
const argumentsCursor = this.arguments.length;
const localSubsCursor = this.localSubs.length;
Expand Down Expand Up @@ -901,11 +879,9 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
sympy: sympyExpression,
implicitParams: this.implicitParams.slice(implicitParamsCursor),
params: this.params.slice(paramsCursor),
unitlessSubExpressions: this.unitlessSubExpressions.slice(exponentsCursor),
functions: this.functions.slice(functionsCursor),
arguments: this.arguments.slice(argumentsCursor),
localSubs: this.localSubs.slice(localSubsCursor),
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isFromPlotCell: false,
Expand Down Expand Up @@ -945,15 +921,13 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
if (this.type === "data_table_expression") {
return {
type: "query",
unitlessSubExpressions: [],
implicitParams: [],
params: [assignment.name],
functions: [],
arguments: [],
localSubs: [],
units: units,
unitsLatex: unitsLatex,
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
Expand All @@ -974,15 +948,13 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
} else {
return {
type: "query",
unitlessSubExpressions: [],
implicitParams: [],
params: [assignment.name],
functions: [],
arguments: [],
localSubs: [],
units: units,
unitsLatex: unitsLatex,
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
Expand Down Expand Up @@ -1036,7 +1008,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl

const rhsUnitsQuery: EqualityUnitsQueryStatement = {
type: "query",
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
Expand All @@ -1047,7 +1018,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
isFromPlotCell: false,
isSubQuery: false,
sympy: rhs,
unitlessSubExpressions: this.unitlessSubExpressions,
functions: this.functions,
arguments: this.arguments,
localSubs: this.localSubs,
Expand All @@ -1068,11 +1038,9 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
sympy: `_Eq(${lhs},${rhs})`,
implicitParams: this.implicitParams,
params: this.params,
unitlessSubExpressions: this.unitlessSubExpressions,
functions: this.functions,
arguments: this.arguments,
localSubs: this.localSubs,
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
equationIndex: this.equationIndex,
Expand Down Expand Up @@ -1174,22 +1142,18 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
let inputUnitsParameter: ImplicitParameter;
let i = 0;
const initialParamCursor = this.params.length;
const initialExponentCursor = this.unitlessSubExpressions.length;
while (ctx.expr(i)) {
const argumentName = this.getNextArgumentName();
const paramCursor = this.params.length;
const exponentCursor = this.unitlessSubExpressions.length;
const expression = this.visit(ctx.expr(i)) as string;

newArguments.push({
type: "assignment",
name: argumentName,
sympy: expression,
params: this.params.slice(paramCursor),
isUnitlessSubExpression: false,
isFunctionArgument: true,
isFunction: false,
unitlessSubExpressions: this.unitlessSubExpressions.slice(exponentCursor)
});

newSubs.push({
Expand Down Expand Up @@ -1243,7 +1207,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
}

unitQueryArgument.params = this.params.slice(initialParamCursor);
unitQueryArgument.unitlessSubExpressions = this.unitlessSubExpressions.slice(initialExponentCursor);

this.arguments.push(unitQueryArgument);

Expand Down Expand Up @@ -1356,11 +1319,9 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
name: functionName,
sympy: `_function_id_wrapper(__unique_marker_${this.equationIndex}_${this.functionIndex},${variableName})`,
params: [variableName],
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: true,
isRange: false,
unitlessSubExpressions: [],
functionParameters: parameters
}
}
Expand All @@ -1373,10 +1334,8 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
name: functionName,
sympy: `_function_id_wrapper(__unique_marker_${this.equationIndex}_${this.functionIndex},${variableName})`,
params: [variableName],
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: true,
unitlessSubExpressions: [],
functionParameters: parameters,
isRange: true,
freeParameter: lowerLimitArg.parameter,
Expand All @@ -1394,20 +1353,16 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
name: currentFunction.unitsQueryFunction,
sympy: `_function_id_wrapper(__unique_marker_${this.equationIndex}_${this.functionIndex},${variableName})`,
params: [variableName],
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: true,
isRange: false,
unitlessSubExpressions: [],
functionParameters: parameters
};

const unitsQuery: FunctionUnitsQuery = {
type: "query",
sympy: unitsFunction.name,
params: [unitsFunction.name],
unitlessSubExpressions: [],
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isRange: false,
Expand Down Expand Up @@ -2192,11 +2147,9 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
sympy: sympyExpression,
implicitParams: this.implicitParams,
params: this.params,
unitlessSubExpressions: this.unitlessSubExpressions,
functions: this.functions,
arguments: this.arguments,
localSubs: this.localSubs,
isUnitlessSubExpression: false,
isFunctionArgument: false,
isFunction: false,
isFromPlotCell: false,
Expand Down
22 changes: 3 additions & 19 deletions src/parser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type BlankStatement = {
type: "blank";
params: [];
implicitParams: [];
unitlessSubExpressions: [];
isFromPlotCell: false;
};

Expand Down Expand Up @@ -87,10 +86,8 @@ type BaseAssignmentStatement = {
name: string;
sympy: string;
params: string[];
isUnitlessSubExpression: false;
isFunctionArgument: false;
isFunction: false;
unitlessSubExpressions: UnitlessSubExpression[];
};

export type UserFunction = Omit<BaseAssignmentStatement, "isFunction"> & {
Expand All @@ -110,17 +107,9 @@ export type UserFunctionRange = Omit<UserFunction, "isRange"> & {
};


export type UnitlessSubExpression = Omit<BaseAssignmentStatement, "isUnitlessSubExpression"> & {
isUnitlessSubExpression: true;
unitlessContext: string;
isFunctionArgument: false;
isFunction: false;
};

export type FunctionArgumentAssignment = Pick<BaseAssignmentStatement,
"type" | "name" | "sympy" |
"params" | "unitlessSubExpressions"> & {
isUnitlessSubExpression: false;
"params"> & {
isFunctionArgument: true;
isFunction: false;
};
Expand Down Expand Up @@ -168,13 +157,11 @@ export type EqualityStatement = Omit<AssignmentStatement, "type" | "name"> & {
type BaseQueryStatement = {
type: "query";
sympy: string;
unitlessSubExpressions: UnitlessSubExpression[];
implicitParams: ImplicitParameter[];
params: string[];
functions: (UserFunction | UserFunctionRange | FunctionUnitsQuery)[];
arguments: (FunctionArgumentAssignment | FunctionArgumentQuery) [];
localSubs: (LocalSubstitution | LocalSubstitutionRange)[];
isUnitlessSubExpression: false;
isFunctionArgument: false;
isFunction: false;
isUnitsQuery: false;
Expand Down Expand Up @@ -264,7 +251,6 @@ export type ScatterQueryStatement = {
arguments: (FunctionArgumentAssignment | FunctionArgumentQuery) [];
localSubs: (LocalSubstitution | LocalSubstitutionRange)[];
implicitParams: ImplicitParameter[];
unitlessSubExpressions: UnitlessSubExpression[];
equationIndex: number;
cellNum: number;
isFromPlotCell: boolean;
Expand Down Expand Up @@ -298,9 +284,8 @@ export type CodeFunctionRawQuery = BaseQueryStatement & {
isCodeFunctionRawQuery: true;
}

export type FunctionArgumentQuery = Pick<BaseQueryStatement, "type" | "sympy" | "params" | "unitlessSubExpressions" > & {
export type FunctionArgumentQuery = Pick<BaseQueryStatement, "type" | "sympy" | "params" > & {
name: string;
isUnitlessSubExpression: false;
isFunctionArgument: true;
isFunction: false;
isUnitsQuery: false;
Expand All @@ -310,9 +295,8 @@ export type FunctionArgumentQuery = Pick<BaseQueryStatement, "type" | "sympy" |
isCodeFunctionRawQuery: false;
};

export type FunctionUnitsQuery = Pick<BaseQueryStatement, "type" | "sympy" | "params" | "unitlessSubExpressions" > & {
export type FunctionUnitsQuery = Pick<BaseQueryStatement, "type" | "sympy" | "params" > & {
units: '';
isUnitlessSubExpression: false;
isFunctionArgument: false;
isFunction: false;
isUnitsQuery: true;
Expand Down
Loading

0 comments on commit 9d69544

Please sign in to comment.