Skip to content

Commit

Permalink
docs: Add marking comments in some files
Browse files Browse the repository at this point in the history
  • Loading branch information
NriotHrreion committed May 4, 2024
1 parent 9b5c8c8 commit 861c9c2
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 76 deletions.
39 changes: 22 additions & 17 deletions src/compiler/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export default class Compiler {
this.root = new RootToken([]);
}

public compile(): NumberSymbol {
var tokenized = this.tokenize();

if(tokenized && !this.hasError) return new Evaluator(tokenized, this.variables).evaluate().toString();
return "NaN";
}

public tokenize(): RootToken | void {

/**
Expand All @@ -69,6 +76,7 @@ export default class Compiler {
*
* Mountain of shit be like:
*/
// MARK: Layers

if(this.layer > 0) { // in bracket or function
if(Is.leftBracket(symbol) || Is.mathFunction(symbol)) this.layer++;
Expand Down Expand Up @@ -117,7 +125,7 @@ export default class Compiler {
* Main
*/

if(Is.number(symbol, this.isProgrammingMode) || (Is.variable(symbol) && this.raw[1] !== "=")) { // number
if(Is.number(symbol, this.isProgrammingMode) || (Is.variable(symbol) && this.raw[1] !== "=")) { // MARK: Number

// Variable and Constant
if((Is.variable(symbol) || Is.constant(symbol)) && !this.isProgrammingMode) {
Expand Down Expand Up @@ -149,11 +157,11 @@ export default class Compiler {
}
if(tempNumber !== "NaN") this.pushNumber(tempNumber);

} else if(Is.operator(symbol)) { // operator
} else if(Is.operator(symbol)) { // MARK: Operator

this.pushOperator(symbol as Operator, i === 0);

} else if(Is.leftBracket(symbol)) { // left bracket
} else if(Is.leftBracket(symbol)) { // MARK: Left Bracket

// Process something like `3(5-2)`
if(i !== 0 && !Is.operator(this.raw[i - 1])) {
Expand All @@ -162,7 +170,7 @@ export default class Compiler {

this.layer++;

} else if(Is.rightBracket(symbol)) { // right bracket
} else if(Is.rightBracket(symbol)) { // MARK: Right Bracket

if(this.sigmaI > -1 && this.sigmaN > -1) { // sum (sigma)
this.root.add(new SumToken(this.sigmaI, this.sigmaN, this.secondaryRaw, this.variables));
Expand Down Expand Up @@ -221,7 +229,7 @@ export default class Compiler {
this.pushOperator(Operator.MUL);
}

} else if(symbol === "|") { // absolute value
} else if(symbol === "|") { // MARK: Absolute Value

if(this.inAbs) {
var secondaryCompiler = new Compiler(this.secondaryRaw, this.variables, this.isProgrammingMode, this.numberSys);
Expand All @@ -244,7 +252,7 @@ export default class Compiler {
this.inAbs = true;
}

} else if(symbol.indexOf("\\Sigma") > -1) { // sum (sigma)
} else if(symbol.indexOf("\\Sigma") > -1) { // MARK: Sum (sigma)

if(i !== 0 && (Is.number(this.raw[i - 1], this.isProgrammingMode) || Is.constant(this.raw[i - 1]) || Is.variable(this.raw[i - 1]))) {
this.pushOperator(Operator.MUL);
Expand All @@ -256,7 +264,7 @@ export default class Compiler {

this.layer++;

} else if(symbol.indexOf("\\Pi") > -1) { // prod
} else if(symbol.indexOf("\\Pi") > -1) { // MARK: Product

if(i !== 0 && (Is.number(this.raw[i - 1], this.isProgrammingMode) || Is.constant(this.raw[i - 1]) || Is.variable(this.raw[i - 1]))) {
this.pushOperator(Operator.MUL);
Expand All @@ -268,7 +276,7 @@ export default class Compiler {

this.layer++;

} else if(symbol.indexOf("\\smallint") > -1) { // integral
} else if(symbol.indexOf("\\smallint") > -1) { // MARK: Integral

if(i !== 0 && (Is.number(this.raw[i - 1], this.isProgrammingMode) || Is.constant(this.raw[i - 1]) || Is.variable(this.raw[i - 1]))) {
this.pushOperator(Operator.MUL);
Expand All @@ -280,7 +288,7 @@ export default class Compiler {

this.layer++;

} else if(Is.mathFunction(symbol)) { // function
} else if(Is.mathFunction(symbol)) { // MARK: Function

// Process something like `2sin(pi/6)`
if(i !== 0 && !Is.operator(this.raw[i - 1])) {
Expand All @@ -296,15 +304,15 @@ export default class Compiler {
this.currentFunction = functionName ?? "";
this.layer++;

} else if(symbol[0] === "^") { // pow
} else if(symbol[0] === "^") { // MARK: Power

var exponential = parseInt(symbol[1]);
var poweredToken = this.root.getLastChild<NumberToken | VariableToken>();
poweredToken instanceof VariableToken
? poweredToken.exponential = exponential
: poweredToken.setValue(Compute.safePow(this.root.getLastChild().value, exponential));

} else if(symbol[0] === "!") { // factorial
} else if(symbol[0] === "!") { // MARK: Factorial

var factorialToken = this.root.getLastChild();
if(factorialToken instanceof VariableToken) {
Expand Down Expand Up @@ -341,12 +349,7 @@ export default class Compiler {
return this.root;
}

public compile(): NumberSymbol {
var tokenized = this.tokenize();

if(tokenized && !this.hasError) return new Evaluator(tokenized, this.variables).evaluate().toString();
return "NaN";
}
// MARK: Token Operations

private pushNumber(numString: string) {
this.root.add(NumberToken.create(numString, this.numberSys));
Expand Down Expand Up @@ -377,6 +380,8 @@ export default class Compiler {
return i + di < this.raw.length && this.raw[i + di][0] === "^";
}

// MARK: Utilities

/**
* To resolve some professional-only symbols
*/
Expand Down
8 changes: 5 additions & 3 deletions src/compiler/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class Evaluator {
// Read token to make number list & operator list
var [numbers, operators] = this.readToken();

// Logical Operator
// MARK: Logical Operator
for(let i = 0; i < operators.length; i++) {
var operator = operators.get(i).value;

Expand Down Expand Up @@ -77,7 +77,7 @@ export default class Evaluator {
}
}

// Multiply & Divide
// MARK: Multiply & Divide
var firstLoop = true;
for(let i = 0; i < operators.length; i++) {
var operator = operators.get(i).value;
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class Evaluator {
}
}

// Add & Sub
// MARK: Add & Sub
var firstLoop = true;
for(let i = 0; i < operators.length; i++) {
var operator = operators.get(i).value;
Expand Down Expand Up @@ -161,6 +161,8 @@ export default class Evaluator {
return Float.calibrate(parseFloat(result.toFixed(14)));
}

// MARK: Read Token

private readToken(): [List<NumberToken>, List<OperatorToken>] {
const root = RootToken.create(this.token);

Expand Down
4 changes: 4 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FunctionInfo, Shortcut, RollbackToward } from "@/types";
export const version = "1.5.1a";
export const errorText = "\\text{Error}";

// MARK: Function List
export const functions: Map<string, FunctionInfo> = new Map([
["sin", [(x) => Math.sin(x), 1]],
["cos", [(x) => Math.cos(x), 1]],
Expand Down Expand Up @@ -52,6 +53,7 @@ export const functions: Map<string, FunctionInfo> = new Map([
["W_0", [(x) => lambertW0(x), 1]],
]);

// MARK: Constant List
export const constants: Map<string, number> = new Map([
["\\pi", Math.PI],
["e", Math.E],
Expand All @@ -63,6 +65,7 @@ export const constants: Map<string, number> = new Map([
/**
* Auto Complete symbols Table (AC Table)
*/
// MARK: Auto Complete Table
export const acTable: Map<string, string> = new Map([
["alpha", "\\alpha"],
["beta", "\\beta"],
Expand Down Expand Up @@ -124,6 +127,7 @@ export const acTable: Map<string, string> = new Map([
["kun", "\\text{坤}"],
]);

// MARK: Shortcut List
export const shortcuts: Map<string[], Shortcut> = new Map([
[["ctrl", "x"], {
description: "清空输入框",
Expand Down
22 changes: 15 additions & 7 deletions src/renderer/Graphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export default class Graphics {
return { x, y };
}

// MARK: Axis

protected refreshAxisLine() {
this.config.getAxisType() === Axis.CARTESIAN
? this.drawCartesianAxis()
Expand Down Expand Up @@ -165,6 +167,8 @@ export default class Graphics {
}
}

// MARK: Basic Methods

protected drawLine(begin: Point, end: Point, color: string, width: number = 1) {
this.ctx.beginPath();
this.ctx.strokeStyle = color;
Expand Down Expand Up @@ -215,6 +219,8 @@ export default class Graphics {
this.canvas.height = this.canvas.height;
}

// MARK: Utilities

private getMaxDrawingRange(): number {
return Math.max(
Math.sqrt(this.center.x ** 2 + this.center.y ** 2),
Expand All @@ -237,13 +243,6 @@ export default class Graphics {
return this.createPoint(x, y);
}

// To render each frame
public render() {
this.clear();

this.refreshAxisLine();
}

public getTextWidth(text: string): number {
return this.ctx.measureText(text).width;
}
Expand All @@ -263,8 +262,17 @@ export default class Graphics {
y: this.center.y - (point.y * unitPx)
};
}

// To render each frame
// MARK: Render
public render() {
this.clear();

this.refreshAxisLine();
}
}

// MARK: Configuration
class GraphicsConfig {
private static readonly lightColors: ColorScheme = {
primary: "#404041",
Expand Down
Loading

1 comment on commit 861c9c2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.