-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/core/compile: add integer division builtins
These are to replace the namesake operators, which will be removed at some point. Next step is to create a rewriter that rewrites old uses to the new one. Change-Id: Ib674559e40f714e24e40e6c9ed71f7eb43231389 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7784 Reviewed-by: CUE cueckoo <cueckoo@gmail.com> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
- Loading branch information
Showing
6 changed files
with
292 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
-- in.cue -- | ||
quo1: quo(5, 2) // 2 | ||
quo2: quo(5, -2) // -2 | ||
quo3: quo(-5, 2) // -2 | ||
quo4: quo(-5, -2) // 2 | ||
|
||
quoDivByZero: quo(2, 0) | ||
|
||
quoTypeError1: quo(2.0, 1) | ||
quoTypeError2: quo(2, 1.0) | ||
|
||
|
||
rem1: rem(5, 2) // 1 | ||
rem2: rem(5, -2) // 1 | ||
rem3: rem(-5, 2) // -1 | ||
rem4: rem(-5, -2) // -1 | ||
|
||
remDivByZero: rem(2, 0) | ||
|
||
remTypeError1: rem(2.0, 1) | ||
remTypeError2: rem(2, 1.0) | ||
|
||
|
||
div1: div(5, 2) // 2 | ||
div2: div(5, -2) // -2 | ||
div3: div(-5, 2) // -3 | ||
div4: div(-5, -2) // 3 | ||
|
||
divDivByZero: div(2, 0) | ||
|
||
divTypeError1: div(2.0, 1) | ||
divTypeError2: div(2, 1.0) | ||
|
||
|
||
mod1: mod(5, 2) // 1 | ||
mod2: mod(5, -2) // 1 | ||
mod3: mod(-5, 2) // 1 | ||
mod4: mod(-5, -2) // 1 | ||
|
||
modDivByZero: mod(2, 0) | ||
|
||
modTypeError1: mod(2.0, 1) | ||
modTypeError2: mod(2, 1.0) | ||
|
||
-- out/eval -- | ||
Errors: | ||
quoDivByZero: division by zero: | ||
./in.cue:6:15 | ||
quoTypeError1: cannot use 2.0 (type float) as int in argument 1 to quo: | ||
./in.cue:8:20 | ||
quoTypeError2: cannot use 1.0 (type float) as int in argument 2 to quo: | ||
./in.cue:9:23 | ||
remDivByZero: division by zero: | ||
./in.cue:17:15 | ||
remTypeError1: cannot use 2.0 (type float) as int in argument 1 to rem: | ||
./in.cue:19:20 | ||
remTypeError2: cannot use 1.0 (type float) as int in argument 2 to rem: | ||
./in.cue:20:23 | ||
divDivByZero: division by zero: | ||
./in.cue:28:15 | ||
divTypeError1: cannot use 2.0 (type float) as int in argument 1 to div: | ||
./in.cue:30:20 | ||
divTypeError2: cannot use 1.0 (type float) as int in argument 2 to div: | ||
./in.cue:31:23 | ||
modDivByZero: division by zero: | ||
./in.cue:39:15 | ||
modTypeError1: cannot use 2.0 (type float) as int in argument 1 to mod: | ||
./in.cue:41:20 | ||
modTypeError2: cannot use 1.0 (type float) as int in argument 2 to mod: | ||
./in.cue:42:23 | ||
|
||
Result: | ||
(_|_){ | ||
// [eval] | ||
quo1: (int){ 2 } | ||
quo2: (int){ -2 } | ||
quo3: (int){ -2 } | ||
quo4: (int){ 2 } | ||
quoDivByZero: (_|_){ | ||
// [eval] quoDivByZero: division by zero: | ||
// ./in.cue:6:15 | ||
} | ||
quoTypeError1: (_|_){ | ||
// [eval] quoTypeError1: cannot use 2.0 (type float) as int in argument 1 to quo: | ||
// ./in.cue:8:20 | ||
} | ||
quoTypeError2: (_|_){ | ||
// [eval] quoTypeError2: cannot use 1.0 (type float) as int in argument 2 to quo: | ||
// ./in.cue:9:23 | ||
} | ||
rem1: (int){ 1 } | ||
rem2: (int){ 1 } | ||
rem3: (int){ -1 } | ||
rem4: (int){ -1 } | ||
remDivByZero: (_|_){ | ||
// [eval] remDivByZero: division by zero: | ||
// ./in.cue:17:15 | ||
} | ||
remTypeError1: (_|_){ | ||
// [eval] remTypeError1: cannot use 2.0 (type float) as int in argument 1 to rem: | ||
// ./in.cue:19:20 | ||
} | ||
remTypeError2: (_|_){ | ||
// [eval] remTypeError2: cannot use 1.0 (type float) as int in argument 2 to rem: | ||
// ./in.cue:20:23 | ||
} | ||
div1: (int){ 2 } | ||
div2: (int){ -2 } | ||
div3: (int){ -3 } | ||
div4: (int){ 3 } | ||
divDivByZero: (_|_){ | ||
// [eval] divDivByZero: division by zero: | ||
// ./in.cue:28:15 | ||
} | ||
divTypeError1: (_|_){ | ||
// [eval] divTypeError1: cannot use 2.0 (type float) as int in argument 1 to div: | ||
// ./in.cue:30:20 | ||
} | ||
divTypeError2: (_|_){ | ||
// [eval] divTypeError2: cannot use 1.0 (type float) as int in argument 2 to div: | ||
// ./in.cue:31:23 | ||
} | ||
mod1: (int){ 1 } | ||
mod2: (int){ 1 } | ||
mod3: (int){ 1 } | ||
mod4: (int){ 1 } | ||
modDivByZero: (_|_){ | ||
// [eval] modDivByZero: division by zero: | ||
// ./in.cue:39:15 | ||
} | ||
modTypeError1: (_|_){ | ||
// [eval] modTypeError1: cannot use 2.0 (type float) as int in argument 1 to mod: | ||
// ./in.cue:41:20 | ||
} | ||
modTypeError2: (_|_){ | ||
// [eval] modTypeError2: cannot use 1.0 (type float) as int in argument 2 to mod: | ||
// ./in.cue:42:23 | ||
} | ||
} | ||
-- out/compile -- | ||
--- in.cue | ||
{ | ||
quo1: quo(5, 2) | ||
quo2: quo(5, -2) | ||
quo3: quo(-5, 2) | ||
quo4: quo(-5, -2) | ||
quoDivByZero: quo(2, 0) | ||
quoTypeError1: quo(2.0, 1) | ||
quoTypeError2: quo(2, 1.0) | ||
rem1: rem(5, 2) | ||
rem2: rem(5, -2) | ||
rem3: rem(-5, 2) | ||
rem4: rem(-5, -2) | ||
remDivByZero: rem(2, 0) | ||
remTypeError1: rem(2.0, 1) | ||
remTypeError2: rem(2, 1.0) | ||
div1: div(5, 2) | ||
div2: div(5, -2) | ||
div3: div(-5, 2) | ||
div4: div(-5, -2) | ||
divDivByZero: div(2, 0) | ||
divTypeError1: div(2.0, 1) | ||
divTypeError2: div(2, 1.0) | ||
mod1: mod(5, 2) | ||
mod2: mod(5, -2) | ||
mod3: mod(-5, 2) | ||
mod4: mod(-5, -2) | ||
modDivByZero: mod(2, 0) | ||
modTypeError1: mod(2.0, 1) | ||
modTypeError2: mod(2, 1.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.