Skip to content

Commit

Permalink
V1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
qti3e committed Jun 29, 2017
1 parent 2249cb3 commit 2c4d7cc
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 35 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Esy is a new JS preprocessor allows you to use custom block structures.
To run tests just run:
```bash
git clone https://github.com/Slye-team/esy-language.git
cd esy-language/tests
cd esy-language
mkdir tmp
cd tests
node test.js
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esy-language",
"version": "1.0.2",
"version": "1.1.0",
"description": "Esy is a new JS preprocessor allows you to use custom block structures.",
"main": "src/index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/core/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ function find(code){
var j = 0;
matches[j] = matches[j].trim();
var len = matches[j].length;
if(isClosed(matches[j])) {
var ll = code[code.length - len - 1];
if(isClosed(matches[j]) && ll !== '.') {
results.push(len);
}
}
Expand Down
21 changes: 17 additions & 4 deletions src/libs/tree/OLB.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function OLB2(code){
isAfter = false,
opens = -1,
closed = -1,
isEnd = false;
isEnd = false,
fOB ;
for(; offset < code.length;offset++){
if(!isEnd)
re += code[offset];
Expand All @@ -60,7 +61,7 @@ function OLB2(code){

var h = code.substr(offset);
if(h[0] == ';')
h = h.substr(1)
h = h.substr(1);
if(h.startsWithA(OLBs)){
var c = OLB2(h);
var io = 0,
Expand All @@ -74,7 +75,7 @@ function OLB2(code){
io++;
}
if(c.substr(io, 4) == 'else'){
var fOB = ob;
fOB = ob;
while(ob !== cb || ob == fOB){
if(c[io] == '{')
ob++;
Expand All @@ -88,10 +89,18 @@ function OLB2(code){
break;
}else {
re += '{';
var ob = 0,
cb = 0;
do{
if(code[offset] == '{')
ob++;
if(code[offset] == '}')
cb++;
re += code[offset];
offset++;
}while(code[offset] !== ';');
}while(code[offset] !== ';' && (code[offset] !== '}' || ob != cb));
if(code[offset] == '}')
re += '}';
re += '}';
}
isEnd = false;
Expand Down Expand Up @@ -127,6 +136,10 @@ function OLB2(code){
j++
}
// j = OLBs[s].length
var nc = code[offset + j];
if(nc >= 'A' && nc <= 'z'){
p = false;
}
if(p){
var l = 1;
while(l < j){
Expand Down
22 changes: 0 additions & 22 deletions src/libs/tree/arrow_function.js

This file was deleted.

8 changes: 5 additions & 3 deletions src/libs/tree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {find} = require('../../core/blocks');
const comments = require('./comments');
const spaces = require('./spaces');
const quotations= require('./quotations');
const AF = require('./arrow_function');
const OLB = require('./OLB');
const K2B = require('./keywords2block');
const EsyError = require('../errors/esy_error');
Expand All @@ -30,7 +29,6 @@ function prepare_code(code) {
code = quotations.encode(code);
code = comments.encode(code);
code = spaces(code);
code = AF(code);
code = OLB(code);
return code;
})
Expand Down Expand Up @@ -103,10 +101,14 @@ function Tree(code, first_call = true){
insert();
preCode = '';
if(head == ''){
if(['var', 'const', 'let', 'import'].indexOf(re[re.length - 1]) == -1)
re.push('');
else
re[re.length - 1] += ' ';
// This is an object
if(body.length > 0) {
if(typeof body[0] == 'string')
body[0] = '{' + body[0];
body[0] = '\{' + body[0];
else
body = ['{'].concat(body);

Expand Down
135 changes: 135 additions & 0 deletions tests/examples/es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* _____ __
* / ___// /_ _____
* \__ \/ / / / / _ \
* ___/ / / /_/ / __/
* /____/_/\__, /\___/
* /____/
* Copyright 2017 Slye Development Team. All Rights Reserved.
* Licence: MIT License
*/

const {compare} = require('../tools');

var codes = [
// test 1
'const PI = 3.141593',

// test 2
`re = []
function foo () { return 1 }
re.push(foo() === 1)
{
function foo () { return 2 }
re.push(foo() === 2)
}
re.push(foo() === 1)`,

// test 3
`evens = [2,4,6,8,10,12,14,20]
odds = evens.map(v => v + 1)
pairs = evens.map(v => ({ even: v, odd: v + 1 }))
nums = evens.map((v, i) => v + i)`,

// test 4
`nums = [13,35,53,56,90,20,25,32+3]
fives = []
nums.forEach(v => {
if (v % 5 === 0)
fives.push(v)
})`,

// test 5
`function f (x, y = 7, z = 42) {
return x + y + z
}
re = f(1) === 50`,

// test 6
`function f (x, y, ...a) {
return (x + y) * a.length
}
re = f(1, 2, "hello", true, 7) === 9`,

// test 7
`var params = [ "hello", true, 7 ]
var other = [ 1, 2, ...params ] // [ 1, 2, "hello", true, 7 ]
function f (x, y, ...a) {
return (x + y) * a.length
}
var re = f(1, 2, ...params) === 9
var str = "foo"
var chars = [ ...str ] // [ "f", "o", "o" ]`,

// test 8
`var customer = { name: "Foo" }
var card = { amount: 7, product: "Bar", unitprice: 42 }
var message = \`Hello \${customer.name},
want to buy \${card.amount} \${card.product} for
a total of \${card.amount * card.unitprice} bucks?\``,

// test 9
`0b111110111 === 503
0o767 === 503`,

// test 10
`"𠮷".length === 2
"𠮷".match(/./u)[0].length === 2
a = "𠮷" === "\uD842\uDFB7"
b = "𠮷" === "\u{20BB7}"
c = "𠮷".codePointAt(0) == 0x20BB7
for (let codepoint of "𠮷") console.log(codepoint)`,

// test 11
`x = 5, y = 8
obj = { x, y }`,

// test 12
`function quux(){return 10};
let obj = {
foo: "bar",
[ "baz" + quux() ]: 42
}`,

// test 13
`var list = [ 1, 2, 3 ]
var [ a, , b ] = list
[ b, a ] = [ a, b ]`,

// test 14
`function getASTNode(){
return {
op: 5,
lhs: {
op: 8
},
rhs: 7
}
}
var { op, lhs, rhs } = getASTNode()`,

// test 15
`function getASTNode(){
return {
op: 5,
lhs: {
op: 8
},
rhs: 7
}
}
var { op: a, lhs: { op: b }, rhs: c } = getASTNode()`,
];

for(let i in codes){
let code = codes[i];
exports['$test' + (parseInt(i) + 1)] = function(assert){
compare(code).then(re => {
assert(re)
}, () => {
assert(false);
})
};
}
10 changes: 7 additions & 3 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var queue = [],
i = 0,
names = [],
failed = 0,
passed = 0;
passed = 0,
failed_n= [];
function test() {
if(i == queue.length) {
_console.log('_'.repeat(59).gray);
Expand All @@ -43,8 +44,10 @@ function test() {
_console.log(' '.repeat(Math.floor((59 - m.length) / 2)), m.green);
process.exit();
}else {
m = `${xmark} ${failed} test${failed == 1 ? '' : 's'} failed.`;
_console.log(' '.repeat(Math.floor((59 - m.length) / 2)), m.red);
m = `${xmark} Failed at ${failed} test${failed == 1 ? '' : 's'}:`;
_console.log(m.red);
var j = 0, l = failed_n.length.toString().length;
_console.log((' ' + failed_n.map(n => (j++, '0'.repeat(l - j.toString().length) + j + '- ' + n)).join('\n ')).red);
process.exit(1);
}
return;
Expand All @@ -70,6 +73,7 @@ function test() {
_console.log(m + `${checkmark} passed`.cyan)
}else {
failed++;
failed_n.push(name);
_console.log(m + `${xmark} failed${timeout ? ' (timeout)' : ''}`.red)
}
i++;
Expand Down

0 comments on commit 2c4d7cc

Please sign in to comment.