beef331 / website

Code for the official Nim programming language website
https://nim-lang.org
18 stars 1 forks source link

CPython #26

Closed juancarlospaco closed 3 years ago

juancarlospaco commented 3 years ago

Name: CPython

Author: Juan Carlos

Posting:

Hijacks the Python 3.10+ standard library for Nim.

A whole new standard library becomes usable for Nim, without Nim having to spend any resources.

Showcases the easy and transparent interoperability of Nim (Python's CTypes is a lot harder, etc). Nim is also likely less verbose than Python (files are <200 lines each).

Code is very simple and easy to hack for new users, no complex Macros, just DRY templates.

Each file is completely self-contained standalone, you can copy just 1 file on your project and use it, steal the code.

Same API as Python standard library, same function names, same argument names, same module filenames, same imports, any Python documentation becomes a documentation you can use with Nim.

It is also a place to pile up "Nim-ified" Python stuff, anything that people are interested in can be added in the future (games?, science?, etc), if you are interested, Pull Requests are welcome !.

Arbitrary precision big Decimals

import cpython/decimal

let x: PyDecimal = newDecimal"999999999999999999999999999999999999999.999999999999999999999999999999999999999"
let y: PyDecimal = -999999999999999999999999999999999999999.999999999999999999999999999999999999999'PyD
let z: PyDecimal = newDecimal BiggestUint.high
echo toString(x * y + z - z)

No limits on the size, only limited by memory capacity.

Decimals have all the math operators, like -, +, <, <=, ==, >, >=, !=, *, div, **, shl, shr, inc, dec, etc

Input can be a Nim string, float, int, etc. math modules are available.

You can get back a Nim type in the end using toFloat or toInt or toUint etc.

os and sys modules work too.

Pet the Turtle

import cpython/turtle

title "Nim Turtle"
shape "turtle"
shapesize 2, 2, 8
color "green", "yellow"
speed "fast"
beginFill()

while true:
  forward 200
  left 170
  if position() < (1.0, 1.0):
    break

endFill()
mainLoop()

Thank you for reading

Whats the rule that dictates that a programming language can only have 1 standard library?, lets break it..., lets have 2, why 2?, lets have 4!, maybe in the future there can be a standard library optimized for JavaScript, a standard library optimized for working with Python, etc lets keep cool stuff incoming...