Organize
This commit is contained in:
18
demo/blockexample.dee
Normal file
18
demo/blockexample.dee
Normal file
@@ -0,0 +1,18 @@
|
||||
fun <- ->
|
||||
do-something()
|
||||
do-something-else()
|
||||
|
||||
|
||||
foo <- ->
|
||||
bar <- ->
|
||||
baz <- ->
|
||||
print("Deep in here")
|
||||
print ("Inside foo but not in baz or bar")
|
||||
|
||||
foo() ## Inside foo but not in baz or bar
|
||||
|
||||
add <- x -> y ->
|
||||
return x + y ## Multivariable functions by currying
|
||||
|
||||
add(x) ## Returns a _function_
|
||||
add(x, y) ## This is equivalent to add(x)(y)
|
||||
31
demo/buildup.dee
Normal file
31
demo/buildup.dee
Normal file
@@ -0,0 +1,31 @@
|
||||
## Comments
|
||||
apple <- 1 ## assignments
|
||||
pear() ## function calls
|
||||
pear(x, y) ## function calls with arguments
|
||||
apple <- pear(x, y) + z ## compound assignments
|
||||
foo <- -> bar() ## function definitions
|
||||
foo <- ->
|
||||
bar()
|
||||
baz()
|
||||
## functions with block statements
|
||||
foo <- x -> y -> x * y + 7 ## Multiple argument functions
|
||||
if test?()
|
||||
do-something()
|
||||
elif other-test?()
|
||||
do-something-else()
|
||||
else
|
||||
a <- 4
|
||||
## Block conditionals
|
||||
|
||||
if test?()
|
||||
do-nothing()
|
||||
else
|
||||
if second-test?()
|
||||
do-a-real-bad-thing()
|
||||
## Nested blocks
|
||||
|
||||
obj <- {
|
||||
field <- "hello"
|
||||
method <- x -> x * 2
|
||||
}
|
||||
## Object creation
|
||||
57
demo/example.dee
Normal file
57
demo/example.dee
Normal file
@@ -0,0 +1,57 @@
|
||||
## By the end of the book I want to have an interpreter
|
||||
## for this little language, maybe a compiler too
|
||||
|
||||
print(1 + 1) ## 2
|
||||
print("Hello world!") ## Hello world
|
||||
|
||||
name <- read()
|
||||
print("Hi there $name") ## Hi there <name>
|
||||
|
||||
have <- 10
|
||||
want <- 11
|
||||
need <- have - want ## This is subtraction
|
||||
have-want <- 1 ## This is a variable named "have-want"
|
||||
|
||||
print(HaVe-WAnt) ## 1 (case doesn't matter)
|
||||
%%option meaningful-casing
|
||||
Apples <- 1
|
||||
print(apples) ## <undef> (or maybe it does)
|
||||
|
||||
print("one fish"); print("two fish") ## Two statements on the same line
|
||||
|
||||
say-hi <- -> print("Hi from inside a function")
|
||||
say-hi() ## Hi from inside a function
|
||||
|
||||
say-hi-again <- say-hi
|
||||
say-hi-again() ## "Hi from inside a function" (First class functions!)
|
||||
|
||||
duck <- {
|
||||
bills <- 1
|
||||
talk <- -> print("Quack")
|
||||
eats? <- food -> food = "bread"
|
||||
} ## Objects created on the fly
|
||||
|
||||
print duck.eats?("bread") ## true
|
||||
print duck.eats?("corn") ## false
|
||||
|
||||
cow <- {
|
||||
talk <- print("Moo")
|
||||
eats? <- food -> food oneof "grass", "corn"
|
||||
}
|
||||
|
||||
human <- {
|
||||
eats? <- _ -> true
|
||||
}
|
||||
|
||||
print cow.eats?("grass") ## true
|
||||
print cow.eats?("corn") ## true
|
||||
|
||||
talk-or-pass-wind <- character ->
|
||||
if character has talk then
|
||||
character.talk()
|
||||
else
|
||||
print("*Fart*")
|
||||
|
||||
talk-or-pass-wind(duck) ## "Quack"
|
||||
talk-or-pass-wind(cow) ## "Moo"
|
||||
talk-or-pass-wind(human) ## "*Fart*"
|
||||
Reference in New Issue
Block a user