REPL and Compiler

The REPL

Let's start with the REPL, it stands for Read Eval Print Loop, it is a common feature in many languages and almost cultural in Lisp based languages, the way it works is quite simple, it reads the expression or statement given by the user, then evaluate it, print it's result and do it all over again. A REPL is great tool for testing and prototyping a idea direct from the terminal.

In Carp, you can start the REPL invoking the carp binary without parameters.

On UNIX/UNIX-Like:

$ carp

On Windows:

> carp

Inside the REPL, there is some special features that can be used and probably will be helpful when writing or debugging code.

Help: With no parameter, it displays the general help message, while giving a chapter name.

;; No parameter
(help)

;; With parameter
(help "language")

Type: It receive a symbol and it gives the type of that symbol

(def a 10)
(type a)

It should return:

a : Int

Because a is defined as 10 a Integer number.

Info: It receives a symbol and it gives the information about that symbol

(def a 10)
(info a)

In this example, it should return something like:

a : Int
Defined at line 1, column 1 in 'REPL'

Compiler

Note: Unless your project is a library, you project must have a main function.

Carp compiles to C and then call a C compiler to generate the binary. By default the generated files and binaries will be on a directory called out, but it can be changed in the project configuration.

Configuring the project

This part is quite simple, following the same pattern in the toplevel of the project:

(Project.config <setting> <value>)

To configure more than one setting, just keep adding the same pattern one more time in the next line.

Here is the setting options and what it takes as values:

  • "cflag": Takes a String: Add a flag to the C compiler.
  • "libflag": Takes a String: Add a library flag to the compiler.
  • "compiler": Takes a String: Set what compiler should be run with the build command.
  • "title": Takes a String: Set the title of the current project (the final binary name will be this string)
  • "prompt": Takes a String: Set the prompt in the REPL
  • "search-path": Takes a String: Add a path where the Carp compiler will look for '*.carp' files
  • "output-directory": Takes a String: Where to put build artifacts
  • "docs-directory": Takes a String: Where to put generated docs
  • "generate-only": Takes a Bool: Set to true if you don't want to run the C compiler when building
  • "echo-c": Takes a Bool: When a form is defined using 'def' or 'defn' its C code will be printed
  • "echo-compiler-cmd": Takes a Bool: When building the project the command for running the C compiler will be printed
  • "print-ast": Takes a Bool: The 'info' command will print the AST for a binding

Compiler flags

  • -b: Just compile the files
  • -x: Compile and run the files
  • --optimize: Compile the generated C file with -O3 optimization level
  • --check: Just check the files if they are valid Carp and report all errors
  • --generate-only: Only generate the C file.