Skip to content

Commit

Permalink
Created pylib.ts, added functions as built-ins to Python supporting B…
Browse files Browse the repository at this point in the history
…igInt typing. (#1591)

* Changed createContext, created pylib to support BigInt type in Python

* Added pylib.ts for bigint-supported binary operations

* Fixed typos in pylib and createContext

* Removed obsolete defining Math library in PYTHON_1

* Fixed formatting to pass Prettier test

* Fix format

---------

Co-authored-by: Richard Dominick <[email protected]>
Co-authored-by: Martin Henz <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2024
1 parent 7db94b5 commit c4fc20d
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 20 deletions.
70 changes: 50 additions & 20 deletions src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { list_to_vector } from './stdlib/list'
import { listPrelude } from './stdlib/list.prelude'
import { localImportPrelude } from './stdlib/localImport.prelude'
import * as misc from './stdlib/misc'
import * as pylib from './stdlib/pylib'
import { nonDetPrelude } from './stdlib/non-det.prelude'
import * as parser from './stdlib/parser'
import * as stream from './stdlib/stream'
Expand Down Expand Up @@ -698,26 +699,55 @@ export const importBuiltins = (context: Context, externalBuiltIns: CustomBuiltIn
defineBuiltin(context, 'None', undefined)
defineBuiltin(context, 'NaN', NaN)
defineBuiltin(context, 'Infinity', Infinity)
// Define all Math libraries
const mathLibraryNames = Object.getOwnPropertyNames(Math)
// Short param names for stringified version of math functions
const parameterNames = [...'abcdefghijklmnopqrstuvwxyz']
for (const name of mathLibraryNames) {
const value = Math[name]
if (typeof value === 'function') {
let paramString: string
let minArgsNeeded = undefined
if (name === 'max' || name === 'min') {
paramString = '...values'
minArgsNeeded = 0
} else {
paramString = parameterNames.slice(0, value.length).join(', ')
}
defineBuiltin(context, `math_${name}(${paramString})`, value, minArgsNeeded)
} else {
defineBuiltin(context, `math_${name}`, value)
}
}

// Binary operators
defineBuiltin(context, '__py_adder(x, y)', pylib.__py_adder)
defineBuiltin(context, '__py_minuser(x, y)', pylib.__py_minuser)
defineBuiltin(context, '__py_multiplier(x, y)', pylib.__py_multiplier)
defineBuiltin(context, '__py_divider(x, y)', pylib.__py_divider)
defineBuiltin(context, '__py_modder(x, y)', pylib.__py_modder)
defineBuiltin(context, '__py_powerer(x, y)', pylib.__py_powerer)
defineBuiltin(context, '__py_floorer(x, y)', pylib.__py_floorer)

// Unary operator +
defineBuiltin(context, '__py_unary_plus(x)', pylib.__py_unary_plus)

// Math Library
defineBuiltin(context, 'math_abs(x)', pylib.math_abs)
defineBuiltin(context, 'math_acos(x)', pylib.math_acos)
defineBuiltin(context, 'math_acosh(x)', pylib.math_acosh)
defineBuiltin(context, 'math_asin(x)', pylib.math_asin)
defineBuiltin(context, 'math_asinh(x)', pylib.math_asinh)
defineBuiltin(context, 'math_atan(x)', pylib.math_atan)
defineBuiltin(context, 'math_atan2(x)', pylib.math_atan2)
defineBuiltin(context, 'math_atanh(x)', pylib.math_atanh)
defineBuiltin(context, 'math_cbrt(x)', pylib.math_cbrt)
defineBuiltin(context, 'math_ceil(x)', pylib.math_ceil)
defineBuiltin(context, 'math_clz32(x)', pylib.math_clz32)
defineBuiltin(context, 'math_cos(x)', pylib.math_cos)
defineBuiltin(context, 'math_cosh(x)', pylib.math_cosh)
defineBuiltin(context, 'math_exp(x)', pylib.math_exp)
defineBuiltin(context, 'math_expm1(x)', pylib.math_expm1)
defineBuiltin(context, 'math_floor(x)', pylib.math_floor)
defineBuiltin(context, 'math_fround(x)', pylib.math_fround)
defineBuiltin(context, 'math_hypot(...values)', pylib.math_hypot)
defineBuiltin(context, 'math_imul(x, y)', pylib.math_imul)
defineBuiltin(context, 'math_log(x)', pylib.math_log)
defineBuiltin(context, 'math_log1p(x)', pylib.math_log1p)
defineBuiltin(context, 'math_log2(x)', pylib.math_log2)
defineBuiltin(context, 'math_log10(x)', pylib.math_log10)
defineBuiltin(context, 'math_max(...values)', pylib.math_max)
defineBuiltin(context, 'math_min(...values)', pylib.math_min)
defineBuiltin(context, 'math_pow(base, exponent)', pylib.math_pow)
defineBuiltin(context, 'math_random()', pylib.math_random)
defineBuiltin(context, 'math_round(x)', pylib.math_round)
defineBuiltin(context, 'math_sign(x)', pylib.math_sign)
defineBuiltin(context, 'math_sin(x)', pylib.math_sin)
defineBuiltin(context, 'math_sinh(x)', pylib.math_sinh)
defineBuiltin(context, 'math_sqrt(x)', pylib.math_sqrt)
defineBuiltin(context, 'math_tan(x)', pylib.math_tan)
defineBuiltin(context, 'math_tanh(x)', pylib.math_tanh)
defineBuiltin(context, 'math_trunc(x)', pylib.math_trunc)
}
}
}
Expand Down
204 changes: 204 additions & 0 deletions src/stdlib/pylib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { Value } from '../types'

export function __py_adder(x: Value, y: Value) {
if (typeof x === typeof y) {
return x + y
}
return Number(x) + Number(y)
}

export function __py_minuser(x: Value, y: Value) {
if (typeof x === typeof y) {
return x - y
}
return Number(x) - Number(y)
}

export function __py_multiplier(x: Value, y: Value) {
if (typeof x === typeof y) {
return x * y
}
return Number(x) * Number(y)
}

export function __py_divider(x: Value, y: Value) {
return Number(x) / Number(y)
}

export function __py_modder(x: Value, y: Value) {
if (typeof x === typeof y) {
return x % y
}
return Number(x) % Number(y)
}

export function __py_powerer(x: Value, y: Value) {
if (typeof x == 'bigint' && typeof y == 'bigint') {
let res = BigInt(1)
for (let i = 0; i < y; i++) {
res = res * x
}
return res
}
return Math.pow(Number(x), Number(y))
}

export function __py_floorer(x: Value, y: Value) {
return Math.floor(Number(x) / Number(y))
}

export function __py_unary_plus(x: Value) {
if (typeof x == 'bigint') {
return +Number(x)
}
return +x
}

export function math_abs(x: Value) {
return Math.abs(Number(x))
}

export function math_acos(x: Value) {
return Math.acos(Number(x))
}

export function math_acosh(x: Value) {
return Math.acosh(Number(x))
}

export function math_asin(x: Value) {
return Math.asin(Number(x))
}

export function math_asinh(x: Value) {
return Math.asinh(Number(x))
}

export function math_atan(x: Value) {
return Math.atan(Number(x))
}

export function math_atan2(y: Value, x: Value) {
return Math.atan2(Number(y), Number(x))
}

export function math_atanh(x: Value) {
return Math.atanh(Number(x))
}

export function math_cbrt(x: Value) {
return Math.cbrt(Number(x))
}

export function math_ceil(x: Value) {
return Math.ceil(Number(x))
}

export function math_clz32(x: Value) {
return Math.clz32(Number(x))
}

export function math_cos(x: Value) {
return Math.cos(Number(x))
}

export function math_cosh(x: Value) {
return Math.cosh(Number(x))
}

export function math_exp(x: Value) {
return Math.exp(Number(x))
}

export function math_expm1(x: Value) {
return Math.expm1(Number(x))
}

export function math_floor(x: Value) {
return Math.floor(Number(x))
}

export function math_fround(x: Value) {
return Math.fround(Number(x))
}

export function math_hypot(...elements: Value[]) {
const coercedElements: number[] = elements.map(el => {
return Number(el)
})
return Math.hypot(...coercedElements)
}

export function math_imul(x: Value, y: Value) {
return Math.imul(Number(x), Number(y))
}

export function math_log(x: Value) {
return Math.log(Number(x))
}

export function math_log1p(x: Value) {
return Math.log1p(Number(x))
}

export function math_log2(x: Value) {
return Math.log2(Number(x))
}

export function math_log10(x: Value) {
return Math.log10(Number(x))
}

export function math_max(...elements: Value[]) {
const coercedElements: number[] = elements.map(el => {
return Number(el)
})
return Math.max(...coercedElements)
}

export function math_min(...elements: Value[]) {
const coercedElements: number[] = elements.map(el => {
return Number(el)
})
return Math.min(...coercedElements)
}

export function math_pow(x: Value, y: Value) {
return Math.pow(Number(x), Number(y))
}

export function math_random() {
return Math.random()
}

export function math_round(x: Value) {
return Math.round(Number(x))
}

export function math_sign(x: Value) {
return Math.sign(Number(x))
}

export function math_sin(x: Value) {
return Math.sin(Number(x))
}

export function math_sinh(x: Value) {
return Math.sinh(Number(x))
}

export function math_sqrt(x: Value) {
return Math.sqrt(Number(x))
}

export function math_tan(x: Value) {
return Math.tan(Number(x))
}

export function math_tanh(x: Value) {
return Math.tanh(Number(x))
}

export function math_trunc(x: Value) {
return Math.trunc(Number(x))
}

0 comments on commit c4fc20d

Please sign in to comment.