Magpie腳本解析器是Monkey腳本解析器的後續版本。git
關於Monkey語言的功能,請參看個人另外一篇文章:Monkey程序語言。github
Monkey
腳本解析器如今已經再也不維護。全部的後續開發都會放在Magpie
中。post
Magpie
語言包含了Monkey
語言的全部功能,同時進行了以下擴展/更改:spa
可選類型(Optional)
支持for i in xxx => 語句
linq
處理(80%)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