Skip to content

Commit

Permalink
rename stat to stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
uzmoi committed Jan 15, 2024
1 parent 9e2daa5 commit 552cf46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions examples/__snapshots__/script.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
exports[`script 1`] = `
{
"last": null,
"stats": [
"stmts": [
{
"body": {
"last": {
"elements": [],
"type": "Tuple",
},
"stats": [
"stmts": [
{
"expr": {
"arguments": [
Expand Down
30 changes: 15 additions & 15 deletions examples/script.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, test } from "@jest/globals";
import { expr, stat } from "./script";
import { expr, stmt } from "./script";

describe("stat", () => {
describe("stmt", () => {
test("Let", () => {
const result = stat.parse("let hoge = 0;");
const result = stmt.parse("let hoge = 0;");
expect(result.success && result.value).toEqual({
type: "Let",
name: "hoge",
Expand All @@ -12,41 +12,41 @@ describe("stat", () => {
});

test("DefFn", () => {
const result = stat.parse("fn main(arg) {};");
const result = stmt.parse("fn main(arg) {};");
expect(result.success && result.value).toEqual({
type: "DefFn",
name: "main",
params: ["arg"],
body: { type: "Block", stats: [], last: null },
body: { type: "Block", stmts: [], last: null },
});
});

test("Return", () => {
const result = stat.parse("return;");
const result = stmt.parse("return;");
expect(result.success && result.value).toEqual({
type: "Return",
body: null,
});
});

test("While", () => {
const result = stat.parse("while (false) {};");
const result = stmt.parse("while (false) {};");
expect(result.success && result.value).toEqual({
type: "While",
test: { type: "Bool", value: false },
body: { type: "Block", stats: [], last: null },
body: { type: "Block", stmts: [], last: null },
});
});

test("Break", () => {
const result = stat.parse("break;");
const result = stmt.parse("break;");
expect(result.success && result.value).toEqual({
type: "Break",
});
});

test("Expr", () => {
const result = stat.parse("0;");
const result = stmt.parse("0;");
expect(result.success && result.value).toEqual({
type: "Expr",
expr: { type: "Number", value: 0 },
Expand Down Expand Up @@ -127,23 +127,23 @@ describe("expr", () => {
const result = expr.parse("{}");
expect(result.success && result.value).toEqual({
type: "Block",
stats: [],
stmts: [],
last: null,
});
});
test("stats", () => {
test("stmts", () => {
const result = expr.parse("{ 0; }");
expect(result.success && result.value).toEqual({
type: "Block",
stats: [{ type: "Expr", expr: { type: "Number", value: 0 } }],
stmts: [{ type: "Expr", expr: { type: "Number", value: 0 } }],
last: null,
});
});
test("expr", () => {
const result = expr.parse("{ 0 }");
expect(result.success && result.value).toEqual({
type: "Block",
stats: [],
stmts: [],
last: { type: "Number", value: 0 },
});
});
Expand All @@ -155,7 +155,7 @@ describe("expr", () => {
expect(result.success && result.value).toEqual({
type: "If",
test: { type: "Bool", value: true },
then: { type: "Block", stats: [], last: null },
then: { type: "Block", stmts: [], last: null },
else: null,
});
});
Expand Down
24 changes: 12 additions & 12 deletions examples/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type Expr =
| { type: "Number"; value: number }
| { type: "String"; value: string }
| { type: "Tuple"; elements: readonly Expr[] }
| { type: "Block"; stats: Stat[]; last: Expr | null }
| { type: "Block"; stmts: Stmt[]; last: Expr | null }
| { type: "If"; test: Expr; then: Expr; else: Expr | null }
| { type: "Ident"; name: string }
| { type: "Call"; callee: Expr; arguments: readonly Expr[] }
Expand Down Expand Up @@ -39,7 +39,7 @@ const keyword = (keyword: string): P.Parser<unknown, string> => {

const Ident = P.regex(/\w+/).map(name => ({ type: "Ident", name }) satisfies Expr);

export type Stat =
export type Stmt =
| { type: "Let"; name: string; init: Expr }
| { type: "DefFn"; name: string; params: readonly string[]; body: Expr }
| { type: "Return"; body: Expr | null }
Expand All @@ -50,7 +50,7 @@ export type Stat =
const Let = keyword("let")
.then(Ident.between(ws))
.skip(P.el("="))
.andMap(expr, ({ name }, init): Stat => ({ type: "Let", name, init }));
.andMap(expr, ({ name }, init): Stmt => ({ type: "Let", name, init }));

const DefFn = P.seq([
keyword("fn").then(Ident.between(ws)),
Expand All @@ -60,7 +60,7 @@ const DefFn = P.seq([
.between(P.el("("), P.el(")"))
.map(nodes => nodes.map(node => node.name)),
expr,
]).map<Stat>(([{ name }, params, body]) => ({
]).map<Stmt>(([{ name }, params, body]) => ({
type: "DefFn",
name,
params,
Expand All @@ -70,18 +70,18 @@ const DefFn = P.seq([
const Return = keyword("return")
.then(expr.option(null))
.skip(ws)
.map<Stat>(body => ({ type: "Return", body }));
.map<Stmt>(body => ({ type: "Return", body }));

const While = keyword("while")
.skip(ws)
.then(expr.between(P.el("("), P.el(")")))
.andMap(expr, (test, body): Stat => ({ type: "While", test, body }));
.andMap(expr, (test, body): Stmt => ({ type: "While", test, body }));

const Break = keyword("break").return<Stat>({ type: "Break" }).skip(ws);
const Break = keyword("break").return<Stmt>({ type: "Break" }).skip(ws);

const Expr = expr.map<Stat>(expr => ({ type: "Expr", expr }));
const Expr = expr.map<Stmt>(expr => ({ type: "Expr", expr }));

export const stat: P.Parser<Stat, string> = P.choice([
export const stmt: P.Parser<Stmt, string> = P.choice([
Let,
DefFn,
Return,
Expand Down Expand Up @@ -126,10 +126,10 @@ const Tuple = expr
.between(P.el("("), P.el(")"))
.map(elements => ({ type: "Tuple", elements }) satisfies Expr);

const Block = stat
const Block = stmt
.apply(P.many)
.andMap(expr.option(null), (stats, last) => {
return { type: "Block", stats, last } satisfies Expr;
.andMap(expr.option(null), (stmts, last) => {
return { type: "Block", stmts, last } satisfies Expr;
})
.skip(ws)
.between(P.el("{"), P.el("}"));
Expand Down

0 comments on commit 552cf46

Please sign in to comment.