Skip to content

Commit 59c0b9d

Browse files
authored
Adding C# to the build system (using the Mono toolchain) (#1012)
* 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
1 parent cc8ad62 commit 59c0b9d

File tree

6 files changed

+103
-46
lines changed

6 files changed

+103
-46
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,3 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
109109
RUN pip install wheel matplotlib numpy coconut scons
110110

111111
RUN sudo sh -c 'npm install -g typescript'
112-

SConstruct

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ available_languages = {
2828
'bash',
2929
'c',
3030
'cpp',
31+
'csharp',
3132
'fortran',
3233
'java',
3334
'julia',
@@ -43,6 +44,7 @@ available_languages = {
4344

4445
languages_to_import = {
4546
'coconut': ['coconut'],
47+
'csharp': ['mcs'],
4648
'go': ['go'],
4749
'rust': ['rustc', 'cargo'],
4850
'kotlin': ['kotlin'],
@@ -77,6 +79,7 @@ languages = {
7779
'c': 'c',
7880
'coconut': 'coco',
7981
'cpp': 'cpp',
82+
'csharp': 'cs',
8083
'fortran': 'f90',
8184
'go': 'go',
8285
'java': 'java',

builders/mcs.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from SCons.Builder import Builder
2+
import SCons.Util
3+
4+
class ToolMCSWarning(SCons.Warnings.SConsWarning):
5+
pass
6+
7+
class MCSNotFound(ToolMCSWarning):
8+
pass
9+
10+
SCons.Warnings.enableWarningClass(ToolMCSWarning)
11+
12+
def _detect(env):
13+
try:
14+
return env['mcs']
15+
except KeyError:
16+
pass
17+
18+
mcs = env.WhereIs('mcs')
19+
if mcs:
20+
return mcs
21+
22+
SCons.Warnings.warn(MCSNotFound, 'Could not find mcs executable')
23+
24+
def exists(env):
25+
env.Detect('mcs')
26+
27+
def generate(env):
28+
env['MCS'] = _detect(env)
29+
env['MCSFLAGS'] = []
30+
31+
mcs_builder = Builder(
32+
action='"$MCS" -out:$TARGET $MCSFLAGS $SOURCES',
33+
src_suffix='.cs',
34+
suffix='$PROGSUFFIX',
35+
)
36+
37+
env.Append(BUILDERS={'MCS': mcs_builder})

contents/huffman_encoding/code/csharp/HuffmanCoding.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,26 @@ private Node CreateTree(string input)
133133
return nodePriorityList.Pop();
134134
}
135135

136+
137+
private void CreateDictionary(Node node, string bitString, Dictionary<char, string> localDictionary)
138+
{
139+
if (node.IsLeaf)
140+
localDictionary.Add(node.Key[0], bitString);
141+
else
142+
{
143+
if (node.LeftChild != null)
144+
CreateDictionary(node.LeftChild, bitString + '0', localDictionary);
145+
if (node.RightChild != null)
146+
CreateDictionary(node.RightChild, bitString + '1', localDictionary);
147+
}
148+
}
149+
136150
private Dictionary<char, string> CreateDictionary(Node root)
137151
{
138152
// 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.
139153
var dictionary = new Dictionary<char, string>();
140154
CreateDictionary(root, "", dictionary);
141155
return dictionary;
142-
143-
void CreateDictionary(Node node, string bitString, Dictionary<char, string> localDictionary)
144-
{
145-
if (node.IsLeaf)
146-
localDictionary.Add(node.Key[0], bitString);
147-
else
148-
{
149-
if (node.LeftChild != null)
150-
CreateDictionary(node.LeftChild, bitString + '0', localDictionary);
151-
if (node.RightChild != null)
152-
CreateDictionary(node.RightChild, bitString + '1', localDictionary);
153-
}
154-
}
155156
}
156157

157158

@@ -165,4 +166,4 @@ private string CreateBitString(string input, Dictionary<char, string> dictionary
165166
return bitString;
166167
}
167168
}
168-
}
169+
}

contents/tree_traversal/code/csharp/Tree.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,58 @@ private Tree(int id, int depthCount, int childrenCount)
3030
}
3131
}
3232

33+
private void DFSRecursive(Tree tree) {
34+
Console.Write(tree.Id + " ");
35+
36+
foreach (var c in tree._children)
37+
DFSRecursive(c);
38+
}
39+
3340
public void DFSRecursive()
3441
{
3542
DFSRecursive(this);
3643

37-
void DFSRecursive(Tree tree)
38-
{
39-
Console.Write(tree.Id + " ");
44+
}
4045

41-
foreach (var c in tree._children)
42-
DFSRecursive(c);
43-
}
46+
private void DFSRecursivePostorder(Tree tree)
47+
{
48+
foreach (var c in tree._children)
49+
DFSRecursivePostorder(c);
50+
51+
Console.Write(tree.Id + " ");
4452
}
4553

4654
public void DFSRecursivePostorder()
4755
{
4856
DFSRecursivePostorder(this);
4957

50-
void DFSRecursivePostorder(Tree tree)
51-
{
52-
foreach (var c in tree._children)
53-
DFSRecursivePostorder(c);
58+
}
5459

55-
Console.Write(tree.Id + " ");
60+
private void DFSRecursiveInorderBinary(Tree tree)
61+
{
62+
switch (tree._children.Count)
63+
{
64+
case 2:
65+
DFSRecursiveInorderBinary(tree._children[0]);
66+
Console.Write(tree.Id + " ");
67+
DFSRecursiveInorderBinary(tree._children[1]);
68+
break;
69+
case 1:
70+
DFSRecursiveInorderBinary(tree._children[0]);
71+
Console.Write(tree.Id + " ");
72+
break;
73+
case 0:
74+
Console.Write(tree.Id + " ");
75+
break;
76+
default:
77+
throw new Exception("Not binary tree!");
5678
}
5779
}
5880

5981
public void DFSRecursiveInorderBinary()
6082
{
6183
DFSRecursiveInorderBinary(this);
6284

63-
void DFSRecursiveInorderBinary(Tree tree)
64-
{
65-
switch (tree._children.Count)
66-
{
67-
case 2:
68-
DFSRecursiveInorderBinary(tree._children[0]);
69-
Console.Write(tree.Id + " ");
70-
DFSRecursiveInorderBinary(tree._children[1]);
71-
break;
72-
case 1:
73-
DFSRecursiveInorderBinary(tree._children[0]);
74-
Console.Write(tree.Id + " ");
75-
break;
76-
case 0:
77-
Console.Write(tree.Id + " ");
78-
break;
79-
default:
80-
throw new Exception("Not binary tree!");
81-
}
82-
}
8385
}
8486

8587
public void DFSStack()

sconscripts/csharp_SConscript

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Import('files_to_compile env')
2+
3+
language = files_to_compile[0].language
4+
chapter = files_to_compile[0].chapter
5+
6+
from collections import defaultdict
7+
chapter_files = defaultdict(list)
8+
9+
for file_info in files_to_compile:
10+
chapter_files[file_info.chapter].append(file_info.path)
11+
12+
for chapter, files in chapter_files.items():
13+
build_target = f'#/build/{language}/{chapter}/{chapter}'
14+
build_result = env.MCS(build_target, [str(file) for file in files])
15+
env.Alias(str(chapter), build_result)

0 commit comments

Comments
 (0)