Magpie脚本解析器

Magpie程序语言

Magpie脚本解析器是Monkey脚本解析器的后续版本。git

关于Monkey语言的功能,请参看个人另外一篇文章:Monkey程序语言github

Monkey脚本解析器如今已经再也不维护。全部的后续开发都会放在Magpie中。post

Magpie语言包含了Monkey语言的全部功能,同时进行了以下扩展/更改:spa

  • 增长了相似Java8的可选类型(Optional)支持
  • Hash的key若是是字符串类型,则能够不须要带双引号。
  • 若是for循环中只有一个语句,则能够写成以下形式:for i in xxx => 语句
  • 增长了相似于C#的linq处理(80%)
  • 修复了一些小的bug

主页

magpiecode

开源不易,若是你喜欢此项目,请麻烦使用你的金手指帮忙star一下,谢谢!orm

您的支持,是对个人鼓励!开发

举例

下面是Magpie语言的一个使用linq例子:字符串

class Linq {
    static fn TestSimpleLinq() {
        //Prepare Data Source
        let ingredients = [
            {Name: "Sugar",  Calories: 500},
            {Name: "Egg",    Calories: 100},
            {Name: "Milk",   Calories: 150},
            {Name: "Flour",  Calories: 50},
            {Name: "Butter", Calories: 200},
        ]

	//Query Data Source
        ingredient = from i in ingredients where i.Calories >= 150 orderby i.Name select i

        //Display
        for item in ingredient => println(item)
    }

    static fn TestFileLinq() {
        //Read Data Source from file.
        file = newFile("./examples/linqSample.csv", "r")

        //Query Data Source
        result = from field in file where int(field[1]) > 300000 select field[0] //Display for item in result => printf("item = %s\n", item)

        //Close file
        file.close()
    }

    static fn TestComplexLinq() {
        //Prepare Data Source
        stringList = [
            "A penny saved is a penny earned.",
            "The early bird catches the worm.",
            "The pen is mightier than the sword." 
        ]

        //Query Data Source
        earlyBirdQuery =
            from sentence in stringList
            let words = sentence.split(" ")
            from word in words
            let w = word.lower()
            where w[0] == "a" || w[0] == "e" ||
                  w[0] == "i" || w[0] == "o" ||
                  w[0] == "u"
            select word

        //Display
        for v in earlyBirdQuery => printf("'%s' starts with a vowel\n", v)
    }
}

Linq.TestSimpleLinq()
println("======================================")
Linq.TestFileLinq()
println("======================================")
Linq.TestComplexLinq()

//Test Optional
fn safeDivision?(a, b) {
    if (b == 0){
        return optional.empty();
    } else {
        return optional.of(a/b);
    }
}

op = safeDivision?(10, 2)
if (op.isPresent()) {
    println(op)

    let val = op.get()
    printf("safeDivision?(10, 2)=%d\n", int(val))
}
复制代码

许可证

MITget

相关文章
相关标签/搜索