This repository has been archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56f5f04
commit adb4c64
Showing
9 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// TODO: make this include position data. | ||
type TermInput<T> = { | ||
application: (left: T, right: T) => T | ||
abstraction: (name: string, body: T) => T | ||
variable: (name: string) => T | ||
} | ||
|
||
type Term<T> = (input: TermInput<T>) => T | ||
|
||
export const call = <T>(left: Term<T>, right: Term<T>): Term<T> => (config) => | ||
config.application(left(config), right(config)) | ||
|
||
export const abstract = <T>(name: string, body: Term<T>): Term<T> => (config) => | ||
config.abstraction(name, body(config)) | ||
|
||
export const variable = <T>(name: string): Term<T> => (config) => | ||
config.variable(name) | ||
|
||
export const abstractMany = <T>(names: string[], body: Term<T>): Term<T> => | ||
names.reduceRight((previous, name) => abstract(name, previous), body) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import moo from "moo" | ||
|
||
export interface Position { | ||
line: number | ||
col: number | ||
} | ||
|
||
export interface Token { | ||
start: Position | ||
end: Position | ||
type?: string | ||
value: string | ||
} | ||
|
||
export const lexer = moo.compile({ | ||
ws: /[ \t]+/, | ||
nl: { match: "\n", lineBreaks: true }, | ||
lambdaChar: ["λ", "\\"], | ||
arrowChar: ["->", "."], | ||
lparan: "(", | ||
rparan: ")", | ||
identifier: { | ||
match: /[a-z_][a-z_0-9]*/ | ||
} | ||
}) | ||
|
||
export function tokenStart(token: moo.Token): Position { | ||
return { | ||
line: token.line, | ||
col: token.col - 1 | ||
} | ||
} | ||
|
||
export function tokenEnd(token: moo.Token): Position { | ||
const lastNewLine = token.text.lastIndexOf("\n") | ||
|
||
if (lastNewLine !== -1) { | ||
throw new Error("Unsupported case: token with line breaks") | ||
} | ||
|
||
return { | ||
line: token.line, | ||
col: token.col + token.text.length - 1 | ||
} | ||
} | ||
|
||
export function convertToken(token: moo.Token): Token { | ||
return { | ||
type: token.type, | ||
value: token.value, | ||
start: tokenStart(token), | ||
end: tokenEnd(token) | ||
} | ||
} | ||
|
||
export function convertTokenId(data: moo.Token[]): Token { | ||
return convertToken(data[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// @ts-ignore | ||
import syntax from "./syntax.ne" | ||
import { Parser, Grammar } from "nearley" | ||
|
||
const parser = new Parser(Grammar.fromCompiled(syntax)) | ||
|
||
parser.feed("\\f a b. f b a \\l. l") | ||
const result = parser.results[0]({ | ||
variable: (d) => d, | ||
abstraction: (name, body) => ({ name, body }), | ||
application: (left, right) => ({ left, right }) | ||
}) | ||
|
||
console.log(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@{% | ||
const { lexer, tokenStart, convertTokenId } = require("./lexer.ts") | ||
const { call, abstract, variable, abstractMany } = require("./ast.ts") | ||
%} | ||
|
||
@lexer lexer | ||
|
||
expression | ||
-> application {% id %} | ||
| abstraction {% id %} | ||
|
||
application | ||
-> no_lambda_application {% id %} | ||
| no_lambda_application __ abstraction | ||
{% ([left, _, right]) => call(left, right) %} | ||
|
||
no_lambda_application | ||
-> atom {% id %} | ||
| no_lambda_application __ atom | ||
{% ([left, _, right]) => call(left, right) %} | ||
|
||
|
||
atom | ||
-> "(" _ expression _ ")" {% d => d[2] %} | ||
| variable {% id %} | ||
|
||
variable | ||
-> identifier | ||
{% ([d]) => variable(d.value) %} | ||
|
||
abstraction | ||
-> lambdaChar lambdaArgs _ arrowChar _ expression | ||
{% d => abstractMany(d[1].map(a => a.value), d[5]) %} | ||
|
||
lambdaArgs -> (_ identifier _ {% d => d[1] %}):+ {% id %} | ||
|
||
# Lexing | ||
arrowChar -> %arrowChar {% convertTokenId %} | ||
lambdaChar -> %lambdaChar {% convertTokenId %} | ||
identifier -> %identifier {% convertTokenId %} | ||
__ -> %ws:+ | ||
_ -> %ws:* |