Constant definition

Basic definitions

Constant is defined by keyword let followed by its name. Then follows equals sign = and its value which can be any expression. At the end of constant definition is semicolon ;. In value expression is possible to use earlier defined constants, functions, operators etc. You can look at list of predefined constants, functions and operators. It is also possible to define new function, details about that are on function definition page.

Input

1
2
3
4
5
6
// constant definition example 01
let a = 3;
let b = (2 + 2) * 2 ^ 2;
let c = sqrt(a * b);
let a = 0;
process Constants with ConstantDumper;

Output

a = 0;
b = 16;
c = 6.92820323027551;

Global and local scope

Constants can be defined in global scope or in local scope of some L-system.

Input

1
2
3
4
5
6
7
// constant definition example 02
let glob = 8;
lsystem Lsystem {
let loc = 7;
set symbols axiom = A(glob) B(loc);
}
process all with SymbolPrinter;

Output

A(8) B(7)

Constants defined in local scope overlays constants from global scope, but they do not affect them.

Input

1
2
3
4
5
6
7
8
9
10
11
12
// constant definition example 03
let x = 8;
lsystem LsystemA {
let x = 0;
// x is 0 only in this L-system
set symbols axiom = A(x);
}
lsystem LsystemB {
// x is 8 here
set symbols axiom = B(x);
}
process all with SymbolPrinter;

Output

  1. A(0)
  2. B(8)

Inheritance of constants

Inheritance of constants works as expected. Constants from derived L-systems are defined and new constants can redefine them.

Input

1
2
3
4
5
6
7
8
9
10
// constant definition example 04
abstract lsystem Base {
let a = 1;
let b = 2;
}
lsystem Derived extends Base {
let a = 3;
set symbols axiom = A(a) B(b);
}
process all with SymbolPrinter;

Output

A(3) B(2)

Formal grammar

Defines constant with name represented by token ID as value of expression. If constant with same name already exists, it is redefined by new definition.

1
constant_def = 'let' ID '=' expression ';'