Skip to content

Commit

Permalink
Adding C# to the build system (using the Mono toolchain) (#1012)
Browse files Browse the repository at this point in the history
* Added C# compilation through the mono-devel package

* Moved out void functions inside functions as it doesn't work with the mono compiler

* Removed mono-devel from Dockerfile

* Revert "Removed mono-devel from Dockerfile"

This reverts commit 8ee8f63.

* fixed incorrect naming typo
  • Loading branch information
Amaras authored Aug 23, 2023
1 parent cc8ad62 commit 59c0b9d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 46 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'

3 changes: 3 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ available_languages = {
'bash',
'c',
'cpp',
'csharp',
'fortran',
'java',
'julia',
Expand All @@ -43,6 +44,7 @@ available_languages = {

languages_to_import = {
'coconut': ['coconut'],
'csharp': ['mcs'],
'go': ['go'],
'rust': ['rustc', 'cargo'],
'kotlin': ['kotlin'],
Expand Down Expand Up @@ -77,6 +79,7 @@ languages = {
'c': 'c',
'coconut': 'coco',
'cpp': 'cpp',
'csharp': 'cs',
'fortran': 'f90',
'go': 'go',
'java': 'java',
Expand Down
37 changes: 37 additions & 0 deletions builders/mcs.py
Original file line number Diff line number Diff line change
@@ -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

mcs = env.WhereIs('mcs')
if mcs:
return mcs

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})
29 changes: 15 additions & 14 deletions contents/huffman_encoding/code/csharp/HuffmanCoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,26 @@ private Node CreateTree(string input)
return nodePriorityList.Pop();
}


private void CreateDictionary(Node node, string bitString, Dictionary<char, string> 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<char, string> 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<char, string>();
CreateDictionary(root, "", dictionary);
return dictionary;

void CreateDictionary(Node node, string bitString, Dictionary<char, string> 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);
}
}
}


Expand All @@ -165,4 +166,4 @@ private string CreateBitString(string input, Dictionary<char, string> dictionary
return bitString;
}
}
}
}
64 changes: 33 additions & 31 deletions contents/tree_traversal/code/csharp/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,58 @@ 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!");
}
}

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()
Expand Down
15 changes: 15 additions & 0 deletions sconscripts/csharp_SConscript
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 59c0b9d

Please sign in to comment.