Add Math methods
This commit is contained in:
parent
7a4af60805
commit
ca184dcf40
1 changed files with 9 additions and 8 deletions
|
@ -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<any> // 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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue