Skip to content

Commit

Permalink
Chapter preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
OCTO-FRAH committed Jun 25, 2020
1 parent d7f8e3f commit 04a2ab1
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Chapter 20 : Preprocessor

<dialog character="mechanics"></dialog>

Instead of writing the whole code in a single file, it is possible to split code into different files and include some external code into our file. The preprocessor is responsible to handle code inclusion. While working with multiple files we may encounter a problem of cyclic inclusion. To prevent such situation some pre-processor commands are available.

* #if
* #define
* #include

⚠️ Notice all pre-processor command names start with a _#_.

## Include

Preprocessor command *#include* permits code inclusion. It allows to merge some code into your file. Command *#include* requires a path to a ligo file.

```
#include "tzip-12/fa2_interface.mligo"
```

## Define

Preprocessor command *#define* allows to introduce tags.

```
#define FA2_NFT_TOKEN
```

Such tags can be used with a conditionnal command.


## Conditionnal

Preprocessor commands *#if* and *#endif* allows to consider / ignore some part of the file depending on tags. Tags are defined with the *#define* command.

```
#if !FA2_NFT_TOKEN
#define FA2_NFT_TOKEN
#include "tzip-12/fa2_interface.mligo"
let substr_special (s: string) : string =
String.sub 0n 4n s
#else
#include "test/fa2_int.mligo"
let substr_special (s: string) : string =
String.sub 0n 3n s
#endif
```

⚠️ Notice that this pattern prevents from redefinition in case of cyclic inclusion.

```
#if !X
#define X
```


## Your mission

Our service is modular. we provide to client only desired module and not the extra modules . They have to pay for it !
The main module *inventory* can be customized. If we define WITH_EXTRA tag then the code in extra.mligo will be included.
The module *extra* defines the tag EXTRA and implements a function doSomethingExtra

We want you to make the code responsive to the tag EXTRA


<!-- prettier-ignore -->1- Modify the function *doSomethingInventory* in the inventory contract *inventory* so as to execute *doSomethingExtra* on given parameter if the EXTRA tag is defined. Otherwise apply *removeFirst* to the given parameter.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if !EXTRA

#define EXTRA

let doSomethingExtra (str: string) : string =
String.sub 0n 2n str

#endif
14 changes: 14 additions & 0 deletions src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint import/no-webpack-loader-syntax: off */
// @ts-ignore
import course from "!raw-loader!./course.md";
/* eslint import/no-webpack-loader-syntax: off */
// @ts-ignore
import exercise from "!raw-loader!./inventory_exercise.mligo";
/* eslint import/no-webpack-loader-syntax: off */
// @ts-ignore
import solution from "!raw-loader!./inventory_solution.mligo";
/* eslint import/no-webpack-loader-syntax: off */
// @ts-ignore
import support1 from "!raw-loader!./extra.mligo";

export const data = { course, exercise, solution, support1 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#define WITH_EXTRA

#if WITH_EXTRA
#include "extra.mligo"
#endif

let removeFirst (str: string) : string =
String.sub 0n 1n str

let doSomethingInventory (str: string) : string =
// Type your solution below
str

type param = Apply of string

let main (p, store: param * string) : operation list * string =
let new_storage : string = match p with
| Apply str -> doSomethingInventory str
in (([]: operation list), new_storage)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#define WITH_EXTRA

#if WITH_EXTRA
#include "extra.mligo"
#endif

let removeFirst (str: string) : string =
String.sub 0n 1n str

let doSomethingInventory (str: string) : string =
#if EXTRA
doSomethingExtra str
#else
removeFirst str
#endif

type param = Apply of string

let main (p, store: param * string) : operation list * string =
let new_storage : string = match p with
| Apply str -> doSomethingInventory str
in (([]: operation list), new_storage)

0 comments on commit 04a2ab1

Please sign in to comment.