From d1cc8eb565ddc04e43e132221e511b65a1a752f6 Mon Sep 17 00:00:00 2001 From: leeing Date: Wed, 1 Apr 2020 19:16:41 +0800 Subject: [PATCH] Init project --- .editorconfig | 22 ++++++++++++++++ .gitignore | 1 + package.json | 26 ++++++++++++++++++ src/apply.ts | 0 src/eval.ts | 34 ++++++++++++++++++++++++ src/index.ts | 9 +++++++ src/scope.ts | 0 src/types.ts | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 165 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 package.json create mode 100644 src/apply.ts create mode 100644 src/eval.ts create mode 100644 src/index.ts create mode 100644 src/scope.ts create mode 100644 src/types.ts diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f598a3c --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/package.json b/package.json new file mode 100644 index 0000000..de69c99 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/src/apply.ts b/src/apply.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/eval.ts b/src/eval.ts new file mode 100644 index 0000000..f94b481 --- /dev/null +++ b/src/eval.ts @@ -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; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..b128d67 --- /dev/null +++ b/src/index.ts @@ -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; diff --git a/src/scope.ts b/src/scope.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..023dd08 --- /dev/null +++ b/src/types.ts @@ -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 +}