Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
(fix) issues with JS runtime for singleton/double
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Mar 10, 2019
1 parent 902c0ac commit 10a43d3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
18 changes: 15 additions & 3 deletions backends/javascript/ergo-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ function enhanced_cast(brands,v) {
}
function singleton(v) {
if (v.length == 1) {
return left(v[0]);
return v[0];
} else {
return right(null); /* Not a singleton */
return null; /* Not a singleton */
}
}
function unbrand(v) {
Expand Down Expand Up @@ -526,7 +526,19 @@ function substringNoLength(v, start) {
}

// Math operations
function float_of_string(s) { return parseFloat(s); }
function floatOfString(s) {
// Check whether we're dealing with nan, since it's the error case for Number.parseFloat
if (s === 'nan') {
return nan;
} else {
let num = Number.parseFloat(s);
if (Number.isNaN(num)) {
return null;
} else {
return num
}
}
}
function acos(x) { return Math.acos(x); }
function asin(x) { return Math.asin(x); }
function atan(x) { return Math.atan(x); }
Expand Down
6 changes: 3 additions & 3 deletions examples/volumediscount/logic8.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ contract VolumeDiscount over VolumeDiscountContract {
// Clause for volume discount
clause volumediscount(request : VolumeDiscountRequest) : VolumeDiscountResponse {
if request.netAnnualChargeVolume < contract.firstVolume
then return VolumeDiscountResponse{ discountRate: double(toString(contract.firstRate)) }
then return VolumeDiscountResponse{ discountRate: double("2000") }
else if request.netAnnualChargeVolume < contract.secondVolume
then return VolumeDiscountResponse{ discountRate: contract.secondRate }
else return VolumeDiscountResponse{ discountRate: contract.thirdRate }
then return VolumeDiscountResponse{ discountRate: double("2000") }
else return VolumeDiscountResponse{ discountRate: double("2000") }
}
}
8 changes: 4 additions & 4 deletions mechanization/Backend/Model/MathModelPart.v
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Inductive math_unary_op

Definition math_unary_op_tostring (f:math_unary_op) : String.string
:= match f with
| uop_math_of_string => "float_of_string"
| uop_math_of_string => "floatOfString"
| uop_math_acos => "acos"
| uop_math_asin => "asin"
| uop_math_atan => "atan"
Expand All @@ -101,7 +101,7 @@ Definition math_to_java_unary_op
(quotel:String.string) (fu:math_unary_op)
(d:java_json) : java_json
:= match fu with
| uop_math_of_string => mk_java_unary_op0 "float_of_string" d
| uop_math_of_string => mk_java_unary_op0 "floatOfString" d
| uop_math_acos => mk_java_unary_op0 "acos" d
| uop_math_asin => mk_java_unary_op0 "asin" d
| uop_math_atan => mk_java_unary_op0 "atan" d
Expand All @@ -118,7 +118,7 @@ Definition math_to_javascript_unary_op
(quotel:String.string) (fu:math_unary_op)
(d:String.string) : String.string
:= match fu with
| uop_math_of_string => "float_of_string(" ++ d ++ ")"
| uop_math_of_string => "floatOfString(" ++ d ++ ")"
| uop_math_acos => "acos(" ++ d ++ ")"
| uop_math_asin => "asin(" ++ d ++ ")"
| uop_math_atan => "atan(" ++ d ++ ")"
Expand All @@ -134,7 +134,7 @@ Definition math_to_ajavascript_unary_op
(fu:math_unary_op)
(e:JsSyntax.expr) : JsSyntax.expr
:= match fu with
| uop_math_of_string => call_runtime "float_of_string" [ e ]
| uop_math_of_string => call_runtime "floatOfString" [ e ]
| uop_math_acos => call_runtime "acos" [ e ]
| uop_math_asin => call_runtime "asin" [ e ]
| uop_math_atan => call_runtime "atan" [ e ]
Expand Down
2 changes: 1 addition & 1 deletion packages/ergo-cli/lib/ergoc-lib.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ergo-cli/lib/ergotop-lib.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ergo-compiler/lib/ergo-core.js

Large diffs are not rendered by default.

0 comments on commit 10a43d3

Please sign in to comment.