LightAndLight / ipso

A functional scripting language.
https://ipso.dev
16 stars 1 forks source link

Number printing builtins #413

Open LightAndLight opened 1 year ago

LightAndLight commented 1 year ago

Factored out of #393.

# int.printBin 5 == "101"
# int.printBin -5 == "-101"
int.printBin : Int -> String

# int.printOct 10 == "12"
# int.printOct -10 == "-12"
int.printOct : Int -> String

# int.printDec 15 == "15"
# int.printDec -15 == "-15"
int.printDec : Int -> String

# int.printDec 30 == "1e"
# int.printDec -30 == "-1e"
int.printHex : Int -> String

And some base-generic functions:

int.binary : Array Char
int.octal : Array Char
int.decimal : Array Char
int.hexLower : Array Char
int.hexUpper : Array Char

int.printBase : Array Char -> Int -> String
int.printBase base = int.printBasePrefixed base ""

int.printBasePrefixed : Array Char -> String -> Int -> String
int.printBasePrefixed base prefix n =
  case int.toBase base n of
    { negative, value } -> "${if negative then "-" else ""}$prefix$value"

int.toBase : Array Char -> Int -> { negative : Bool, value : String }