From 04a2ab17e25d15ac63bb3ad14f8a1128bbf49400 Mon Sep 17 00:00:00 2001 From: Frank Hillard Date: Thu, 25 Jun 2020 17:24:00 +0200 Subject: [PATCH] Chapter preprocessor --- .../Camel/ChapterPreprocessor/course.md | 72 +++++++++++++++++++ .../Camel/ChapterPreprocessor/extra.mligo | 8 +++ .../Camel/ChapterPreprocessor/index.ts | 14 ++++ .../inventory_exercise.mligo | 19 +++++ .../inventory_solution.mligo | 22 ++++++ 5 files changed, 135 insertions(+) create mode 100644 src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/course.md create mode 100644 src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/extra.mligo create mode 100644 src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/index.ts create mode 100644 src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_exercise.mligo create mode 100644 src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_solution.mligo diff --git a/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/course.md b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/course.md new file mode 100644 index 0000000..6ab6ad3 --- /dev/null +++ b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/course.md @@ -0,0 +1,72 @@ +# Chapter 20 : Preprocessor + + + +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 + + +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. + diff --git a/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/extra.mligo b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/extra.mligo new file mode 100644 index 0000000..f44b8eb --- /dev/null +++ b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/extra.mligo @@ -0,0 +1,8 @@ +#if !EXTRA + +#define EXTRA + +let doSomethingExtra (str: string) : string = + String.sub 0n 2n str + +#endif \ No newline at end of file diff --git a/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/index.ts b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/index.ts new file mode 100644 index 0000000..53082cd --- /dev/null +++ b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/index.ts @@ -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 }; diff --git a/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_exercise.mligo b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_exercise.mligo new file mode 100644 index 0000000..b409f75 --- /dev/null +++ b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_exercise.mligo @@ -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) \ No newline at end of file diff --git a/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_solution.mligo b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_solution.mligo new file mode 100644 index 0000000..1225759 --- /dev/null +++ b/src/frontend/src/pages/Chapters/Camel/ChapterPreprocessor/inventory_solution.mligo @@ -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) \ No newline at end of file