Demo updated.
This commit is contained in:
parent
50ff6c272c
commit
ff64dec3f7
|
|
@ -1,6 +1,6 @@
|
|||

|
||||
|
||||
ColaScript is a language that compiles in JavaScript. This language is similar to Dart, CoffeeScript, Python and PHP, with some original ideas. Compiler based on [UglifyJS2](https://github.com/mishoo/UglifyJS2). In present time compiler in development. Play with language you can in `lib/index.html`.
|
||||
ColaScript is a language that compiles in JavaScript. This language is similar to Dart, CoffeeScript, Python and PHP, with some original ideas. Compiler based on [UglifyJS2](https://github.com/mishoo/UglifyJS2). In present time compiler in development. Play with language you can [here](http://develop.trigen.pro/cola/).
|
||||
|
||||
# to do:
|
||||
|
||||
|
|
@ -326,4 +326,4 @@ ColaScript is a language that compiles in JavaScript. This language is similar t
|
|||
### Statistic
|
||||
|
||||
- 34 feature ( without classes )
|
||||
- 23 done
|
||||
- 26 done
|
||||
|
|
|
|||
121
demo.cola
121
demo.cola
|
|
@ -1,51 +1,102 @@
|
|||
// `main` functions may binding to a onload event
|
||||
// Functions can defined without `function` keyword, with and without `type`
|
||||
main(){
|
||||
|
||||
// Unary operators
|
||||
// Two variants of `isset` operator. Which is better?
|
||||
bool _seted = true, _empty;
|
||||
console.log("`_seted` is", _seted?? ? "seted" : "empty");
|
||||
console.log("`_empty` is", isset _empty ? "seted" : "empty");
|
||||
|
||||
// `clone` operator
|
||||
Array _first = [584], _second = clone _first;
|
||||
_first.push(404);
|
||||
console.log("`_first`:",_first,"`_second`:",_second);
|
||||
|
||||
// Binary operators
|
||||
// `pow` operator
|
||||
console.log("5 ** 3 =", 5 ** 3);
|
||||
|
||||
// `modulo` operator
|
||||
console.log("10 %% 4 = ", 10 %% 4);
|
||||
|
||||
// `isset` conditional assigment
|
||||
int a, b = 4;
|
||||
console.log("`a` is", a, ", now `a` is", a ?= b);
|
||||
console.log("`a` is", a, ", now `a` still", a ?= 584);
|
||||
|
||||
// `isset` binary conditional
|
||||
int x, y = 5;
|
||||
console.log("`x` is", x, ", `y` is", y, ", x ? y = ", x ? y), x = 1;
|
||||
console.log("`x` is", x, ", `y` is", y, ", x ? y = ", x ? y);
|
||||
|
||||
// `is` operator
|
||||
console.log("'hello'", "hello" is String ? "is" : "isnt", "`String`");
|
||||
|
||||
// `isnt` operator
|
||||
console.log("'goodby'", "goodby" isnt Number ? "isnt" : "is", "`Number`");
|
||||
|
||||
// Boolean alternatives
|
||||
console.log("`yes` and `no`", yes is Boolean && no is Boolean ? "is" : "isnt", "`Boolean`");
|
||||
console.log("`on` and `off`", on is Boolean && off is Boolean ? "is" : "isnt", "`Boolean`");
|
||||
|
||||
// Strings
|
||||
// New quotes symbol, multi-lining and templating
|
||||
console.log(`
|
||||
|
||||
Hello!
|
||||
My name is ColaScript, and i know who you are }:->
|
||||
@{navigator.userAgent}
|
||||
User agent: @{navigator.userAgent}
|
||||
Vendor: {{navigator.vendor}}
|
||||
|
||||
`);
|
||||
|
||||
console.log("pow:", 5 ** 2, "; modulo:", 5 %% 3, ";");
|
||||
// Raw strings
|
||||
console.log(r"\n \r \t some@email.ru");
|
||||
|
||||
Number a = 3.14, b = 584, c;
|
||||
|
||||
a ?= b; console.log(a);
|
||||
a = undefined;
|
||||
a ?= b; console.log(a);
|
||||
|
||||
console.log(a = c ? b);
|
||||
c = 404;
|
||||
console.log(a = c ? b);
|
||||
|
||||
b = undefined;
|
||||
|
||||
Array x, y;
|
||||
x = [123];
|
||||
y = clone x;
|
||||
y[] = 321;
|
||||
console.log('original:',x,'cloned:',y, y[]);
|
||||
|
||||
y[0..1] = [1..10];
|
||||
console.log(y[0..4]);
|
||||
|
||||
y.forEach((val) => console.log(val));
|
||||
|
||||
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");
|
||||
|
||||
console.log(`is:`, location.href is String, `; isnt:`, 123 isnt Number, ";");
|
||||
|
||||
if(yes === true && on === true && no === false && off === false) console.log('Boolean alternatives');
|
||||
|
||||
console.log('Raw string:', r`@test \n \t \r`);
|
||||
// Multiline RegExp's
|
||||
RegExp isEmail = /
|
||||
([\w-\.]+)
|
||||
@
|
||||
((?:[\w]+\.)+)
|
||||
([a-zA-Z]{2,4})
|
||||
/, email = r'danon0404@gmail.com';
|
||||
console.log("@email is email:", isEmail.test(email));
|
||||
/;
|
||||
String email = r'some@email.ru';
|
||||
console.log("@email {{ isEmail.test(email) ? "is" : "isnt" }} valid email adress");
|
||||
|
||||
// Arrays
|
||||
// Empty getter and setter ( pushig )
|
||||
Array arr = [2, 4, 8, 16, 32];
|
||||
console.log("Last element of `arr` is", arr[]), arr[] = 64;
|
||||
console.log("Now last element of `arr` is", arr[]);
|
||||
|
||||
// Splice assigment
|
||||
arr[2..4] = [3, 2, 1];
|
||||
console.log("`arr` is", arr);
|
||||
|
||||
// Range
|
||||
console.log("`[0..10]` is", [0..10]);
|
||||
|
||||
// Calling function
|
||||
console.log(Profile(
|
||||
"Dan", "Green",
|
||||
"HTML5", "JavaScript", "Dart", "PHP", "CSS3", "LESS", "Qt",
|
||||
age : 19,
|
||||
petName : "Felix"
|
||||
));
|
||||
}
|
||||
|
||||
main();
|
||||
// Functions
|
||||
// Named, positional and splated arguments, with default values.
|
||||
// Arrow functions.
|
||||
Object Profile(String firstName, String secondName, String country = "Russia", Array skills..., age:, petName: "Tux"){
|
||||
skills.forEach((val) => val == "JavaScript" && console.log("JavaScript - It Awesome!"));
|
||||
return {
|
||||
firstName : firstName,
|
||||
secondName : secondName,
|
||||
age : age,
|
||||
country : country,
|
||||
skills : skills,
|
||||
petName : petName
|
||||
};
|
||||
}
|
||||
|
|
|
|||
146
lib/index.html
146
lib/index.html
|
|
@ -27,12 +27,14 @@
|
|||
}
|
||||
|
||||
textarea {
|
||||
-moz-box-sizing: border-box;
|
||||
box-sizing: border-box;
|
||||
width: 33.333%;
|
||||
height: 100%;
|
||||
font-size: 12px;
|
||||
float: left;
|
||||
font-family: "Andale Mono";
|
||||
padding: 0 5px 26px 5px;
|
||||
}
|
||||
|
||||
#controls {
|
||||
|
|
@ -61,60 +63,117 @@
|
|||
opacity: 0.5;
|
||||
}
|
||||
|
||||
#lenstat {
|
||||
float: right;
|
||||
margin: 5px 8px 0 0;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<textarea id="source" onkeyup="compile()">main(){
|
||||
<textarea id="source" onkeyup="compile()">// `main` functions may binding to a onload event
|
||||
// Functions can defined without `function` keyword, with and without `type`
|
||||
main(){
|
||||
|
||||
// Unary operators
|
||||
// Two variants of `isset` operator. Which is better?
|
||||
bool _seted = true, _empty;
|
||||
console.log("`_seted` is", _seted?? ? "seted" : "empty");
|
||||
console.log("`_empty` is", isset _empty ? "seted" : "empty");
|
||||
|
||||
// `clone` operator
|
||||
Array _first = [584], _second = clone _first;
|
||||
_first.push(404);
|
||||
console.log("`_first`:",_first,"`_second`:",_second);
|
||||
|
||||
// Binary operators
|
||||
// `pow` operator
|
||||
console.log("5 ** 3 =", 5 ** 3);
|
||||
|
||||
// `modulo` operator
|
||||
console.log("10 %% 4 = ", 10 %% 4);
|
||||
|
||||
// `isset` conditional assigment
|
||||
int a, b = 4;
|
||||
console.log("`a` is", a, ", now `a` is", a ?= b);
|
||||
console.log("`a` is", a, ", now `a` still", a ?= 584);
|
||||
|
||||
// `isset` binary conditional
|
||||
int x, y = 5;
|
||||
console.log("`x` is", x, ", `y` is", y, ", x ? y = ", x ? y), x = 1;
|
||||
console.log("`x` is", x, ", `y` is", y, ", x ? y = ", x ? y);
|
||||
|
||||
// `is` operator
|
||||
console.log("'hello'", "hello" is String ? "is" : "isnt", "`String`");
|
||||
|
||||
// `isnt` operator
|
||||
console.log("'goodby'", "goodby" isnt Number ? "isnt" : "is", "`Number`");
|
||||
|
||||
// Boolean alternatives
|
||||
console.log("`yes` and `no`", yes is Boolean && no is Boolean ? "is" : "isnt", "`Boolean`");
|
||||
console.log("`on` and `off`", on is Boolean && off is Boolean ? "is" : "isnt", "`Boolean`");
|
||||
|
||||
// Strings
|
||||
// New quotes symbol, multi-lining and templating
|
||||
console.log(`
|
||||
|
||||
Hello!
|
||||
My name is ColaScript, and i know who you are }:->
|
||||
@{navigator.userAgent}
|
||||
User agent: @{navigator.userAgent}
|
||||
Vendor: {{navigator.vendor}}
|
||||
|
||||
`);
|
||||
|
||||
console.log("pow:", 5 ** 2, "; modulo:", 5 %% 3, ";");
|
||||
// Raw strings
|
||||
console.log(r"\n \r \t some@email.ru");
|
||||
|
||||
Number a = 3.14, b = 584, c;
|
||||
|
||||
a ?= b; console.log(a);
|
||||
a = undefined;
|
||||
a ?= b; console.log(a);
|
||||
|
||||
console.log(a = c ? b);
|
||||
c = 404;
|
||||
console.log(a = c ? b);
|
||||
|
||||
b = undefined;
|
||||
|
||||
Array x, y;
|
||||
x = [123];
|
||||
y = clone x;
|
||||
y[] = 321;
|
||||
console.log('original:',x,'cloned:',y, y[]);
|
||||
|
||||
y[0..1] = [1..10];
|
||||
console.log(y[0..4]);
|
||||
|
||||
y.forEach((val) => console.log(val));
|
||||
|
||||
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");
|
||||
|
||||
console.log(`is:`, location.href is String, `; isnt:`, 123 isnt Number, ";");
|
||||
|
||||
if(yes === true && on === true && no === false && off === false) console.log('Boolean alternatives');
|
||||
|
||||
console.log('Raw string:', r`@test \n \t \r`);
|
||||
// Multiline RegExp's
|
||||
RegExp isEmail = /
|
||||
([\w-\.]+)
|
||||
@
|
||||
((?:[\w]+\.)+)
|
||||
([a-zA-Z]{2,4})
|
||||
/, email = r'danon0404@gmail.com';
|
||||
console.log("@email is email:", isEmail.test(email));
|
||||
/;
|
||||
String email = r'some@email.ru';
|
||||
console.log("@email {{ isEmail.test(email) ? "is" : "isnt" }} valid email adress");
|
||||
|
||||
// Arrays
|
||||
// Empty getter and setter ( pushig )
|
||||
Array arr = [2, 4, 8, 16, 32];
|
||||
console.log("Last element of `arr` is", arr[]), arr[] = 64;
|
||||
console.log("Now last element of `arr` is", arr[]);
|
||||
|
||||
// Splice assigment
|
||||
arr[2..4] = [3, 2, 1];
|
||||
console.log("`arr` is", arr);
|
||||
|
||||
// Range
|
||||
console.log("`[0..10]` is", [0..10]);
|
||||
|
||||
// Calling function
|
||||
console.log(Profile(
|
||||
"Dan", "Green",
|
||||
"HTML5", "JavaScript", "Dart", "PHP", "CSS3", "LESS", "Qt",
|
||||
age : 19,
|
||||
petName : "Felix"
|
||||
));
|
||||
}
|
||||
|
||||
main();</textarea>
|
||||
// Functions
|
||||
// Named, positional and splated arguments, with default values.
|
||||
// Arrow functions.
|
||||
Object Profile(String firstName, String secondName, String country = "Russia", Array skills..., age:, petName: "Tux"){
|
||||
skills.forEach((val) => val == "JavaScript" && console.log("JavaScript - It Awesome!"));
|
||||
return {
|
||||
firstName : firstName,
|
||||
secondName : secondName,
|
||||
age : age,
|
||||
country : country,
|
||||
skills : skills,
|
||||
petName : petName
|
||||
};
|
||||
}
|
||||
</textarea>
|
||||
<textarea id="translation"></textarea>
|
||||
<textarea id="result"></textarea>
|
||||
<div id="controls">
|
||||
|
|
@ -124,6 +183,8 @@ main();</textarea>
|
|||
<button id="benchmark" onclick="benchmark()">Benchmark</button>
|
||||
<input type="checkbox" id="is_js" onclick="compile()">
|
||||
<label for="is_js">js parser</label>
|
||||
<input type="checkbox" id="main_binding" onclick="compile()">
|
||||
<label for="main_binding">`main` binding</label>
|
||||
<span id="lenstat"></span>
|
||||
</div>
|
||||
</body>
|
||||
|
|
@ -132,16 +193,19 @@ main();</textarea>
|
|||
translationArea = document.getElementById("translation"),
|
||||
resultArea = document.getElementById("result"),
|
||||
isjs = document.getElementById("is_js"),
|
||||
mainbinding = document.getElementById("main_binding"),
|
||||
source;
|
||||
|
||||
if(!localStorage.source) localStorage.source = source = sourceArea.value;
|
||||
else sourceArea.value = source = localStorage.source;
|
||||
isjs.checked = localStorage.isjs == "t";
|
||||
mainbinding.checked = localStorage.mainbinding == "t";
|
||||
|
||||
function compile(){
|
||||
source = sourceArea.value;
|
||||
localStorage.source = source;
|
||||
localStorage.isjs = isjs.checked ? "t" : "f";
|
||||
localStorage.mainbinding = mainbinding.checked ? "t" : "f";
|
||||
|
||||
stream = new Cola.OutputStream({ beautify : true });
|
||||
compressor = new Cola.Compressor({ is_js : isjs.checked });
|
||||
|
|
@ -149,7 +213,7 @@ main();</textarea>
|
|||
try {
|
||||
// 1. compile
|
||||
ast = Cola.parse(source, { is_js : isjs.checked });
|
||||
if(!isjs.checked) ast = ast.toJavaScript({ main_binding : false });
|
||||
if(!isjs.checked) ast = ast.toJavaScript({ main_binding : mainbinding.checked });
|
||||
ast.print(stream);
|
||||
translationArea.value = stream.toString();
|
||||
|
||||
|
|
@ -176,13 +240,9 @@ main();</textarea>
|
|||
}
|
||||
|
||||
function benchmark(){
|
||||
console.time("Uncompressed");
|
||||
eval(translationArea.result);
|
||||
console.timeEnd("Uncompressed");
|
||||
|
||||
console.time("Compressed");
|
||||
console.time("ColaScript");
|
||||
eval(resultArea.result);
|
||||
console.timeEnd("Compressed");
|
||||
console.timeEnd("ColaScript");
|
||||
}
|
||||
|
||||
function exec(){
|
||||
|
|
|
|||
|
|
@ -469,7 +469,7 @@ Cola.Tokenizer.prototype.read_string = Cola.Tokenizer.with_eof_error("Unterminat
|
|||
} else
|
||||
|
||||
this.S.string.at[this.S.string.level].quote = quote;
|
||||
if (this.peek() == '@' || this.peek() == '{' && this.peek(1) == '{') return this.token("string", "");
|
||||
if (this.peek() == '@' || this.peek() == '{' && this.peek(1) == '{') return this.token("string", ret);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
|
@ -1432,7 +1432,7 @@ Cola.Parser.prototype.new_ = function() {
|
|||
var newexp = this.expr_atom(false), args;
|
||||
if (this.is("punc", "(")) {
|
||||
this.next();
|
||||
args = this.expr_list(")");
|
||||
args = this.expr_list(")", false, false, !this.is_js);
|
||||
} else {
|
||||
args = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ Cola.$_cola_array_range.i = 7;
|
|||
|
||||
Cola.$_cola_array_asplice = function $_cola_array_asplice(_array, _from, _to, _a){
|
||||
_to = _to - _from + 1;
|
||||
return [].splice.apply(_array, [_from, _to + 1].concat(_a)), _a;
|
||||
return [].splice.apply(_array, [_from, _to].concat(_a)), _a;
|
||||
}
|
||||
Cola.$_cola_array_asplice.i = 8;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user