Introduction
------------

There are 3 labs and you are expected to complete one of them (each pair in a group takes one of the assignments):

#. Build systems
#. Embedded languages
#. Vector graphics

The lab skeleton is on `Gitlab <https://gitlab.liu.se/tdde45/lab4-dsl>`_ and the instructions for the individual parts are below.

1. Build systems
----------------

Build systems often build on a basis of running system commands to perform tasks.
In this assignment, you will create two build systems for the same project – one using autoconf and one using cmake.

The C++ and C files in the build system are incomplete.
You will need to add some missing implementation for non-Linux operating systems (you can stub these away and make the implementation do nothing with a comment on what should be done).
The ``copyfile.c`` file should be compiled into a shared object and ``main.cpp`` into an executable.
It should be possible to disable usage of the trigonometric functions by setting an option when calling cmake or the generated configure script.
You will need to create at least the following files:

* ``CMakeLists.txt``
* ``configure.ac``
* ``Makefile.in``

Sample usage of `autoconf`:

.. code-block :: bash

  autoconf
  ./configure --without-trig-functions CC=clang CXX=clang++
  make

Sample usage of `cmake`:

.. code-block :: bash

  mkdir -p build
  cmake -DTRIG_FUNCTIONS:BOOL=OFF ..
  make

.. note ::

  You should detect if FICLONE is available inside the configure/cmake script.

  It should not be handled by preprocessor macros (except ones defined by cmake or configure).

2. Embedded languages
---------------------

One usage of domain-specific languages is: languages that you embed into another programming language. Examples of this include regular expressions (see courses in `compiler construction <https://www.ida.liu.se/~TDDB44/>`_ or `formal languages <https://www.ida.liu.se/~TDDD14/>`_), SQL (for querying databases), or scripting languages such as Lua.

Regular expressions
^^^^^^^^^^^^^^^^^^^

Regular expressions are used in a variety of applications.
There are ways to use it in most good text editors, unix shells, and programming languages.
In this exercise, you will use the unix commands ``grep`` and ``sed``, which are used to search or manipulate text files using regular expressions.

#. Using grep on ``file1.txt``, find all records containing the entry "memory" in books 19-23.
#. Using ``sed``, create a copy of ``file2.txt`` where entries that have ``Carl`` or ``Diana`` in column 1 have column 2 replaced with ``4``.
#. Write a C++ program that performs essentially the same operations as in 1 and 2 above using `C++11 regular expressions <https://en.cppreference.com/w/cpp/regex>`_.

SQL
^^^

Regular expressions are good for many things, but it is cumbersome to access structured data using regular expressions (which hopefully the previous exercise showed you, and CSV-style files are still among the easiest to manipulate using regular expressions).
The next exercise uses Python and sqlite, which is a small and simple database engine with support for SQL.
There is a database of the bible which contains a table called ``t_bbe``; it has columns called ``b``, ``c``, ``v`` and ``t`` (book, chapter, verse, text). Make SQL queries to get the following:

* All book numbers and the number of verses in these books
* The book numbers that have a verse containing the string "memory"
* The average number of verses per chapter

.. note ::

  Only use SQL queries – do not return data and process it in Python.

Lua
^^^

Finish the C program which is supposed to load ``test.lua`` and call the function defined in the file, which in turn should call the ``fromC`` function defined in the C file (C main function calls function in lua script which calls a function in C).

.. digraph :: luacallchain
  :caption: Call chain through the program. Dashed lines are the returned results.

  compound=true;
    labelloc="t";
    subgraph clusterLua {
        label="test.lua"
        Lua[shape="none"][style="invis"][label=""];
        addFromC
    }
    subgraph clusterMain {
        label="main.c"
        Main[shape="none"][style="invis"][label=""];
        fromC
        main
    }
    main -> addFromC [label="(x=41,y=22)"][ltail ="clusterLua"];
    addFromC -> fromC [label="v=22"][ltail ="clusterMain"];
    fromC -> addFromC [style=dashed, label="result=-22"][ltail ="clusterLua"];
    addFromC -> main [style=dashed, label="result=19"][ltail ="clusterMain"];

3. Vector graphics
------------------

There exists many ways to draw text or shapes in computer programs.
An efficient way to make sure a certain figure always looks reasonably good on a computer regardless of screen resolution is to use vector graphics.
There are of course ways to draw in frameworks such as Qt, or using OpenGL – these are great if you want to draw something inside of a program.
In this exercise you will try to use `SVG <https://www.w3.org/TR/SVG11/>`_ (an XML-based image format for 2D vector graphics), Postscript (a programming language in which you describe a vector image that is rasterized by a Postscript interpreter), and Graphviz (a language for describing graphs and shapes in a convenient way).

Postscript
^^^^^^^^^^

Create a postscript program that loops a certain number of times, writing text and line numbers. Then write an overlapping circle and a triangle somewhere using a filled colour.

.. figure :: _static/ps.png
  :scale: 50%

  Example of what the postscript program could produce.

SVG
^^^

Create an SVG file with similar elements as the postscript program, but skip creating a loop to create text objects.
Do propose possible solutions to how you would use for loops to construct the SVG image for the seminar discussion.

.. figure :: _static/svg.png
  :scale: 50%

  Example of what the SVG file could produce.

Graphviz
^^^^^^^^

Using graphs to visualize something is very common, and might be used in for example PowerPoint (or similar) presentations.
The problem is that you don't want to draw these graphs in a graphics editor such as InkScape or Dia if you ever need to update this graph (because you need to move everything around to fit the screen).
If you want to create graphs that can easily be updated, use a language to describe graphs and use a tool to create a nice layout for you instead.
Use GraphViz and its associated command-line tools to do the exercise.

There is a small, empty program and a script to create SVG and Postscript files for some of the layout engines available in GraphViz.
You are supposed to create two graphs in GraphViz – one where you draw edges from a *part* of a node and one that represents a state machine.

.. figure :: _static/dot1.png
  :scale: 50%

  Example of GraphViz drawing edges from part of a node.
  (0,1,2,3) is a single node in the graph with 4 different parts that it is possible to draw edges from.

.. figure :: _static/dot2.png
  :scale: 50%

  Example of a state machine produced by GraphViz.
  The regular expression

  .. code-block :: cfg

    (b*a+b(ab)*b)*(b*a+b(ab)*)

