Carp/docs/Manual.md

74 lines
3.8 KiB
Markdown
Raw Normal View History

2017-06-26 12:45:18 +03:00
## The Compiler
2017-06-26 12:49:49 +03:00
The Carp language is very tightly integrated with the REPL, everything you want to do to your program can be controlled from here.
2017-06-26 12:45:18 +03:00
2017-06-26 12:49:49 +03:00
To explore the commands available, enter ```(help)``` and press enter.
2017-06-26 12:45:18 +03:00
2017-06-26 13:04:39 +03:00
To load code, use ```(load <filename>)```, this will add the source file to the current 'project'. A project is a light weight concept in the repl that ties together source files and compiler settings much like in an IDE like Eclipse or Visual Studio.
To build your current project, call ```(build)```. This will emit an executable or dynamic library depending on if you have defined a main-function or not. Everything emitted by the compiler will be saved in a directory named ```out``` by default. This, and other settings regarding the project can be changed by various commands. To see a list of available commands, call ```(help project)```.
2017-06-26 12:45:18 +03:00
2017-06-26 14:36:33 +03:00
There are a bunch of handy shortcuts for doing common things at the REPL:
```
:r Reload all the files of the project
:b Build the project
:x Run the executable (if it exists)
:c Look at the emitted C code
:e Display the environment with all the function bindings and their types
:p Show project information and settings
:h Show the help screen
:q Quit the repl
```
2017-10-11 18:04:17 +03:00
### Getting types from bindings
```
(type <binding>)
```
### Listing bindings in a module
```
(info <module name>)
```
### Configuring a project
The current session in the repl is called a "project" and can be configured using the `(Project.config <setting> <value>)` command. The following settings can be configured with this command:
2018-02-08 19:34:26 +03:00
* 'cflag' - Add a flag (String) to the compiler.
* 'libflag' - Add a library flag (String) to the compiler.
* 'compiler' - Set what compiler should be run with the 'build' command.
* 'title' - Set the title of the current project, will affect the name of the binary produced.
* 'prompt' - Set the prompt in the repl.
* 'search-path' - Add a path where the Carp compiler will look for '*.carp' files.
* 'echo-c' - When a form is defined using 'def' or 'defn' its C code will be printed.
* 'echo-compiler-cmd' - When building the project the command for running the C compiler will be printed.
* 'print-ast' - The 'info' command will print the AST for a binding.
For example, to set the title of your project:
```
(Project.config "title" "Fishy")
```
2017-06-26 12:49:49 +03:00
<!-- ### Special Files -->
<!-- If a file called ```user.carp``` is placed in the folder ```~/.carp/```, that file will get loaded after the compiler has started. This file is meant for user specific settings that you want in all your projects, like little helper functions and other customizations. -->
2017-06-26 12:45:18 +03:00
2017-06-26 12:49:49 +03:00
<!-- If a file called ```project.carp``` is placed in the folder where you invoke the ```carp``` command this file will get loaded after the compiler has started (and after 'user.carp' has loaded). This files is intended for setting up the build process of this particular project, for example by loading the correct source files, configuring the compiler variables, etc. -->
2017-11-20 17:58:03 +03:00
### Compiler flags
When invoking the compiler from the command line you can supply the following flags to configure the behaviour:
2017-11-22 18:34:18 +03:00
* ```-b``` Build the code, then quit the compiler.
* ```-x``` Build and run the code (make sure it has a main function defined), then quit the compiler.
* ```--no-core``` Run the compiler without loading any of the core libraries.
* ```--log-memory``` The executable will log all calls to malloc and free.
2018-02-06 10:47:13 +03:00
* ```--optimize``` Removes safety checks (like array bounds access, etc.) and runs the C-compiler with the `-O3` flag.
2017-11-22 18:41:27 +03:00
### Adding search paths
Use `(project-set! search-path "path/to/directory/")` to add more search paths. These will be available for the `(load ...)` command.
2017-12-23 08:38:45 +03:00
### Inspecting the C code generated by an expression
```
(c '(+ 2 3))
```