Skip to content

Commit

Permalink
feat: fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liulinboyi committed Jul 27, 2021
1 parent 6c300dc commit 2f2c41d
Show file tree
Hide file tree
Showing 41 changed files with 135 additions and 62 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
/dist/vm/*.js.map
/dist/test/*.js.map
/dist/src/*/*.js.map
/dist/vm/*/*.js.map
/dist/vm/*/*.js.map
/vm.js/*
/vm.ts/build/*
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "test-ts",
"type": "node",
"request": "launch",
"args": [
"${workspaceRoot}\\${relativeFile}" // 入口文件
],
"runtimeArgs": [
"--nolazy",
"-r",
"ts-node/register"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"name": "Test",
"program": "${workspaceFolder}/test/test.js",
Expand Down
18 changes: 18 additions & 0 deletions demo/hello-world-14.pineapple
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$a = 4 + ( ( 1 + 2 ) + 3) # 10
$b = ( ( ( 1 + 2 ) + 3) + 4 ) + 5 # 15
$c = 20 # 20
$d = $a + 20 # 30
$e = 20 + 30 # 50
$f = "1" + 2 # "12"
$p_a = "a: " + $a
$p_b = "b: " + $b
$p_c = "c: " + $c
$p_d = "d: " + $d
$p_e = "e: " + $e
$p_f = "f: " + $f
print($p_a)
print($p_b)
print($p_c)
print($p_d)
print($p_e)
print($p_f)
3 changes: 3 additions & 0 deletions demo/hello-world-15.pineapple
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$a = 10 # 10
$p_a = "a: " + $a
print($p_a)
22 changes: 10 additions & 12 deletions dist/src/parser/Assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,11 @@ function parseAssignment(lexer) {
VariableDeclarator.init = expression.expression;
assignment.type = "VariableDeclaration";
}
else if (tokenType === lexer1_1.NUMBER) {
else if (tokenType === lexer1_1.NUMBER || tokenType === lexer1_1.TOKEN_LEFT_PAREN) {
// 需要Expressions处理
let ex = new BinaryExpression('');
const lineNumber = lexer.GetLineNum();
while (lexer.GetLineNum() === lineNumber) {
const expr = Expressions(lexer, ex);
ex = expr;
// ex.left = expr;
// let ahead = lexer.LookAhead()
// ex.operator = ahead.token
console.log(expr);
}
const expr = Expressions(lexer, ex);
ex = expr;
console.log(ex);
// const literial = new Literal(parseNumber(lexer)) // 这里面会把邻近的空格回车删掉
VariableDeclarator.init = ex;
Expand Down Expand Up @@ -180,9 +173,14 @@ exports.Trem_tail = Trem_tail;
function Factor(lexer, expr) {
let ahead = lexer.LookAhead();
if (lexer.isNumber(ahead.token)) {
lexer.NextTokenIs(lexer1_1.NUMBER);
let token = "";
while (lexer.isNumber(ahead.token)) {
token += ahead.token;
lexer.NextTokenIs(lexer1_1.NUMBER);
ahead = lexer.LookAhead();
}
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED);
return new Literal(+ahead.token);
return new Literal(+token); // 转换为数字
}
else if (ahead.token === "(") {
// (1 + 2)
Expand Down
4 changes: 2 additions & 2 deletions dist/test/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";
const fs = require('fs');
const path = require('path');
const Execute = require('../dist/src/backend.js').Execute;
code = fs.readFileSync(path.resolve(__dirname, '../demo/hello-world-13.pineapple'), { encoding: 'utf-8' });
const Execute = require('../src/backend.ts').Execute;
let code = fs.readFileSync(path.resolve(__dirname, '../demo/hello-world-15.pineapple'), { encoding: 'utf-8' });
console.log(code, 'code');
if (code.length > 0) {
Execute(code);
Expand Down
24 changes: 15 additions & 9 deletions dist/vm.js/standard/es2015.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
}) : (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function (o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
}) : function (o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
if (mod && mod.__esModule)
return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
if (mod != null)
for (var k in mod)
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Expand Down
24 changes: 15 additions & 9 deletions dist/vm.js/standard/es2016.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
}) : (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function (o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
}) : function (o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
if (mod && mod.__esModule)
return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
if (mod != null)
for (var k in mod)
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Expand Down
24 changes: 15 additions & 9 deletions dist/vm.js/standard/es5.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } });
}) : (function (o, m, k, k2) {
if (k2 === undefined)
k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function (o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
}) : function (o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
if (mod && mod.__esModule)
return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
if (mod != null)
for (var k in mod)
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Expand Down
24 changes: 11 additions & 13 deletions src/parser/Assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,11 @@ export function parseAssignment(lexer: Lexer) {
const expression = parseExpression(lexer)
VariableDeclarator.init = expression.expression
assignment.type = "VariableDeclaration"
} else if (tokenType === NUMBER) {
} else if (tokenType === NUMBER || tokenType === TOKEN_LEFT_PAREN) {
// 需要Expressions处理
let ex = new BinaryExpression('')
const lineNumber = lexer.GetLineNum()
while (lexer.GetLineNum() === lineNumber) {
const expr = Expressions(lexer, ex)
ex = expr
// ex.left = expr;
// let ahead = lexer.LookAhead()
// ex.operator = ahead.token
console.log(expr)
}
const expr = Expressions(lexer, ex)
ex = expr
console.log(ex)
// const literial = new Literal(parseNumber(lexer)) // 这里面会把邻近的空格回车删掉
VariableDeclarator.init = ex
Expand Down Expand Up @@ -201,9 +194,14 @@ export function Trem_tail(lexer: Lexer, value: any) {
export function Factor(lexer: Lexer, expr?: any) {
let ahead = lexer.LookAhead()
if (lexer.isNumber(ahead.token)) {
lexer.NextTokenIs(NUMBER)
lexer.LookAheadAndSkip(TOKEN_IGNORED)
return new Literal(+ahead.token)
let token = ""
while (lexer.isNumber(ahead.token)) {
token += ahead.token
lexer.NextTokenIs(NUMBER);
ahead = lexer.LookAhead();
}
lexer.LookAheadAndSkip(TOKEN_IGNORED);
return new Literal(+token); // 转换为数字
} else if (ahead.token === "(") {
// (1 + 2)
lexer.NextTokenIs(TOKEN_LEFT_PAREN)
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const path = require('path')
const Execute = require('../dist/src/backend.js').Execute

code = fs.readFileSync(path.resolve(__dirname, '../demo/hello-world-13.pineapple'), {encoding: 'utf-8'})
code = fs.readFileSync(path.resolve(__dirname, '../demo/hello-world-15.pineapple'), {encoding: 'utf-8'})
console.log(code, 'code')
if (code.length > 0) {
Execute(code)
Expand Down
9 changes: 9 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require('fs')
const path = require('path')
const Execute = require('../src/backend.ts').Execute

let code = fs.readFileSync(path.resolve(__dirname, '../demo/hello-world-15.pineapple'), {encoding: 'utf-8'})
console.log(code, 'code')
if (code.length > 0) {
Execute(code)
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"exclude": [
"vm"
"vm",
"vm.ts",
],
}
File renamed without changes.
3 changes: 3 additions & 0 deletions vm.ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```
npm run build
```
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion vm.js/context.ts → vm.ts/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class Context {
for (const attr in ctx) {
/* istanbul ignore next */
if (ctx.hasOwnProperty(attr)) {
this[attr] = ctx[attr];
(this as any)[attr] = ctx[attr];
}
}
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion vm.js/evaluate.ts → vm.ts/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ const visitors: IEcmaScriptMap = {

export default function evaluate(path: Path<types.Node>) {
path.evaluate = evaluate;
const handler = visitors[path.node.type];
const handler = (visitors as any)[path.node.type];
return handler(path);
}
5 changes: 5 additions & 0 deletions vm.ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"build": "rm -rf ./build/ && tsc"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions vm.js/tsconfig.json → vm.ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
"noImplicitThis": false,
"rootDirs": ["."],
// "inlineSourceMap": true,
"inlineSources": true,
// "inlineSources": true,
"outDir": "./build",
"typeRoots": ["node_modules/@types"],
"types": ["node"],
"sourceMap": true,
"sourceMap": false,
"declaration": true
},
"include": ["src/**/*.ts", "test/**/*.ts"],
"include": ["./*.ts"],
"exclude": ["node_modules"]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2f2c41d

Please sign in to comment.