Created
April 9, 2011 19:38
-
-
Save hodzanassredin/911702 to your computer and use it in GitHub Desktop.
implementation of internal DSL for Prototype-based programming in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
# Имплементация прототип ориентированного internal DSL для Python по мотивам Io Language http://habrahabr.ru/blogs/crazydev/28041/ | |
# Implementation of prototype oriented internal DSL for python based on article about Io Language http://habrahabr.ru/blogs/crazydev/28041/ | |
class IoObject(dict): | |
def __init__(self): | |
self.clone = copy.copy | |
def __getattr__(self, attr): | |
if 'get_slot' in self: | |
val = self["get_slot"](attr)#может вызвать бесконечность / infinite loop can occur | |
else: | |
val = self[attr] | |
if callable(val): | |
return lambda *args: val(self,*args)# делаем керреинг и замыкание на вызове чтобы работало клонирование без замыканий / do currying on get | |
return val | |
def __setattr__(self, attr, val): | |
if 'set_slot' in self: | |
self["set_slot"](attr,val)#может вызвать бесконечность / infinite loop can occur | |
else: | |
self[attr] = val | |
Object = IoObject() | |
#все готово поехали / ready to go | |
Mushroom = Object.clone() | |
Mushroom.isPoison = False | |
Mushroom.whenEaten = lambda self, person: person.kill() if self.isPoison else None | |
InfectedMushroom = Mushroom.clone() | |
InfectedMushroom.isPoison = True | |
Man = Object.clone() | |
Man.state = "Living" | |
Man.eat = lambda self, food: food.whenEaten(self) | |
def kill(self): | |
self.state = "Dead" | |
Man.kill = kill | |
Lenin = Mushroom.clone() | |
Lenin.isPoison = False | |
Lenin.speak = lambda self: print("Патриотизм - ") | |
Lenin.speak() | |
Man.eat(Mushroom) | |
print(Man.state) | |
Man.eat(InfectedMushroom) | |
print(Man.state) | |
Singleton = Object.clone() | |
Singleton.clone = lambda obj: Singleton | |
Singleton2 = Singleton.clone() | |
Singleton.test = "singleton is ok" | |
print(Singleton2.test) | |
''' | |
This is original io code | |
Mushroom := Object clone | |
Mushroom isPoison := false | |
Mushroom whenEaten := method(person, | |
if(self isPoison == true, | |
person kill | |
) | |
) | |
Man := Object clone | |
Man state := "Living" | |
Man eat := method(food, | |
food whenEaten(self) | |
) | |
Man kill := method( | |
self state := "Dead" | |
) | |
Lenin := Mushroom clone | |
Lenin isPoison := false | |
Lenin speak := method( | |
"Патриотизм - одно из наиболее глубоких чувств, | |
закрепленных веками и тысячелетиями обособленных отечеств." println | |
) | |
InfectedMushroom := Mushroom clone | |
InfectedMushroom isPoison := true | |
Man eat(Mushroom) | |
Man state println | |
Man eat(InfectedMushroom) | |
Man state println | |
Lenin speak | |
Man eat(Lenin) | |
Singleton := Object clone | |
Singleton clone := Singleton | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment