-
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
Showing
24 changed files
with
560 additions
and
250 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { parseLambda } from '../parser/untyped.js'; | ||
|
||
export const [, predLambda] = parseLambda('λn.λf.λx.n(λg.λh.h(gf))(λu.x)(λu.u)'); | ||
export const [, predLambda] = parseLambda('λn.λf.λx.n(λg.λh.h(g f))(λu.x)(λu.u)'); |
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,48 @@ | ||
import { SKIExpression } from '../ski/expression.js'; | ||
import { UntypedLambda } from '../terms/lambda.js'; | ||
import { SystemFTerm } from '../terms/systemF.js'; | ||
import { TypedLambda } from '../types/typedLambda.js'; | ||
import { BaseType } from '../types/types.js'; | ||
|
||
export interface TripLangProgram { | ||
kind: 'program'; | ||
terms: TripLangTerm[]; | ||
} | ||
|
||
export type TripLangTerm = | ||
| PolyDefinition | ||
| TypedDefinition | ||
| UntypedDefinition | ||
| CombinatorDefinition | ||
| TypeDefinition; | ||
|
||
export interface PolyDefinition { | ||
kind: 'poly'; | ||
name: string; | ||
term: SystemFTerm; | ||
} | ||
|
||
export interface TypedDefinition { | ||
kind: 'typed'; | ||
name: string; | ||
type: BaseType; | ||
term: TypedLambda; | ||
} | ||
|
||
export interface UntypedDefinition { | ||
kind: 'untyped'; | ||
name: string; | ||
term: UntypedLambda; | ||
} | ||
|
||
export interface CombinatorDefinition { | ||
kind: 'combinator'; | ||
name: string; | ||
term: SKIExpression; | ||
} | ||
|
||
export interface TypeDefinition { | ||
kind: 'type'; | ||
name: string; | ||
type: BaseType; | ||
} |
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 |
---|---|---|
@@ -1,95 +1,75 @@ | ||
import { ParseError } from './parseError.js'; | ||
|
||
/** | ||
* An immutable parser state. | ||
*/ | ||
export interface ParserState { | ||
buf: string; | ||
idx: number; | ||
} | ||
|
||
/** | ||
* Creates a new parser state from the given string. | ||
*/ | ||
export function createParserState(buf: string): ParserState { | ||
return { buf, idx: 0 }; | ||
} | ||
|
||
/** | ||
* Advances the index past any whitespace characters. | ||
*/ | ||
function skipWhitespace(rdb: ParserState): ParserState { | ||
let idx = rdb.idx; | ||
while (idx < rdb.buf.length && /\s/.test(rdb.buf[idx])) { | ||
export function skipWhitespace(state: ParserState): ParserState { | ||
let idx = state.idx; | ||
while (idx < state.buf.length && /\s/.test(state.buf[idx])) { | ||
idx++; | ||
} | ||
return { buf: rdb.buf, idx }; | ||
return { buf: state.buf, idx }; | ||
} | ||
|
||
/** | ||
* Returns the next non‐whitespace character (or null if at end‐of-buffer), | ||
* along with the updated state. | ||
*/ | ||
export function peek(rdb: ParserState): [string | null, ParserState] { | ||
const state = skipWhitespace(rdb); | ||
if (state.idx < state.buf.length) { | ||
return [state.buf[state.idx], state]; | ||
export function peek(state: ParserState): [string | null, ParserState] { | ||
const newState = skipWhitespace(state); | ||
if (newState.idx < newState.buf.length) { | ||
return [newState.buf[newState.idx], newState]; | ||
} | ||
return [null, state]; | ||
return [null, newState]; | ||
} | ||
|
||
/** | ||
* Consumes one character and returns the updated state. | ||
*/ | ||
export function consume(rdb: ParserState): ParserState { | ||
return { buf: rdb.buf, idx: rdb.idx + 1 }; | ||
export function consume(state: ParserState): ParserState { | ||
return { buf: state.buf, idx: state.idx + 1 }; | ||
} | ||
|
||
/** | ||
* Matches the given character (after skipping whitespace). Throws a ParseError | ||
* if the next non‐whitespace character is not the expected one. | ||
*/ | ||
export function matchCh(rdb: ParserState, ch: string): ParserState { | ||
const [next, state] = peek(rdb); | ||
export function matchCh(state: ParserState, ch: string): ParserState { | ||
const [next, newState] = peek(state); | ||
if (next !== ch) { | ||
throw new ParseError(`expected ${ch} but found ${next ?? 'null'}`); | ||
throw new ParseError(`expected '${ch}' but found '${next ?? 'EOF'}'`); | ||
} | ||
return consume(state); | ||
return consume(newState); | ||
} | ||
|
||
/** | ||
* Matches a left parenthesis. | ||
*/ | ||
export function matchLP(rdb: ParserState): ParserState { | ||
return matchCh(rdb, '('); | ||
export function matchLP(state: ParserState): ParserState { | ||
return matchCh(state, '('); | ||
} | ||
|
||
/** | ||
* Matches a right parenthesis. | ||
*/ | ||
export function matchRP(rdb: ParserState): ParserState { | ||
return matchCh(rdb, ')'); | ||
export function matchRP(state: ParserState): ParserState { | ||
return matchCh(state, ')'); | ||
} | ||
|
||
/** | ||
* Checks whether there is any non‐whitespace character left, returning both the | ||
* boolean result and the updated state. | ||
*/ | ||
export function remaining(rdb: ParserState): [boolean, ParserState] { | ||
const state = skipWhitespace(rdb); | ||
return [state.idx < state.buf.length, state]; | ||
} | ||
|
||
/** | ||
* Parses a variable from the input. The variable is expected to be a letter. | ||
*/ | ||
export function parseVariable(rdb: ParserState): [string, ParserState] { | ||
const [next, state] = peek(rdb); | ||
if (next === null) { | ||
throw new ParseError('failed to parse variable: no next character'); | ||
export function parseIdentifier(state: ParserState): [string, ParserState] { | ||
let id = ''; | ||
let currentState = skipWhitespace(state); | ||
while (currentState.idx < currentState.buf.length) { | ||
const ch = currentState.buf[currentState.idx]; | ||
if (!/[a-zA-Z0-9_]/.test(ch)) break; | ||
id += ch; | ||
currentState = consume(currentState); | ||
} | ||
if (id.length === 0) { | ||
throw new ParseError('expected an identifier'); | ||
} | ||
if (!/[a-zA-Z]/.test(next)) { | ||
throw new ParseError(`failed to parse variable: ${next} did not match`); | ||
return [id, currentState]; | ||
} | ||
|
||
export function remaining(state: ParserState): [boolean, ParserState] { | ||
const newState = skipWhitespace(state); | ||
return [newState.idx < newState.buf.length, newState]; | ||
} | ||
|
||
export function parseKeyword(state: ParserState, keywords: string[]): [string, ParserState] { | ||
const [word, nextState] = parseIdentifier(state); | ||
if (!keywords.includes(word)) { | ||
throw new ParseError(`expected keyword, found ${word}`); | ||
} | ||
return [next, consume(state)]; | ||
return [word, nextState]; | ||
} |
Oops, something went wrong.