-
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
1 parent
8596f91
commit 3496461
Showing
14 changed files
with
483 additions
and
12 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
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,8 @@ | ||
|
||
func add($a,$b) { | ||
return $a + $b | ||
} | ||
|
||
$res = add(1,2) | ||
|
||
print($res) |
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
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
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
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,44 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseExpression = void 0; | ||
const lexer1_1 = require("../lexer1"); | ||
const parser_1 = require("../parser"); | ||
const Assignment_1 = require("./Assignment"); | ||
function parseExpression(lexer) { | ||
const ExpressionStatement = { | ||
type: "ExpressionStatement", | ||
expression: { | ||
type: "CallExpression", | ||
callee: { | ||
type: "Identifier", | ||
}, | ||
arguments: [] | ||
} | ||
}; | ||
const IdentifierName = parser_1.parseName(lexer); | ||
ExpressionStatement.expression.callee.name = IdentifierName; | ||
lexer.NextTokenIs(lexer1_1.TOKEN_LEFT_PAREN); // ( | ||
const params = []; | ||
const tokenType = lexer.LookAhead().tokenType; | ||
while (tokenType !== lexer1_1.TOKEN_RIGHT_PAREN) { | ||
let p; | ||
// $ | ||
if (tokenType === lexer1_1.TOKEN_VAR_PREFIX) { | ||
p = parser_1.parseVariable(lexer); | ||
params.push(new Assignment_1.Identifier(p)); | ||
} | ||
else if (tokenType === lexer1_1.NUMBER) { | ||
p = parser_1.parseNumber(lexer); | ||
params.push(new Assignment_1.Literal(p)); | ||
} | ||
if (lexer.nextTokenType === lexer1_1.TOKEN_RIGHT_PAREN) { | ||
break; | ||
} | ||
lexer.NextTokenIs(lexer1_1.TOKEN_FUNC_PARAMS_DIV); // , | ||
} | ||
lexer.NextTokenIs(lexer1_1.TOKEN_RIGHT_PAREN); // ) | ||
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED); // 空格 | ||
ExpressionStatement.expression.arguments = params; | ||
return ExpressionStatement; | ||
} | ||
exports.parseExpression = parseExpression; |
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,136 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.paseReturnStatement = exports.paseBlock = exports.parseFunction = void 0; | ||
const lexer1_1 = require("../lexer1"); | ||
const parser_1 = require("../parser"); | ||
const Assignment_1 = require("./Assignment"); | ||
function parseFunction(lexer) { | ||
const FunctionDeclaration = { | ||
type: "FunctionDeclaration", | ||
id: { | ||
type: "Identifier", | ||
}, | ||
params: [ | ||
// { | ||
// type: "Identifier", | ||
// name: "" | ||
// } | ||
], | ||
body: { | ||
type: "BlockStatement", | ||
body: [ | ||
// { | ||
// type: "ReturnStatement", | ||
// argument: { | ||
// type: "BinaryExpression", | ||
// left: { | ||
// }, | ||
// operator: '+', | ||
// right: { | ||
// } | ||
// } | ||
// } | ||
] | ||
} | ||
}; | ||
lexer.NextTokenIs(lexer1_1.TOKEN_FUNC); | ||
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED); // 空格 | ||
const Identifier = parser_1.parseName(lexer); | ||
FunctionDeclaration.id.name = Identifier; | ||
lexer.NextTokenIs(lexer1_1.TOKEN_LEFT_PAREN); // ( | ||
const params = []; | ||
while (lexer.LookAhead().tokenType !== lexer1_1.TOKEN_RIGHT_PAREN) { | ||
const p = parser_1.parseVariable(lexer); | ||
params.push(p); | ||
if (lexer.nextTokenType === lexer1_1.TOKEN_RIGHT_PAREN) { | ||
break; | ||
} | ||
lexer.NextTokenIs(lexer1_1.TOKEN_FUNC_PARAMS_DIV); | ||
} | ||
for (let item of params) { | ||
FunctionDeclaration.params.push({ | ||
type: "Identifier", | ||
name: item.Name | ||
}); | ||
} | ||
lexer.NextTokenIs(lexer1_1.TOKEN_RIGHT_PAREN); // ) | ||
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED); // 去除空格回车等 | ||
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 | ||
}); | ||
lexer.NextTokenIs(lexer1_1.BLOCK_END); | ||
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED); | ||
return FunctionDeclaration; | ||
} | ||
exports.parseFunction = parseFunction; | ||
function paseBlock(lexer) { | ||
const ahead = lexer.LookAhead(); | ||
if (ahead.tokenType === lexer1_1.TOKEN_RETURN) { | ||
lexer.NextTokenIs(lexer1_1.TOKEN_RETURN); | ||
lexer.LookAheadAndSkip(lexer1_1.TOKEN_IGNORED); | ||
return paseReturnStatement(lexer); | ||
} | ||
} | ||
exports.paseBlock = paseBlock; | ||
function paseReturnStatement(lexer) { | ||
let assignment = new Assignment_1.Assignment(); | ||
assignment.LineNum = lexer.GetLineNum(); | ||
assignment.declarations = []; | ||
let VariableDeclarator = { type: "VariableDeclarator" }; | ||
VariableDeclarator.id = { type: "Identifier" }; | ||
const tokenType = lexer.LookAhead().tokenType; | ||
console.log(tokenType, 'lexer.LookAhead().tokenType'); | ||
// 如果后面仍是$ | ||
if (tokenType === lexer1_1.TOKEN_VAR_PREFIX) { | ||
const Variable = parser_1.parseVariable(lexer); // 标识符,这里面会把邻近的空格回车删掉 | ||
console.log(Variable, 'Variable'); | ||
const identifier = new Assignment_1.Identifier(Variable.Name); | ||
VariableDeclarator.init = identifier; | ||
assignment.type = "VariableDeclaration"; | ||
assignment.declarations.push(VariableDeclarator); // 一行只允许声明和初始化一个变量 | ||
let ahead = lexer.LookAhead(); | ||
console.log(ahead, 'parseAssignment Variable ahead'); | ||
if (ahead.tokenType !== lexer1_1.Operator) { | ||
return assignment; | ||
} | ||
else { | ||
lexer.NextTokenIs(lexer1_1.Operator); // +-*/ | ||
// lexer.LookAheadAndSkip(TOKEN_IGNORED) // 空格 | ||
lexer.isIgnored(); | ||
const idAndinit = assignment.declarations.pop(); | ||
return Assignment_1.parseBinaryExpression(lexer, idAndinit, assignment, "Identifier"); | ||
} | ||
} | ||
else { | ||
if (tokenType === lexer1_1.NUMBER) { | ||
// console.log('parseNumber start') | ||
const literial = new Assignment_1.Literal(parser_1.parseNumber(lexer)); // 这里面会把邻近的空格回车删掉 | ||
VariableDeclarator.init = literial; | ||
assignment.type = "VariableDeclaration"; | ||
// console.log('parseNumber end') | ||
} | ||
else { | ||
const literial = new Assignment_1.Literal(parser_1.parseString(lexer)); // 这里面会把邻近的空格回车删掉 | ||
VariableDeclarator.init = literial; | ||
assignment.type = "VariableDeclaration"; | ||
} | ||
assignment.declarations.push(VariableDeclarator); // 一行只允许声明和初始化一个变量 | ||
let ahead = lexer.LookAhead(); | ||
console.log(ahead, 'parseAssignment not Variable ahead'); | ||
if (ahead.tokenType !== lexer1_1.Operator) { | ||
return assignment; | ||
} | ||
else { | ||
lexer.NextTokenIs(lexer1_1.Operator); // +-*/ | ||
// lexer.LookAheadAndSkip(TOKEN_IGNORED); // 空格 | ||
lexer.isIgnored(); | ||
const idAndinit = assignment.declarations.pop(); | ||
return Assignment_1.parseBinaryExpression(lexer, idAndinit, assignment, "Literal"); | ||
} | ||
} | ||
} | ||
exports.paseReturnStatement = paseReturnStatement; |
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
Oops, something went wrong.