From 631995cc476edba1397d0cd2153b3686ab508f18 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Sun, 20 Aug 2023 04:38:58 +0200 Subject: [PATCH 1/5] Added C# compilation through the mono-devel package --- Dockerfile | 5 ++--- SConstruct | 3 +++ builders/mcs.py | 37 +++++++++++++++++++++++++++++++++++ sconscripts/csharp_SConscript | 15 ++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 builders/mcs.py create mode 100644 sconscripts/csharp_SConscript diff --git a/Dockerfile b/Dockerfile index a0808c1ef..b648ea7d8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT} # [Optional] Uncomment this section to install additional OS packages. RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ - && apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake + && apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake mono-devel # Setup Crystal RUN echo 'deb http://download.opensuse.org/repositories/devel:/languages:/crystal/xUbuntu_20.04/ /' | sudo tee /etc/apt/sources.list.d/devel:languages:crystal.list @@ -98,7 +98,7 @@ ENV PATH=$PATH:~/elm # Setup V RUN mkdir -p ~/vlang && wget https://github.com/vlang/v/releases/download/weekly.2021.44/v_linux.zip -O ~/vlang/vlang.zip && \ - unzip ~/vlang/vlang.zip -d ~/vlang + unzip ~/vlang/vlang.zip -d ~/vlang ENV PATH=$PATH:~/vlang/v # Install the packages that needed extra help @@ -109,4 +109,3 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ RUN pip install wheel matplotlib numpy coconut scons RUN sudo sh -c 'npm install -g typescript' - diff --git a/SConstruct b/SConstruct index e5ecf82d9..74e05ea85 100644 --- a/SConstruct +++ b/SConstruct @@ -28,6 +28,7 @@ available_languages = { 'bash', 'c', 'cpp', + 'csharp', 'fortran', 'java', 'julia', @@ -43,6 +44,7 @@ available_languages = { languages_to_import = { 'coconut': ['coconut'], + 'csharp': ['mcs'], 'go': ['go'], 'rust': ['rustc', 'cargo'], 'kotlin': ['kotlin'], @@ -77,6 +79,7 @@ languages = { 'c': 'c', 'coconut': 'coco', 'cpp': 'cpp', + 'csharp': 'cs', 'fortran': 'f90', 'go': 'go', 'java': 'java', diff --git a/builders/mcs.py b/builders/mcs.py new file mode 100644 index 000000000..f6038a184 --- /dev/null +++ b/builders/mcs.py @@ -0,0 +1,37 @@ +from SCons.Builder import Builder +import SCons.Util + +class ToolMCSWarning(SCons.Warnings.SConsWarning): + pass + +class MCSNotFound(ToolMCSWarning): + pass + +SCons.Warnings.enableWarningClass(ToolMCSWarning) + +def _detect(env): + try: + return env['mcs'] + except KeyError: + pass + + go = env.WhereIs('mcs') + if go: + return go + + SCons.Warnings.warn(MCSNotFound, 'Could not find mcs executable') + +def exists(env): + env.Detect('mcs') + +def generate(env): + env['MCS'] = _detect(env) + env['MCSFLAGS'] = [] + + mcs_builder = Builder( + action='"$MCS" -out:$TARGET $MCSFLAGS $SOURCES', + src_suffix='.cs', + suffix='$PROGSUFFIX', + ) + + env.Append(BUILDERS={'MCS': mcs_builder}) diff --git a/sconscripts/csharp_SConscript b/sconscripts/csharp_SConscript new file mode 100644 index 000000000..366471f19 --- /dev/null +++ b/sconscripts/csharp_SConscript @@ -0,0 +1,15 @@ +Import('files_to_compile env') + +language = files_to_compile[0].language +chapter = files_to_compile[0].chapter + +from collections import defaultdict +chapter_files = defaultdict(list) + +for file_info in files_to_compile: + chapter_files[file_info.chapter].append(file_info.path) + +for chapter, files in chapter_files.items(): + build_target = f'#/build/{language}/{chapter}/{chapter}' + build_result = env.MCS(build_target, [str(file) for file in files]) + env.Alias(str(chapter), build_result) From f2255127618320a8cff826602e9521493d1eb51e Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Sun, 20 Aug 2023 04:40:03 +0200 Subject: [PATCH 2/5] Moved out void functions inside functions as it doesn't work with the mono compiler --- .../code/csharp/HuffmanCoding.cs | 29 +++++---- contents/tree_traversal/code/csharp/Tree.cs | 64 ++++++++++--------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/contents/huffman_encoding/code/csharp/HuffmanCoding.cs b/contents/huffman_encoding/code/csharp/HuffmanCoding.cs index ef711af26..cc8a711cd 100644 --- a/contents/huffman_encoding/code/csharp/HuffmanCoding.cs +++ b/contents/huffman_encoding/code/csharp/HuffmanCoding.cs @@ -133,25 +133,26 @@ private Node CreateTree(string input) return nodePriorityList.Pop(); } + + private void CreateDictionary(Node node, string bitString, Dictionary localDictionary) + { + if (node.IsLeaf) + localDictionary.Add(node.Key[0], bitString); + else + { + if (node.LeftChild != null) + CreateDictionary(node.LeftChild, bitString + '0', localDictionary); + if (node.RightChild != null) + CreateDictionary(node.RightChild, bitString + '1', localDictionary); + } + } + private Dictionary CreateDictionary(Node root) { // We're using a string instead of a actual bits here, since it makes the code somewhat more readable and this is an educational example. var dictionary = new Dictionary(); CreateDictionary(root, "", dictionary); return dictionary; - - void CreateDictionary(Node node, string bitString, Dictionary localDictionary) - { - if (node.IsLeaf) - localDictionary.Add(node.Key[0], bitString); - else - { - if (node.LeftChild != null) - CreateDictionary(node.LeftChild, bitString + '0', localDictionary); - if (node.RightChild != null) - CreateDictionary(node.RightChild, bitString + '1', localDictionary); - } - } } @@ -165,4 +166,4 @@ private string CreateBitString(string input, Dictionary dictionary return bitString; } } -} \ No newline at end of file +} diff --git a/contents/tree_traversal/code/csharp/Tree.cs b/contents/tree_traversal/code/csharp/Tree.cs index 28df47c91..5e8bc263c 100644 --- a/contents/tree_traversal/code/csharp/Tree.cs +++ b/contents/tree_traversal/code/csharp/Tree.cs @@ -30,29 +30,51 @@ private Tree(int id, int depthCount, int childrenCount) } } + private void DFSRecursive(Tree tree) { + Console.Write(tree.Id + " "); + + foreach (var c in tree._children) + DFSRecursive(c); + } + public void DFSRecursive() { DFSRecursive(this); - void DFSRecursive(Tree tree) - { - Console.Write(tree.Id + " "); + } - foreach (var c in tree._children) - DFSRecursive(c); - } + private void DFSRecursivePostorder(Tree tree) + { + foreach (var c in tree._children) + DFSRecursivePostorder(c); + + Console.Write(tree.Id + " "); } public void DFSRecursivePostorder() { DFSRecursivePostorder(this); - void DFSRecursivePostorder(Tree tree) - { - foreach (var c in tree._children) - DFSRecursivePostorder(c); + } - Console.Write(tree.Id + " "); + private void DFSRecursiveInorderBinary(Tree tree) + { + switch (tree._children.Count) + { + case 2: + DFSRecursiveInorderBinary(tree._children[0]); + Console.Write(tree.Id + " "); + DFSRecursiveInorderBinary(tree._children[1]); + break; + case 1: + DFSRecursiveInorderBinary(tree._children[0]); + Console.Write(tree.Id + " "); + break; + case 0: + Console.Write(tree.Id + " "); + break; + default: + throw new Exception("Not binary tree!"); } } @@ -60,26 +82,6 @@ public void DFSRecursiveInorderBinary() { DFSRecursiveInorderBinary(this); - void DFSRecursiveInorderBinary(Tree tree) - { - switch (tree._children.Count) - { - case 2: - DFSRecursiveInorderBinary(tree._children[0]); - Console.Write(tree.Id + " "); - DFSRecursiveInorderBinary(tree._children[1]); - break; - case 1: - DFSRecursiveInorderBinary(tree._children[0]); - Console.Write(tree.Id + " "); - break; - case 0: - Console.Write(tree.Id + " "); - break; - default: - throw new Exception("Not binary tree!"); - } - } } public void DFSStack() From 8ee8f634c43d2583a090dc79348d1f7c663c0044 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Tue, 22 Aug 2023 21:58:18 +0200 Subject: [PATCH 3/5] Removed mono-devel from Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index b648ea7d8..e2139f6fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT} # [Optional] Uncomment this section to install additional OS packages. RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ - && apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake mono-devel + && apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake # Setup Crystal RUN echo 'deb http://download.opensuse.org/repositories/devel:/languages:/crystal/xUbuntu_20.04/ /' | sudo tee /etc/apt/sources.list.d/devel:languages:crystal.list From 209abcfebed284998bad9cd6fae6591c1d4be8bb Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Tue, 22 Aug 2023 23:10:59 +0200 Subject: [PATCH 4/5] Revert "Removed mono-devel from Dockerfile" This reverts commit 8ee8f634c43d2583a090dc79348d1f7c663c0044. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e2139f6fc..b648ea7d8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT} # [Optional] Uncomment this section to install additional OS packages. RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ - && apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake + && apt-get -y install --no-install-recommends build-essential software-properties-common xz-utils g++ sbcl julia python3 python3-pip python3-dev ghc openjdk-11-jdk libssl-dev gfortran libxml2-dev libyaml-dev libgmp-dev libz-dev libncurses5 gnuplot nodejs npm lua5.3 ocaml php ruby-full gnu-smalltalk scratch libfftw3-dev cmake mono-devel # Setup Crystal RUN echo 'deb http://download.opensuse.org/repositories/devel:/languages:/crystal/xUbuntu_20.04/ /' | sudo tee /etc/apt/sources.list.d/devel:languages:crystal.list From 31c0ddf71cebc6ce7fa85b9886172b222210f238 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Tue, 22 Aug 2023 23:58:14 +0200 Subject: [PATCH 5/5] fixed incorrect naming typo --- builders/mcs.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builders/mcs.py b/builders/mcs.py index f6038a184..07f1bd76c 100644 --- a/builders/mcs.py +++ b/builders/mcs.py @@ -15,9 +15,9 @@ def _detect(env): except KeyError: pass - go = env.WhereIs('mcs') - if go: - return go + mcs = env.WhereIs('mcs') + if mcs: + return mcs SCons.Warnings.warn(MCSNotFound, 'Could not find mcs executable')