Overview

Sacalon's standard library(salivan) is a part of Sacalon programming language. It has been designed to be tiny, simple, useful.

Design

salivan uses C++ codes in backgroud and only we writed some interfaces to C++ files. Some C++ files written by people outside of Sacalon's community and thier's code licenses are there in salivan/libcpp directory in Sacalon's repository.

list of third-party libraries used in salivan :

Contributing

We welcome contributions of all kinds.

You can contribute to this book by opening an issue or forking and sending a pull request to the main Sacalon repository. Knowing what people use this book for the most helps direct our attention to making those sections the best that they can be. We also want the reference to be as normative as possible, so if you see anything that is wrong, please also file an issue.

Builtin Functions

Sacalon builtin functions is a part of Sacalon's runtime library that link to any Sacalon program.

print(...)

Standard Sacalon's print function.

Example :

print("Hello",1,1.0,'a',[1,2,3,4])

ReadStr() : string

Standard Sacalon's read string function

ReadInt() : int

Standard Sacalon's read integer function

ReadFloat() : float

Standard Sacalon's read float function

ReadBool() : bool

Standard Sacalon's read boolean function

ReadChar() : char

Standard Sacalon's read character function

ReadStr(text:string) : string

Standard Sacalon's read string function with printing a prompt.

ReadInt(text:string) : int

Standard Sacalon's read integer function with printing a prompt.

ReadFloat(text:string) : float

Standard Sacalon's read float function with printing a prompt.

ReadBool(text:string) : bool

Standard Sacalon's read boolean function with printing a prompt.

ReadChar(text:string) : char

Standard Sacalon's read character function with printing a prompt.

exit(exit_code:int)

Exit program with an exit_code.

sizeof(T) : int

Get sizeof a type or expression.

If input value is a type, it will return the amount of memory is allocated to that data types. Else if input value is a expression, it will return size of expression.

typeof(T) : string

Get name of a type.

Example :

print(typeof(int)) // int
print(typeof(MyStructType)) // MyStructType

assert(cond:bool) : bool

The assert function is used when debugging code, assert lets you test if a condition in your code returns true, if not, the program will be exit.

Example :

var foo = 1
var bar = 2

assert(foo == bar)

range function

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. range has 3 overloaded functions:

range(stop:int): [int]

returns a sequence of numbers from 0 to stop(not included).

Example:

function main(): int {
	for i in range(10){
		print(i)
	}
    return 0
}

/* Output :
0
1
2
3
4
5
6
7
8
9
*/

range(start:int, stop:int): [int]

returns a sequence of numbers from start(included) to stop(not included).

Example:

function main(): int {
    for i in range(1,11){
        print(i)
    }
    return 0
}

/* Output:
1
2
3
4
5
6
7
8
9
10
*/

range(start:int, stop:int, step:int): [int]

returns a sequence of numbers from start(included) to stop(not included), stop is specifying the incrementation(in other overloads is 1).

Example:

function main(): int {
	for i in range(0,10,2){
		print(i)
	}
    return 0
}

/* Output:
0
2
4
6
8
*/

conv

Converting types together.

to_int(val:T):int

Converts a value to int.

to_string(val:T):string

Converts a value to string.

to_float(val:T):float

Converts a value to float.

to_char(val:T):char

Converts a value to char.

file

Working with files and directories.

read_file(file_name:string) : string

Reads a file and returns its content.

write_file(file_name:string,content:string)

Writes to a file.

os

Interaction with OS.

os_name() : string

Returns OS name

OS(Platfrom)os_name() output
Windows 32-Bitwin32
Windows 64-Bitwin64
Mac OSmacos
Linuxlinux
FreeBSDfreebsd
AmigaOSamiga
Androidandroid
OpenBSD, NetBSD, DragonFlybsd
Cygwincygwin
Minixminix
Solarissolaris
Symbiansymbian
z/VMzvm

For other OSs, os_name will return unknown.

system(command:string) : int

Executes a command and returns command's exit code.

Example :

system("gcc main.c")

compiler_name() : string

Returns Sacalon C\C++ compiler name.

Compilercompiler_name() output
GCC\G++gcc
Clang(LLVM)clang
Microsoft Visual C++msvc
Digital Mars C\C++dmc
Intel C\C++icc

For other compilers, compiler_name will return unknown.

arch() : string

Returns CPU architecture.

Architecturearch() output
x86intel32
x86_64amd64
Armarm
Intel Itanium 64-Bitia64
Mipsmips
PowerPCpowerpc

is_x86() : bool

Returns true if CPU architecture is x86.

is_x64() : bool

Returns true if CPU architecture is x86_64.

datetime

Working with time/date

get_year():int

Returns current year.

get_month():int

Returns current month.

get_day():int

Returns current day.

get_hour():int

Returns current hour.

get_minute():int

Returns current minute.

get_second():int

Returns current second.

math

Math library has properties and methods for mathematical constants and functions

NOTE:All functions that return and give double type, also can return and give float type

Trigonometric ratios

sin(x:double):double

Returns sine of x.

cos(x:double):double

Returns cosine of x.

tan(x:double):double

Returns tangent of x.

asin(x:double):double

Returns arc sine of x.

acos(x:double):double

Returns arx cosine of x.

atan(x:double):double

Returns arc tangent of x.

asin(x:double):double

Returns arc sine of x.

acos(x:double):double

Returns arc cosine of x.

atan(x:double):double

Returns arc tangent of x.

atan2(y:double,x:double):double

Returns the principal value of the arc tangent of y/x, expressed in radians.

sinh(x:double):double

Returns hyperbolic sine of x.

acos(x:double):double

Returns hyperbolic cosine of x.

tan(x:double):double

Returns hyperbolic tangent of x.

atan(x:double):double

Returns area hyperbolic tangent of x.

asin(x:double):double

Returns area hyperbolic sine of x.

acos(x:double):double

Returns area hyperbolic cosine of x.

atan(x:double):double

Returns arc tangent of x.


Exponential and logarithmic functions

exp(x:double):double

Returns the base-e exponential function of x, which is e raised to the power x: e^x.

exp2(x:double):double

Returns the base-2 exponential function of x, which is 2 raised to the power x: 2^x.

frexp(x:double,exp:int^): double

Breaks the floating point number x into its binary significand (a floating point with an absolute value between 0.5(included) and 1.0(excluded)) and an integral exponent for 2, such that:

x = significand * (2  ^ exponent)

The exponent is stored in the location pointed by exp, and the significand is the value returned by the function.

If x is zero, both parts (significand and exponent) are zero. If x is negative, the significand returned by this function is negative.

ldexp(x:float|dobule,exp:int): float|dobule

Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent).

log(x:float|dobule):float|dobule

Returns the natural logarithm of x.

log10(x:float|dobule):float|dobule

Returns the common (base-10) logarithm of x.

log2(x:float|dobule):float|dobule

Returns the binary (base-2) logarithm of x.


Power functions

pow(base:double,exponent:double): double

Returns base raised to the power exponent.

sqrt(x:double):double

Returns the square root of x.


Rounding and remainder functions

ceil(x:double):double

Rounds x upward, returning the smallest integral value that is not less than x.

floor(x:double):double

Rounds x downward, returning the largest integral value that is not greater than x.

fmod(x:double):double

Returns the floating-point remainder of numer/denom (rounded towards zero):

fmod = numer - tquot * denom

Where tquot is the truncated (i.e., rounded towards zero) result of: numer/denom.

round(x:double):double

Roundes x to nearest ineger value


Minimum, maximum, difference functions

fmax(x:double,y:double):double

Returns the larger of its arguments: either x or y(floating point).

fmin(x:double,y:double):double

Returns the smaller of its arguments: either x or y(floating point).

max(x:int,y:int):int

Returns the larger of x and y.

min(x:int,y:int):int

Returns the smaller of x and y.

fdim(x:double,y:double)

Returns the positive difference between x and y.


Other functions and constants

const PI : double

PI number.

NaN():double

Returns generic NaN(Not-A-Number) value.

fabs(x:double):double

Calculates the absolute value of x(floating point).

abs(x:int):int

Calculates the absolute value of x(ineger).

strings

Utility library for working with string data types.

is_alpha(c:char): bool

check if character is alphabetic.

is_alphanum(c:char): bool

check if character is alphanumeric.

is_number(c:char): bool

check if character is number.

is_space(c:char): bool

check if character is space.

is_lower(c:char): bool

check if character is lowercase.

is_upper(c:char): bool

check if character is uppercase.

string_reverse(s:string): string

reverses a string.

http

HTTP-client library.

NOTE : If you want to use http library, your C++ backend compiler should have libcurl and its dsependencies.

get(url:string) : string

get content from given url.

post(url:string,data:string):string

post content to given url.

  • url : url to post data
  • data : post data

Example :

post("https://example.com/post.php","name=john&family=doe")

download(url:string,path:string):bool

download and save a url to storage.

  • url : your file url to download
  • path : path to save downloaded file

upload(url:string,path:string):bool

upload a file to a url.

  • url : your url to upload file
  • path : path to your file to upload

See example for http library

path

Working with pathes.

path_normalize(path:string):string

Normalize a path.

Example :

print(path_normalize("/home/myapp/files/////config"))
// Output(*Nix) :
/home/myapp/files/config

// Output(Windows):
/home\myapp\files\config

path_join(path_a:string, path_b:string):string

Join path_a and path_b pathes.

Example :

print(path_normalize("/home/myapp","files/config"))
// Output:
/home/myapp/files/config

random

Random number generator

randint(min:int ,max:int):int

Returns a random integer between min and max.

uniform(min:float ,max:float):float

Returns a random floating point number between min and max(float).

uniform(min:double ,max:double):double

Returns a random floating point number between min and max(double).

regex

Working with regular expressions.

regex(regex_string:string ,str:string):bool

Check if a string matchs with a regex rule.

termcolor

Colorizing terminal.

cprint(txt:string,color:string)

print text with passed color.

Colors :

  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • black

Example :

cprint("Hello World!","green")

reset_color()

resets termianl color.

crypto.sha256

SHA256 hash generator

generate_sha256(data:string) : string

Returns sha256 hash of a string.