Skip to content

Commit

Permalink
feat: Continue add function
Browse files Browse the repository at this point in the history
  • Loading branch information
liulinboyi committed Feb 27, 2021
1 parent 3496461 commit 08c16e3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
9 changes: 9 additions & 0 deletions demo/hello-world-6.pineapple
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

func add($a,$b) {
$_a = $a + $b
return $_a + 1
}

$res = add(1,2)

print($res)
33 changes: 24 additions & 9 deletions dist/src/parser/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,37 @@ function parseFunction(lexer) {
lexer.NextTokenIs(lexer1_1.BLOCK_START);
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED); // 去除空格回车等
const block = paseBlock(lexer);
FunctionDeclaration.body.body.push({
type: "ReturnStatement",
argument: block.declarations[0].init
});
FunctionDeclaration.body.body = block;
lexer.NextTokenIs(lexer1_1.BLOCK_END);
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED);
return FunctionDeclaration;
}
exports.parseFunction = parseFunction;
const BlockStatementBody = [];
function paseBlock(lexer) {
const ahead = lexer.LookAhead();
if (ahead.tokenType === lexer1_1.TOKEN_RETURN) {
if (ahead.tokenType === lexer1_1.TOKEN_RETURN) { // return
lexer.NextTokenIs(lexer1_1.TOKEN_RETURN);
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED);
return paseReturnStatement(lexer);
const returnStatement = paseReturnStatement(lexer);
// returnStatement.argument = returnStatement.declarations[0].init
// delete returnStatement.declarations
BlockStatementBody.push({
type: "ReturnStatement",
argument: returnStatement.declarations[0].init
});
}
else if (ahead.tokenType === lexer1_1.TOKEN_VAR_PREFIX) { // $
const VariableDeclaration = Assignment_1.parseAssignment(lexer);
console.log(VariableDeclaration);
BlockStatementBody.push({
type: VariableDeclaration.type,
declarations: VariableDeclaration.declarations,
kind: VariableDeclaration.kind
});
paseBlock(lexer);
}
return BlockStatementBody;
}
exports.paseBlock = paseBlock;
function paseReturnStatement(lexer) {
Expand All @@ -90,7 +105,7 @@ function paseReturnStatement(lexer) {
console.log(Variable, 'Variable');
const identifier = new Assignment_1.Identifier(Variable.Name);
VariableDeclarator.init = identifier;
assignment.type = "VariableDeclaration";
assignment.type = "ReturnStatement";
assignment.declarations.push(VariableDeclarator); // 一行只允许声明和初始化一个变量
let ahead = lexer.LookAhead();
console.log(ahead, 'parseAssignment Variable ahead');
Expand All @@ -110,13 +125,13 @@ function paseReturnStatement(lexer) {
// console.log('parseNumber start')
const literial = new Assignment_1.Literal(parser_1.parseNumber(lexer)); // 这里面会把邻近的空格回车删掉
VariableDeclarator.init = literial;
assignment.type = "VariableDeclaration";
assignment.type = "ReturnStatement";
// console.log('parseNumber end')
}
else {
const literial = new Assignment_1.Literal(parser_1.parseString(lexer)); // 这里面会把邻近的空格回车删掉
VariableDeclarator.init = literial;
assignment.type = "VariableDeclaration";
assignment.type = "ReturnStatement";
}
assignment.declarations.push(VariableDeclarator); // 一行只允许声明和初始化一个变量
let ahead = lexer.LookAhead();
Expand Down
2 changes: 1 addition & 1 deletion dist/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-5.pineapple'), { encoding: 'utf-8' });
code = fs.readFileSync(path.resolve(__dirname, '../demo/hello-world-6.pineapple'), { encoding: 'utf-8' });
console.log(code, 'code');
if (code.length > 0) {
Execute(code);
Expand Down
35 changes: 24 additions & 11 deletions src/parser/Function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TOKEN_IGNORED, TOKEN_LEFT_PAREN, Lexer, TOKEN_RIGHT_PAREN, TOKEN_FUNC_PARAMS_DIV, TOKEN_FUNC, BLOCK_START, TOKEN_RETURN, NUMBER, TOKEN_VAR_PREFIX, Operator, BLOCK_END } from "../lexer1";
import { parseName, parseNumber, parseString, parseVariable } from "../parser";
import { Assignment, Identifier, Literal, parseBinaryExpression } from "./Assignment";
import { Assignment, Identifier, Literal, parseAssignment, parseBinaryExpression } from "./Assignment";

export function parseFunction(lexer: Lexer) {
const FunctionDeclaration: any = {
Expand Down Expand Up @@ -60,22 +60,35 @@ export function parseFunction(lexer: Lexer) {
lexer.NextTokenIs(BLOCK_START)
lexer.LookAheadAndSkip(TOKEN_IGNORED) // 去除空格回车等
const block = paseBlock(lexer)
FunctionDeclaration.body.body.push({
type: "ReturnStatement",
argument: block.declarations[0].init
})
FunctionDeclaration.body.body = block
lexer.NextTokenIs(BLOCK_END)
lexer.LookAheadAndSkip(TOKEN_IGNORED)
return FunctionDeclaration
}

const BlockStatementBody: any[] = []
export function paseBlock(lexer: Lexer) {
const ahead = lexer.LookAhead()
if (ahead.tokenType === TOKEN_RETURN) {
if (ahead.tokenType === TOKEN_RETURN) { // return
lexer.NextTokenIs(TOKEN_RETURN)
lexer.LookAheadAndSkip(TOKEN_IGNORED)
return paseReturnStatement(lexer)
const returnStatement = paseReturnStatement(lexer)
// returnStatement.argument = returnStatement.declarations[0].init
// delete returnStatement.declarations
BlockStatementBody.push({
type: "ReturnStatement",
argument: returnStatement.declarations[0].init
})
} else if (ahead.tokenType === TOKEN_VAR_PREFIX) { // $
const VariableDeclaration = parseAssignment(lexer)
console.log(VariableDeclaration)
BlockStatementBody.push({
type: VariableDeclaration.type,
declarations: VariableDeclaration.declarations,
kind: VariableDeclaration.kind
})
paseBlock(lexer)
}
return BlockStatementBody
}

export function paseReturnStatement(lexer: Lexer) {
Expand All @@ -96,7 +109,7 @@ export function paseReturnStatement(lexer: Lexer) {
console.log(Variable, 'Variable')
const identifier = new Identifier(Variable.Name);
VariableDeclarator.init = identifier
assignment.type = "VariableDeclaration"
assignment.type = "ReturnStatement"
assignment.declarations.push(VariableDeclarator) // 一行只允许声明和初始化一个变量

let ahead = lexer.LookAhead()
Expand All @@ -116,12 +129,12 @@ export function paseReturnStatement(lexer: Lexer) {
// console.log('parseNumber start')
const literial = new Literal(parseNumber(lexer)) // 这里面会把邻近的空格回车删掉
VariableDeclarator.init = literial
assignment.type = "VariableDeclaration"
assignment.type = "ReturnStatement"
// console.log('parseNumber end')
} else {
const literial = new Literal(parseString(lexer)) // 这里面会把邻近的空格回车删掉
VariableDeclarator.init = literial
assignment.type = "VariableDeclaration"
assignment.type = "ReturnStatement"
}

assignment.declarations.push(VariableDeclarator) // 一行只允许声明和初始化一个变量
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-5.pineapple'), {encoding: 'utf-8'})
code = fs.readFileSync(path.resolve(__dirname, '../demo/hello-world-6.pineapple'), {encoding: 'utf-8'})
console.log(code, 'code')
if (code.length > 0) {
Execute(code)
Expand Down

0 comments on commit 08c16e3

Please sign in to comment.