A minimal, science-friendly Lisp implemented in C, inspired by C and Python

Jules 1436d9d392 Major updates to parser and lexer. 5 months ago
lib 1436d9d392 Major updates to parser and lexer. 5 months ago
.gitignore 1436d9d392 Major updates to parser and lexer. 5 months ago
CONTRIBUTING.md 1436d9d392 Major updates to parser and lexer. 5 months ago
LICENSE 0a462aa966 Initial commit 6 months ago
Makefile 1436d9d392 Major updates to parser and lexer. 5 months ago
README.md 1436d9d392 Major updates to parser and lexer. 5 months ago
changelog.md 1436d9d392 Major updates to parser and lexer. 5 months ago
lex.c 1436d9d392 Major updates to parser and lexer. 5 months ago
licp.c 1436d9d392 Major updates to parser and lexer. 5 months ago
parse.c 1436d9d392 Major updates to parser and lexer. 5 months ago

README.md

LiCp

A minimal, science-friendly Lisp implemented in C, inspired by C and Python

(And with just a bit of Haskell DNA)

Syntax examples

# you'll never guess what that does
(print "Hello, world!")

# automatic promotion of ints to floats
(+ 5 2 3.141592)

# arbitrary-precision ints. tyvm, GMP!
(set bignum 10e50)

# it can do all the stuff normal languages can, like define functions
# (type safety + annotations coming soon)
(def some_func (a b c)
    (if (a `has_trait` reversible)
        (reverse a))
    (sum b)
    (sum (map (* c) a) b))   # partial application?! you betcha!

# Haskell-style infix notation for two-argument functions, too!
(5 `mod` 2)

# trait-or!
(instance strfy (MyType as m)
    (return (cat
        (strfy m.val1)
        (sp)
        (strfy m.val2)
        (sp)
        (strfy m.val3))))

Built-in functions

Arithmetic
  • sum (AKA +). Takes n >= 2 numerical arguments and promotes them all to the type preserving as much precision as possible.

  • prod (AKA *). Takes n >= 2 numerical arguemnts and promotes them all to the type preserving as much precision as possible.

  • dif (AKA -). Takes 2 numerical arguments (a and b) and computes the difference, a-b.

  • div (AKA /). Takes 2 numerical arguments (a and b) and performs floating-point division. e.g., (/ 5 2) yields 2.5.

  • idiv (AKA //). Takes 2 numerical arguments (a and b) and performs integer division. e.g., (// 5 2) yields 2.

  • mod (AKA %). Modulo/remainder division. Takes 2 arguments (a and b) and computes their modulo. e.g., (% 5 2) yields 1.

Bitwise operations
  • ~ (AKA NOT). Unary operator, returns the binary NOT of its argument.

  • ^ (AKA XOR). Binary operator, returns the binary XOR of its of arguments.

  • | (AKA OR). Binary operator, returns the binary OR of its arguments.

  • & (AKA AND). Binary operator, returns the binary AND of its arguments.

  • >> is a right arithmetic (unsigned) shift. Returns its first argument shifted by its secont argument.

  • >>> is a right logical (signed) shift. Returns its first argument shifted by its second argument.

  • << is a left arithmetic (unsigned) shift. Returns its first argument shifted by its secont argument.

  • <<< is a left logical (signed) shift. Returns its first argument shifted by its second argument.

I/0
  • print takes n arguments of arbitrary type and concatenates their string representations. If a string representation isn't available (via an instance of strfy defined over that type), an autogenned string of form <object of type MYTYPE located at ADDRESS> will be substituted. Writes to STDOUT.

  • read takes a strfy-able value as its first argument, which is displayed as a prompt. It then returns the value which is inputted into STDIN during its execution.

  • eprint just like print, but instead writes to STDERR.

  • fopen opens a file (first argument) in a given mode (second argument). File modes are a subset of those available in Python (for now):

    • r, for reading
    • w, for writing (truncates the file if it exists)
    • a, for appending
    • x, for exclusive creation (fails if the file already exists)
    • +, for updating (to be combined with any of the above). File is open for reading and writing. Can also include a third parameter to specify input file encoding from any of the following (for now):
    • ascii, ASCII
    • utf8, UTF8, utf-8, UTF-8
    • utf16, UTF16, utf-16, UTF-16 This function returns a reference to an open file which is automatically closed when a program is terminated cleanly.
  • fread loads the contents of the file into a string and returns the string.

  • fwrite takes a reference to a file (as created by fopen) and a string, and writes the string to the file, overwriting or appending it depending on the file opening mode.

Built-in types

  • int : an arbitrary-precision integer, supplied via the GPM library.
  • float : a double-precision IEEE 754 floating-point number.
  • string : an arbitrary-length array of characters. Implemented as a (char) array.
  • tuple(t0, t1) : a statically-typed, optionally heterogeneous, fixed-length collection of data. Similar to a tuple in other languages or a struct in C.
  • Function data types: expressed as a mapping between any of the above three types; e.g. (int, int) -> string for a function taking two ints and returning a string.