descriptionnone
ownerinfinte.cdda@hotmail.com
last changeSat, 3 Nov 2012 17:40:56 +0000 (4 01:40 +0800)
content tags
add:
README.md

Moescript

Sharp and cute ><

Usage

  1. npm install moe
  2. Enjoy

Features

less braces. Use indent

    def max(list):
            var m = list[0]
            for(i <- 0..list.length)
                    if(list[i] > m) m = list[i]
            return m

    // trace means "console.log" with last argument returned.
    trace max [5, 4, 3, 2, 1]

Literals: use []

    def emptyObject = [:]
    def gameConfig = [
            player: [
                    name: "tom",
                    type: Human,
                    level: 1,
                    weapon: sword,
                    inventory: [food, food, potion, bomb]
            ],
            enemy: [
                    name: "Dragon",
                    type: Dragon,
                    level: 9
            ]
    ]

Currying, and def wrappers

    def Y(g) =
            // use "=" here...
            def rec(x)(y) = g(x(x)) y
            rec(rec) // ...means this value will be returned.

    // Define recursive function using Y
    // fibonacci = Y(functon(recurse){return function(n){...}})
    def Y fibonacci(recurse)(n) =
            if(n <= 2) 1
            else recurse(n - 2) + recurse(n - 1)

    fibonacci(5) // 5

Simple "class" definition

    def Man(name):
            this.name = name
    def Man::speak(something):
            trace something

    def outof(Man) Child(name):
            resend Man(name) // Man.call this, name
    def Child::speak(something):
            trace "Ah!"
            resend Man::speak(something) // Man.prototype.speak.call this, something

    var tom = new Child "Tom"
    tom.speak "Thanks!"

Monadic transformation for async / enumerator / list comprehension......

Asyncs

    def asyncLib = require "moe/libs/async"
    def async = asyncLib.async
    def sleep = asyncLib.sleep // sleep(f, dt) === setTimrout(dt, f)

    def f() = process wait loadResource(something)
    // def f = [build: fBuild]
    // where fBuild(schemata)()():
    //     return schemata.yield loadResource something, (resource) :>
    //         return schemata.return process resource

    def async randPrintNums(n):
            def tasks = []
            for(i <- 0..n)
                    tasks.push async :>
                            sleep! (100 * Math.random())
                            trace index
                    where index = i
            join! tasks

    randPrintNums 100

Enumerators

    def enumeration String::getEnumerator():
            for(i <- 0..this.length)
                    enumeration.yield! this.charAt(i), i

    for(c <- "this is a string")
            trace c

List comprehension

    -- Enumerator comprehension monad
    var ecSchemata = object defaultMonadSchemata, => 
            def Enumerable @return(x):
                    if(x != undefined)
                            enumeration.yield! x
            def @bindYield(f, thisp, args) = f.apply thisp, arguments.slice(2)
            def Enumerable @bind(list, callback):
                    for(x <- list) 
                            for(y <- callback x)
                                    enumeration.yield! y

    var mktable(G) =
            var f = G.build ecSchemata
            f.apply(this, arguments)()

    // simple usage
    for(item <- mktable {var x <- (1..100); x * 2 + 1}) trace item

    // complicated usage
    var t = mktable {var x <- (1...9); var y <- (x...9); x + ' * ' + y + ' = ' + x * y }
    // t = mktable [build: fBuild]
    // where fBuild(schemata)()() =
    //     schemata.bind (1...9), (x) =>
    //         schemata.bind (x...9), (y) =>
    //             schemata.return (x + ' * ' + y + ' = ' + x * y)
    for(item <- t) trace item
shortlog
2012-11-03 be5invisMoved gvm into compiler. Added --explicit for moecmaster
2012-11-03 be5invisremoved smap demo. It will be redone soon, and charmy.
2012-11-02 be5invisMoved global variable management system into gvm
2012-11-01 Belleve InvisOptimized build sequence and NESSAT tool
2012-11-01 Belleve Invisadded prelude/list.shuffle and prelude/hash
2012-10-30 Belleve InvisAdded new options for moec; optimized makefile and...
2012-10-09 Belleve InvisStrictened same-indentation-rule
2012-10-06 be5invisWebtest script simplified
2012-10-03 be5invisChanged signature for async-generated functions; added...
2012-10-02 be5invisAdded math.randomInt
2012-09-27 Belleve InvisChanged heredoc format into lua style.
2012-09-26 Belleve InvisMoved option handler into parser.
2012-09-23 Belleve Invis[+] Added `catch-try` statement for multiple branched...
2012-09-23 be5invisVer number upgraded to 0.3
2012-09-23 be5invis[*] Prelude: `trace` now uses STDERR; added global...
2012-09-20 Belleve Invis[/] Fixed bug ivolved with regular expression literals.
...
heads
11 years ago master