-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
leeing
committed
Apr 1, 2020
1 parent
1841fb0
commit d1cc8eb
Showing
8 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
# Apply for all files | ||
[*] | ||
|
||
charset = utf-8 | ||
|
||
indent_style = space | ||
indent_size = 2 | ||
|
||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
# package.json | ||
[package.json] | ||
indent_size = 2 | ||
indent_style = space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "nvwa", | ||
"version": "0.0.1", | ||
"description": "A javascript interpreter.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/1eeing/Nvwa.js.git" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/1eeing/Nvwa.js/issues" | ||
}, | ||
"homepage": "https://github.com/1eeing/Nvwa.js#readme", | ||
"dependencies": { | ||
"@babel/parser": "^7.9.4", | ||
"@babel/types": "^7.9.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^7.1.1" | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as t from '@babel/types'; | ||
|
||
const evaluateMap = { | ||
Program(program: t.Program, scope) { | ||
for (const node of program.body) { | ||
evaluate(node, scope); | ||
} | ||
}, | ||
|
||
Identifier(node: t.Identifier, scope) { | ||
const $var = scope.$find(node.name); | ||
if (!$var) { | ||
throw `[Error] ${node.loc}, '${node.name}' 未定义`; | ||
} | ||
return $var.$get(); | ||
}, | ||
|
||
Literal(node: t.Literal, scope) { | ||
if (node.type === 'NullLiteral') { | ||
return null; | ||
} | ||
return (node as any).value; | ||
}, | ||
|
||
BlockStatement(block: t.BlockStatement, scope) { | ||
|
||
}, | ||
} | ||
|
||
const evaluate = (node: t.Node, scope) => { | ||
return evaluateMap[node.type](node, scope); | ||
} | ||
|
||
export default evaluate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { parse } from '@babel/parser'; | ||
import evaluate from './eval'; | ||
|
||
const run = (code: string) => { | ||
const ast = parse(code); | ||
evaluate(ast); | ||
} | ||
|
||
export default run; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as t from '@babel/types'; | ||
|
||
export interface NodeTypeMap { | ||
Identifier: t.Identifier | ||
Literal: t.Literal | ||
Program: t.Program | ||
FunctionDeclaration: t.FunctionDeclaration | ||
FunctionExpression: t.FunctionExpression | ||
ArrowFunctionExpression: t.ArrowFunctionExpression | ||
SwitchCase: t.SwitchCase | ||
CatchClause: t.CatchClause | ||
VariableDeclarator: t.VariableDeclarator | ||
ExpressionStatement: t.ExpressionStatement | ||
BlockStatement: t.BlockStatement | ||
EmptyStatement: t.EmptyStatement | ||
DebuggerStatement: t.DebuggerStatement | ||
WithStatement: t.WithStatement | ||
ReturnStatement: t.ReturnStatement | ||
LabeledStatement: t.LabeledStatement | ||
BreakStatement: t.BreakStatement | ||
ContinueStatement: t.ContinueStatement | ||
IfStatement: t.IfStatement | ||
SwitchStatement: t.SwitchStatement | ||
ThrowStatement: t.ThrowStatement | ||
TryStatement: t.TryStatement | ||
WhileStatement: t.WhileStatement | ||
DoWhileStatement: t.DoWhileStatement | ||
ForStatement: t.ForStatement | ||
ForInStatement: t.ForInStatement | ||
ForOfStatement: t.ForOfStatement | ||
VariableDeclaration: t.VariableDeclaration | ||
ClassDeclaration: t.ClassDeclaration | ||
ThisExpression: t.ThisExpression | ||
ArrayExpression: t.ArrayExpression | ||
ObjectExpression: t.ObjectExpression | ||
YieldExpression: t.YieldExpression | ||
UnaryExpression: t.UnaryExpression | ||
UpdateExpression: t.UpdateExpression | ||
BinaryExpression: t.BinaryExpression | ||
AssignmentExpression: t.AssignmentExpression | ||
LogicalExpression: t.LogicalExpression | ||
MemberExpression: t.MemberExpression | ||
ConditionalExpression: t.ConditionalExpression | ||
CallExpression: t.CallExpression | ||
NewExpression: t.NewExpression | ||
SequenceExpression: t.SequenceExpression | ||
TemplateLiteral: t.TemplateLiteral | ||
TaggedTemplateExpression: t.TaggedTemplateExpression | ||
ClassExpression: t.ClassExpression | ||
MetaProperty: t.MetaProperty | ||
AwaitExpression: t.AwaitExpression | ||
Property: t.Property | ||
Super: t.Super | ||
TemplateElement: t.TemplateElement | ||
SpreadElement: t.SpreadElement | ||
ObjectPattern: t.ObjectPattern | ||
ArrayPattern: t.ArrayPattern | ||
RestElement: t.RestElement | ||
AssignmentPattern: t.AssignmentPattern | ||
ClassBody: t.ClassBody | ||
ImportDeclaration: t.ImportDeclaration | ||
ExportNamedDeclaration: t.ExportNamedDeclaration | ||
ExportDefaultDeclaration: t.ExportDefaultDeclaration | ||
ExportAllDeclaration: t.ExportAllDeclaration | ||
ImportSpecifier: t.ImportSpecifier | ||
ImportDefaultSpecifier: t.ImportDefaultSpecifier | ||
ImportNamespaceSpecifier: t.ImportNamespaceSpecifier | ||
ExportSpecifier: t.ExportSpecifier | ||
} | ||
|
||
export type EvaluateMap = { | ||
[key in NodeTypeMap]: () => any | ||
} |