Fixes in code
This commit is contained in:
parent
cf28a22ddf
commit
80c6d14249
@ -365,7 +365,7 @@ class Parser {
|
|||||||
if (this.test("[")) {
|
if (this.test("[")) {
|
||||||
this.step();
|
this.step();
|
||||||
const value = this.parseExpr();
|
const value = this.parseExpr();
|
||||||
if (!this.test("]") {
|
if (!this.test("]")) {
|
||||||
this.report("expected ']'");
|
this.report("expected ']'");
|
||||||
return this.expr({ type: "error" }, pos);
|
return this.expr({ type: "error" }, pos);
|
||||||
}
|
}
|
||||||
@ -405,7 +405,7 @@ class Parser {
|
|||||||
if (this.test("(")) {
|
if (this.test("(")) {
|
||||||
this.step();
|
this.step();
|
||||||
let args: Expr[] = [];
|
let args: Expr[] = [];
|
||||||
if (!this.test(")") {
|
if (!this.test(")")) {
|
||||||
args.push(this.parseExpr());
|
args.push(this.parseExpr());
|
||||||
while (this.test(",")) {
|
while (this.test(",")) {
|
||||||
this.step();
|
this.step();
|
||||||
@ -414,7 +414,6 @@ class Parser {
|
|||||||
args.push(this.parseExpr());
|
args.push(this.parseExpr());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const value = this.parseExpr();
|
|
||||||
if (!this.test(")") {
|
if (!this.test(")") {
|
||||||
this.report("expected ')'");
|
this.report("expected ')'");
|
||||||
return this.expr({ type: "error" }, pos);
|
return this.expr({ type: "error" }, pos);
|
||||||
@ -642,7 +641,7 @@ class Parser {
|
|||||||
public parseBreak(): Stmt {
|
public parseBreak(): Stmt {
|
||||||
const pos = this.pos();
|
const pos = this.pos();
|
||||||
this.step();
|
this.step();
|
||||||
if (!this.test(";")) {
|
if (this.test(";")) {
|
||||||
return this.stmt({ type: "break" }, pos);
|
return this.stmt({ type: "break" }, pos);
|
||||||
}
|
}
|
||||||
const expr = this.parseExpr();
|
const expr = this.parseExpr();
|
||||||
@ -672,7 +671,7 @@ class Parser {
|
|||||||
public parseReturn(): Stmt {
|
public parseReturn(): Stmt {
|
||||||
const pos = this.pos();
|
const pos = this.pos();
|
||||||
this.step();
|
this.step();
|
||||||
if (!this.test(";")) {
|
if (this.test(";")) {
|
||||||
return this.stmt({ type: "return" }, pos);
|
return this.stmt({ type: "return" }, pos);
|
||||||
}
|
}
|
||||||
const expr = this.parseExpr();
|
const expr = this.parseExpr();
|
||||||
@ -781,7 +780,7 @@ class Parser {
|
|||||||
public parseParam(): { ok: true, value: Param } | { ok: false } {
|
public parseParam(): { ok: true, value: Param } | { ok: false } {
|
||||||
const pos = this.pos();
|
const pos = this.pos();
|
||||||
if (this.test("ident")) {
|
if (this.test("ident")) {
|
||||||
const ident = this.current().value;
|
const ident = this.current().identValue!;
|
||||||
this.step();
|
this.step();
|
||||||
return { ok: true, value: { ident, pos } };
|
return { ok: true, value: { ident, pos } };
|
||||||
}
|
}
|
||||||
@ -913,6 +912,7 @@ class Parser {
|
|||||||
// ...
|
// ...
|
||||||
while (!this.done()) {
|
while (!this.done()) {
|
||||||
if (this.test("}")) {
|
if (this.test("}")) {
|
||||||
|
this.step();
|
||||||
return this.expr({ type: "block", stmts }, pos);
|
return this.expr({ type: "block", stmts }, pos);
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@ -1042,8 +1042,10 @@ class Parser {
|
|||||||
this.eatSemicolon();
|
this.eatSemicolon();
|
||||||
stmts.push(this.stmt({ type: "assign", subject: expr, value }, pos));
|
stmts.push(this.stmt({ type: "assign", subject: expr, value }, pos));
|
||||||
} else if (this.test(";")) {
|
} else if (this.test(";")) {
|
||||||
|
this.step();
|
||||||
stmts.push(this.stmt({ type: "expr", expr }, expr.pos));
|
stmts.push(this.stmt({ type: "expr", expr }, expr.pos));
|
||||||
} else if (this.test("}")) {
|
} else if (this.test("}")) {
|
||||||
|
this.step();
|
||||||
return this.expr({ type: "block", stmts, expr }, pos);
|
return this.expr({ type: "block", stmts, expr }, pos);
|
||||||
} else {
|
} else {
|
||||||
this.report("expected ';' or '}'");
|
this.report("expected ';' or '}'");
|
||||||
@ -1129,7 +1131,7 @@ class Parser {
|
|||||||
if (this.test("fn")) {
|
if (this.test("fn")) {
|
||||||
// ...
|
// ...
|
||||||
} else if (this.test("{") || this.test("if") || this.test("loop")) {
|
} else if (this.test("{") || this.test("if") || this.test("loop")) {
|
||||||
let expr = this.parseMultiLineBlockExpr();
|
const expr = this.parseMultiLineBlockExpr();
|
||||||
stmts.push(this.stmt({ type: "expr", expr }, expr.pos));
|
stmts.push(this.stmt({ type: "expr", expr }, expr.pos));
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@ -1152,6 +1154,7 @@ class Parser {
|
|||||||
// ...
|
// ...
|
||||||
} else {
|
} else {
|
||||||
stmts.push(this.parseAssign());
|
stmts.push(this.parseAssign());
|
||||||
|
this.eatSemicolon();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stmts;
|
return stmts;
|
||||||
|
@ -290,7 +290,7 @@ Let's make a function `evalExpr` for evaluating expressions.
|
|||||||
```ts
|
```ts
|
||||||
class Evaluator {
|
class Evaluator {
|
||||||
// ...
|
// ...
|
||||||
public evalExpr(expr: Expr, syms: Expr): Flow {
|
public evalExpr(expr: Expr, syms: Syms): Flow {
|
||||||
if (expr.kind.type === "error") {
|
if (expr.kind.type === "error") {
|
||||||
throw new Error("error in AST");
|
throw new Error("error in AST");
|
||||||
}
|
}
|
||||||
@ -785,7 +785,7 @@ class Evaluator {
|
|||||||
sym.value = value;
|
sym.value = value;
|
||||||
return flowValue({ type: "null" });
|
return flowValue({ type: "null" });
|
||||||
}
|
}
|
||||||
if (stmt.kind.subject.type === "field") {
|
if (stmt.kind.subject.kind.type === "field") {
|
||||||
const [subject, subjectFlow] = expectValue(this.evalExpr(stmt.kind.subject.kind.subject, syms));
|
const [subject, subjectFlow] = expectValue(this.evalExpr(stmt.kind.subject.kind.subject, syms));
|
||||||
if (!subject)
|
if (!subject)
|
||||||
return subjectFlow;
|
return subjectFlow;
|
||||||
@ -906,8 +906,8 @@ We'll want a function for evaluating the top-level statements.
|
|||||||
```ts
|
```ts
|
||||||
class Evaluator {
|
class Evaluator {
|
||||||
// ...
|
// ...
|
||||||
public evalStmts(stmts: Stmt[], syms: Syms) {
|
public evalStmts(stmts: Stmt[]) {
|
||||||
let scopeSyms = new Syms(syms);
|
let scopeSyms = new Syms(this.root);
|
||||||
for (const stmt of stmts) {
|
for (const stmt of stmts) {
|
||||||
const flow = this.evalStmt(stmt, scopeSyms);
|
const flow = this.evalStmt(stmt, scopeSyms);
|
||||||
if (flow.type !== "value")
|
if (flow.type !== "value")
|
||||||
|
@ -77,15 +77,15 @@ Exactly like in chapter 1, we need a function for evaluating the expression stru
|
|||||||
|
|
||||||
```rs
|
```rs
|
||||||
fn eval_expr(node) {
|
fn eval_expr(node) {
|
||||||
if (node.type == "int") {
|
if == node.type "int" {
|
||||||
return node.value;
|
return node.value;
|
||||||
}
|
}
|
||||||
if (node.type == "add") {
|
if == node.type "add" {
|
||||||
let left = eval_expr(node.left);
|
let left = eval_expr(node.left);
|
||||||
let right = eval_expr(node.right);
|
let right = eval_expr(node.right);
|
||||||
return + left right;
|
return + left right;
|
||||||
}
|
}
|
||||||
if (node.type == "multiply") {
|
if == node.type "multiply" {
|
||||||
let left = eval_expr(node.left);
|
let left = eval_expr(node.left);
|
||||||
let right = eval_expr(node.right);
|
let right = eval_expr(node.right);
|
||||||
return * left right;
|
return * left right;
|
||||||
@ -150,12 +150,12 @@ fn lex(text) {
|
|||||||
} else if == text[i] "+"[0] {
|
} else if == text[i] "+"[0] {
|
||||||
i = + i 1;
|
i = + i 1;
|
||||||
let token = struct();
|
let token = struct();
|
||||||
token["type"] = "+"[0];
|
token["type"] = "+";
|
||||||
array_push(token);
|
array_push(token);
|
||||||
} else if == text[i] "*"[0] {
|
} else if == text[i] "*"[0] {
|
||||||
i = + i 1;
|
i = + i 1;
|
||||||
let token = struct();
|
let token = struct();
|
||||||
token["type"] = "*"[0];
|
token["type"] = "*";
|
||||||
array_push(token);
|
array_push(token);
|
||||||
} else {
|
} else {
|
||||||
println("illegal character");
|
println("illegal character");
|
||||||
|
Loading…
Reference in New Issue
Block a user