-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathswisscalc.lib.operator.js
37 lines (33 loc) · 1.48 KB
/
swisscalc.lib.operator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// Eric Morgan
// Copyright (c) 2014.
//
// Generic operator class. Implementation of individual operators will be defined in the OperatorCache.
// "evaluate" should be a function that takes a swisscalc.lib.shuntingYard instance as a parameter and returns the result.
var swisscalc = swisscalc || {};
swisscalc.lib = swisscalc.lib || {};
swisscalc.lib.operator = function(arity, associativity, precedence, numOperands, isOpenParen, isCloseParen, evaluate) {
this.Arity = arity;
this.Associativity = associativity;
this.Precedence = precedence;
this.NumOperands = numOperands;
this.IsOpenParen = isOpenParen;
this.IsCloseParen = isCloseParen;
this.evaluate = evaluate;
};
// Constants
swisscalc.lib.operator.ARITY_NULLARY = 0;
swisscalc.lib.operator.ARITY_UNARY = 1;
swisscalc.lib.operator.ARITY_BINARY = 2;
swisscalc.lib.operator.ASSOCIATIVITY_NONE = 0;
swisscalc.lib.operator.ASSOCIATIVITY_RIGHT = 1;
swisscalc.lib.operator.ASSOCIATIVITY_LEFT = 2;
// Static functions
swisscalc.lib.operator.degreesToRadians = function(degrees) { return degrees * (Math.PI / 180.0); };
swisscalc.lib.operator.radiansToDegrees = function(radians) { return radians * (180.0 / Math.PI); };
// Returns true if precedence is higher than given operator
swisscalc.lib.operator.prototype.isHigherPrecedence = function(operator) {
if (this.Precedence == operator.Precedence)
return (this.Associativity == swisscalc.lib.operator.ASSOCIATIVITY_LEFT);
return (this.Precedence > operator.Precedence);
};