Skip to content

Commit

Permalink
Merge pull request #61 from cheddar-lang/fix-float-ranges
Browse files Browse the repository at this point in the history
Fix float ranges
  • Loading branch information
vihanb committed Jul 14, 2016
2 parents e0fd2aa + 6c82e22 commit c3dad6a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
25 changes: 13 additions & 12 deletions src/interpreter/core/primitives/op/number.es6
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import CheddarBool from '../Bool';

import HelperInit from '../../../../helpers/init';

// from Cyoce the almighty platypus modified;
// from Cyoce the almighty platypus, modified quite heavily.
// http://chat.stackexchange.com/transcript/message/27392766#27392766
const range = (a, b) => {
let CheddarNumber = require('../Number');
if (a > b) return range(b, a).reverse();
b = ~~b;
var iPart = a - ~~a;
a = ~~a;
var out = Array(b - a + 1);
while (b + iPart >= a) out[b - a] = HelperInit(CheddarNumber, 10, 0, b-- + iPart);
if(iPart) out.pop();
let CheddarNumber = require('../Number');
let out = [];
let i = 0;
if(b < a){
while(a >= b) out[i++] = HelperInit(CheddarNumber, 10, 0, a--);
} else {
while(a <= b) out[i++] = HelperInit(CheddarNumber, 10, 0, a++);
}
return out;
};

Expand Down Expand Up @@ -164,10 +164,12 @@ export default new Map([
}],

// Special Operators
[':', (LHS, RHS) => {
['|>', (LHS, RHS) => {
let CheddarArray = require("../Array");
if (LHS && RHS instanceof LHS.constructor)
return HelperInit(CheddarArray, ...range(LHS.value, RHS.value));
else if(LHS === null)
return HelperInit(CheddarArray, ...range(0, RHS.value - 1));
else
return CheddarError.NO_OP_BEHAVIOR;
}],
Expand Down Expand Up @@ -261,8 +263,7 @@ export default new Map([

/*
TODO:
'&', '|', // will do later
'+=', '-=', '*=', '/=', '^=', '%=', '&=', '|=', '<<', '>>', '<<=', '>>=',
'and', 'or', 'xor',
*/
*/
5 changes: 3 additions & 2 deletions src/tokenizer/consts/ops.es6
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const EXCLUDE_META_ASSIGNMENT = new Set(['==', '!=', '<=', '>=' ]);
export const OP = [
'^', '*', '/', '%', '+', '-', '<=', '>=', '<', '>', '==', '&', '|',
'!=', '=', '+=', '-=', '*=', '/=', '^=', '%=', '&=', '|=', '<<', '>>', '<<=', '>>=',
':', '::',
'|>', '::',
'@"', 'has',
'and', 'or', 'xor',
'log', 'sign',
Expand All @@ -38,6 +38,7 @@ export const OP = [
export const UOP = [
'-', '+',
'!',
'|>',
'sqrt', 'cbrt',
'sin', 'cos', 'tan',
'acos', 'asin', 'atan',
Expand Down Expand Up @@ -126,4 +127,4 @@ export const TYPE = {
UNARY: Symbol('Unary Operator'),
LTR: Symbol('LTR Operator'),
RTL: Symbol('RTL Operator')
}
}
8 changes: 4 additions & 4 deletions test/tests/primitives/op/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,13 @@ describe('Numbers', function(){

describe('range', function(){
it('should work', TestCheddarFrom.Code(
'print "1:5=" + String::(1:5)',
'1:5=[1, 2, 3, 4, 5]'
'print "1|>5=" + String::(1|>5)',
'1|>5=[1, 2, 3, 4, 5]'
))

it('should work reversed', TestCheddarFrom.Code(
'print "5:1=" + String::(5:1)',
'5:1=[5, 4, 3, 2, 1]'
'print "5|>1=" + String::(5|>1)',
'5|>1=[5, 4, 3, 2, 1]'
))
})

Expand Down

0 comments on commit c3dad6a

Please sign in to comment.