Skip to content

Commit

Permalink
Fix wrong number system converation of negation numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
5anthosh committed Nov 14, 2019
1 parent 15f8448 commit 1c734a2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
42 changes: 21 additions & 21 deletions dist/fcal.js
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ var ASTPrinter = /** @class */ (function () {
this.depth += ASTPrinter.tab;
var value = this.evaluate(expr.value);
this.depth -= ASTPrinter.tab;
return ASTPrinter.createPrefix(this.depth, 'ASSIGN') + " \n|\n" + value;
return ASTPrinter.createPrefix(this.depth, 'ASSIGN') + " " + expr.name + " \n|\n" + value;
};
ASTPrinter.prototype.visitVariableExpr = function (expr) {
return ASTPrinter.createPrefix(this.depth, 'VARIABLE') + " " + expr.name + "\n|\n";
Expand Down Expand Up @@ -1663,33 +1663,16 @@ var Parser = /** @class */ (function () {
return expr;
};
Parser.prototype.multiply = function () {
var expr = this.unary();
while (this.match([token_1.TT.TIMES, token_1.TT.SLASH, token_1.TT.MOD, token_1.TT.OF])) {
var operator = this.previous();
var right = this.unary();
expr = new expr_1.Expr.Binary(expr, operator, right, expr.start, right.end);
}
return expr;
};
Parser.prototype.unary = function () {
if (this.match([token_1.TT.PLUS, token_1.TT.MINUS])) {
var operator = this.previous();
var right = this.unary();
return new expr_1.Expr.Unary(operator, right, operator.start, right.end);
}
return this.exponent();
};
Parser.prototype.exponent = function () {
var expr = this.unitConvert();
while (this.match([token_1.TT.CAP])) {
while (this.match([token_1.TT.TIMES, token_1.TT.SLASH, token_1.TT.MOD, token_1.TT.OF])) {
var operator = this.previous();
var right = this.unary();
var right = this.unitConvert();
expr = new expr_1.Expr.Binary(expr, operator, right, expr.start, right.end);
}
return expr;
};
Parser.prototype.unitConvert = function () {
var expr = this.suffix();
var expr = this.unary();
if (this.match([token_1.TT.IN])) {
if (this.match([token_1.TT.UNIT])) {
var unit = this.previous();
Expand All @@ -1709,6 +1692,23 @@ var Parser = /** @class */ (function () {
}
return expr;
};
Parser.prototype.unary = function () {
if (this.match([token_1.TT.PLUS, token_1.TT.MINUS])) {
var operator = this.previous();
var right = this.unary();
return new expr_1.Expr.Unary(operator, right, operator.start, right.end);
}
return this.exponent();
};
Parser.prototype.exponent = function () {
var expr = this.suffix();
while (this.match([token_1.TT.CAP])) {
var operator = this.previous();
var right = this.unary();
expr = new expr_1.Expr.Binary(expr, operator, right, expr.start, right.end);
}
return expr;
};
Parser.prototype.suffix = function () {
var expr = this.call();
if (this.match([token_1.TT.PERCENTAGE])) {
Expand Down
2 changes: 1 addition & 1 deletion dist/fcal.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/__test__/fcal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ test('Infinity', () => {
test('AST print()', () => {
const expr = new Fcal().expression('y = PI * radius cm ^ 2 + sinh(8) as cm + log(23) in hex + (--100)%');
expect(expr.getAST()).toStrictEqual(`\
+ (0)ASSIGN
+ (0)ASSIGN y
|
+---- (1)BINARY < + + (56, 57)>
|
Expand Down
2 changes: 1 addition & 1 deletion src/parser/astPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ASTPrinter implements Expr.IVisitor<string> {
this.depth += ASTPrinter.tab;
const value = this.evaluate(expr.value);
this.depth -= ASTPrinter.tab;
return `${ASTPrinter.createPrefix(this.depth, 'ASSIGN')} \n|\n${value}`;
return `${ASTPrinter.createPrefix(this.depth, 'ASSIGN')} ${expr.name} \n|\n${value}`;
}

public visitVariableExpr(expr: Expr.Variable): string {
Expand Down
44 changes: 22 additions & 22 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,17 @@ class Parser {
}

private multiply(): Expr {
let expr = this.unary();
let expr = this.unitConvert();
while (this.match([TT.TIMES, TT.SLASH, TT.MOD, TT.OF])) {
const operator = this.previous();
const right = this.unary();
expr = new Expr.Binary(expr, operator, right, expr.start, right.end);
}
return expr;
}

private unary(): Expr {
if (this.match([TT.PLUS, TT.MINUS])) {
const operator = this.previous();
const right = this.unary();
return new Expr.Unary(operator, right, operator.start, right.end);
}
return this.exponent();
}

private exponent(): Expr {
let expr = this.unitConvert();
while (this.match([TT.CAP])) {
const operator = this.previous();
const right = this.unary();
const right = this.unitConvert();
expr = new Expr.Binary(expr, operator, right, expr.start, right.end);
}
return expr;
}

private unitConvert(): Expr {
const expr = this.suffix();
const expr = this.unary();
if (this.match([TT.IN])) {
if (this.match([TT.UNIT])) {
const unit = this.previous();
Expand All @@ -116,6 +97,25 @@ class Parser {
return expr;
}

private unary(): Expr {
if (this.match([TT.PLUS, TT.MINUS])) {
const operator = this.previous();
const right = this.unary();
return new Expr.Unary(operator, right, operator.start, right.end);
}
return this.exponent();
}

private exponent(): Expr {
let expr = this.suffix();
while (this.match([TT.CAP])) {
const operator = this.previous();
const right = this.unary();
expr = new Expr.Binary(expr, operator, right, expr.start, right.end);
}
return expr;
}

private suffix(): Expr {
const expr = this.call();
if (this.match([TT.PERCENTAGE])) {
Expand Down

0 comments on commit 1c734a2

Please sign in to comment.