
Toyland 程式設計
|
313
DoNothing
和
Sequence
的
#type
的實作物非常容易。估算
DoNothing
永遠都會成功,而只
要陳述式和它的連接沒有任何錯誤,估算
Sequence
也都會成功:
class DoNothing
def type(context)
Type::VOID
end
end
class Sequence
def type(context)
if first.type(context) == Type::VOID && second.type(context) == Type::VOID
Type::VOID
end
end
end
If
和
While
就稍微有點挑剔。它們都包含了扮演條件式的運算式,而且對正常運作的程
式來說,條件式必須估算布林值:
class If
def type(context)
if condition.type(context) == Type::BOOLEAN &&
consequence.type(context) == Type::VOID &&
alternative.type(context) == Type::VOID
Type::VOID
end
end
end
class While
def type(context)
ifcondition.type(context) == Type::BOOLEAN && body.type(context) == Type::VOID
Type::VOID
end ...