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

Commit

Permalink
(fix) Ergo-specific validation&serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Oct 2, 2019
1 parent e7ccbed commit 500b0f7
Show file tree
Hide file tree
Showing 39 changed files with 5,342 additions and 3,178 deletions.
112 changes: 73 additions & 39 deletions backends/javascript/ergo-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ function arithMean(b) {
}
}
function toString(v) {
return toStringQ(v, "\"", false);
return toStringQ(v, "\"");
}
function generateText(v) {
return toStringQ(v, "", true);
return toTextQ(v, "");
}
function toStringQ(v, quote, generateText) {
function toStringQ(v, quote) {
if (v === null)
return "null";
var t = typeof v;
Expand All @@ -220,27 +220,66 @@ function toStringQ(v, quote, generateText) {
if ({}.toString.apply(v) == "[object Array]") {
v = v.slice();
v.sort();
var result = generateText ? "" : "[";
var result = "[";
for (var i=0, n=v.length; i<n; i++) {
if (i > 0)
generateText ? result += "" : result += ", ";
result += toStringQ(v[i], quote);
result += ", ";
result += toStringQ(v[i], quote);
}
return generateText ? result + "" : result + "]";
return result + "]";
}
if (moment.isMoment(v)) {
return v.format();
}
if(v.hasOwnProperty('nat')){
return "" + v.nat;
}
var result2 = generateText ? "" : "{";
var result2 = "{";
var first = true;
for (var key in v) {
if (first) first = false; else result2 += generateText ? "" : ", ";
result2 += generateText ? toStringQ(v[key], quote) : toStringQ(key, quote) + ": " + toStringQ(v[key], quote);
if (first) first = false; else result2 += ", ";
result2 += toStringQ(key, quote) + ": " + toStringQ(v[key], quote);
}
result2 += "}";
return result2;
}
function toTextQ(v, quote) {
if (v === null)
return "null";
var t = typeof v;
if (t == "string")
return quote + v + quote;
if (t == "boolean")
return "" + v;
if (t == "number") {
if (Math.floor(v) == v) return (new Number(v)).toFixed(1); // Make sure there is always decimal point
else return "" + v;
}
if ({}.toString.apply(v) == "[object Array]") {
v = v.slice();
v.sort();
var result = "";
for (var i=0, n=v.length; i<n; i++) {
if (i > 0)
result += "";
result += toTextQ(v[i], quote);
}
return result + "";
}
if (moment.isMoment(v)) {
return v.format();
}
if(v.hasOwnProperty('nat')){
return "" + v.nat;
}
var result2 = "";
var first = true;
for (var key in v) {
if (key !== "$class") {
if (first) first = false; else result2 += " ";
result2 += toTextQ(v[key], quote);
}
}
result2 += generateText ? "" : "}";
return result2;
}
function bunion(b1, b2) {
Expand Down Expand Up @@ -307,10 +346,7 @@ function bmax(b1, b2) {
return result;
}
function bnth(b1, n) {
var index = n;
if(n.hasOwnProperty('nat')){
index = n.nat;
}
var index = natUnbox(n);
if (b1[index]) {
return b1[index];
} else {
Expand Down Expand Up @@ -461,80 +497,79 @@ function escapeRegExp(string){
}

// Nat operations
function natBox(v) {
return { "nat": v };
}
function natUnbox(v) {
var t = typeof v;
if (t == "number") { return Math.floor(v); }
if (t == "object") { if (v !== null) if (v.hasOwnProperty('nat')) return Math.floor(v.nat) };
return v;
}
function natPlus(v1, v2) {
return natUnbox(v1) + natUnbox(v2);
return natBox(natUnbox(v1) + natUnbox(v2));
}
function natMinus(v1, v2) {
return natUnbox(v1) - natUnbox(v2);
return natBox(natUnbox(v1) - natUnbox(v2));
}
function natMult(v1, v2) {
return natUnbox(v1) * natUnbox(v2);
return natBox(natUnbox(v1) * natUnbox(v2));
}
function natDiv(v1, v2) {
return Math.floor(natUnbox(v1) / natUnbox(v2));
return natBox(Math.floor(natUnbox(v1) / natUnbox(v2)));
}
function natRem(v1, v2) {
return Math.floor(natUnbox(v1) % natUnbox(v2));
return natBox(Math.floor(natUnbox(v1) % natUnbox(v2)));
}
function natMin(v1, v2) {
return Math.min(natUnbox(v1),natUnbox(v2));
return natBox(Math.min(natUnbox(v1),natUnbox(v2)));
}
function natMax(v1, v2) {
return Math.max(natUnbox(v1),natUnbox(v2));
return natBox(Math.max(natUnbox(v1),natUnbox(v2)));
}
function natAbs(v) {
return Math.abs(natUnbox(v1),natUnbox(v2));
return natBox(Math.abs(natUnbox(v1),natUnbox(v2)));
}
function natLog2(v) {
return Math.floor(Math.log2(natUnbox(v))); // Default Z.log2 is log_inf, biggest integer lower than log2
return natBox(Math.floor(Math.log2(natUnbox(v)))); // Default Z.log2 is log_inf, biggest integer lower than log2
}
function natSqrt(v) {
return Math.floor(Math.sqrt(natUnbox(v))); // See Z.sqrt biggest integer lower than sqrt
return natBox(Math.floor(Math.sqrt(natUnbox(v)))); // See Z.sqrt biggest integer lower than sqrt
}
function natSum(b) {
var result = 0;
for (var i=0; i<b.length; i++)
result += natUnbox(b[i]);
return result;
return natBox(result);
}
function natMinApply(b) {
var numbers = [ ];
for (var i=0; i<b.length; i++)
numbers.push(natUnbox(b[i].nat));
return Math.min.apply(Math,numbers);
return natBox(Math.min.apply(Math,numbers));
}
function natMaxApply(b) {
var numbers = [ ];
for (var i=0; i<b.length; i++)
numbers.push(natUnbox(b[i]));
return Math.max.apply(Math,numbers);
return natBox(Math.max.apply(Math,numbers));
}
function natArithMean(b) {
var len = b.length;
if(len == 0) {
return 0;
return natBox(0);
} else {
return Math.floor(natSum(b)/len);
return natBox(Math.floor(natSum(b)/len));
}
}
function count(v) {
return v.length;
return natBox(v.length);
}
function stringLength(v) {
return { "nat" : v.length };
return natBox(v.length);
}
function floatOfNat(v) {
if(v.hasOwnProperty('nat')){
return "" + v.nat;
} else {
return v;
}
return natUnbox(v);
}
function substring(v, start, len) {
return v.substring(start,len);
Expand Down Expand Up @@ -654,8 +689,7 @@ function dateTimePeriodFromString(stringDuration) {

function dateTimeDurationFromNat(part, v) {
mustBeUnit(part);
var num;
if (v.hasOwnProperty('nat')) { num = v.nat; } else { num = v; }
var num = natUnbox(v);
// 'quarters' not built into durations
if (part === QUARTERS) {
return moment.duration(num * 3,'months');
Expand Down
4 changes: 3 additions & 1 deletion examples/helloworld/logic8.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*/
namespace org.accordproject.helloworld

import org.accordproject.markdown.*

contract HelloWorld over TemplateModel {
// Generate Text
clause generateText() : TextResponse {
clause toText(options:MarkdownOptions) : TextResponse {
return TextResponse { text : {{
Name of the person to greet: {{contract.name}}.
Thank you!
Expand Down
21 changes: 21 additions & 0 deletions examples/helloworld/logic9.ergo
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace org.accordproject.helloworld

contract HelloWorld over TemplateModel {
// Simple Clause
clause helloworld(request : Request) : Response {
return Response{ output: "Hello " ++ contract.name ++ " (" ++ request.input ++ ")", number: doubleToInteger(0.0) }
}
}
39 changes: 39 additions & 0 deletions examples/helloworld/model2.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.helloworld

transaction Request {
o String input
}

transaction Response {
o String output
o Integer number
}

/**
* The template model
*/
@AccordTemplateModel("helloworld")
concept TemplateModel {
/**
* The name for the clause
*/
o String name
}

concept TextResponse {
o String text
}
14 changes: 14 additions & 0 deletions examples/interests/interests.ergo
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.loan.interests

import org.accordproject.time.*
Expand Down
3 changes: 2 additions & 1 deletion examples/interests/logic.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.interests

import org.accordproject.loan.interests.*
import org.accordproject.markdown.*

contract Interests over TemplateModel {
// Generate Text
clause generateText(options:MarkdownOptions) : String {
clause toText(options:MarkdownOptions) : String {
return {{
This is a fixed interest loan to the amount of [{loanAmount}]
at the yearly interest rate of [{rate}]%
Expand Down
2 changes: 1 addition & 1 deletion examples/interests/logic2.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.accordproject.markdown.*

contract Interests over TemplateModel {
// Generate Text
clause generateText(options:MarkdownOptions) : String {
clause toText(options:MarkdownOptions) : String {
return {{
This is a fixed interest loan to the amount of [{loanAmount}]
at the yearly interest rate of [{rate}]%
Expand Down
2 changes: 1 addition & 1 deletion examples/interests/logic3.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.accordproject.markdown.*

contract Interests over TemplateModel {
// Generate Text
clause generateText(options:MarkdownOptions) : String {
clause toText(options:MarkdownOptions) : String {
return {{
This is a fixed interest loan to the amount of [{loanAmount}]
at the yearly interest rate of [{rate}]%
Expand Down
14 changes: 14 additions & 0 deletions examples/interestsvar/interests.ergo
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.loan.interests

import org.accordproject.time.*
Expand Down
3 changes: 2 additions & 1 deletion examples/interestsvar/logic.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.interests

import org.accordproject.loan.interests.*
import org.accordproject.markdown.*

contract Interests over TemplateModel {
// Generate Text
clause generateText(options:MarkdownOptions) : String {
clause toText(options:MarkdownOptions) : String {
return {{
This is a fixed interest loan to the amount of [{loanAmount}]
at the yearly interest rate of [{rate}]%
Expand Down
1 change: 1 addition & 0 deletions examples/interestsvar/logic2.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.interests

import org.accordproject.loan.interests.*
Expand Down
Loading

0 comments on commit 500b0f7

Please sign in to comment.