Skip to content

Commit

Permalink
- Ratio denominator is always positive
Browse files Browse the repository at this point in the history
- Ratio.round() implemented
- Ratio.to_real() rounds instead of truncating
- Real.round() using quotientInteger for division instead of divideInteger
  • Loading branch information
christianschmitz committed Sep 13, 2024
1 parent 07a56fb commit 5332353
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helios-lang/compiler",
"version": "0.17.0-79",
"version": "0.17.0-80",
"description": "Helios is a Domain Specific Language that compiles to Plutus-Core (i.e. Cardano on-chain validator scripts). Helios is a non-Haskell alternative to Plutus. With this library you can compile Helios scripts and build Cardano transactions, all you need to build 100% client-side dApps for Cardano.",
"main": "src/index.js",
"types": "types/index.d.ts",
Expand Down
81 changes: 69 additions & 12 deletions src/codegen/makeRawFuncs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ export function makeRawFunctions(simplify, isTestnet) {
`(a, b) -> {
bt = __helios__ratio__top(b);
bb = __helios__ratio__bottom(b);
__helios__ratio__new(
__helios__ratio__new_internal(
__helios__int____sub(
__helios__int____mul(a, bb),
bt
Expand Down Expand Up @@ -2059,7 +2059,7 @@ export function makeRawFunctions(simplify, isTestnet) {
)
add(
new RawFunc(
"__helios__ratio__new",
"__helios__ratio__new_internal",
`(top, bottom) -> {
__core__listData(
__core__mkCons(
Expand All @@ -2073,6 +2073,22 @@ export function makeRawFunctions(simplify, isTestnet) {
}`
)
)
add(
new RawFunc(
"__helios__ratio__new",
`(top, bottom) -> {
__core__ifThenElse(
__core__lessThanInteger(bottom, 0),
() -> {
__helios__ratio__new_internal(__core__multiplyInteger(top, -1), __core__multiplyInteger(bottom, -1))
},
() -> {
__helios__ratio__new_internal(top, bottom)
}
)()
}`
)
)
add(
new RawFunc(
"__helios__ratio__top",
Expand Down Expand Up @@ -2102,7 +2118,7 @@ export function makeRawFunctions(simplify, isTestnet) {
__helios__int____mul(at, bb),
__helios__int____mul(bt, ab)
);
__helios__ratio__new(new_top, new_bottom)
__helios__ratio__new_internal(new_top, new_bottom)
}`
)
)
Expand All @@ -2116,7 +2132,7 @@ export function makeRawFunctions(simplify, isTestnet) {
at,
__helios__int____mul(b, ab)
);
__helios__ratio__new(new_top, ab)
__helios__ratio__new_internal(new_top, ab)
}`
)
)
Expand All @@ -2133,7 +2149,7 @@ export function makeRawFunctions(simplify, isTestnet) {
__helios__int____mul(at, bb),
__helios__int____mul(bt, ab)
);
__helios__ratio__new(new_top, new_bottom)
__helios__ratio__new_internal(new_top, new_bottom)
}`
)
)
Expand All @@ -2147,7 +2163,7 @@ export function makeRawFunctions(simplify, isTestnet) {
at,
__helios__int____mul(b, ab)
);
__helios__ratio__new(new_top, ab)
__helios__ratio__new_internal(new_top, ab)
}`
)
)
Expand All @@ -2161,7 +2177,7 @@ export function makeRawFunctions(simplify, isTestnet) {
bb = __helios__ratio__bottom(b);
new_bottom = __helios__int____mul(ab, bb);
new_top = __helios__int____mul(at, bt);
__helios__ratio__new(new_top, new_bottom)
__helios__ratio__new_internal(new_top, new_bottom)
}`
)
)
Expand All @@ -2172,7 +2188,7 @@ export function makeRawFunctions(simplify, isTestnet) {
at = __helios__ratio__top(a);
ab = __helios__ratio__bottom(a);
new_top = __helios__int____mul(at, b);
__helios__ratio__new(new_top, ab)
__helios__ratio__new_internal(new_top, ab)
}`
)
)
Expand Down Expand Up @@ -2361,6 +2377,31 @@ export function makeRawFunctions(simplify, isTestnet) {
}`
)
)
add(
new RawFunc(
"__helios__ratio__round",
`(self) -> {
() -> {
top = __helios__ratio__top(self);
bottom = __helios__ratio__bottom(self);
__core__quotientInteger(
__core__addInteger(
top,
__core__quotientInteger(
bottom,
__core__ifThenElse(
__core__lessThanInteger(top, 0),
-2,
2
)
)
),
bottom
)
}
}`
)
)
add(
new RawFunc(
"__helios__ratio__to_real",
Expand All @@ -2369,7 +2410,17 @@ export function makeRawFunctions(simplify, isTestnet) {
top = __helios__ratio__top(self);
bottom = __helios__ratio__bottom(self);
__core__quotientInteger(
__core__multiplyInteger(top, __helios__real__ONE),
__core__addInteger(
__core__multiplyInteger(top, __helios__real__ONE),
__core__quotientInteger(
bottom,
__core__ifThenElse(
__core__lessThanInteger(top, 0),
-2,
2
)
)
),
bottom
)
}
Expand Down Expand Up @@ -2398,6 +2449,12 @@ export function makeRawFunctions(simplify, isTestnet) {
"5" + new Array(REAL_PRECISION - 1).fill("0").join("")
)
)
add(
new RawFunc(
"__helios__real__MINUS_HALF",
"-5" + new Array(REAL_PRECISION - 1).fill("0").join("")
)
)
add(
new RawFunc(
"__helios__real__NEARLY_ONE",
Expand Down Expand Up @@ -2587,8 +2644,8 @@ export function makeRawFunctions(simplify, isTestnet) {
"__helios__real__round",
`(self) -> {
() -> {
__core__divideInteger(
__core__addInteger(self, __helios__real__HALF),
__core__quotientInteger(
__core__addInteger(self, __core__ifThenElse(__core__lessThanInteger(self, 0), __helios__real__MINUS_HALF, __helios__real__HALF)),
__helios__real__ONE
)
}
Expand Down Expand Up @@ -2639,7 +2696,7 @@ export function makeRawFunctions(simplify, isTestnet) {
"__helios__real__to_ratio",
`(self) -> {
() -> {
__helios__ratio__new(
__helios__ratio__new_internal(
self,
__helios__real__ONE
)
Expand Down
11 changes: 8 additions & 3 deletions src/program/Program.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { UserFunc } from "./UserFunc.js"

/**
* @typedef {import("@helios-lang/compiler-utils").Site} Site
* @typedef {import("@helios-lang/ir").OptimizeOptions} OptimizeOptions
* @typedef {import("@helios-lang/ir").ParseOptions} ParseOptions
* @typedef {import("@helios-lang/uplc").UplcData} UplcData
* @typedef {import("@helios-lang/uplc").UplcValue} UplcValue
Expand All @@ -44,7 +45,7 @@ import { UserFunc } from "./UserFunc.js"

/**
* @typedef {{
* optimize?: boolean
* optimize?: boolean | OptimizeOptions
* dependsOnOwnHash?: boolean
* hashDependencies?: Record<string, string>
* validatorIndices?: Record<string, number>
Expand Down Expand Up @@ -275,7 +276,7 @@ export class Program {
: optimizeOrOptions

const hashDependencies = options.hashDependencies ?? {}
const optimize = options.optimize ?? false
const optimize = !!(options.optimize ?? false)

const ir = this.toIR({
dependsOnOwnHash: options.dependsOnOwnHash ?? false,
Expand All @@ -285,7 +286,11 @@ export class Program {

const uplc = compileIR(ir, {
optimize: optimize,
parseOptions: IR_PARSE_OPTIONS
parseOptions: IR_PARSE_OPTIONS,
optimizeOptions:
options.optimize && typeof options.optimize != "boolean"
? options.optimize
: undefined
})

// userfuncs might depend on own hash, which is easer to inject after compilation of main program
Expand Down
2 changes: 1 addition & 1 deletion src/program/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.17.0-79"
export const VERSION = "0.17.0-80"
3 changes: 2 additions & 1 deletion src/typecheck/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ export const RatioType = new GenericType({
ceil: new FuncType([], IntType),
floor: new FuncType([], IntType),
to_real: new FuncType([], RealType),
trunc: new FuncType([], IntType)
trunc: new FuncType([], IntType),
round: new FuncType([], IntType)
}),
genTypeMembers: (self) => ({
...genCommonTypeMembers(self),
Expand Down
4 changes: 4 additions & 0 deletions test/Int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ describe("Int", () => {
runner3([int(-9), int(10)], int(0))
})

it("-9999999999999 / 10000000000000 == 0", () => {
runner3([int(-9999999999999), int(10000000000000)], int(0))
})

it("-10 / 10 == -1", () => {
runner3([int(-10), int(10)], int(-1))
})
Expand Down
Loading

0 comments on commit 5332353

Please sign in to comment.