Fly Language Reference - fly-lang/fly GitHub Wiki
Fly is:
- High-Level language
- Multi-Paradigm
- Strongly typed
Data Types
Fly programs accept different data types which determines what type of value an object can have and what operations can be performed.
Numeric
Integer
This is a 32bit number type without decimals and can be positive or negative, which means you can store up to 2^32, or 4,294,967,296 unique values, from -2147483648 to 2147483648.
Syntax:
int a
Float
This is a 32-bit floating point number type with decimals and can be positive or negative.
Syntax:
float a
Boolean
This data type represent a value true or false. Even if we can consider it as a number (0 or 1) this type of data (only this) permit to use boolean logic operations.
Syntax:
bool a
Variables
Fly consider variables by different scope levels.
Local Variable
Is declared into a function and can be accessed only from the function and from child blocks. If you declare a variable without a value it takes the default value of declared type. All number types uses 0 as default value or false for bool. You can assign a value to a variable by considering the accept data from type. Each local variable is stack allocated, so born and die with the function.
Syntax:
{
int c
c = 1
}
Global Variable
Is declared at top level of the source code, it is not contained into a function, can be of all data types, but can be assigned only into a function. You have always a scope and optionally a qualifiers declaration.
int g
int f() { ... }
Functions
A function is composed by:
- a scope (public, private or default).
- a type.
- an identifier name.
- possibly empty or comma-separated list of the parameters.
- the body with instructions.
[public|private] [type] [identifier]([parameter1 [, parameter2 ...]]) {
[instructions ...]
}
Function declaration
Function declaration permit to define the function and its behavior, this is mandatory to make the function invocable. The return type in the body must be of the same type of the function type, if the function is void you don't need to return.
int do(int a) {
return a
}
Function call
You can call a function by invoking its name, with the correct number, order and type of parameters and (if is not void) by assigning to a var of the correct function type.
do(1)
Operations
These are the operations allowed.
Arithmetic operations
Description | Operator |
---|---|
Add | + |
Subtract | - |
Multiply | * |
Division | / |
Module | % |
And | & |
Or | | |
Xor ^ | |
Shift right | >> |
Shift left | << |
Increment | ++ |
Decrement | -- |
Boolean operations
Description | Operator |
---|---|
AND | && |
OR | || |
NOT | ! |
Comparations
Description | Operator |
---|---|
Equal | == |
Not equal | != |
Less than | < |
Less than or equal | <= |
Greater than | > |
Greater than or equal | >= |
Assignments
Description | Operator |
---|---|
Assignment | = |
Sum and assign | += |
Subtract and assign | -= |
Multiply and assign | *= |
Divide and assign | /= |
Module | %= |
And and assign | &= |
Or and assign | |= |
Shift left and assign | <<= |
Shift right and assign | >>= |
Xor and assign | ^= |
Flow Control
Fly permit to create different control statements for changing the flow control of the running program.
Condition
All of these are based on condition statement.
If Else
This is the base condition used for checking an expression and choosing between two branch, if the result is true take the first way, else take the second. You can enhance the number of ways by adding more else if serially.
if a == 1 {
} elsif a == 2 {
} else {
}
Switch Case
This statement permit to evaluate if one expression is equal to one ore more values.
switch a {
case 1:
...
break
case 2:
...
break
default:
...
}
Loop
All of these are based on cycle loop of statements.
While
While loop repeat until the condition is true.
while a < 10 {
...
}
For
With for loop you can iterate in a controlled way, by an initialization, a condition and an increment.
for int i=1; i<10; i++ {
...
}
Namespace
You can define a Namespace for grouping parts of source or close visibility like in a package.
The namespace declaration must be the first of the module, if you don't specify a namespace it takes the default
namespace.
namespace n1
int a = 1
public int f() {}
It is declared a namespace called n1 with a global var and a function.
n1:f()
You have to call f() from another namespace by specifying the namespace thanks to public.
a = 2
You can assign a
without specifying the namespace if you are in the same module or a in module with same namespace n1
(thanks to default).
Imports
The import keyword permit to import a namespace in the current source of code.
Scopes
Each declaration (like global variable and function) has a scope, different scopes exist with different visibility level:
Private
Visible only into current module.
private int a
Default
Visible into current module and all modules declared with the same namespace.
int a
Public
Visible into current module, in and out of the current namespace modules.
public int a
Qualifiers
All Variables can be declared with qualifiers.
Constant
Represents a constant value, so a variable which cannot be modified after the first assignment.
const public int a = 1;