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

相關文章
相關標籤/搜索