Skip to content

Latest commit

 

History

History
343 lines (336 loc) · 6.22 KB

Expressions.md

File metadata and controls

343 lines (336 loc) · 6.22 KB

Expressions

In Seplin expressions are the only constructs which have some inherent value. They come in two flavours, 'value' and 'reference', where 'value' is just a value and 'reference' is a reference to some value.

Index


Bool

Flavour: value
Syntax: true | false
Type: bool
Explaination: A boolean literal.
Examples:

true
false

Int

Flavour: value
Syntax: n
Type: int
Explaination: An integer literal.
Examples:

0
1337

Char

Flavour: value
Syntax: 'n' | '\n'
Type: char
Explaination: A character literal.
Examples:

'a'
'\n'

String

Flavour: value
Syntax: " .* "
Type: char [ ]
Explaination: A string is simply a char array. Escape characters are not supported yet.
Examples:

"Hello world!"
"The next one is the empty string"
""

Null

Flavour: reference
Syntax: null
Type: N/A
Explaination: A reference to nothing.
Examples:

null

Access

Flavour: reference
Syntax:
name
context_alias # _name:
Type: the type of the accessed name.
Explaination: Access a declared name.
Examples:

x
my_array
other_context#my_routine

ArrayAccess

Flavour: reference
Syntax: reference [ int_expression ]
Type: the type of the array elements.
Explaination: Access an index in an array, the index must be an integer.
Examples:

x[0]
my_array[4]

StructAccess

Flavour: reference
Syntax: reference . field
Type: the type of the field.
Explaination: Access a field of a struct.
Examples:

l.head
my_tuple.fst

BinaryOperation

Flavour: value
Syntax: expression operator expression
Type: look at this table
Explaination: Compute some value from two expressions.
Examples:

1 + 2
true && false

UnaryOperation

Flavour: value
Syntax: operator expression
Type: look at this table
Explaination: Compute some value from a single expression.
Examples:

!true
-3

Ternary

Flavour: value
Syntax: bool_condition ? expression : expression
Type: The type of expression
Explaination: Compute some value depending on the result of bool_condition. The expression's must be of equatable types.
Examples:

true ? 1 : 2
i > 1 ? true : false

ArraySize

Flavour: value
Syntax: | expression |
Type: int
Explaination: Get the size of an array.
Examples:

|a|
|my_arrays[1]|

Read

Flavour: value
Syntax: read<type>
Type: type
Explaination: Get user input, only simple types are currently supported.
Examples:

read<int>
read<char>

NewArray

Flavour: value
Syntax: new type [ int_expression ]
Type: type[ ]
Explaination: Create a new empty array of some size.
Examples:

new int[5]
new tuple<int,int>[5]

ArrayLiteral

Flavour: value
Syntax: [ expressions ]
Type: depends on the expressions.
Explaination: Create a filled array. All expressions must be of the same type.
Examples:

[1, 2, 3]
[[1,2], [3,4], [4,5]]

NewStruct

Flavour: value
Syntax: new struct_name <type_arguments> ( arguments )
Type: stuct_name<type_arguments>
Explaination: Create a new struct instance. If the type arguments are left out, the compiler will attempt to infer them.
Examples:

new list<int>(1,null)
new tuple(1,2)

StructLiteral

Flavour: value
Syntax: { expressions }
Type: any struct type matching the structure of the expression.
Explaination: Create a new struct instance. Cannot be typed on its own.
Examples:

{1,2}
{1, {2, null}}

AnonymousRoutine

Flavour: value
Syntax:
( parameters ) block
< type_variables > ( parameters ) block
Type: Routine type of the types in parameters.
Explaination: Declares an unnamed routine. Parameters to routines can be of a routine type, making it a higher-order routine.
Examples:

(i: int) { i +:= 10; }

<T>(a: T, b: T) {
  tmp ::= a;
  a := b;
  b := tmp;
}

(i: int, routine: (int)) { 
  routine(i); 
}

Binary operators

Operator Left types Right types Result type Explaination
+ int int int Addition
- int int int Subtraction
* int int int Multiplication
/ int int int Division
% int int int Modolo
= * * bool Equivalence, can also be used for null checking
!= * * bool Non Equivalence, can also be used for null checking
< int int bool Less than
<= int int bool Less than or equal
> int int bool Greater than
>= int int bool Greate than or equal
&& bool bool bool Conjunction
|| bool bool bool Disjunction
* = any type

Unary operators

Operator Types Result type Explaination
! bool bool Negation
- int int Negation
$ * * Get the value of the operand, useful for dereferencing
* = any type