Skip to content

Instantly share code, notes, and snippets.

Created October 25, 2010 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/644243 to your computer and use it in GitHub Desktop.
Save anonymous/644243 to your computer and use it in GitHub Desktop.
import structs/ArrayList
// build the house
house := CustomHouse new("home", ||
floor (1, ||
room ("den")
room ("kitchen")
)
floor (2, ||
room ("bedroom")
room ("batchroom")
)
)
house println()
// DSL implementation
CustomHouse: class {
name: String
floors := ArrayList<Floor> new()
init: func (=name, f: Method<This>()) {
f(this)
}
floor: func (number: Int, f: Method<Floor>()) {
floors add(Floor new(number, f))
}
println: func {
"House named %s has %d floors" format(name, floors size()) println()
for(f in floors) f println()
}
}
Floor: class {
number: Int
rooms := ArrayList<String> new()
init: func (=number, f: Method<This>()) {
f(this)
}
room: func (type: String) {
rooms add(type)
}
println: func {
"Floor %d has %d rooms (%s)" format(number, rooms size(), rooms join(", ")) println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment