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:
parent
5b40e9d298
commit
216fe6bd30
7 changed files with 587 additions and 115 deletions
|
|
@ -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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue