From d87a5db0fd2d08d0fc11e493d55c229366a0afc1 Mon Sep 17 00:00:00 2001 From: Dan Onoshko Date: Wed, 31 Dec 2014 03:18:16 +0700 Subject: [PATCH] modules are done --- README.md | 68 +++++---- bin/cola | 28 +++- lib/ast.js | 27 +--- lib/index.html | 33 ++++- lib/parse.js | 217 +++++++++++++++++++--------- lib/std.js | 51 ++++++- lib/translate.js | 367 ++++++++++++++++++++++++++++++++++++++++------- lib/utils.js | 64 ++++++++- tools/node.js | 10 +- 9 files changed, 670 insertions(+), 195 deletions(-) diff --git a/README.md b/README.md index 6dabadb6..608c0f36 100644 --- a/README.md +++ b/README.md @@ -390,16 +390,14 @@ Future plans _ColaRuntime$$updateProperty(Math, "rand", { get: function rand() { return 123; - }, - configurable: true + } }); _ColaRuntime$$updateProperty(Math, "rand", { set: function rand() { var val = arguments[0] !== undefined ? arguments[0] : _ColaRuntime$$error("Argument `val` is required!"); return console.log(val); - }, - configurable: true + } }); - operator `?.`. status: done @@ -424,12 +422,13 @@ Future plans - var modificators. status: done - readonly int Math.Pi = 3.14; + const int Math.Pi = 3.14; list of modificators - * static: vars, funcs, getters and setters in class - * const: vars - * covert: vars, funcs, getters and setters in class and objects + * static: vars, funcs, getters and setters in classes + * const: vars, props + * covert: vars, funcs, getters and setters in classes and objects, props + * export: vars, funcs, classes, singletones - classes. status: done @@ -464,7 +463,7 @@ Future plans set some(val) => about += val; } -- singletones +- singletones. status: done singleton S { // in fact this is object int x = 45; @@ -478,18 +477,40 @@ Future plans operator[]=(int index, int val) => x = index + val; } - -- injectors - injector String { - String replaceAll(a, b){ - String res = this; - while(res.indexOf(a) != -1) res = res.replace(a, b); - return res; - } - } +- Compiler command `@import` to import modules ( CommonJS ), status: done + + @import 'sugar' + @import 'fs' as fs + @import dirname from 'path' + @import print as echo, prompt as ask, test from 'mylib' -- add `observe` modificator. + String code = fs.readFileSync(dirname(filePath) + "/main.cola", "utf8"); + echo("hello!"); + +- Compiler command `@export` to export modules ( CommonJS ), status: done + + Object hash = {}; + Array users = []; + + @export hash as _, users + @export each as forEach from "underscore" + @export "mylib" + +- `await` operator (only with Promise support) + + String name = await getNameFromServer(id); + console.log(name); + + to + + getNameFromServer(id).then(function(_ColaRuntime$$fulfilled, _ColaRuntime$$rejected) { + if (_ColaRuntime$$rejected) throw _ColaRuntime$$rejected; + String name = __ColaRuntime$$fulfilled; + console.log(name); + }); + + - static typing - `@use` expressions @@ -540,15 +561,6 @@ Future plans } -- Compiler command `@import` to import modules ( AMD, CommonJS... ) - - // node.js - @import 'fs' as fs - @import dirname from 'path' - - String code = fs.readFileSync(dirname(filePath) + "/main.cola", "utf8"); - - - write documentation of tokenizer/parser methods - more informative exceptions - better source maps diff --git a/bin/cola b/bin/cola index 7752d04c..921fd6bd 100755 --- a/bin/cola +++ b/bin/cola @@ -1,4 +1,4 @@ -#! /usr/bin/env node +#!/usr/bin/env node // -*- js -*- "use strict"; @@ -20,6 +20,8 @@ with whitespace and comments discarded. To achieve compression and\n\ mangling you need to use `-c` and `-m`.\ ") .describe("j", "Work with JavaScript (by default Cola will expect ColaScript).") + .describe("n", "Enable compilation for node.") + .describe("k", "Path to packages.json (packages.json by default).") .describe("source-map", "Specify an output file where to generate source map.") .describe("source-map-root", "The path to the original source to be included in the source map.") .describe("source-map-url", "The path to the source map to be added in //# sourceMappingURL. Defaults to the value passed with --source-map.") @@ -27,7 +29,7 @@ mangling you need to use `-c` and `-m`.\ .describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.") .describe("screw-ie8", "Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks (by default Cola will try to be IE-proof).") .describe("expr", "Parse a single expression, rather than a program (for parsing JSON)") - .describe("n", "Disable `main` binding.") + .describe("i", "ignore `main` binding.") .describe("p", "Skip prefix for original filenames that appear in source maps. \ For example -p 3 will drop 3 directories from file names and ensure they are relative paths. \ You can also specify -p relative, which will make Cola figure out itself the relative paths between original sources, \ @@ -69,7 +71,9 @@ You need to pass an argument to this option to specify the name that your module .alias("j", "js") .alias("j", "javascript") - .alias("n", "no-main-binding") + .alias("n", "node") + .alias("k", "packages") + .alias("i", "ignore-main-binding") .alias("p", "prefix") .alias("o", "output") .alias("v", "verbose") @@ -81,6 +85,7 @@ You need to pass an argument to this option to specify the name that your module .alias("V", "version") .alias("e", "enclose") + .string("k") .string("source-map") .string("source-map-root") .string("source-map-url") @@ -94,8 +99,9 @@ You need to pass an argument to this option to specify the name that your module .string("p") .boolean("j") - .boolean("expr") .boolean("n") + .boolean("expr") + .boolean("i") .boolean("source-map-include-sources") .boolean("screw-ie8") .boolean("export-all") @@ -293,7 +299,9 @@ async.eachLimit(files, 1, function (file, cb) { }); if (!ARGS.j) TOPLEVEL = TOPLEVEL.toJavaScript({ - main_binding: !ARGS.n, + main_binding: !ARGS.i, + is_node: ARGS.n, + modules: Cola.getPackages(ARGS.k), path: path.dirname(file), parser: { expression : ARGS.expr, @@ -423,7 +431,15 @@ function getOptions(x, constants) { var ast; try { ast = Cola.parse(x, { expression: true, is_js: ARGS.j }); - if (!ARGS.j) ast = ast.toJavaScript({ main_binding: !ARGS.n, parser: { expression: true, is_js: ARGS.j } }); + if (!ARGS.j) ast = ast.toJavaScript({ + main_binding: !ARGS.i, + is_node: ARGS.n, + modules: Cola.getPackages(ARGS.k), + parser: { + expression: true, + is_js: ARGS.j + } + }); } catch(ex) { if (ex instanceof Cola.JS_Parse_Error) { sys.error("Error parsing arguments in: " + x); diff --git a/lib/ast.js b/lib/ast.js index b0a9b57b..d7e30906 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -310,10 +310,12 @@ Cola.AST_Scope = Cola.DEFNODE("Scope", "directives variables functions uses_with }, }, Cola.AST_Block); -Cola.AST_Toplevel = Cola.DEFNODE("Toplevel", "globals language", { +Cola.AST_Toplevel = Cola.DEFNODE("Toplevel", "globals language requiredModules", { $documentation: "The toplevel scope", $propdoc: { globals: "[Object/S] a map of name -> SymbolDef for all undeclared names", + language: "[String] JavaScript or ColaScript", + requiredModules: "[Object] modules required in this code" }, wrap_enclose: function(arg_parameter_pairs) { var self = this; @@ -378,7 +380,7 @@ Cola.AST_Toplevel = Cola.DEFNODE("Toplevel", "globals language", { } }, Cola.AST_Scope); -Cola.AST_Class = Cola.DEFNODE("Class", "name extends", { +Cola.AST_Class = Cola.DEFNODE("Class", "name extends mods", { $iscola: true, $documentation: "Base class for classes", $propdoc: { @@ -394,7 +396,7 @@ Cola.AST_Class = Cola.DEFNODE("Class", "name extends", { } }, Cola.AST_Scope); -Cola.AST_Singleton = Cola.DEFNODE("Singleton", "name", { +Cola.AST_Singleton = Cola.DEFNODE("Singleton", "name mods", { $iscola: true, $documentation: "Base class for singletons", $propdoc: { @@ -408,20 +410,6 @@ Cola.AST_Singleton = Cola.DEFNODE("Singleton", "name", { } }, Cola.AST_Scope); -Cola.AST_Injector = Cola.DEFNODE("Injector", "name", { - $iscola: true, - $documentation: "Base class for injectors", - $propdoc: { - name: "[AST_SymbolInjector] the name of this injector" - }, - _walk: function(visitor) { - return visitor._visit(this, function(){ - this.name._walk(visitor); - Cola.walk_body(this, visitor); - }); - } -}, Cola.AST_Scope); - Cola.AST_Lambda = Cola.DEFNODE("Lambda", "name argnames uses_arguments", { $documentation: "Base class for functions", $propdoc: { @@ -984,11 +972,6 @@ Cola.AST_SymbolSingleton = Cola.DEFNODE("SymbolSingleton", null, { $documentation: "Symbol defining a singleton", }, Cola.AST_SymbolDeclaration); -Cola.AST_SymbolInjector = Cola.DEFNODE("SymbolInjector", null, { - $iscola: true, - $documentation: "Symbol defining an injector", -}, Cola.AST_SymbolDeclaration); - Cola.AST_SymbolFunarg = Cola.DEFNODE("SymbolFunarg", "type", { $documentation: "Symbol naming a function argument", }, Cola.AST_SymbolVar); diff --git a/lib/index.html b/lib/index.html index 073c2caa..98bde3ed 100644 --- a/lib/index.html +++ b/lib/index.html @@ -2,6 +2,13 @@ ColaScript Playground + @@ -89,7 +96,7 @@ - + + v0.6.0 @@ -222,18 +231,37 @@ main();