Commit 841e39cd authored by Matthias Simon's avatar Matthias Simon
Browse files

Add constants and module parameters

parent ec2c95c6
Loading
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -14,11 +14,8 @@ Declarator ::= name {"[" integer "]"} [":=" Expr].
A _variable declaration_ adds a new variable with given _name_ into its
enclosing scope (see [uniqueness of identifiers]).

A variable shall only be declared in scopes where permitted explicitly.

> NOTE: For example, a variable may be declared in component type definition, but
> not at module scope. In which scope a variable can be declared is specified in
> the respective chapters.
A variable shall only be declared in [statement blocks], [alt blocks],
[component type definitions] and in [class definitions].

Each variable has a static type associated with it.
The type of the variable is specified using a _[type expression]_
@@ -117,8 +114,8 @@ A variable declaration may also specify attributes using a `with`-clause.
 }
 ```

```ttcn3 example = "ambigous type"
var t := ? // error: ambigous type
```ttcn3 example = "ambiguous type"
var t := ? // error: ambiguous type
```

```ttcn3 example = "inferred template type"
+48 −0
Original line number Diff line number Diff line
# 11. Declaring constants

A constant is a variable that can be assigned a value only once.

**Syntactical Structure**

```ebnf
ConstDecl ::= [visibility] "const" [TypeExpr] Declarator { "," Declarator} [With].
```

**Semantic Description**

The rules for constants are the same as for [variables], with following
differences:

- every constant in a _constant declaration_ shall have an _initial value assignment_.
- a constant shall be assigned a value to, only once during its [lifetime].
- a constant may be declared in a module scope, also.

> NOTE: Constants and regular variables have very different use-cases;
> conceptionally they are both addressable memory with a name, value, type,
> inference rules, ...

> NOTE: A tool is permitted to implement optimizations, such as pre-computing
> values, moving expensive expression out of the loop as long TTCN-3 semantics
> are not violated.

> NOTE: A tool vendor is permitted to forbid the use of constant as call by
> reference parameters.

**Examples**

```ttcn3 example = "required initial value assignment"
const integer i, j := 0; // error: initial value assignment missing for i
```

```ttcn3 example = "only one assignment during lifetime"
for (var i in {1,2,3}) {
    const j := i*2; // ok: every iteration is a new constant
}
```

```ttcn3 example = "multiple assignments"
const integer i := 0;
{
    i := 1; // error: i has already a value assigned to it (0)
}
```
+0 −12
Original line number Diff line number Diff line
## 11. Declaring module parameters

A module parameter is a variable that can be assigned a value only once per test execution.

**Syntactic Structure**

```ebnf
ModuleparDecl ::= "modulepar" [TypeExpr] Declarator { "," Declarator} [With].
```

**Semantic Description**

src/12-declaring-constants.md

deleted100644 → 0
+0 −11
Original line number Diff line number Diff line
## 12. Declaring constants

A constant is a variable that can be assigned a value only once per test execution.

**Syntactic Structure**

```ebnf
ConstDecl ::= "const" [TypeExpr] Declarator { "," Declarator} [With].
```

**Semantic Description**
+36 −0
Original line number Diff line number Diff line
# 12. Declaring module parameters

A module parameter is a constant that can be assigned a value provided by the
test environment, only once per [test run].

**Syntactical Structure**

```ebnf
ModuleparDecl ::= [visibility] "modulepar" [TypeExpr] Declarator { "," Declarator} [With].
```

**Semantic Description**

The rules for module parameters are the same as for [variables], with following
differences:

- a module parameter may be declared in a module scope, only.
- a module parameter shall be assigned a value, not once per lifetime, but once per [test run].
- the value can be provided by the test environment during runtime.



**Examples**

```ttcn3 example = "module parameters"
module M {
    modulepar integer i;
    modulepar integer j := 0;
}
```

```ttcn3 example = "module parameter variations"
module M {
    modulepar template integer i, j[3] := {1,2,3}
}
```