From e8ce804af1255f984f687aed3bcc74eae22f59b3 Mon Sep 17 00:00:00 2001 From: methr0 Date: Sat, 1 Jun 2024 12:17:17 +0200 Subject: [PATCH 1/4] add rounding to syntax --- .../src/language/grammar/safe-ds.langium | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/ttsl-lang/src/language/grammar/safe-ds.langium b/packages/ttsl-lang/src/language/grammar/safe-ds.langium index 79698e7a..df6469cf 100644 --- a/packages/ttsl-lang/src/language/grammar/safe-ds.langium +++ b/packages/ttsl-lang/src/language/grammar/safe-ds.langium @@ -329,16 +329,14 @@ fragment TslFunctionFragment: ; interface TslFunctionBlock extends TslBlock{ - timespans: TslTimespan[] + timespanStatement: TslTimespanStatement[] } TslFunctionBlock returns TslFunctionBlock: {TslFunctionBlock} - '{' ( statements+=TslStatement* - | timespans+=TslTimespan - '{' ( timespans+=TslTimespan statements+=TslStatement + '{' ( timespanStatement+=TslTimespanStatement | statements+=TslStatement)* - '}')* '}' + '}' ; interface TslConstant extends TslModuleMember { @@ -1044,6 +1042,14 @@ TslGroupedBy returns TslGroupedBy: 'groupedBy' id=TslExpression ; +interface TslRounding extends TslModifier{ + rounding: TslString +} + +TslRounding returns TslRounding: + ':' rounding=TslString +; + // ----------------------------------------------------------------------------- // Timespan // ----------------------------------------------------------------------------- @@ -1057,6 +1063,16 @@ TslTimespan returns TslTimespan: ('from' start=TslDate)? ('to' end=TslDate)? ; +interface TslTimespanStatement extends TslBlock, TslStatement{ + timespan: TslTimespan + rounding?: TslRounding + block: TslFunctionBlock +} + +TslTimespanStatement returns TslTimespanStatement: + timespan=TslTimespan (rounding=TslRounding)* '{' block=TslFunctionBlock '}' +; + // ----------------------------------------------------------------------------- // Types // ----------------------------------------------------------------------------- From 9b03cbb90727c745e8996182c7419514d97a7579 Mon Sep 17 00:00:00 2001 From: methr0 Date: Mon, 3 Jun 2024 10:49:17 +0200 Subject: [PATCH 2/4] add rounding for complete function body --- packages/ttsl-lang/src/language/grammar/safe-ds.langium | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/ttsl-lang/src/language/grammar/safe-ds.langium b/packages/ttsl-lang/src/language/grammar/safe-ds.langium index df6469cf..013d64ce 100644 --- a/packages/ttsl-lang/src/language/grammar/safe-ds.langium +++ b/packages/ttsl-lang/src/language/grammar/safe-ds.langium @@ -307,6 +307,7 @@ TslEnumVariant returns TslEnumVariant: interface TslFunction extends TslCallable, TslClassMember, TslModuleMember { isID: boolean typeParameterList?: TslTypeParameterList + rounding?: TslRounding resultList?: TslResultList timeunit?: TslTimeunit groupedBy?: TslGroupedBy @@ -322,6 +323,7 @@ fragment TslFunctionFragment: name=ID typeParameterList=TslTypeParameterList? parameterList=TslParameterList + ('round' rounding=TslRounding)? resultList=TslResultList? (groupedBy=TslGroupedBy)? constraintList=TslConstraintList? @@ -1070,7 +1072,7 @@ interface TslTimespanStatement extends TslBlock, TslStatement{ } TslTimespanStatement returns TslTimespanStatement: - timespan=TslTimespan (rounding=TslRounding)* '{' block=TslFunctionBlock '}' + timespan=TslTimespan (rounding=TslRounding)? '{' block=TslFunctionBlock '}' ; // ----------------------------------------------------------------------------- From b432271ef9678e1d1f7c1154bf61497ab8893a55 Mon Sep 17 00:00:00 2001 From: methr0 Date: Mon, 3 Jun 2024 11:12:08 +0200 Subject: [PATCH 3/4] Documentation for rounding --- docs/language/common/functions.md | 6 ++++++ docs/language/common/modifier.md | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/docs/language/common/functions.md b/docs/language/common/functions.md index fb88b5a3..612ee78e 100644 --- a/docs/language/common/functions.md +++ b/docs/language/common/functions.md @@ -67,6 +67,11 @@ function taxes(name: String): List GroupedBy XYZ_id {...} In this example the return value of the function is grouped by XYZ_id. +## Rounding + +The [rounding modifier][rounding] can be added to a function of a single [timespan][Validity] in the function to define the rounding direction for the return value. More detailed Documentation can be found in the [rounding modifier][rounding] section. + + [Parameters]: parameters.md [Type]: types.md [id]: modifier.md#id @@ -77,3 +82,4 @@ In this example the return value of the function is grouped by XYZ_id. [modifier timespan]: modifier.md#timespan [Statement]: statements.md [Aggregations]: aggregations.md +[rounding]: modifier.md#rounding diff --git a/docs/language/common/modifier.md b/docs/language/common/modifier.md index 0f769f10..c2cff812 100644 --- a/docs/language/common/modifier.md +++ b/docs/language/common/modifier.md @@ -57,7 +57,25 @@ var x = per week f() y = per year salary ``` +## Rounding + +[Functions][Functions] can return a rounded value. The direction in which the value should be rounded can be specified for the whole function of for specific [timespans][Timespans]. + +```ttsl +# For the whole function: +function f() round:ceil : Int {...} + +# For a specific timespan: +from 2001-01-01: ceil {} +``` + +The options for rounding are following: +- ceil: round up +- floor: round down +- round: round depending on the following + [Constants]:constants.md [Data]:data.md [Functions]:functions.md [Aggregation]: aggregations.md +[Timespans]: validity.md From 1517465405002a268824b036588f785af5f8fb0a Mon Sep 17 00:00:00 2001 From: methr0 Date: Wed, 5 Jun 2024 11:40:33 +0200 Subject: [PATCH 4/4] change rounding so that only round, ceil and floor are accepted --- .../ttsl-lang/src/language/grammar/safe-ds.langium | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/ttsl-lang/src/language/grammar/safe-ds.langium b/packages/ttsl-lang/src/language/grammar/safe-ds.langium index 013d64ce..5b6f5207 100644 --- a/packages/ttsl-lang/src/language/grammar/safe-ds.langium +++ b/packages/ttsl-lang/src/language/grammar/safe-ds.langium @@ -335,9 +335,9 @@ interface TslFunctionBlock extends TslBlock{ } TslFunctionBlock returns TslFunctionBlock: - {TslFunctionBlock} + {TslFunctionBlock} '{' ( timespanStatement+=TslTimespanStatement - | statements+=TslStatement)* + | statements+=TslStatement)* '}' ; @@ -1019,7 +1019,7 @@ TslTemplateStringEnd returns TslExpression: interface TslModifier extends TslObject{} interface TslVisibility extends TslModifier{ - isPublic?: boolean + isPublic?: boolean isPackageprivate?: boolean isPrivate?: boolean } @@ -1045,11 +1045,11 @@ TslGroupedBy returns TslGroupedBy: ; interface TslRounding extends TslModifier{ - rounding: TslString + rounding: string } TslRounding returns TslRounding: - ':' rounding=TslString + ':' rounding=('round'|'ceil'|'floor') ; // -----------------------------------------------------------------------------