Adding builtins as native-functions. desugaring symbols as native function application. except ~ which is the new pipe operator, > is now greater than

This commit is contained in:
Dustin Swan 2026-02-02 20:10:32 -07:00
parent 5b40e9d298
commit 216fe6bd30
No known key found for this signature in database
GPG key ID: 30D46587E2100467
7 changed files with 587 additions and 115 deletions

View file

@ -43,18 +43,34 @@ export class Parser {
const kind = this.current().kind;
return kind === 'plus' || kind === 'minus' ||
kind === 'star' || kind === 'slash' ||
kind === 'greater-than' || kind === 'ampersand';
kind === 'percent' || kind === 'caret' ||
kind === 'ampersand' ||
kind === 'equals-equals' || kind === 'not-equals' ||
kind === 'less-than' || kind === 'less-equals' ||
kind === 'greater-than' || kind === 'greater-equals' ||
kind === 'tilde';
}
private tokenToOpName(token: Token): string {
switch (token.kind) {
case 'plus': return '+';
case 'minus': return '-';
case 'star': return '*';
case 'slash': return '/';
case 'greater-than': return '>';
case 'ampersand': return '&';
case 'ampersand': return 'cat';
// Arithmetic
case 'plus': return 'add';
case 'minus': return 'sub';
case 'star': return 'mul';
case 'slash': return 'div';
case 'percent': return 'mod';
case 'caret': return 'pow';
// Comparison
case 'equals-equals': return 'eq';
case 'not-equals': return 'neq';
case 'greater-than': return 'gt';
case 'greater-equals': return 'gte';
case 'less-than': return 'lt';
case 'less-equals': return 'lte';
default: throw new Error(`Not an operator: ${token.kind}`);
}
}
@ -246,13 +262,26 @@ export class Parser {
while (this.isInfixOp()) {
const opToken = this.advance();
const opName = this.tokenToOpName(opToken);
const right = this.parseApplication();
left = {
kind: 'apply',
func: { kind: 'variable', name: opName },
args: [left, right]
if (opToken.kind === 'tilde') {
// function application operator
const right = this.parseApplication();
left = {
kind: 'apply',
func: right,
args: [left]
};
} else {
// operators desugar to function calls
const opName = this.tokenToOpName(opToken);
const right = this.parseApplication();
left = {
kind: 'apply',
func: { kind: 'variable', name: opName },
args: [left, right]
}
}
}