Skip to content

Built in functions

Jean Dubois edited this page Jun 5, 2024 · 53 revisions

List of built-in functions

Basics

  • print(str): prints str in console. Returns None. Example: print("hello, world!").
  • print_ret(str): prints str in console and returns it. Example: print_ret("hello, world!")
  • print_in_red(str): prints str in red in console. Returns None. Example: print_in_red("Error: this error is RED").
  • print_in_red_ret(str): prints str in red in console and returns it. Example: print_in_red_ret("Error: this error is RED and returned")
  • print_in_green(str): prints str in green in console. Returns None. Example: print_in_green("Info: this info is GREEN").
  • print_in_green_ret(str): prints str in green in console and returns it. Example: print_in_green_ret("Info: this info is GREEN and returned")
  • input(text_to_display): inputs a str. text_to_display is optional.
  • input_int(text_to_display): inputs an int. text_to_display is optional.
  • input_num(text_to_display): inputs an int or a float. text_to_display is optional.

Types

Note: Python isinstance() returns True if value is an object of the given type.

  • type(value): returns a str of the type of the value. It can be str, int, float, list, func, built-in func, module, ...
  • is_int(value): returns True if value is an integer, False if it is not. Corresponds to isinstance(value, int).
  • is_float(value): returns True if value is a float, False if it is not. Corresponds to isinstance(value, float).
  • is_num(value): returns True if value is a float or an integer, False if it is not. Corresponds to isinstance(value, float) or isinstance(value, int).
  • is_str(value): returns True if value is a str, False if it is not. Corresponds to isinstance(value, str).
  • is_list(value): returns True if value is a list, False if it is not. Corresponds to isinstance(value, list).
  • is_func(value): returns True if value is a function, False if it is not. Corresponds to isinstance(value, BaseFunction) where BaseFunction is function and builtin function.
  • is_module(value): returns True if value is a module, False if it is not. Corresponds to isinstance(value, module). Example: import math; is_module(math) will be True, but is_module(3.2) will be False.
  • is_none(value): returns True if value is None, False if it is not. Corresponds to isinstance(value, NoneValue) or value is None.
  • is_constructor(value) (new in 0.20.0): returns True if value is a constructor, False if it is not. Corresponds to isinstance(value, Constructor).
  • is_object(value) (new in 0.20.0): returns True if value is an object, False if it is not. Corresponds to isinstance(value, object). Note: in Nougaro, everything is not an object!

Type conversions

  • str(value): value to string
  • int(value): value to integer
  • float(value): value to float
  • list(value): value to list

String operators

  • upper(value): Return upper-cased string. e.g. upper('nougaro') returns 'NOUGARO'.
  • lower(value): Return lower-cased string. e.g. lower('NOUGARO') returns 'nougaro'.
  • split(value, char): Return splitted string. e.g. split('a b c') returns ['a', 'b', 'c'] ; split('a b c', 'b') returns ['a ', ' c']. char is optional.
  • len(value): returns the number of elements in (the lenght of) a list, or the number of chars in a str.
  • startswith(value, substring): returns True if value starts with substring. Example: "nougaro" starts with "noug", but "canal du midi" does not start with "minimes"
  • endswith(value, substring): returns True if value ends with substring. Example: "nougaro" ends with "garo", but "canal du midi" does not end with "minimes"

Number operators

  • round(number, n_digits): Return number with n_digits decimals. n_digits is optional (default: return int(number)) and may be negative.

Characters and Unicode

  • ord(c): Return an integer representing the Unicode code point of that c. c is a string with only one character. Ex: ord('a') returns 97 and ord('€') returns 8364
  • chr(i): Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string 'a', and chr(8364) returns the string '€'.

Lists operators

  • append(list, value): append value at the end of the list. Returns a list. If the list is a variable, modify the variable.
  • pop(list, index): delete value at given index. Returns the popped element. If the list is a variable, modify the variable. If index is not given, the last element of the list is popped.
  • insert(list, value, index): append value at the given index in the list. index is optional.
  • replace(list, index, value): replace value at the given index in the list.
  • extend(list1, list2, remove_duplicates): append to list1 the elements of list2. Returns list1. If list1 is a variable, modify the variable. If remove_duplicates is True, removes the list2 elements already in list1 before extending. remove_duplicates is an optional argument, but must be a number.
  • get(list, index): returns the element of the list at the given index
  • max(list, ignore_not_num): returns the maximum element of the list. Ignore other than numbers if ignore_not_num is True. ignore_not_num is optional (default: False)
  • min(list, ignore_not_num): returns the minimum element of the list. Ignore other than numbers if ignore_not_num is True. ignore_not_num is optional (default: False)
  • len(value): returns the number of elements in (the lenght of) a list, or the number of chars in a str.
  • reverse(value): reverse list or string value by reference and returns the result. Example: reverse([1, 2, 3]) is [3, 2, 1].
  • sort(value, mode): sorts list value according to mode mode. Available modes are: timsort (default), stalin, sleep, miracle and panic. mode is optional (default: "timsort")
    • timsort is Python’s default algorithm
    • stalin sends to gulag values that are not sorted (i.e. pop them from the list). Example: [1, 4, 3, 7, 6, 10, 2, 5, 23] will be sorted as [1, 4, 7, 10, 23].
    • sleepsort take only a list of positive integers. It starts as many threads as elements in the list, and each thread waits for as much seconds as the element. For instance, when the list [5, 2, 3, 0] is passed in sleepsort, it starts by launching 4 threads that wait for 5, 2, 3 and 0 seconds. When a thread finishes to wait, it append its element to the list. Use the mode sleep-verbose to see what’s happening.
    • miraclesort literally waits for a miracle. It checks if the list is sorted, and if it is not, it checks again. If a cosmic ray (or another miracle) flips the right bits in your RAM so the list is sorted, miraclesort will return the sorted list.
    • panicsort checks if the list is sorted. If so, it returns it. If the list is not sorted, this causes a segfault.

License

  • __gpl__(print_in_term): if print_in_term is True, prints the General Public License 3.0 in the terminal. Otherwise, it opens the GPL in the default system program. print_in_term is optional (default: False)

Files

  • path_exists(path): return True (1) if the path exists, False (0) otherwise.

Technical

  • exit(stop_code): just quits the interpreter. stop_code is optional.
  • clear(): clears the screen. Returns None.
  • void(): do nothing. Returns None.
  • run(file_name): execute the code in the file file_name. file_name is a str.
  • example(example_name, return_example_value): execute the code in the file examples/(example_name).noug. example_name is a str. return_example_value is an int (it returns the list value from the example execution if it is True). example_name is optional (default is example.noug in Nougaro parent directory). return_example_value is optional (default False (0)).
  • system_call(cmd): calls cmd. Print the result of the command and return the stop code as str.
  • __python__(sourcce): runs python’s eval(source). source is a str.
  • __is_keyword__(word): check if word is a valid Nougaro keyword, such as 'import' or 'int'. word is a str.
  • __is_valid_token_type__(tok_type): check if tok_type is a valid Nougaro token type, such as 'EE' or 'BITWISENOT'. tok_type is a str.
  • __test__(should_print_ok, should_i_return): executes the test file for the nougaro interpreter. should_print_ok is optional (prints "OK" each times a test was succesful). should_i_return is optional (return the value of the interpreted file).
  • __py_type__(value): returns python string for python’s type(value). As boring as it sounds.
  • __how_many_lines_of_code__() let you know how many lines of code there are in Nougaro. Pass 0 as argument to see only the number.

Mathematical functions

Please refer to Math module.

Clone this wiki locally