Browser-Cola added.

This commit is contained in:
Onoshko Dan 2014-05-22 22:44:11 +07:00
parent 428438c4a0
commit 00623e5e4c
9 changed files with 142 additions and 59 deletions

View File

@ -85,24 +85,20 @@ ColaScript is a language that compiles in JavaScript. This language is similar t
### Compiler
- `@require`
- `@require`, status: done
@require "./library/jquery.js" "./library/underscore.js"
- `@include`, status: done
@include "./app/my.js"
- `@use`
@use strict
@use asmjs
@use closure
- `@if @end_if @else`
@if target == 'web'
@require './main.cola'
@else
@require './mobile/main.cola'
@end_if
## Expressions
- `switch` assignment, status: done!

View File

@ -1,9 +1,8 @@
// `main` functions may binding to a onload event
// Functions can defined without `function` keyword, with and without `type`
@require './sugar.min.js'
@require '../sugar.min.js'
main(){
@require './sugar.min.js'
// Unary operators
// Two variants of `isset` operator. Which is better?
bool _seted = true, _empty;

8
lib/browser-cola.js Normal file

File diff suppressed because one or more lines are too long

1
lib/browser.js Normal file
View File

@ -0,0 +1 @@
window.addEventListener("DOMContentLoaded", Cola.bootstrap, false);

View File

@ -2,17 +2,8 @@
<html>
<head>
<title>ColaScript Playground</title>
<script src="./utils.js"></script>
<script src="./ast.js"></script>
<script src="./parse.js"></script>
<script src="./transform.js"></script>
<script src="./scope.js"></script>
<script src="./output.js"></script>
<script src="./compress.js"></script>
<script src="./sourcemap.js"></script>
<script src="./mozilla-ast.js"></script>
<script src="./translate.js"></script>
<script src="./std.js"></script>
<script src="../cola.js"></script>
<script src="../demo.cola" type="text/colascript"></script>
<style>
body {
@ -286,7 +277,7 @@ main();</textarea>
}
function exec(){
eval(resultArea.value)
eval.call(window, translationArea.value);
}
function Translate(){

View File

@ -1051,22 +1051,36 @@ Cola.Parser.prototype.statement = Cola.Parser.embed_tokens(function() {
if (!this.is_js && this.next_is("name"))
return new Cola.AST_Command({
name : this.next().value,
args : (function(){
args : (function(name){
var args = [];
if (name == 'require' || name == 'include') {
while (!this.peek().nlb && !this.next_is('eof')){
if (this.next_is('punc', '{')) {
this.next();
args.push(new Cola.AST_BlockStatement({
if (!this.is("string")) this.unexpected();
args.push(this.S.token.value);
}
this.next();
} else
if (name == 'use') {
this.next();
if (!this.is("name")) this.unexpected();
args.push(this.S.token.value);
if (this.S.token.value == 'closure') {
this.next();
args.push(this.is("punc","{"), new Cola.AST_BlockStatement({
start : this.S.token,
body : this.block_(),
body : this.block_(!this.is("punc","{")),
end : this.prev()
}));
} else
args.push(this.next());
}
this.next();
if (this.S.token.value != "asmjs" && this.S.token.value != "strict") this.unexpected();
} else this.unexpected();
return args;
}).call(this)
}).call(this, this.S.token.value)
});
case "{":
@ -1371,13 +1385,18 @@ Cola.Parser.prototype.if_ = function (inline) {
});
};
Cola.Parser.prototype.block_ = function () {
this.expect("{");
Cola.Parser.prototype.block_ = function (to_eof) {
if(this.is_js || !to_eof) this.expect("{");
var a = [];
if (this.is_js || !to_eof)
while (!this.is("punc", "}")) {
if (this.is("eof")) this.unexpected();
a.push(this.statement());
}
else
while (!this.is("eof")) a.push(this.statement());
this.next();
return a;
};

View File

@ -130,11 +130,13 @@ Cola.$_cola_func_set_named_args = function $_cola_func_set_named_args(_args){
}
}
Cola.$_cola_func_set_named_args.i = 10;
Cola.$_cola_arguments_def = { i : 11 };
Cola.$_cola =
Cola.$_cola_is + Cola.$_cola_isnt + Cola.$_cola_modulo + Cola.$_cola_isset +
Cola.$_cola_isntset + Cola.$_cola_clone + Cola.$_cola_array_last + Cola.$_cola_array_range +
Cola.$_cola_array_asplice + Cola.$_cola_func_named_args + Cola.$_cola_func_set_named_args;
Cola.$_cola_array_asplice + Cola.$_cola_func_named_args + Cola.$_cola_func_set_named_args +
"var arguments;";
Cola.Compressor.StdFuncs = {
$_cola_is : true,

View File

@ -786,6 +786,8 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
*/
if(node instanceof Cola.AST_Cascade){
$_cola_hash[Cola.$_cola_arguments_def.i] = true;
props = {
type : "dynamic",
body : [],
@ -866,7 +868,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
props = {
expression : new Cola.AST_Function(props),
property : "call"
}
};
node = new Cola.AST_Call({
start : node.start,
@ -1326,6 +1328,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
if(node instanceof Cola.AST_Assign && (node.left instanceof Cola.AST_ArrayTemplate || node.left instanceof Cola.AST_ObjectTemplate ||
(node.left instanceof Cola.AST_Array || node.left instanceof Cola.AST_Object) && node.left.template)){
if(node.left.vardef) Cola.Parser.prototype.unexpected.call(Cola.Parser.prototype, node.start);
$_cola_hash[Cola.$_cola_arguments_def.i] = true;
var defs = [];
@ -1536,6 +1539,8 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
*/
if(node instanceof Cola.AST_If && node.inline && !(parent instanceof Cola.AST_If && parent.inline)){
$_cola_hash[Cola.$_cola_arguments_def.i] = true;
var s = node;
while (true) {
@ -1584,6 +1589,8 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
*/
if(node instanceof Cola.AST_Switch && !(parent instanceof Cola.AST_Block)){
$_cola_hash[Cola.$_cola_arguments_def.i] = true;
node.body.forEach(function(branch){
if(!branch.body.length) return;
branch.body[0] = new Cola.AST_Return({
@ -1598,7 +1605,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
argnames : []
}),
property : "apply"
}
};
node = new Cola.AST_Call({
args : [new Cola.AST_SymbolRef({ name : "this" }), new Cola.AST_SymbolRef({ name : "arguments" })],
@ -1749,8 +1756,6 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
var included = [];
node.args.forEach(function(file){
if (file.type != "string") Cola.Parser.prototype.unexpected.call(Cola.Parser.prototype, file);
else file = file.value;
options.parser.is_js = /\.js$/.test(file);
options.parser.filename = file;
@ -1771,8 +1776,6 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
if(node instanceof Cola.AST_Command && node.name == "require"){
node.args.forEach(function(file){
if (file.type != "string") Cola.Parser.prototype.unexpected.call(Cola.Parser.prototype, file);
else file = file.value;
if (required_hash[file]) return;
required_hash[file] = true;
@ -1792,6 +1795,33 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
});
return false;
} else
if (node instanceof Cola.AST_Command && node.name == "use") {
if (node.args[0] == "closure") {
props = new Cola.AST_Function({
type : "dynamic",
body : node.args[2].body,
argnames : []
});
if (node.args[1]){
$_cola_hash[Cola.$_cola_arguments_def.i] = true;
props = new Cola.AST_Dot({
expression : props,
property : "apply"
});
}
node = new Cola.AST_Call({
args : node.args[1] ? [new Cola.AST_SymbolRef({ name : "this" }), new Cola.AST_SymbolRef({ name : "arguments" })] : [],
expression : props
});
node = new Cola.AST_SimpleStatement({ body : node });
} else
node = new Cola.AST_Directive({ value : "use " + node.args[0] });
}
if(node instanceof Array){
@ -1808,8 +1838,10 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
_this.body = required.concat(_this.body);
if(options.std) for(var i in $_cola_hash) if($_cola_hash.hasOwnProperty(i))
if(options.std){
for(var i in $_cola_hash) if($_cola_hash.hasOwnProperty(i))
_this.body.unshift($_cola_ast.body[i]);
}
return _this;
};

View File

@ -41,9 +41,55 @@
***********************************************************************/
"use strict";
!this.Cola && (this.Cola = {});
Cola.getSource = function (url) {
var xhr = new XMLHttpRequest;
xhr.open('GET', url, false);
xhr.send();
return xhr.responseText;
};
Cola.translate = function (source, opts) {
var stream = new Cola.OutputStream(),
ast;
try {
// 1. compile
ast = Cola.parse(source).toJavaScript(opts);
ast.print(stream);
return stream.toString();
} catch(e){
throw e;
}
};
Cola.eval = function (source, opts) {
return eval(Cola.translate(source, opts));
};
Cola.bootstrap = function () {
var source = "";
Array.prototype.forEach.call(document.querySelectorAll('script[type="text/colascript"][src]'),
function(script){
source += Cola.getSource(script.src);
});
Cola.eval(source, { main_event : "ColaScriptMain" });
var event = document.createEvent("HTMLEvents");
event.initEvent("ColaScriptMain", true, true);
event.eventName = "ColaScriptMain";
window.dispatchEvent(event);
};
"use strict";
Cola.array_to_hash = function (a) {
var ret = Object.create(null);
for (var i = 0; i < a.length; ++i)
@ -340,14 +386,3 @@ Cola.clone = function (item) {
return item;
};
Cola.nodeCompare = function(_a, _b){
return _a.__proto__ === _b.__proto__ && _a.start.pos === _b.start.pos && _a.end.pos === _b.end.pos;
};
Cola.getSource = function(url){
var xhr = new XMLHttpRequest;
xhr.open('GET', url, false);
xhr.send();
return xhr.responseText;
};