Magpie程序语言
关于Monkey语言的功能,请参看我的另一篇文章:Monkey程序语言。
Monkey
脚本解析器现在已经不再维护。所有的后续开发都会放在Magpie
中。
Magpie
语言包含了Monkey
语言的所有功能,同时进行了如下扩展/更改:
- 增加了类似Java8的
可选类型(Optional)
支持 - Hash的key如果是字符串类型,则可以不需要带双引号。
- 如果for循环中只有一个语句,则可以写成如下形式:
for i in xxx => 语句
- 增加了类似于C#的
linq
处理(80%) - 修复了一些小的bug
主页
开源不易,如果你喜欢此项目,请麻烦使用你的金手指帮忙star一下,谢谢!
您的支持,是对我的鼓励!
举例
下面是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))
}
复制代码
许可证
MIT