Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add rounding to language specification #47

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/language/common/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
18 changes: 18 additions & 0 deletions docs/language/common/modifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 26 additions & 8 deletions packages/ttsl-lang/src/language/grammar/safe-ds.langium
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -322,23 +323,22 @@ fragment TslFunctionFragment:
name=ID
typeParameterList=TslTypeParameterList?
parameterList=TslParameterList
('round' rounding=TslRounding)?
resultList=TslResultList?
(groupedBy=TslGroupedBy)?
constraintList=TslConstraintList?
body=TslFunctionBlock
;

interface TslFunctionBlock extends TslBlock{
timespans: TslTimespan[]
timespanStatement: TslTimespanStatement[]
}

TslFunctionBlock returns TslFunctionBlock:
{TslFunctionBlock}
'{' ( statements+=TslStatement*
| timespans+=TslTimespan
'{' ( timespans+=TslTimespan statements+=TslStatement
| statements+=TslStatement)*
'}')* '}'
{TslFunctionBlock}
'{' ( timespanStatement+=TslTimespanStatement
| statements+=TslStatement)*
'}'
;

interface TslConstant extends TslModuleMember {
Expand Down Expand Up @@ -1019,7 +1019,7 @@ TslTemplateStringEnd returns TslExpression:
interface TslModifier extends TslObject{}

interface TslVisibility extends TslModifier{
isPublic?: boolean
isPublic?: boolean
isPackageprivate?: boolean
isPrivate?: boolean
}
Expand All @@ -1044,6 +1044,14 @@ TslGroupedBy returns TslGroupedBy:
'groupedBy' id=TslExpression
;

interface TslRounding extends TslModifier{
rounding: string
}

TslRounding returns TslRounding:
':' rounding=('round'|'ceil'|'floor')
;

// -----------------------------------------------------------------------------
// Timespan
// -----------------------------------------------------------------------------
Expand All @@ -1057,6 +1065,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
// -----------------------------------------------------------------------------
Expand Down
Loading