Io Language学习:基本语法

阅读更多
Io Language没有关键字,所以它的语法很简单:

// 赋值
a := "hello world"

// 取值
a

// 方法调用:
a println

// 方法调用2:
a split(" ")

// 方法调用3:
a slice(1, 3)

// 方法调用在Io里面叫作message,和Ada, Ruby, Objective-C这类语言相似

// 操作符:
1 + 1

// 操作符本身也是函数:
1 + 1 // 等价于 1 getSlot("+")(1),函数不能像操作符这么使用

// \n 和 ; 作用是一样的,都可以分割语句

// 字符串语法:
quote ::= MonoQuote | TriQuote
MonoQuote ::= """ [ "\"" | not(""")] """
TriQuote ::= """"" [ not(""""")] """""

// 注释:
# 单行注释
// 单行注释
/* 多行注释 */

// 数字:
a := 0x10
a := 1.2e10

// 创建对象:
a := Object clone
lst := List clone

// 创建类(实际上是原型扩展,clone原型并扩展):
Person := Object clone do(
    name := nil
    age := 1
    sayhi := method(
        "hi" println
    )
)

// 或者不用指定成员了
Person := Object clone

// 创建实例(也是原型扩展)
a := Person clone
me := Person clone do(
    name := 'xxx'
    age := 0
)

// 函数编程:
fib := method(n,
    if(n < 2,
      1,
      fib(n-1) + fib(n-2)
    )
)
fib(5)


就这些东西了,所有东西都在这上面扩展的,比如if/else/for/foreach/while/loop,这和LISP有些相似。

简单程序:

// 创建List:
people := list(
  Person clone do(name := "aaa"; age := 19),
  Person clone do(name := "bbb"; age := 20),
  Person clone do(name := "ccc"; age := 21),
  Person clone do(name := "ddd"; age := 22)
)

// map:
people map(person, person name)
// 或简单写为:
people map(name)

// select:
people select(person, person age < 21)
// 或简写为:
people select(age < 21)

你可能感兴趣的:(LISP,Objective-C,Ruby,编程,C)