From ca184dcf40b908232b8189fffce154898a83a1d8 Mon Sep 17 00:00:00 2001 From: Seviche CC Date: Sun, 11 Jun 2023 13:11:55 +0800 Subject: [PATCH] Add Math methods --- TypeScript/lisp.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/TypeScript/lisp.ts b/TypeScript/lisp.ts index a45d3e7..524a320 100644 --- a/TypeScript/lisp.ts +++ b/TypeScript/lisp.ts @@ -3,8 +3,8 @@ const program = '(begin (define r 10) (* pi (* r r)))' //--------- types ------------ -type LSymbolType = string // A Lisp Symbol(alias TSymbol) is implemented as TypeScript string -type LNumber = number // A Lisp Symbol(alias TSymbol) is implemented as TypeScript number +type LSymbolType = string // A Lisp Symbol is implemented as TypeScript string +type LNumber = number // A Lisp Number is implemented as TypeScript number type Atom = LSymbolType | number // A Lisp Atom is a Symbol or Number impl type List = Array // A Lisp List is implemented as a TypeScript array type Exp = Atom | List // A Lisp expression is an Atom or List @@ -105,13 +105,11 @@ const standardEnv = (): Env => { env.set('>=', (a: number, b: number) => a >= b) env.set('<=', (a: number, b: number) => a <= b) env.set('=', (a: number, b: number) => a === b) - env.set('abs', Math.abs) env.set('append', (a: any[], b: any[]) => a.concat(b)) env.set('apply', (proc: Function, args: any[]) => proc(...args)) env.set('begin', (...x: any) => x[x.length - 1]) env.set('car', (...x: any[]) => x[0]) env.set('cdr', (...x: any[]) => x.slice(1)) - env.set('cos', Math.cos) env.set('cons', (x: any, y: any[]) => [x, ...y]) env.set('eq?', (a: any, b: any) => Object.is(a, b)) env.set('equal?', (a: any, b: any) => deepEqual(a, b)) @@ -120,17 +118,20 @@ const standardEnv = (): Env => { env.set('list', (...x: any[]) => Array.from(x)) env.set('list?', (x: any) => Array.isArray(x)) env.set('map', (func: (value: any, index: number, array: any[]) => unknown, arr: any[]) => arr.map(func)) - env.set('max', Math.max) - env.set('min', Math.min) env.set('not', (x: boolean) => !x) env.set('null?', (x: any[]) => x.length === 0) env.set('number?', (x: any) => typeof x === 'number') env.set('print', console.log) env.set('pi', Math.PI) env.set('procedure?', (x: any) => typeof x === 'function') - env.set('round', Math.round) - env.set('sin', Math.sin) env.set('symbol?', (x: any) => x instanceof LSymbol) + + Object.entries(Math).reduce((acc, [key, value]) => { + if (typeof value === 'function') { + acc.set(key,value) + } + return acc + },env) return env }