From 13c4c81a0f58692eab76a15848bdde2d5c34577e Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Sun, 13 Oct 2019 09:35:26 -0700 Subject: [PATCH 1/4] Fix error in comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb7164b..d6fd0d5 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ To waranty binary outputs: . . . - out[n+e-1] * (out[n+e-1] - 1) == 0 + out[n+e-1] * (out[n+e-1] - 1) === 0 */ From aecc28a79bdacf1678ece528e8e92004e5673e4d Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Wed, 4 Dec 2019 17:40:48 +0100 Subject: [PATCH 2/4] Fix array assignement and not allow assign with equal --- src/exec.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/exec.js b/src/exec.js index 92e6a7c..245a49d 100644 --- a/src/exec.js +++ b/src/exec.js @@ -638,12 +638,14 @@ function execVarAssignement(ctx, ast) { if (num.type == "COMPONENT") return execInstantiateComponet(ctx, v, ast.values[1]); if (ctx.error) return; -// if (num.type == "SIGNAL") return error(ctx, ast, "Cannot assign to a signal with `=` use <-- or <== ops"); + if ((num.type == "SIGNAL")&&(ast.op == "=")) return error(ctx, ast, "Cannot assign to a signal with `=` use <-- or <== ops"); + if ((["NUMBER", "COMPONENT"].indexOf(num.type) >= 0 )&&(ast.op != "=")) return error(ctx, ast, `Cannot assign to a var with ${ast.op}. use = op`); const res = exec(ctx, ast.values[1]); if (ctx.error) return; - setScope(ctx, v.name, v.selectors, res); + Object.assign(num, res); +// setScope(ctx, v.name, v.selectors, res); return v; } @@ -915,13 +917,13 @@ function execMul(ctx, ast) { function execVarAddAssignement(ctx, ast) { const res = execAdd(ctx,{ values: [ast.values[0], ast.values[1]] } ); if (ctx.error) return; - return execVarAssignement(ctx, { values: [ast.values[0], res] }); + return execVarAssignement(ctx, { op:"=", values: [ast.values[0], res] }); } function execVarMulAssignement(ctx, ast) { const res = execMul(ctx,{ values: [ast.values[0], ast.values[1]] } ); if (ctx.error) return; - return execVarAssignement(ctx, { values: [ast.values[0], res] }); + return execVarAssignement(ctx, { op:"=", values: [ast.values[0], res] }); } function execPlusPlusRight(ctx, ast) { @@ -929,7 +931,7 @@ function execPlusPlusRight(ctx, ast) { if (ctx.error) return; const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } ); if (ctx.error) return; - execVarAssignement(ctx, { values: [ast.values[0], resAfter] }); + execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] }); return resBefore; } @@ -937,7 +939,7 @@ function execPlusPlusLeft(ctx, ast) { if (ctx.error) return; const resAfter = execAdd(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } ); if (ctx.error) return; - execVarAssignement(ctx, { values: [ast.values[0], resAfter] }); + execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] }); return resAfter; } @@ -946,7 +948,7 @@ function execMinusMinusRight(ctx, ast) { if (ctx.error) return; const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } ); if (ctx.error) return; - execVarAssignement(ctx, { values: [ast.values[0], resAfter] }); + execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] }); return resBefore; } @@ -954,7 +956,7 @@ function execMinusMinusLeft(ctx, ast) { if (ctx.error) return; const resAfter = execSub(ctx,{ values: [ast.values[0], {type: "NUMBER", value: bigInt(1)}] } ); if (ctx.error) return; - execVarAssignement(ctx, { values: [ast.values[0], resAfter] }); + execVarAssignement(ctx, { op:"=", values: [ast.values[0], resAfter] }); return resAfter; } From 7181c372d9895a4c282aa39fff2622e4f7a9514c Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Wed, 4 Dec 2019 21:53:39 +0100 Subject: [PATCH 3/4] Error with bad assignments --- src/exec.js | 178 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 113 insertions(+), 65 deletions(-) diff --git a/src/exec.js b/src/exec.js index 245a49d..98199af 100644 --- a/src/exec.js +++ b/src/exec.js @@ -181,24 +181,6 @@ function iterateSelectors(ctx, sizes, baseName, fn) { return res; } -function setScope(ctx, name, selectors, value) { - let l = getScopeLevel(ctx, name); - if (l==-1) l= ctx.scopes.length-1; - - if (selectors.length == 0) { - ctx.scopes[l][name] = value; - } else { - setScopeArray(ctx.scopes[l][name], selectors); - } - - function setScopeArray(a, sels) { - if (sels.length == 1) { - a[sels[0].value] = value; - } else { - setScopeArray(a[sels[0]], sels.slice(1)); - } - } -} function getScope(ctx, name, selectors) { @@ -221,11 +203,57 @@ function getScope(ctx, name, selectors) { } for (let i=ctx.scopes.length-1; i>=0; i--) { - if (ctx.scopes[i][name]) return select(ctx.scopes[i][name], sels); + if (ctx.scopes[i][name]) return select(ctx.scopes[i][name].value, sels); } return null; } +function getScopeRef(ctx, name, selectors) { + + const sels = []; + if (selectors) { + for (let i=0; i< selectors.length; i++) { + const idx = exec(ctx, selectors[i]); + if (ctx.error) return; + + if (idx.type != "NUMBER") return error(ctx, selectors[i], "expected a number"); + sels.push( idx.value.toJSNumber() ); + } + } + + + function select(v, s, t) { + s = s || []; + if (s.length == 0) return [v, sels, t]; + return select(v[s[0]], s.slice(1), t); + } + + for (let i=ctx.scopes.length-1; i>=0; i--) { + if (ctx.scopes[i][name]) return select(ctx.scopes[i][name].value, sels, ctx.scopes[i][name].type); + } + return [null, [], ""]; +} + + +function setScopeRef(ctx, name, sels, value) { + let l = getScopeLevel(ctx, name); + if (l==-1) l= ctx.scopes.length-1; + + if (sels.length == 0) { + ctx.scopes[l][name].value = value; + } else { + setScopeArray(ctx.scopes[l][name].value, sels); + } + + function setScopeArray(a, sels) { + if (sels.length == 1) { + a[sels[0]] = value; + } else { + setScopeArray(a[sels[0]], sels.slice(1)); + } + } +} + function getScopeLevel(ctx, name) { for (let i=ctx.scopes.length-1; i>=0; i--) { if (ctx.scopes[i][name]) return i; @@ -249,11 +277,14 @@ function execTemplateDef(ctx, ast) { } scope[ast.name] = { type: "TEMPLATE", - params: ast.params, - block: ast.block, - fileName: ctx.fileName, - filePath: ctx.filePath, - scopes: copyScope(ctx.scopes) + value: { + type: "TEMPLATE", + params: ast.params, + block: ast.block, + fileName: ctx.fileName, + filePath: ctx.filePath, + scopes: copyScope(ctx.scopes) + } }; } @@ -266,11 +297,14 @@ function execFunctionDef(ctx, ast) { ctx.functionParams[ast.name] = ast.params; scope[ast.name] = { type: "FUNCTION", - params: ast.params, - block: ast.block, - fileName: ctx.fileName, - filePath: ctx.filePath, - scopes: copyScope(ctx.scopes) + value: { + type: "FUNCTION", + params: ast.params, + block: ast.block, + fileName: ctx.fileName, + filePath: ctx.filePath, + scopes: copyScope(ctx.scopes) + } }; } @@ -294,15 +328,18 @@ function execDeclareComponent(ctx, ast) { } - scope[ast.name.name] = iterateSelectors(ctx, sizes, baseName, function(fullName) { + scope[ast.name.name] = { + type: "COMPONENT", + value: iterateSelectors(ctx, sizes, baseName, function(fullName) { - ctx.components[fullName] = "UNINSTANTIATED"; + ctx.components[fullName] = "UNINSTANTIATED"; - return { - type: "COMPONENT", - fullName: fullName - }; - }); + return { + type: "COMPONENT", + fullName: fullName + }; + }) + }; return { type: "VARIABLE", @@ -378,7 +415,10 @@ function execInstantiateComponet(ctx, vr, fn) { const scope = {}; for (let i=0; i< template.params.length; i++) { - scope[template.params[i]] = paramValues[i]; + scope[template.params[i]] = { + type: "VARIABLE", + value: paramValues[i] + }; ctx.components[ctx.currentComponent].params[template.params[i]] = extractValue(paramValues[i]); } @@ -430,7 +470,10 @@ function execFunctionCall(ctx, ast) { const scope = {}; for (let i=0; i< fnc.params.length; i++) { - scope[fnc.params[i]] = paramValues[i]; + scope[fnc.params[i]] = { + type: "VARIABLE", + value: paramValues[i] + }; } ctx.fileName = fnc.fileName; @@ -472,21 +515,24 @@ function execDeclareSignal(ctx, ast) { sizes.push( size.value.toJSNumber() ); } - scope[ast.name.name] = iterateSelectors(ctx, sizes, baseName, function(fullName) { - ctx.signals[fullName] = { - fullName: fullName, - direction: ast.declareType == "SIGNALIN" ? "IN" : (ast.declareType == "SIGNALOUT" ? "OUT" : ""), - private: ast.private, - component: ctx.currentComponent, - equivalence: "", - alias: [fullName] - }; - ctx.components[ctx.currentComponent].signals.push(fullName); - return { - type: "SIGNAL", - fullName: fullName, - }; - }); + scope[ast.name.name] = { + type: "SIGNAL", + value: iterateSelectors(ctx, sizes, baseName, function(fullName) { + ctx.signals[fullName] = { + fullName: fullName, + direction: ast.declareType == "SIGNALIN" ? "IN" : (ast.declareType == "SIGNALOUT" ? "OUT" : ""), + private: ast.private, + component: ctx.currentComponent, + equivalence: "", + alias: [fullName] + }; + ctx.components[ctx.currentComponent].signals.push(fullName); + return { + type: "SIGNAL", + fullName: fullName, + }; + }) + }; return { type: "VARIABLE", name: ast.name.name, @@ -509,12 +555,15 @@ function execDeclareVariable(ctx, ast) { sizes.push( size.value.toJSNumber() ); } - scope[ast.name.name] = iterateSelectors(ctx, sizes, "", function() { - return { - type: "NUMBER", - value: bigInt(0) - }; - }); + scope[ast.name.name] = { + type: "VARIABLE", + value: iterateSelectors(ctx, sizes, "", function() { + return { + type: "NUMBER", + value: bigInt(0) + }; + }) + }; return { type: "VARIABLE", @@ -631,21 +680,20 @@ function execVarAssignement(ctx, ast) { } else { v = ast.values[0]; } - const num = getScope(ctx, v.name, v.selectors); + const [num, sels, typ] = getScopeRef(ctx, v.name, v.selectors); if (ctx.error) return; if ((typeof(num) != "object")||(num == null)) return error(ctx, ast, "Variable not defined"); - if (num.type == "COMPONENT") return execInstantiateComponet(ctx, v, ast.values[1]); + if (typ == "COMPONENT") return execInstantiateComponet(ctx, v, ast.values[1]); if (ctx.error) return; - if ((num.type == "SIGNAL")&&(ast.op == "=")) return error(ctx, ast, "Cannot assign to a signal with `=` use <-- or <== ops"); - if ((["NUMBER", "COMPONENT"].indexOf(num.type) >= 0 )&&(ast.op != "=")) return error(ctx, ast, `Cannot assign to a var with ${ast.op}. use = op`); + if ((typ == "SIGNAL")&&(ast.op == "=")) return error(ctx, ast, "Cannot assign to a signal with `=` use <-- or <== ops"); + if ((["NUMBER", "COMPONENT"].indexOf(typ) >= 0 )&&(ast.op != "=")) return error(ctx, ast, `Cannot assign to a var with ${ast.op}. use = op`); const res = exec(ctx, ast.values[1]); if (ctx.error) return; - Object.assign(num, res); -// setScope(ctx, v.name, v.selectors, res); + setScopeRef(ctx, v.name, sels, res); return v; } From 80846667eacdbbd6b44bf5fb6985fdb124a2fb20 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Wed, 4 Dec 2019 21:54:11 +0100 Subject: [PATCH 4/4] 0.0.35 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6e6df2e..eb70461 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "circom", - "version": "0.0.34", + "version": "0.0.35", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 626e695..c30c721 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "circom", - "version": "0.0.34", + "version": "0.0.35", "description": "Language to generate logic circuits", "main": "index.js", "directories": {