diff --git a/bin/dune b/bin/dune index 4f912b5..e630ca1 100644 --- a/bin/dune +++ b/bin/dune @@ -1,15 +1,3 @@ -(executable - (name main) - (public_name main) - (libraries exercises - miniImp - miniFun - analysis - utility) - (package miniImp) - (modes byte exe) -) - (executable (name miniFunInterpreter) (public_name miniFunInterpreter) diff --git a/bin/main.ml b/bin/main.ml deleted file mode 100644 index e0e6c0e..0000000 --- a/bin/main.ml +++ /dev/null @@ -1,79 +0,0 @@ -open MiniImp - -let colorred s = - "\027[31m" ^ s ^ "\027[0m" - -let () = - let program = " -def main with input in output out as - out := in; - a := 1; - b := 2; - c := a + a; -" - in - - (* Printf.printf "%s\n%s\n" (colorred "Program is") program; *) - - let get_result x = Lexing.from_string x |> Parser.prg Lexer.lex in - - let p = get_result program in - - (* Format.printf "%s\n%a\n@?" (colorred "AST is") Types.pp_p_exp p; *) - - let convertedcfg = CfgImp.convert_io 10 p in - - (* Printf.printf "%s\n%a" (colorred "Converted CFG is") CfgImp.SSCfg.pp convertedcfg; *) - - let convertedrisccfg = CfgRISC.convert convertedcfg in - - Printf.printf "%s\n%a" (colorred "Converted RISC CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; - - (* ---------------------------------- *) - - let analysiscfg = DefinedVariables.compute_defined_variables convertedrisccfg in - - (* Printf.printf "%s\n%a" (colorred "Analysis CFG is") DefinedVariables.DVCfg.pp analysiscfg; *) - - (* Printf.printf "%s" (colorred "Undefined Variables are:"); *) - (* ( *) - (* match DefinedVariables.check_undefined_variables analysiscfg with *) - (* | None -> Printf.printf " none"; *) - (* | Some l -> Printf.printf " %a" DefinedVariables.Variable.pplist l; *) - (* ); *) - (* Printf.printf "\n"; *) - - let convertedrisccfg = DefinedVariables.compute_cfg analysiscfg in - - (* Printf.printf "%s\n%a" (colorred "Converted RISC after analysis CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; *) - - - (* let analysiscfg = LiveVariables.compute_live_variables convertedrisccfg in *) - - (* Printf.printf "%s\n%a" (colorred "Live Analysis CFG is") LiveVariables.DVCfg.pp analysiscfg; *) - - (* let convertedrisccfg = LiveVariables.compute_cfg analysiscfg in *) - - (* Printf.printf "%s\n%a" (colorred "Converted RISC with no analysis CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; *) - - - (* let convertedrisccfg = LiveVariables.compute_cfg (LiveVariables.optimize_cfg analysiscfg) in *) - - (* Printf.printf "%s\n%a" (colorred "Converted RISC after analysis CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; *) - - let convertedrisccfg = ReduceRegisters.reduceregisters 4 convertedrisccfg in - - Printf.printf "%s\n%a" (colorred "Converted RISC after reducing registers CFG is") CfgRISC.RISCCfg.pp convertedrisccfg; - - - (* ---------------------------------- *) - - let risc = RISC.convert convertedrisccfg in - - Printf.printf "%s\n%a" (colorred "RISC code is") RISC.RISCAssembly.pp risc; - - let computerisc = RISCSemantics.reduce risc in - - Printf.printf "%s\n%d\n" (colorred "Output of RISC code is") computerisc; - - () diff --git a/bin/test.miniimp b/bin/test.miniimp deleted file mode 100644 index 355d16b..0000000 --- a/bin/test.miniimp +++ /dev/null @@ -1,25 +0,0 @@ -def main with input n output result as - if (n % 2) == 0 then result := 1 - else ( - result := 0; - s := 0; - while (0 == ((n - 1) / (2 ^ s)) % 2) do ( - s := s + 1 - ); - d := ((n - 1) / 2 ^ s); - for (i := 20, i > 0, i := i - 1) do ( - a := rand(n - 4) + 2; - x := powmod(a, d, n); - y := 0; - for (j := 0, j < s, j := j+1) do ( - y := powmod(x, 2, n); - if (y == 1 && (not x == 1) && (not x == n - 1)) then - result := 1; - else - skip; - x := y; - ); - if not y == 1 then result := 1; - else skip; - ) - ) diff --git a/dune-project b/dune-project index 85457a3..b497fd4 100644 --- a/dune-project +++ b/dune-project @@ -21,7 +21,3 @@ (package (name miniFun) (depends ocaml dune utility)) - -(package - (name exercises) - (depends ocaml dune)) diff --git a/lib/exercises/dune b/lib/exercises/dune deleted file mode 100644 index 280c335..0000000 --- a/lib/exercises/dune +++ /dev/null @@ -1,5 +0,0 @@ -(library - (name exercises) - (public_name exercises)) - -(include_subdirs qualified) \ No newline at end of file diff --git a/lib/exercises/exercises.ml b/lib/exercises/exercises.ml deleted file mode 100644 index 895bde5..0000000 --- a/lib/exercises/exercises.ml +++ /dev/null @@ -1,109 +0,0 @@ -type a_exp = - Aval of int - | Plus of a_exp * a_exp - | Minus of a_exp * a_exp - | Times of a_exp * a_exp - | Of_bool of b_exp -and b_exp = - Bval of bool - | And of b_exp * b_exp - | Or of b_exp * b_exp - | Not of b_exp - | Minor of a_exp * a_exp - - -let rec eval_a_exp node = - match node with - Aval (i) -> i - | Plus (i, j) -> (eval_a_exp i) + (eval_a_exp j) - | Minus (i, j) -> (eval_a_exp i) - (eval_a_exp j) - | Times (i, j) -> (eval_a_exp i) * (eval_a_exp j) - | Of_bool b -> if (eval_b_exp b) then 1 else 0 -and eval_b_exp node = - match node with - Bval (b) -> b - | And (a, b) -> (eval_b_exp a) && (eval_b_exp b) - | Or (a, b) -> (eval_b_exp a) || (eval_b_exp b) - | Not b -> not (eval_b_exp b) - | Minor (i, j) -> (eval_a_exp i) < (eval_a_exp j) - -type 'a my_tree = - Leaf of 'a - | Node of ('a my_tree) list - -let mod_list y = - (List.fold_left - (fun acc x -> - match acc with - | [a] when ((List.hd a) = x) -> [x :: a] - | a :: tl when ((List.hd a) = x) -> (x :: a) :: tl - | _ -> [x] :: acc) - [] - y) - |> List.rev - -(* -------------------------------------------------------------------------- *) - -let to_tup f g = - fun x -> match x with - (a, b) -> (f a, g b) - -let partialsum l = - snd (List.fold_left_map (fun acc x -> (acc+x, acc+x)) 0 l) - -type label = - string - -type 'a finite_state_automata = { - l: label; - next: ('a finite_state_automata * 'a list) list; - final: bool; -} - -let rec check_included input fsa = - match input with - [] -> fsa.final - | a::rest -> ( - match List.find_opt (fun x -> List.mem a (snd x)) fsa.next with - None -> false - | Some x -> check_included rest (fst x) - ) - - -(* -------------------------------------------------------------------------- *) - -module StringMap = Map.Make(String) - -type fsa = { - vertices: bool StringMap.t; - edges: (string * char) StringMap.t; - state: string; -} - -let ex8 (instr: char list) (infsa: fsa) = - let rec helper_ex8 (i: char list) (ifsa: fsa) (current: string) = - match i with - [] -> ( - match StringMap.find_opt current ifsa.vertices with - None -> false - | Some b -> b - ) - | a::rest -> ( - match StringMap.find_first_opt (fun _ -> true) (StringMap.filter (fun x (_, y) -> x = current && y = a) ifsa.edges) with - None -> false - | Some (_, (outedge, _)) -> helper_ex8 rest ifsa outedge - ) - in helper_ex8 instr infsa infsa.state - -type binary_tree = - Node of binary_tree * binary_tree - | Leaf of int - -let ex9 b = - let rec helper_ex9 b' n = - match b' with - Leaf a -> a + n - | Node (r, l) -> (helper_ex9 r (helper_ex9 l n)) - in helper_ex9 b 0 - -(* -------------------------------------------------------------------------- *) diff --git a/lib/exercises/exercises.mli b/lib/exercises/exercises.mli deleted file mode 100644 index 7355881..0000000 --- a/lib/exercises/exercises.mli +++ /dev/null @@ -1,60 +0,0 @@ -type a_exp = - Aval of int - | Plus of a_exp * a_exp - | Minus of a_exp * a_exp - | Times of a_exp * a_exp - | Of_bool of b_exp -and b_exp = - Bval of bool - | And of b_exp * b_exp - | Or of b_exp * b_exp - | Not of b_exp - | Minor of a_exp * a_exp - - -val eval_a_exp: a_exp -> int -val eval_b_exp: b_exp -> bool - -type 'a my_tree = - Leaf of 'a - | Node of ('a my_tree) list - -val mod_list: 'a list -> 'a list list - -(* --------------------------------------------------------------------------- *) - -val to_tup : ('a -> 'b) -> ('c -> 'd) -> (('a * 'c) -> ('b * 'd)) - -val partialsum : int list -> int list - -type label = - string - -type 'a finite_state_automata = { - l: label; - next: ('a finite_state_automata * 'a list) list; - final: bool; -} - -val check_included : 'a list -> 'a finite_state_automata -> bool - -(* -------------------------------------------------------------------------- *) - -module StringMap : Map.S with type key = string - -type fsa = { - vertices: bool StringMap.t; - edges: (string * char) StringMap.t; - state: string; -} - -val ex8 : char list -> fsa -> bool - - -type binary_tree = - Node of binary_tree * binary_tree - | Leaf of int - -val ex9 : binary_tree -> int - -(* -------------------------------------------------------------------------- *) diff --git a/report/document.bib b/report/document.bib new file mode 100644 index 0000000..e69de29 diff --git a/report/document.pdf b/report/document.pdf new file mode 100644 index 0000000..1974e01 Binary files /dev/null and b/report/document.pdf differ diff --git a/report/document.tex b/report/document.tex new file mode 100644 index 0000000..fea0d5a --- /dev/null +++ b/report/document.tex @@ -0,0 +1,199 @@ +\documentclass[12pt, oneside]{article} + +%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% +%% Load Packages %% +%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% + +\usepackage[ + top=2cm, + bottom=2cm, + left=2cm, + right=2cm, + headheight=20pt, + centering +]{geometry} +\geometry{a4paper} + +\usepackage[utf8]{inputenc} %% use UTF-8, maybe not needed since 2018 +\usepackage[italian,main=english]{babel} %% language + +\pagestyle{headings} + +\usepackage{scrlayer-scrpage} +\usepackage{csquotes} %% correct language also for citations + +\ifoot[]{} +\cfoot[]{} +\ofoot[\pagemark]{\pagemark} +\pagestyle{scrplain} + +\usepackage[ + backend=biber, + style=numeric, + sorting=ynt +]{biblatex} %% for citations +\addbibresource{document.bib} + +\usepackage{import} %% specify path for import + +%% math packages +\usepackage{graphicx} %% for pictures +\usepackage{float} +\usepackage{amssymb} %% math symbols +\usepackage{amsmath} %% math matrix etc +\usepackage{listings} %% code block +\usepackage{tabularray} %% better tables +\usepackage{booktabs} %% rules for tables +\usepackage{mathrsfs} +\usepackage{mathtools} +\usepackage{algorithm} %% for algorithms +\usepackage{algpseudocode} %% loads algorithmicx +\usepackage{amsthm} +\usepackage{thmtools} %% theorems +\usepackage{syntax} %% BNF +\usepackage{semantic} %% semantics + +%% plot packages +\usepackage{pgfplots} %% plots used with \begin{tikzpicture} +\usepackage{tikz} %% for pictures +\usetikzlibrary{trees,chains,shadows.blur,fit} +\pgfplotsset{width=10cm,compat=newest} + +%% design packages +\usepackage{enumitem} %% for lists and enumerating +\usepackage{color} +\usepackage{xcolor,colortbl} % xcolor for defining colors, colortbl for table colors +\usepackage{makecell} %% for multiple lines in cell of table +\usepackage{cancel} +\usepackage{pgfornament} %% ornaments + +%% load last +\usepackage[hidelinks]{hyperref} %% links for table of contents, load last +\usepackage{bookmark} %% for better table of contents + + +%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% +%% Configuration of the packages %% +%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% + +%% \linespread{1} +\raggedbottom %% spaces if page is empty % chktex 1 + +%% set max table of contents recursion to subsection (3->subsubsecition) +\setcounter{tocdepth}{3} +\setcounter{secnumdepth}{3} + +%% use bar instead of arrow for vectors +\renewcommand{\vec}[1]{\bar{#1}} +%% easy norm +\newcommand{\norm}[1]{\left\lvert#1\right\rvert} + +% argmin and argmax +\DeclareMathOperator*{\argmax}{argmax} +\DeclareMathOperator*{\argmin}{argmin} + +%% itemize use less vertical space (use olditemize for default behaviour) +\let\olditemize=\itemize%% old itemize +\let\endolditemize=\enditemize%% old end itemize +\renewenvironment{itemize}{\olditemize\itemsep-0.2em}{\endolditemize} + +%% items in itemize emph+box +%% usage: \ieb{Class:} for simple item +%% \ieb[4cm]{Class:} for specific size of box +\newcommand{\ieb}[2][2cm]{ + \makebox[#1][l]{\emph{#2}} +} %% TODO: replace with description environment (? maybe) + +% less vertical space around align & align* +\newcommand{\zerodisplayskips}{ + \setlength{\abovedisplayskip}{0pt} + \setlength{\belowdisplayskip}{0pt} + \setlength{\abovedisplayshortskip}{0pt} + \setlength{\belowdisplayshortskip}{0pt} +} + +% make dotfill use all the space available +\renewcommand{\dotfill}{ + \leavevmode\cleaders\hbox to 1.00em{\hss .\hss }\hfill\kern0pt } % chktex 1 chktex 26 + +\setlength{\fboxsep}{-\fboxrule} % for debugging + + +%% PACKAGE algorithm +\floatname{algorithm}{Algorithm} + + +%% PACKAGE tabularray +\UseTblrLibrary{amsmath} + + +%% PACKAGE color +\definecolor{red}{rgb}{1, 0.1, 0.1} +\definecolor{lightgreen}{rgb}{0.55, 0.87, 0.47} +\definecolor{gray}{rgb}{0.3, 0.3, 0.3} +\newcommand{\lgt}{\cellcolor{lightgreen}} %% light green in tables +\newcommand{\gry}{\textcolor{gray}} %% gray text +\newcommand{\rd}{\textcolor{red}} %% red text + +%% PACKAGE minipage +\newcommand{\thend}[1]{\begin{center} + \begin{minipage}[c][1em][c]{#1} + \dotfill{} + \end{minipage} +\end{center}} + + +%% PACKAGE thmtools + + +%% ......................................................................... %% +%% local changes +% \setcounter{secnumdepth}{0} + +\newcommand{\defeq}{\vcentcolon=} + +\lstdefinelanguage{miniimp}{ + keywords={if, then, else, skip, while, do, for, rand}, + keywordstyle=\color{blue}\bfseries, + identifierstyle=\color{black}, + sensitive=false, + morecomment=[s]{(*}{*)}, % chktex 9 + commentstyle=\color{gray}\ttfamily, + stringstyle=\color{red}\ttfamily, + escapeinside={£}{£}, + numbers=left, + stepnumber=1 +} + +\lstset{ + language=miniimp, + extendedchars=true, + basicstyle=\footnotesize\ttfamily, + showstringspaces=false, + showspaces=false, + tabsize=2, + breaklines=true, + showtabs=false +} + +%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% + +\title{Document} +\author{ + Elvis Rossi +} +\date{\today} + +%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% + +\begin{document} + +\input{report} + +\end{document} + +%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %% + +%%% Local Variables: +%%% TeX-command-extra-options: "-shell-escape" +%%% End: diff --git a/report/report.tex b/report/report.tex new file mode 100644 index 0000000..d9b9fd2 --- /dev/null +++ b/report/report.tex @@ -0,0 +1,425 @@ +\begin{section}{Semantics} + \begin{subsection}{MiniImp} + The semantic of the MiniImp language is implemented in the \href{../lib/miniImp/Semantics.mli}{Semantics.mli} and \href{../lib/miniImp/Semantics.ml}{Semantics.ml} file. + A \texttt{reduce} function is provided that transforms an AST into the evaluated value or an error. + The AST type is defined in \href{../lib/miniImp/Types.mli}{Types.mli} and in \href{../lib/miniImp/Types.ml}{Types.ml}. + + A program \texttt{p} is defined as follows: + \begin{grammar} +
\(\defeq\) `def main with input'