Initial commit
This commit is contained in:
commit
38ff06ea45
16 changed files with 1107 additions and 0 deletions
77
src/main.ts
Normal file
77
src/main.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import './style.css'
|
||||
import { SourceText, SourceRegion, sourceText } from 'source-region';
|
||||
import type { SourceLocation, Span } from 'source-region';
|
||||
|
||||
|
||||
type Expr =
|
||||
| { tag: "literal", value: Literal }
|
||||
| { tag: "list", values: Expr[] }
|
||||
|
||||
namespace Expr {
|
||||
export function number(value: number): Expr {
|
||||
return { tag: "literal", value: { tag: "number", value } };
|
||||
}
|
||||
export function identifier(value: Identifier): Expr {
|
||||
return { tag: "literal", value: { tag: "identifier", value } };
|
||||
}
|
||||
export function list(values: Expr[]): Expr {
|
||||
return { tag: "list", values };
|
||||
}
|
||||
|
||||
export function show(e: Expr): string {
|
||||
switch (e.tag) {
|
||||
case "literal":
|
||||
return showLiteral(e.value);
|
||||
case "list":
|
||||
return `(${e.values.map(show).join(" ")})`;
|
||||
}
|
||||
}
|
||||
|
||||
function showLiteral(e: Literal): string {
|
||||
switch (e.tag) {
|
||||
case "number":
|
||||
return `${e.value}`;
|
||||
case "identifier":
|
||||
return `${e.value}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Literal =
|
||||
| { tag: "number", value: number }
|
||||
| { tag: "identifier", value: Identifier }
|
||||
|
||||
type Identifier = string
|
||||
|
||||
|
||||
// === Examples ===
|
||||
|
||||
function example00() {
|
||||
const v: Expr = Expr.list([Expr.identifier("f"), Expr.number(123), Expr.number(512)]);
|
||||
console.log(v);
|
||||
console.log(Expr.show(v));
|
||||
}
|
||||
|
||||
function example01() {
|
||||
const str = `hello, world!
|
||||
foo
|
||||
bar `;
|
||||
|
||||
const source = sourceText(str);
|
||||
const region = source.fullRegion();
|
||||
|
||||
console.log(region);
|
||||
console.log(region.lineCount);
|
||||
region.forEachLine((span, lineNo) => {
|
||||
console.log(lineNo, region.stringOf(span));
|
||||
});
|
||||
}
|
||||
|
||||
[
|
||||
example00,
|
||||
example01,
|
||||
].forEach((f, i) => {
|
||||
console.log(`====${i}===`);
|
||||
f();
|
||||
})
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue