Skip to content

Commit

Permalink
Init project
Browse files Browse the repository at this point in the history
  • Loading branch information
leeing committed Apr 1, 2020
1 parent 1841fb0 commit d1cc8eb
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
26 changes: 26 additions & 0 deletions package.json
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 added src/apply.ts
Empty file.
34 changes: 34 additions & 0 deletions src/eval.ts
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;
9 changes: 9 additions & 0 deletions src/index.ts
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 added src/scope.ts
Empty file.
73 changes: 73 additions & 0 deletions src/types.ts
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
}

0 comments on commit d1cc8eb

Please sign in to comment.