From b9b4881bc9ac07ee2d9e79499a5054b5f310f892 Mon Sep 17 00:00:00 2001 From: Sumanth Yedoti Date: Wed, 20 Apr 2022 10:38:47 +0530 Subject: [PATCH 1/5] Refactor getType funtions --- lib/typeInference.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/typeInference.js b/lib/typeInference.js index b3d2609..8341164 100644 --- a/lib/typeInference.js +++ b/lib/typeInference.js @@ -44,11 +44,11 @@ const unaryExprType = (expr) => { const getType = (expr, expectedType, localTypeObj, id) => { if (expr !== undefined) { const type = exprType(expr, localTypeObj, id) - return type === expectedType || expectedType === 'bool' + return type === expectedType || + expectedType === 'bool' || + type === 'needsInference' ? type - : type === 'needsInference' - ? type - : null + : null } } From 34aab230fef8ea415c5d085165a0e30b804bd3a2 Mon Sep 17 00:00:00 2001 From: Sumanth Yedoti Date: Wed, 20 Apr 2022 17:14:52 +0530 Subject: [PATCH 2/5] Implement static pattern matching --- lib/astupdate.js | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/astupdate.js b/lib/astupdate.js index 58f9790..8c99f8c 100644 --- a/lib/astupdate.js +++ b/lib/astupdate.js @@ -68,6 +68,21 @@ const formMultiPatternAst = (funcdecl, patterndecls) => { return funcdecl } +const formStaticPatternAst = (patterndecls) => { + const funcdeclbody = { type: 'BlockStatement', body: [] } + const blockstmtbody = { type: 'SwitchStatement', cases: [] } + const [cases] = buildExplicitCases(patterndecls) + blockstmtbody.cases = cases + const param = estemplate.identifier('x') + blockstmtbody.discriminant = param + funcdeclbody.body.push(blockstmtbody) + return estemplate.funcDeclaration( + estemplate.identifier(functionName(patterndecls[0])), + [param], + funcdeclbody + ) +} + const isArrowFuncDecl = (decl) => { const declarations = decl.declarations @@ -97,6 +112,13 @@ const isFunction = (decl) => { return isFunc } +const functionName = (decl = {}) => { + const declarations = decl.declarations || null + if (!declarations) return null + const name = declarations[0].id.name + return name +} + const formFunctionAst = (funcdecl, patterndecls) => { const patternscount = patterndecls.length return patternscount === 0 @@ -104,13 +126,21 @@ const formFunctionAst = (funcdecl, patterndecls) => { : formMultiPatternAst(funcdecl, patterndecls) } -const processSubTree = (decl) => { +const processSubTree = (decl, nextDecl) => { if (isArrowFuncDecl(decl)) { if (isFunction(decl)) { // function declarations - const funcast = formFunctionAst(decl, patterns) + const funcAst = formFunctionAst(decl, patterns) + patterns = [] + return funcAst + } else if (patterns.length > 0 && + (!nextDecl || functionName(nextDecl) !== functionName(patterns[0])) + ) { + // static patten matching without variable + patterns.push(decl) + const patternFuncAst = formStaticPatternAst(patterns) patterns = [] - return funcast + return patternFuncAst } else { // pattern matched function declarations patterns.push(decl) @@ -123,8 +153,9 @@ const processSubTree = (decl) => { const updateAst = (ast) => { let newBody = [] const declarations = ast.body - declarations.forEach((decl) => { - const subtree = processSubTree(decl) + declarations.forEach((decl, i) => { + const nextDecl = declarations[i + 1] || null + const subtree = processSubTree(decl, nextDecl) if (subtree !== null) newBody.push(subtree) }) newBody = updateComment(newBody) From f2946a828ac91a5a600fb42d08642440131acd32 Mon Sep 17 00:00:00 2001 From: Sumanth Yedoti Date: Wed, 20 Apr 2022 17:18:39 +0530 Subject: [PATCH 3/5] Test static pattern matching --- package.json | 3 +- test/assert/staticPatternMatching.json | 69 ++++++++++++++++++++++++++ test/src/staticPatternMatching.cl | 5 ++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 test/assert/staticPatternMatching.json create mode 100644 test/src/staticPatternMatching.cl diff --git a/package.json b/package.json index 85a4671..29f321b 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,7 @@ }, "lint-staged": { "**/*.+(js)": [ - "standard --fix", - "git add ." + "standard --fix" ] }, "author": "Santosh Rajan", diff --git a/test/assert/staticPatternMatching.json b/test/assert/staticPatternMatching.json new file mode 100644 index 0000000..9893afa --- /dev/null +++ b/test/assert/staticPatternMatching.json @@ -0,0 +1,69 @@ +{ + "type": "Program", + "body": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "bobAt10" + }, + "init": { + "type": "Literal", + "value": "naughty", + "raw": "naughty", + "sType": "string" + } + } + ], + "kind": "const" + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "bobAt20" + }, + "init": { + "type": "Literal", + "value": "nerdy", + "raw": "nerdy", + "sType": "string" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "computed": false, + "object": { + "type": "Identifier", + "name": "console" + }, + "property": { + "type": "Identifier", + "name": "log" + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "bobAt20" + } + ], + "sType": "IO" + } + } + ], + "sourceType": "script" +} diff --git a/test/src/staticPatternMatching.cl b/test/src/staticPatternMatching.cl new file mode 100644 index 0000000..8a0c935 --- /dev/null +++ b/test/src/staticPatternMatching.cl @@ -0,0 +1,5 @@ + +bobAt10 = 'naughty' +bobAt20 = 'nerdy' + +print bobAt20 From 9f302e94fc52dc67fb2059a54768c072ad36ac19 Mon Sep 17 00:00:00 2001 From: Sumanth Yedoti Date: Wed, 20 Apr 2022 18:05:47 +0530 Subject: [PATCH 4/5] Change variable names to camelCase --- lib/astupdate.js | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/astupdate.js b/lib/astupdate.js index 8c99f8c..41b7f07 100644 --- a/lib/astupdate.js +++ b/lib/astupdate.js @@ -44,42 +44,42 @@ const buildDefaultCase = (funcdecl) => { return defaultcase } -const formMultiPatternAst = (funcdecl, patterndecls) => { - const funcdeclbody = { type: 'BlockStatement', body: [] } - const blockstmtbody = { type: 'SwitchStatement', cases: [] } - const [cases, flag] = buildExplicitCases(patterndecls) - const testTempl = funcdecl.declarations[0].init.params[0] - blockstmtbody.discriminant = testTempl +const formMultiPatternAst = (funcDecl, patternDecls) => { + const funcDeclBody = { type: 'BlockStatement', body: [] } + const blockStmtBody = { type: 'SwitchStatement', cases: [] } + const [cases, flag] = buildExplicitCases(patternDecls) + const testTempl = funcDecl.declarations[0].init.params[0] + blockStmtBody.discriminant = testTempl if (flag === true) { const lengthTempl = estemplate.identifier('length') - blockstmtbody.discriminant = estemplate.memberExpression( + blockStmtBody.discriminant = estemplate.memberExpression( testTempl, lengthTempl ).expression } - cases.push(buildDefaultCase(funcdecl)) - blockstmtbody.cases = cases + cases.push(buildDefaultCase(funcDecl)) + blockStmtBody.cases = cases - funcdeclbody.body.push(blockstmtbody) + funcDeclBody.body.push(blockStmtBody) - funcdecl.declarations[0].init.body = funcdeclbody - funcdecl.declarations[0].init.expression = false + funcDecl.declarations[0].init.body = funcDeclBody + funcDecl.declarations[0].init.expression = false - return funcdecl + return funcDecl } -const formStaticPatternAst = (patterndecls) => { - const funcdeclbody = { type: 'BlockStatement', body: [] } - const blockstmtbody = { type: 'SwitchStatement', cases: [] } - const [cases] = buildExplicitCases(patterndecls) - blockstmtbody.cases = cases +const formStaticPatternAst = (patternDecls) => { + const funcDeclBody = { type: 'BlockStatement', body: [] } + const blockStmtBody = { type: 'SwitchStatement', cases: [] } + const [cases] = buildExplicitCases(patternDecls) + blockStmtBody.cases = cases const param = estemplate.identifier('x') - blockstmtbody.discriminant = param - funcdeclbody.body.push(blockstmtbody) + blockStmtBody.discriminant = param + funcDeclBody.body.push(blockStmtBody) return estemplate.funcDeclaration( - estemplate.identifier(functionName(patterndecls[0])), + estemplate.identifier(functionName(patternDecls[0])), [param], - funcdeclbody + funcDeclBody ) } @@ -119,11 +119,11 @@ const functionName = (decl = {}) => { return name } -const formFunctionAst = (funcdecl, patterndecls) => { - const patternscount = patterndecls.length - return patternscount === 0 - ? funcdecl - : formMultiPatternAst(funcdecl, patterndecls) +const formFunctionAst = (funcDecl, patternDecls) => { + const patternsCount = patternDecls.length + return patternsCount === 0 + ? funcDecl + : formMultiPatternAst(funcDecl, patternDecls) } const processSubTree = (decl, nextDecl) => { From f3562841598c88809ca562004b6d83efdd4c53ab Mon Sep 17 00:00:00 2001 From: Sumanth Yedoti Date: Wed, 20 Apr 2022 18:32:02 +0530 Subject: [PATCH 5/5] Fix test example for static pattern matching --- test/assert/staticPatternMatching.json | 108 ++++++++++++++++++------- test/src/staticPatternMatching.cl | 6 +- 2 files changed, 83 insertions(+), 31 deletions(-) diff --git a/test/assert/staticPatternMatching.json b/test/assert/staticPatternMatching.json index 9893afa..7fffaa1 100644 --- a/test/assert/staticPatternMatching.json +++ b/test/assert/staticPatternMatching.json @@ -8,32 +8,73 @@ "type": "VariableDeclarator", "id": { "type": "Identifier", - "name": "bobAt10" + "name": "bobAt" }, "init": { - "type": "Literal", - "value": "naughty", - "raw": "naughty", - "sType": "string" - } - } - ], - "kind": "const" - }, - { - "type": "VariableDeclaration", - "declarations": [ - { - "type": "VariableDeclarator", - "id": { - "type": "Identifier", - "name": "bobAt20" - }, - "init": { - "type": "Literal", - "value": "nerdy", - "raw": "nerdy", - "sType": "string" + "type": "ArrowFunctionExpression", + "id": null, + "params": [ + { + "type": "Identifier", + "name": "x" + } + ], + "body": { + "type": "BlockStatement", + "body": [ + { + "type": "SwitchStatement", + "discriminant": { + "type": "Identifier", + "name": "x" + }, + "cases": [ + { + "type": "SwitchCase", + "test": { + "type": "Literal", + "value": 10, + "raw": "10", + "sType": "number" + }, + "consequent": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Literal", + "value": "naughty", + "raw": "naughty", + "sType": "string" + } + } + ] + }, + { + "type": "SwitchCase", + "test": { + "type": "Literal", + "value": 20, + "raw": "20", + "sType": "number" + }, + "consequent": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Literal", + "value": "nerdy", + "raw": "nerdy", + "sType": "string" + } + } + ] + } + ] + } + ] + }, + "generator": false, + "expression": false } } ], @@ -42,6 +83,7 @@ { "type": "ExpressionStatement", "expression": { + "sType": "IO", "type": "CallExpression", "callee": { "type": "MemberExpression", @@ -57,11 +99,21 @@ }, "arguments": [ { - "type": "Identifier", - "name": "bobAt20" + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "bobAt" + }, + "arguments": [ + { + "type": "Literal", + "value": 20, + "raw": "20", + "sType": "number" + } + ] } - ], - "sType": "IO" + ] } } ], diff --git a/test/src/staticPatternMatching.cl b/test/src/staticPatternMatching.cl index 8a0c935..33737e5 100644 --- a/test/src/staticPatternMatching.cl +++ b/test/src/staticPatternMatching.cl @@ -1,5 +1,5 @@ +bobAt 10 = 'naughty' +bobAt 20 = 'nerdy' -bobAt10 = 'naughty' -bobAt20 = 'nerdy' +print (bobAt 20) -print bobAt20