Skip to content

Instantly share code, notes, and snippets.

@k33g
Last active December 14, 2015 16:59
Show Gist options
  • Save k33g/5119019 to your computer and use it in GitHub Desktop.
Save k33g/5119019 to your computer and use it in GitHub Desktop.
le "Glloq", 1er DSL pour enfants en Golo
module glloq
function boite = -> DynamicObject():
op(""):
value(0):
nbCailloux(0):
nbAllumettes(0):
define("alumette", |this| {
if this: op(): equals("+") { this: nbAllumettes(this: nbAllumettes() + this: value())}
if this: op(): equals("-") { this: nbAllumettes(this: nbAllumettes() - this: value())}
return this
}):
define("caillou", |this| {
if this: op() == "+" { this: nbCailloux(this: nbCailloux() + this: value())}
if this: op() == "-" { this: nbCailloux(this: nbCailloux() - this: value())}
return this
}):
define("alumettes", |this| {
return this: alumette()
}):
define("cailloux", |this| {
return this: caillou()
}):
define("ajouter", |this| {
this: op("+")
return this
}):
define("enlever", |this| {
this: op("-")
return this
}):
define("une", |this| {
this: value(1)
return this
}):
define("un", |this| {
this: value(1)
return this
}):
define("deux", |this| {
this: value(2)
return this
}):
define("trois", |this| {
this: value(3)
return this
}):
define("quatre", |this| {
this: value(4)
return this
})
function combien = -> DynamicObject():
quoi(""):
define("de", |this| {
return this
}):
define("alumettes", |this| {
this: quoi("alumettes")
return this
}):
define("cailloux", |this| {
this: quoi("cailloux")
return this
}):
define("dans", |this, boite| {
if this: quoi(): equals("alumettes") { println(boite: nbAllumettes() + " alumettes") }
if this: quoi(): equals("cailloux") { println(boite: nbCailloux() + " cailloux") }
return this
})
module main

import glloq

function main = |args| {

  var maBoite = boite(): 
			ajouter(): trois(): alumettes(): 
			ajouter(): quatre(): cailloux(): 
			enlever(): une(): alumette()

	combien(): de(): cailloux(): dans(maBoite)
	combien(): de(): alumettes(): dans(maBoite)

}
@k33g
Copy link
Author

k33g commented Mar 8, 2013

on obtient :
4 cailloux
2 alumettes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment