mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-06 18:56:40 +01:00
Merge branch 'master' into c_build
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "circom",
|
"name": "circom",
|
||||||
"version": "0.0.34",
|
"version": "0.0.35",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "circom",
|
"name": "circom",
|
||||||
"version": "0.0.34",
|
"version": "0.0.35",
|
||||||
"description": "Language to generate logic circuits",
|
"description": "Language to generate logic circuits",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"directories": {
|
"directories": {
|
||||||
|
|||||||
106
src/exec.js
106
src/exec.js
@@ -183,24 +183,6 @@ function iterateSelectors(ctx, sizes, baseName, fn) {
|
|||||||
return res;
|
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) {
|
function getScope(ctx, name, selectors) {
|
||||||
|
|
||||||
@@ -229,7 +211,7 @@ function getScope(ctx, name, selectors) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
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;
|
return null;
|
||||||
|
|
||||||
@@ -250,6 +232,52 @@ function getScope(ctx, name, selectors) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
function getScopeLevel(ctx, name) {
|
||||||
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
for (let i=ctx.scopes.length-1; i>=0; i--) {
|
||||||
if (ctx.scopes[i][name]) return i;
|
if (ctx.scopes[i][name]) return i;
|
||||||
@@ -272,12 +300,15 @@ function execTemplateDef(ctx, ast) {
|
|||||||
return error(ctx, ast, "Name already exists: "+ast.name);
|
return error(ctx, ast, "Name already exists: "+ast.name);
|
||||||
}
|
}
|
||||||
scope[ast.name] = {
|
scope[ast.name] = {
|
||||||
|
type: "TEMPLATE",
|
||||||
|
value: {
|
||||||
type: "TEMPLATE",
|
type: "TEMPLATE",
|
||||||
params: ast.params,
|
params: ast.params,
|
||||||
block: ast.block,
|
block: ast.block,
|
||||||
fileName: ctx.fileName,
|
fileName: ctx.fileName,
|
||||||
filePath: ctx.filePath,
|
filePath: ctx.filePath,
|
||||||
scopes: copyScope(ctx.scopes)
|
scopes: copyScope(ctx.scopes)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
ctx.templates[ast.name] = {
|
ctx.templates[ast.name] = {
|
||||||
block: ast.block,
|
block: ast.block,
|
||||||
@@ -293,12 +324,15 @@ function execFunctionDef(ctx, ast) {
|
|||||||
}
|
}
|
||||||
ctx.functionParams[ast.name] = ast.params;
|
ctx.functionParams[ast.name] = ast.params;
|
||||||
scope[ast.name] = {
|
scope[ast.name] = {
|
||||||
|
type: "FUNCTION",
|
||||||
|
value: {
|
||||||
type: "FUNCTION",
|
type: "FUNCTION",
|
||||||
params: ast.params,
|
params: ast.params,
|
||||||
block: ast.block,
|
block: ast.block,
|
||||||
fileName: ctx.fileName,
|
fileName: ctx.fileName,
|
||||||
filePath: ctx.filePath,
|
filePath: ctx.filePath,
|
||||||
scopes: copyScope(ctx.scopes)
|
scopes: copyScope(ctx.scopes)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
ctx.functions[ast.name] = {
|
ctx.functions[ast.name] = {
|
||||||
block: ast.block,
|
block: ast.block,
|
||||||
@@ -326,9 +360,12 @@ function execDeclareComponent(ctx, ast) {
|
|||||||
const cIdx = ctx.addComponent(ast.name.name, sizes);
|
const cIdx = ctx.addComponent(ast.name.name, sizes);
|
||||||
|
|
||||||
scope[ast.name.name] = {
|
scope[ast.name.name] = {
|
||||||
|
type: "COMPONENT",
|
||||||
|
value: {
|
||||||
type: "COMPONENT",
|
type: "COMPONENT",
|
||||||
sizes: sizes,
|
sizes: sizes,
|
||||||
cIdx: Array.isArray(cIdx) ? cIdx[0] : cIdx
|
cIdx: Array.isArray(cIdx) ? cIdx[0] : cIdx
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -412,7 +449,10 @@ function execInstantiateComponet(ctx, vr, fn) {
|
|||||||
|
|
||||||
const scope = {};
|
const scope = {};
|
||||||
for (let i=0; i< template.params.length; i++) {
|
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[cIdx].params[template.params[i]] = extractValue(paramValues[i]);
|
ctx.components[cIdx].params[template.params[i]] = extractValue(paramValues[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -466,7 +506,10 @@ function execFunctionCall(ctx, ast) {
|
|||||||
|
|
||||||
const scope = {};
|
const scope = {};
|
||||||
for (let i=0; i< fnc.params.length; i++) {
|
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;
|
ctx.fileName = fnc.fileName;
|
||||||
@@ -534,9 +577,12 @@ function execDeclareSignal(ctx, ast) {
|
|||||||
// ctx.components[ctx.currentComponent].signals.push(i);
|
// ctx.components[ctx.currentComponent].signals.push(i);
|
||||||
}
|
}
|
||||||
scope[ast.name.name] = {
|
scope[ast.name.name] = {
|
||||||
|
type: "SIGNAL",
|
||||||
|
value: {
|
||||||
type: "SIGNAL",
|
type: "SIGNAL",
|
||||||
sizes: sizes,
|
sizes: sizes,
|
||||||
sIdx: sIdx[0]
|
sIdx: sIdx[0]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
type: "VARIABLE",
|
type: "VARIABLE",
|
||||||
@@ -560,12 +606,15 @@ function execDeclareVariable(ctx, ast) {
|
|||||||
sizes.push( size.value.toJSNumber() );
|
sizes.push( size.value.toJSNumber() );
|
||||||
}
|
}
|
||||||
|
|
||||||
scope[ast.name.name] = iterateSelectors(ctx, sizes, "", function() {
|
scope[ast.name.name] = {
|
||||||
|
type: "VARIABLE",
|
||||||
|
value: iterateSelectors(ctx, sizes, "", function() {
|
||||||
return {
|
return {
|
||||||
type: "NUMBER",
|
type: "NUMBER",
|
||||||
value: bigInt(0)
|
value: bigInt(0)
|
||||||
};
|
};
|
||||||
});
|
})
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: "VARIABLE",
|
type: "VARIABLE",
|
||||||
@@ -702,21 +751,20 @@ function execVarAssignement(ctx, ast) {
|
|||||||
} else {
|
} else {
|
||||||
v = ast.values[0];
|
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 (ctx.error) return;
|
||||||
|
|
||||||
if ((typeof(num) != "object")||(num == null)) return error(ctx, ast, "Variable not defined");
|
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 (ctx.error) return;
|
||||||
if ((num.type == "SIGNAL")&&(ast.op == "=")) return error(ctx, ast, "Cannot assign to a signal with `=` use <-- or <== ops");
|
if ((typ == "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 ((["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]);
|
const res = exec(ctx, ast.values[1]);
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
|
|
||||||
Object.assign(num, res);
|
setScopeRef(ctx, v.name, sels, res);
|
||||||
// setScope(ctx, v.name, v.selectors, res);
|
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user