Skip to content

Builtins

Bill Hails edited this page Jul 25, 2024 · 31 revisions

There is now limited support for linking to external libraries and native C code, it's only at compile time currently, and the following functionality is provided:

Pseudo-Random Numbers

the built-in function rand takes a floating point number between 0.0 and 1.0 and returns a floating point number in the same range. It is in no way cryptographically secure and uses the most basic RNG algorithm I could find, but it works and is useful.

Assertions

For writing tests. Mostly this is implemented by F Natural in the standard preamble but there is a core built-in that either sets a flag for later (if --assertions-accumulate) or directly exits with a non-zero exit status. Support in the parser recognises the function assert(condition) and provides it with filename and line number to report before calling the built-in.

Character to Unicode Conversion

The built-in ord(char) returns the unicode code point for the character, and conversely chr(number) returns the character at that unicode point.

I/O

There are a number of I/O facilities implemented as built-ins, namely:

  • putc(char) puts a character to the standard output.
  • fputc(file, char) puts a character to the open file handle.
  • getc() returns the next character from stdin.
  • fgetc(file) returns the next character from the file.
  • putn(number) writes the number to stdout.
  • fputn(file, number) writes the number to the file.
  • putv(any) writes any type of argument to stdout.
  • fputv(file, any) you guessed it.
  • puts(string) puts the string of characters to stdout.
  • fputs(file, string)
  • gets() reads the next line from stdin up to EOF or '\n' and returns it, discarding any newline.
  • fgets(file) same for the argument file.
  • open(string, io_mode) attempts to open the file in the given mode (io_read, io_write or io_append). Returns a try(string, opaque file handle) where the string is the error if any.
  • close(file) closes the file, returns true.

to support i/o there are a couple of new types, one is a generic opaque type that is implemented in core and can be sub-typed. That is used to describe files. The other is a try type which is like maybe but treats failure as more of an error. It is defined in the preamble as:

typedef try(#f, #s) { failure(#f) | success(#s) }

which allows flexibility in the type of error value (string or integer code are obvious alternatives).

SQLite

Lightweight database access.

  • sqlite3_open(string) returns a try of a string error message and an opaque database handle (see i/o above for try).
  • sqlite3_close(db_handle) closes the database handle, if it is still open, always returns true currently.
  • sqlite3_prepare(db_handle, string) prepares an sql statement for execution. returns a try of an integer error code and an opaque stmt_handle.
  • sqlite3_bind(stmt_handle, list(basic_type)) binds the arguments to the statement.
  • sqlite3_fetch(stmt_handle) executes the statement and/or returns the next row as a maybe(list(basic_type)).
  • sqlite3_finalize(stmt_handle) frees resources associated with the handle. Called automatically when the handle is garbage-collected, but it's a good idea to call it manually if you're creating a lot of statements.
  • sqlite3_close(db_handle) closes the database handle if it is still open.

The basic_type mentioned above is defined in the preamble as

typedef basic_type { basic_null | basic_number(number) | basic_string(string) | basic_char(char) }
Clone this wiki locally