-
Notifications
You must be signed in to change notification settings - Fork 10
Syntax
Comments in Blueberry can be one liners and multiline
# this is a single line comment
/* this
is a multiline
comment */
number = 1
number = 1.2 # floating point number
str = "my string"
sym = :my_symbol # it's basically just an string but can only have [a-zA-Z_] characters
nothing = nil # equivalent to PHP's null
obj = new MyClass() # instantiate an object
You can assign values to variables just by using =.
my_variable_name = "my value"
if age > 18 and age < 99
doSomething()
else if age > 99
doSomethingElse()
else
doNothing()
end
You can also use switch
switch someVariable
when "mike"
echo("Hello, Mike!")
when "john"
echo("Hello, John!")
when "annie", "marceline"
echo("Hai gals")
else
echo("Hello, World!")
end
To apply a boolean not operation, use the not keyword
if not age > 18
doSomething()
end
myValue = not myVariable
title = age > 18 ? "Mister" : "Boy"
name = getName() ?: "Mike"
If getName() is falsy, "Mike" will be used, that translates as
name = getName() ? getName () : "Mike"
Blueberry has several loop flavours, while and for beeing the most
common ones.
while someFunctionReturnsTrue()
echo("Hello, World!")
end
# The for is very similar to ruby's :)
for i in (0..10)
echo(i)
end
Ranges generate an array from start to end, so you can also do
myArr = (0..returnsInteger())
You can of course call for with any collection
myData = array(1, 2, 3, 4)
for i in myData
echo(i)
end
The concatenation operator is &.
echo("Hello " & name)
You can create arrays with []
Also, arrays can have initial values
myArray = [1, 2, f(1)]
callSomething([1, 2])
emptyArray = []
To get the array item at a given index is the same as PHP
item = arr[2]
Arrays indices start from 0
You can create multidimensional arrays using JSON syntax
myArray = {
"name": "John",
"age": getAge(),
"more_data": {
"married": false
}
}
try
someCodeHere()
catch myError
echo(myError.getMessage())
finally
doSomethingElse()
end
Of course you can omit the finally and the catch argument
try
someCodeHere()
catch
# When you don't include an argument identifier, "ex" is used
# Nevertheless, most of the time you don"t include one it"s because you wont
# use it.
echo(ex.getMessage())
end
You can use CoffeeScript-style list comprehensions to make your life easier.
a = [2 * 1 for i in (1..10) where i % 2 == 0]
Functions are created using the def keyword
def myFunctionName(name)
return "Hello" & name
end
Closures are anonymous functions. They can be defined as follows:
a = (i) -> 2*i
That will compile to
$a = function ($i) { return 2*$i; }
You can also specify a block instead of an expression for the closure body:
a = (i) -> do
b = 2
callSomething()
return f(b)
end
Closures automatically use the variables defined in the parent context:
a = 1
b = () -> a + 1
Will compile to
$a = 1;
$b = function () use ($a) { return $a + 1; };
If you don't want to include the whole scope you can manually specify it
a = 1
b = () use (a) -> a + 1
The plan is to eventually check for the closure body and include only the variables it uses. So feel free to work on that and submit a pull request!
Classes include the @ operator from Ruby, using that operator you can
create instance variables and later on refer to them in a very simple and
readable way.
class Human
# name will be a public instance variable
@name
# age will also be a public instance variable with a value of 18
@age = 18
def __construct(name)
# this is the class's constructor
@name = name
end
def greet
echo("Hi! I'm " & @name)
end
end
mike = new Human()
mike.greet()
echo("Mike is " & mike.age & " years old.")
Access modifiers are similar to Ruby.
class MyClass
private
# private attribute
@foo
def bar()
return @foo
end
end
If no access modifier is used, public is used.
Inheritance is done Ruby-style too.
class B < A
# Class B extends from A
end
In PHP you can define that a variable is a reference to another by using &, in Blueberry, it's the same
a &= b # a is a reference to b
def myFunc(&a)
# a is passed by reference and the value will be modified outside the function
a = a + 1
end
num = 1
myFunc(num)
# num is now 2