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