Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion homework/1/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,39 @@ const acorn = require('acorn');
const astring = require('astring');
const traverse = require('../../common/traverse');



function transform(root, originName, targetName) {

// 替换变量名
function replace(name){
if (name === originName) return targetName
}

// 遍历所有节点
return traverse((node, ctx, next) => {

// TODO: 作业代码写在这里
if (node.type === 'xxx') {
if (node.type === 'VariableDeclarator') {
node.id.name = replace(node.id.name)
}
if (node.type === 'MemberExpression') {
if (node.object.type === 'Identifier') {
node.object.name = replace(node.object.name)
}
}
if (node.type === 'BinaryExpression') {
if (node.left.type === 'Identifier') {
node.left.name = replace(node.left.name)
}
if (node.right.type === 'Identifier') {
node.right.name = replace(node.right.name)
}
}
if (node.type === 'FunctionDeclaration') {
if (node.id.type === 'Identifier') {
node.id.name = replace(node.id.name)
}
}

// 继续往下遍历
Expand Down
98 changes: 94 additions & 4 deletions homework/2/eval.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,109 @@
const acorn = require('acorn');

function evaluate(node, env) {
class Scope {
constructor(type, parent) {
this.variables = {}
this.type = type
this.parent = parent
}
get(name) {
let root = this
while (root.variables[name] === undefined && root) root = root.parent
if (root) {
return root.variables[name]
}
}
set(name, value) {
let root = this
while (root.variables[name] === undefined && root) root = root.parent
if (root) {
root.variables[name] = value
}
}
}

function evaluate(node, scope) {
switch (node.type) {
case 'Literal':
// TODO: 补全作业代码
return node.value
case 'Identifier':
return scope.get(node.name)
case 'BinaryExpression':
switch (node.operator) {
case '+':
return evaluate(node.left, scope) + evaluate(node.right, scope)
case '-':
return evaluate(node.left, scope) - evaluate(node.right, scope)
case '*':
return evaluate(node.left, scope) * evaluate(node.right, scope)
case '/':
return evaluate(node.left, scope) / evaluate(node.right, scope)
case '<=':
return evaluate(node.left, scope) <= evaluate(node.right, scope)
case '>=':
return evaluate(node.left, scope) >= evaluate(node.right, scope)
}
case 'LogicalExpression':
if (node.operator === '&&') {
return evaluate(node.left, scope) && evaluate(node.right, scope)
} else
if (node.operator === '||') {
return evaluate(node.left, scope) || evaluate(node.right, scope)
}
case 'ConditionalExpression':
if (evaluate(node.test, scope)) {
return evaluate(node.consequent, scope)
} else {
return evaluate(node.alternate, scope)
}
case 'ObjectExpression':
let res = {}
node.properties.map((obj) => {
res[obj.key.name] = evaluate(obj.value, scope)
})
return res
case 'ArrayExpression':
let result = []
node.elements.map((obj) => {
result.push(evaluate(obj, scope))
})
return result
case 'CallExpression':
if (typeof evaluate(node.callee, scope) !== 'function') {
}
return evaluate(node.callee, scope)(...node.arguments.map(arg => evaluate(arg, scope)))
case 'ArrowFunctionExpression':
// 箭头函数表达式返回的是一个函数
return function (...args) {
let argsEnv = new Scope('let', scope)
node.params.map((arg, index) => {
argsEnv.variables[arg.name] = args[index]
})
return evaluate(node.body, argsEnv)
}
case 'SequenceExpression':
let SequenceResult
node.expressions.map((exp,index) => {
if (index === node.expressions.length - 1) {
SequenceResult = evaluate(exp, scope)
return
}
evaluate(exp, scope)
})
return SequenceResult
case 'AssignmentExpression':
scope.set(node.left.name, evaluate(node.right, scope))
return scope.get(node.left.name)
}

throw new Error(`Unsupported Syntax ${node.type} at Location ${node.start}:${node.end}`);
}

function customerEval(code, env = {}) {
function customerEval(code, scope = {}) {
const node = acorn.parseExpressionAt(code, 0, {
ecmaVersion: 6
})
return evaluate(node, env)
return evaluate(node, scope)
}

module.exports = customerEval
120 changes: 118 additions & 2 deletions homework/3/eval.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,125 @@
const acorn = require('acorn');

function evaluate(node, env) {
class Scope {
constructor(type, parent) {
this.variables = {}
this.type = type
this.parent = parent
}
declare(kind, name) {
switch (kind) {
case 'var':
let root = this
while (root.parent) root = root.parent
// root.variables[name]
}
}
get(name) {
let root = this
while (root.variables[name] === undefined && root) root = root.parent
if (root) {
return root.variables[name]
}
}
set(name, value) {
let root = this
while (root.variables[name] === undefined && root) root = root.parent
if (root) {
root.variables[name] = value
}
}
}

function evaluate(node, scope) {
switch (node.type) {
case 'Literal':
// TODO: 补全作业代码
return node.value
case 'Identifier':
return scope.get(node.name)
case 'BinaryExpression':
switch (node.operator) {
case '+':
return evaluate(node.left, scope) + evaluate(node.right, scope)
case '-':
return evaluate(node.left, scope) - evaluate(node.right, scope)
case '*':
return evaluate(node.left, scope) * evaluate(node.right, scope)
case '/':
return evaluate(node.left, scope) / evaluate(node.right, scope)
case '<=':
return evaluate(node.left, scope) <= evaluate(node.right, scope)
case '>=':
return evaluate(node.left, scope) >= evaluate(node.right, scope)
}
case 'LogicalExpression':
if (node.operator === '&&') {
return evaluate(node.left, scope) && evaluate(node.right, scope)
} else
if (node.operator === '||') {
return evaluate(node.left, scope) || evaluate(node.right, scope)
}
case 'ConditionalExpression':
if (evaluate(node.test, scope)) {
return evaluate(node.consequent, scope)
} else {
return evaluate(node.alternate, scope)
}
case 'ObjectExpression':
let res = {}
node.properties.map((obj) => {
res[obj.key.name] = evaluate(obj.value, scope)
})
return res
case 'ArrayExpression':
let result = []
node.elements.map((obj) => {
result.push(evaluate(obj, scope))
})
return result
case 'CallExpression':
if (typeof evaluate(node.callee, scope) !== 'function') {
}
return evaluate(node.callee, scope)(...node.arguments.map(arg => evaluate(arg, scope)))
case 'ArrowFunctionExpression':
// 箭头函数表达式返回的是一个函数
return function (...args) {
let argsEnv = new Scope('let', scope)
node.params.map((arg, index) => {
argsEnv.variables[arg.name] = args[index]
})
return evaluate(node.body, argsEnv)
}
case 'SequenceExpression':
let SequenceResult
node.expressions.map((exp,index) => {
if (index === node.expressions.length - 1) {
SequenceResult = evaluate(exp, scope)
return
}
evaluate(exp, scope)
})
return SequenceResult
case 'IfStatement':
if (node.test) {
return evaluate(node.constant, scope)
}
case 'BlockStatement':
const child = new Scope('block', scope)
for (const stat of node.body) {
evaluate(node, child)
}
case 'Declare':
scope.declare(name)
scope.set(name, evaluate(node.init, scope))
case 'AssignmentExpression':
scope.set(node.left.name, evaluate(node.right, scope))
return scope.get(node.left.name)
case 'While':
while (node.test) {
evaluate(node.body)
}
case '':

}

throw new Error(`Unsupported Syntax ${node.type} at Location ${node.start}:${node.end}`);
Expand Down
Loading