Skip to content

Commit

Permalink
feat: implement scatter plot parser visitor
Browse files Browse the repository at this point in the history
Also, update scatter query types to include only necessary fields
  • Loading branch information
mgreminger committed Oct 26, 2023
1 parent 3bde584 commit e033292
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 36 deletions.
20 changes: 5 additions & 15 deletions public/dimensional_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class FunctionUnitsQuery(TypedDict):
isFunction: Literal[False]
isUnitsQuery: Literal[True]
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
index: int # added in Python, not pressent in json
Expand Down Expand Up @@ -202,7 +201,6 @@ class FunctionArgumentQuery(TypedDict):
isFunction: Literal[False]
isUnitsQuery: Literal[False]
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
index: int # added in Python, not pressent in json
Expand Down Expand Up @@ -255,13 +253,11 @@ class BaseQueryStatement(QueryAssignmentCommon):

class QueryStatement(BaseQueryStatement):
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]

class RangeQueryStatement(BaseQueryStatement):
isRange: Literal[True]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
cellNum: int
Expand All @@ -279,7 +275,6 @@ class RangeQueryStatement(BaseQueryStatement):
class ScatterXValuesQueryStatement(QueryAssignmentCommon):
type: Literal["query"]
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
isExponent: Literal[False]
Expand All @@ -298,7 +293,6 @@ class ScatterXValuesQueryStatement(QueryAssignmentCommon):
class ScatterYValuesQueryStatement(QueryAssignmentCommon):
type: Literal["query"]
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
isExponent: Literal[False]
Expand All @@ -314,28 +308,26 @@ class ScatterYValuesQueryStatement(QueryAssignmentCommon):
unitsLatex: str
dimensions: list[float]

class ScatterQueryStatement(BaseQueryStatement):
isRange: Literal[False]
isScatter: Literal[True]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
class ScatterQueryStatement(TypedDict):
type: Literal["scatterQuery"]
equationIndex: int
cellNum: int
xValuesQuery: ScatterXValuesQueryStatement
yValuesQuery: ScatterYValuesQueryStatement
xName: str
yName: str
units: str
unitsLatex: str
inputUnits: str
inputUnitsLatex: str

class CodeFunctionRawQuery(BaseQueryStatement):
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[True]

class CodeFunctionQueryStatement(BaseQueryStatement):
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[True]
isCodeFunctionRawQuery: Literal[False]
functionName: str
Expand All @@ -348,7 +340,6 @@ class CodeFunctionQueryStatement(BaseQueryStatement):
class EqualityUnitsQueryStatement(QueryAssignmentCommon):
type: Literal["query"]
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
isExponent: Literal[False]
Expand All @@ -367,7 +358,6 @@ class EqualityStatement(QueryAssignmentCommon):
isFunction: Literal[False]
isFromPlotCell: Literal[False]
isRange: Literal[False]
isScatter: Literal[False]
isCodeFunctionQuery: Literal[False]
isCodeFunctionRawQuery: Literal[False]
equationIndex: int
Expand Down
88 changes: 80 additions & 8 deletions src/parser/LatexToSympy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import type { FieldTypes, Statement, QueryStatement, RangeQueryStatement, UserFu
EqualityUnitsQueryStatement, Insertion, Replacement,
SolveParameters, AssignmentList, ImmediateUpdate,
CodeFunctionQueryStatement, CodeFunctionRawQuery,
ScatterQueryStatement } from "./types";
ScatterQueryStatement,
ScatterXValuesQueryStatement, ScatterYValuesQueryStatement} from "./types";
import { RESERVED, GREEK_CHARS, UNASSIGNABLE, COMPARISON_MAP,
UNITS_WITH_OFFSET, TYPE_PARSING_ERRORS, BUILTIN_FUNCTION_MAP } from "./constants.js";
import type {
Expand Down Expand Up @@ -484,7 +485,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
isFromPlotCell: this.type === "plot",
sympy: sympy,
isRange: false,
isScatter: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false
};
Expand Down Expand Up @@ -560,7 +560,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
isFromPlotCell: false,
sympy: codeFunction.sympy,
isRange: false,
isScatter: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: true
};
Expand All @@ -581,6 +580,84 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
return finalQuery;
}

visitScatter_plot_query = (ctx: Scatter_plot_queryContext): ScatterQueryStatement => {

const xName = this.visitId(ctx.id(0));
const yName = this.visitId(ctx.id(1));

const xValuesQuery: ScatterXValuesQueryStatement = {
type: "query",
equationIndex: this.equationIndex,
exponents: [],
implicitParams: [],
params: [xName],
functions: [],
arguments: [],
localSubs: [],
units: "",
unitsLatex: "",
isExponent: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
isEqualityUnitsQuery: false,
isScatterXValuesQueryStatement: true,
isScatterYValuesQueryStatement: false,
isFromPlotCell: this.type === "plot",
sympy: xName,
isRange: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false
};

const yValuesQuery: ScatterYValuesQueryStatement = {
type: "query",
equationIndex: this.equationIndex,
exponents: [],
implicitParams: [],
params: [yName],
functions: [],
arguments: [],
localSubs: [],
units: "",
unitsLatex: "",
isExponent: false,
isFunctionArgument: false,
isFunction: false,
isUnitsQuery: false,
isEqualityUnitsQuery: false,
isScatterXValuesQueryStatement: false,
isScatterYValuesQueryStatement: true,
isFromPlotCell: this.type === "plot",
sympy: yName,
isRange: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false
};

let inputUnits: UnitBlockData;
let outputUnits: UnitBlockData;

if (ctx.u_block(0)) {
inputUnits = this.visitU_block(ctx.u_block(0));
outputUnits = this.visitU_block(ctx.u_block(1));
}

return {
type: "scatterQuery",
equationIndex: this.equationIndex,
cellNum: -1,
xValuesQuery: xValuesQuery,
yValuesQuery: yValuesQuery,
xName: xName,
yName: yName,
units: outputUnits.units,
unitsLatex: outputUnits.unitsLatex,
inputUnits: inputUnits.units,
inputUnitsLatex: inputUnits.unitsLatex,
};
}

visitAssign = (ctx: AssignContext): AssignmentStatement | ErrorStatement | EqualityStatement => {
if (!ctx.id()) {
//user is trying to assign to pi
Expand Down Expand Up @@ -663,7 +740,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
isFromPlotCell: false,
sympy: assignment.name,
isRange: false,
isScatter: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false,
assignment: assignment
Expand Down Expand Up @@ -721,7 +797,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
implicitParams: [], // params covered by equality statement below
params: this.params,
isRange: false,
isScatter: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false
}
Expand Down Expand Up @@ -907,7 +982,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
type: "query",
isUnitsQuery: false,
isRange: false,
isScatter: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false
}
Expand All @@ -917,7 +991,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
type: "query",
isUnitsQuery: false,
isRange: false,
isScatter: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false
};
Expand Down Expand Up @@ -1063,7 +1136,6 @@ export class LatexToSympy extends LatexParserVisitor<string | Statement | UnitBl
isFunctionArgument: false,
isFunction: false,
isRange: false,
isScatter: false,
isCodeFunctionQuery: false,
isCodeFunctionRawQuery: false,
units: '',
Expand Down
20 changes: 7 additions & 13 deletions src/parser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ type BaseQueryStatement = {

export type QueryStatement = BaseQueryStatement & {
isRange: false;
isScatter: false;
isCodeFunctionQuery: false;
isCodeFunctionRawQuery: false;
};
Expand All @@ -191,7 +190,6 @@ export type EqualityUnitsQueryStatement = Omit<QueryStatement, "unitsLatex" | "d

export type RangeQueryStatement = BaseQueryStatement & {
isRange: true;
isScatter: false;
isCodeFunctionQuery: false;
isCodeFunctionRawQuery: false;
cellNum: number;
Expand All @@ -207,33 +205,32 @@ export type RangeQueryStatement = BaseQueryStatement & {
outputName: string;
};

export type ScatterXValuesQueryStatement = QueryStatement & {
export type ScatterXValuesQueryStatement = Omit<QueryStatement, "isScatterXValuesQueryStatement"> & {
isScatterXValuesQueryStatement: true;
equationIndex: number;
}

export type ScatterYValuesQueryStatement = QueryStatement & {
export type ScatterYValuesQueryStatement = Omit<QueryStatement, "isScatterYValuesQueryStatement"> & {
isScatterYValuesQueryStatement: true;
equationIndex: number;
}

export type ScatterQueryStatement = BaseQueryStatement & {
isRange: false;
isScatter: true;
isCodeFunctionQuery: false;
isCodeFunctionRawQuery: false;
export type ScatterQueryStatement = {
type: "scatterQuery"
equationIndex: number;
cellNum: number;
xValuesQuery: ScatterXValuesQueryStatement;
yValuesQuery: ScatterYValuesQueryStatement;
xName: string;
yName: string;
units: string;
unitsLatex: string;
inputUnits: string;
inputUnitsLatex: string;
};

export type CodeFunctionQueryStatement = BaseQueryStatement & {
isRange: false;
isScatter: false;
isCodeFunctionQuery: true;
isCodeFunctionRawQuery: false;
functionName: string;
Expand All @@ -246,7 +243,6 @@ export type CodeFunctionQueryStatement = BaseQueryStatement & {

export type CodeFunctionRawQuery = BaseQueryStatement & {
isRange: false;
isScatter: false;
isCodeFunctionQuery: false;
isCodeFunctionRawQuery: true;
}
Expand All @@ -258,7 +254,6 @@ export type FunctionArgumentQuery = Pick<BaseQueryStatement, "type" | "sympy" |
isFunction: false;
isUnitsQuery: false;
isRange: false;
isScatter: false;
isCodeFunctionQuery: false;
isCodeFunctionRawQuery: false;
};
Expand All @@ -270,7 +265,6 @@ export type FunctionUnitsQuery = Pick<BaseQueryStatement, "type" | "sympy" | "pa
isFunction: false;
isUnitsQuery: true;
isRange: false;
isScatter: false;
isCodeFunctionQuery: false;
isCodeFunctionRawQuery: false;
};
Expand Down

0 comments on commit e033292

Please sign in to comment.