For Calsa, we could use Visual Studio Code, Merlin, and vscode-reasonml to provide modern IDE features for OCaml development, including auto completion, static analysis, and source browsing ("jump to the definition").
To setup, please refer to Setting up VS Code for OCaml development by Andrew Barnes for more help. We could ignore the VS Code configuration to simplify setup process.
After setting up the development environement, please see the .merlin
file under root directory, this is the file where we manage the source code path, compiled code path, and dependencies. Currently, we only have the following configuration:
S src
B _build/src
PKG llvm
which means Source code path: src
, Built code path: _build/src
, and our imported package llvm
. Note that if we need to add more packages, the directive should probably be lower-case (e.g. Llvm should be written as llvm). Besides, we should install ocamlfind
package to support the package management. For more help, please refer to Merlin Project configuration for more information.
Note that Merlin will work after we compiled the code, which means only after we built the compiler with the command given in next section, we could notice there will be no errors and no warnings, and it could support important features such as source browsing (e.g. we could see the definition of Ast and Sast in semant.ml) and type inference.
ocamlbuild -pkgs llvm src/microc.native
cd src
microc.native test/list.mc
a.out
ast.ml
: abstract syntax tree (AST) definitionscanner.mll
: scannermicrocparse.mly
: parsersast.ml
: definition of the semantically-checked ASTsemant.ml
: semantic checkingirgen.ml
: LLVM IR code generator
test1.ml
: the file to test the scanner and parsertest2.ml
: the file to test the semantic checkermicroc.ml
: top-level file to test and run microc compilerexample.mc
: a sample microc source codeexample.out
: a sample compiled code of example.mc
xxx.c and xxx.h
write down your c files in src folderMakefile
: add your .c and .h file for object file and library file creationmicroc.ml
: add your library by specifying -lxxx in command of gcc.irgen.ml
: add the declaration of functions and add Call of your functions in build expression. If you define a any type, define it, toosemant.ml
: check your function declaration herescanner.ml, microcparse.mly, ast.ml
: define your custom type here
Run
make
to compile .c and .h files to a .a file and build our complier files into a microc.native file. Then follow the step in "Run the MicroC compiler and generate llvm code" to use linked compiler for example code.
Run
make clean
to clean the created files during "make" and running our compiler.