Skip to content

Commit

Permalink
fix [operator]: defined various logical operators
Browse files Browse the repository at this point in the history
~ renamed '^' to '**'
~ renamed 'xor' to '^'
+ added '&&'
+ added '||'

TODO: short circuit behaviour
  • Loading branch information
ConorOBrien-Foxx committed Jul 14, 2016
1 parent 61491e1 commit 0da208d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
28 changes: 26 additions & 2 deletions src/interpreter/core/env/defaults.es6
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,36 @@ export const DEFAULT_OP = new Map([
);
else
return CheddarError.NO_OP_BEHAVIOR;
}]
}],

// TODO: short-circuiting
['&&', (LHS, RHS) => {
let bool = require("../primitives/Bool");
if (LHS && RHS)
return HelperInit(
bool,
HelperInit(bool, LHS).value && HelperInit(bool, RHS).value
);
else
return CheddarError.NO_OP_BEHAVIOR;
}],

// TODO: short-circuiting
['||', (LHS, RHS) => {
let bool = require("../primitives/Bool");
if (LHS && RHS)
return HelperInit(
bool,
HelperInit(bool, LHS).value || HelperInit(bool, RHS).value
);
else
return CheddarError.NO_OP_BEHAVIOR;
}],

]);

export const DEFAULT_CAST = new Map([
['Bool', (self) => {
self
}]
]);
]);
6 changes: 3 additions & 3 deletions src/interpreter/core/primitives/op/number.es6
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import HelperInit from '../../../../helpers/init';
// from Cyoce the almighty platypus, modified quite heavily.
// http://chat.stackexchange.com/transcript/message/27392766#27392766
const range = (a, b) => {
let CheddarNumber = require('../Number');
let CheddarNumber = require('../Number');
let out = [];
let i = 0;
if(b < a){
Expand Down Expand Up @@ -77,7 +77,7 @@ export default new Map([

}],

['^', (LHS, RHS) => {
['**', (LHS, RHS) => {
if (RHS instanceof LHS.constructor)
return HelperInit(LHS.constructor, 10, 0, Math.pow(LHS.value, RHS.value));
else
Expand Down Expand Up @@ -142,7 +142,7 @@ export default new Map([
return CheddarError.NO_OP_BEHAVIOR;
}],

['xor', (LHS, RHS) => {
['^', (LHS, RHS) => {
if (RHS instanceof LHS.constructor)
return HelperInit(LHS.constructor, 10, 0, LHS.value ^ RHS.value);
else
Expand Down
6 changes: 4 additions & 2 deletions src/tokenizer/consts/ops.es6
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const RESERVED_KEYWORDS = new Set([
export const EXCLUDE_META_ASSIGNMENT = new Set(['==', '!=', '<=', '>=' ]);

export const OP = [
'^', '*', '/', '%', '+', '-', '<=', '>=', '<', '>', '==', '&', '|',
'**', '*', '/', '%', '+', '-', '<=', '>=', '<', '>', '==',
'&', '|', '^',
'&&', '||',
'!=', '=', '+=', '-=', '*=', '/=', '^=', '%=', '&=', '|=', '<<', '>>', '<<=', '>>=',
'|>', '::',
'@"', 'has',
Expand Down Expand Up @@ -119,7 +121,7 @@ export const PRECEDENCE = new Map([
]);

export const RA_PRECEDENCE = new Map([
['^', 14000],
['**', 14000],
['=', 1000]
]);

Expand Down
12 changes: 6 additions & 6 deletions test/tests/primitives/op/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ describe('Numbers', function(){
})
describe('powers', function(){
it('should work', TestCheddarFrom.Code(
'print "2^2=" + String::(2^2)',
'2^2=4'
'print "2**2=" + String::(2**2)',
'2**2=4'
))
// it('should break', TestCheddarFrom.Code(
// 'print String::(^)'
// 'print String::(**)'
// ))
})
describe('modulo', function(){
Expand Down Expand Up @@ -143,11 +143,11 @@ describe('Numbers', function(){

describe('xor', function(){
it('should work', TestCheddarFrom.Code(
'print "4 xor 3=" + String::(4 xor 3)',
'4 xor 3=' + (4 ^ 3)
'print "4^3=" + String::(4^3)',
'4^3=' + (4 ^ 3)
))
// it('should break', TestCheddarFrom.Code(
// 'print String::(|)'
// 'print String::(^)'
// ))
})

Expand Down
2 changes: 1 addition & 1 deletion test/tests/primitives/op/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ describe('Numbers', function() {
'[102, 111, 111]'
))
})
});
});

0 comments on commit 0da208d

Please sign in to comment.