Skip to content

Commit

Permalink
⬆️ UPDATE 语法分析
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwu51 committed Dec 25, 2024
1 parent 7b8561a commit 564aabc
Show file tree
Hide file tree
Showing 11 changed files with 1,198 additions and 322 deletions.
423 changes: 423 additions & 0 deletions 24.11/parser_test10.mjs

Large diffs are not rendered by default.

501 changes: 501 additions & 0 deletions 24.11/parser_test11.mjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions 24.11/parser_test2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VarSentence extends Sentence {
}

toString() {
return `var ${this.name} = ${this.value.toString()}`;
return `var ${this.name} = ${this.value.toString()};`;
}
}

Expand All @@ -26,7 +26,7 @@ class ReturnSentence extends Sentence {
this.value = value; // 这里的value也是表达式
}
toString() {
return `return ${this.value.toString()}`;
return `return ${this.value.toString()};`;
}
}

Expand All @@ -49,7 +49,7 @@ class ExpressionStatement extends Sentence {
}

toString() {
return this.expression.toString();
return this.expression.toString() + ";";
}
}

Expand Down
6 changes: 3 additions & 3 deletions 24.11/parser_test3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class VarSentence extends Sentence {
}

toString() {
return `var ${this.name} = ${this.value.toString()}`;
return `var ${this.name} = ${this.value.toString()};`;
}
}

Expand All @@ -27,7 +27,7 @@ class ReturnSentence extends Sentence {
this.value = value; // 这里的value也是表达式
}
toString() {
return `return ${this.value.toString()}`;
return `return ${this.value.toString()};`;
}
}

Expand All @@ -50,7 +50,7 @@ class ExpressionStatement extends Sentence {
}

toString() {
return this.expression.toString();
return this.expression.toString() + ";";
}
}
///////////////////// 注意 AstNode新增了full属性
Expand Down
6 changes: 3 additions & 3 deletions 24.11/parser_test4.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class VarSentence extends Sentence {
}

toString() {
return `var ${this.name} = ${this.value.toString()}`;
return `var ${this.name} = ${this.value.toString()};`;
}
}

Expand All @@ -27,7 +27,7 @@ class ReturnSentence extends Sentence {
this.value = value; // 这里的value也是表达式
}
toString() {
return `return ${this.value.toString()}`;
return `return ${this.value.toString()};`;
}
}

Expand All @@ -50,7 +50,7 @@ class ExpressionStatement extends Sentence {
}

toString() {
return this.expression.toString();
return this.expression.toString() + ";";
}
}
class AstNode {
Expand Down
6 changes: 3 additions & 3 deletions 24.11/parser_test5.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class VarSentence extends Sentence {
}

toString() {
return `var ${this.name} = ${this.value.toString()}`;
return `var ${this.name} = ${this.value.toString()};`;
}
}

Expand All @@ -27,7 +27,7 @@ class ReturnSentence extends Sentence {
this.value = value; // 这里的value也是表达式
}
toString() {
return `return ${this.value.toString()}`;
return `return ${this.value.toString()};`;
}
}

Expand All @@ -50,7 +50,7 @@ class ExpressionStatement extends Sentence {
}

toString() {
return this.expression.toString();
return this.expression.toString() +";";
}
}
class AstNode {
Expand Down
41 changes: 4 additions & 37 deletions 24.11/parser_test6.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VarSentence extends Sentence {
}

toString() {
return `var ${this.name} = ${this.value.toString()}`;
return `var ${this.name} = ${this.value.toString()};`;
}
}

Expand All @@ -26,7 +26,7 @@ class ReturnSentence extends Sentence {
this.value = value; // 这里的value也是表达式
}
toString() {
return `return ${this.value.toString()}`;
return `return ${this.value.toString()};`;
}
}

Expand All @@ -49,7 +49,7 @@ class ExpressionStatement extends Sentence {
}

toString() {
return this.expression.toString();
return this.expression.toString() + ";";
}
}
// 基础类型
Expand Down Expand Up @@ -139,40 +139,7 @@ class PostfixOperatorAstNode extends AstNode {
return `(${this.left.toString()} ${this.op.value})`;
}
}
// 函数声明
class FunctionDeclarationAstNode extends AstNode {
constructor(nameToken, params, body) {
super();
this.name = name == null ? null :new IdentifierAstNode(nameToken);
this.params = params;
this.body = body;
}
toString() {
return `function${this.name ? ' ' + this.name.toString() : ''}(${this.params.join(',')})${this.body.map(it=>it.toString()).join('\n')}`;
}
}
// 函数调用
class FunctionCallAstNode extends AstNode {
constructor(nameToken, args) {
super();
this.name = new IdentifierAstNode(nameToken);
this.args = args; // args是ast数组
}
toString() {
return `${this.name.toString()}(${this.args.map(it=>it.toString()).join(',')})`
}
}
// 分组节点
class GroupAstNode extends AstNode {
constructor(exp) {
super();
this.exp = exp;
}
toString() {
// 因为小括号已经在运算符的toString中使用了,这里为了更好的凸显使用中文中括号
return `【${this.exp.toString()}】`
}
}

// 语法解析,把tokens转换为sentences
function parse(tokens) {
// 从i开始转换成var语句,校验是不是var xx = xxx;格式,然后需要解析表达式parseExpression函数。
Expand Down
53 changes: 7 additions & 46 deletions 24.11/parser_test7.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VarSentence extends Sentence {
}

toString() {
return `var ${this.name} = ${this.value.toString()}`;
return `var ${this.name} = ${this.value.toString()};`;
}
}

Expand All @@ -26,7 +26,7 @@ class ReturnSentence extends Sentence {
this.value = value; // 这里的value也是表达式
}
toString() {
return `return ${this.value.toString()}`;
return `return ${this.value.toString()};`;
}
}

Expand All @@ -49,7 +49,7 @@ class ExpressionStatement extends Sentence {
}

toString() {
return this.expression.toString();
return this.expression.toString() + ";";
}
}
// 基础类型
Expand Down Expand Up @@ -139,40 +139,6 @@ class PostfixOperatorAstNode extends AstNode {
return `(${this.left.toString()} ${this.op.value})`;
}
}
// 函数声明
class FunctionDeclarationAstNode extends AstNode {
constructor(nameToken, params, body) {
super();
this.name = name == null ? null :new IdentifierAstNode(nameToken);
this.params = params;
this.body = body;
}
toString() {
return `function${this.name ? ' ' + this.name.toString() : ''}(${this.params.join(',')})${this.body.map(it=>it.toString()).join('\n')}`;
}
}
// 函数调用
class FunctionCallAstNode extends AstNode {
constructor(nameToken, args) {
super();
this.name = new IdentifierAstNode(nameToken);
this.args = args; // args是ast数组
}
toString() {
return `${this.name.toString()}(${this.args.map(it=>it.toString()).join(',')})`
}
}
// 分组节点
class GroupAstNode extends AstNode {
constructor(exp) {
super();
this.exp = exp;
}
toString() {
// 因为小括号已经在运算符的toString中使用了,这里为了更好的凸显使用中文中括号
return `【${this.exp.toString()}】`
}
}



Expand Down Expand Up @@ -295,15 +261,10 @@ class Parser {
// 转换为块语句,块语句中包含一个语句数组
parseBlockSentence() {
var tokens = this.tokens;
var braceCount = 0;
for (var j = this.cursor; j < tokens.length; j++) {
if (tokens[j].type == LEX.LBRACE) braceCount++;
if (tokens[j].type == LEX.RBRACE) braceCount--;
if (braceCount == 0) {
return new BlockSentence(parse(tokens.slice(this.cursor + 1, this.cursor = j)));
}
}
throw new Error("brace not close for block sentence")
assert(tokens[this.cursor++].type === LEX.LBRACE, "brace not open for block sentence")
var result = new BlockSentence(this.parse());
assert(tokens[this.cursor++].type === LEX.RBRACE, "brace not close for block sentence");
return result
}

// 表达式解析,解析下一个表达式,遇到无法识别的字符会结束
Expand Down
Loading

0 comments on commit 564aabc

Please sign in to comment.