summaryrefslogtreecommitdiff
path: root/Chalice
diff options
context:
space:
mode:
authorGravatar jansmans <unknown>2009-08-17 12:21:15 +0000
committerGravatar jansmans <unknown>2009-08-17 12:21:15 +0000
commit3fd9f0a2a4f754c039653ac2fab090167f0b2596 (patch)
tree51e8cb2fa4fcb76771c037ddba752d8ae66e4e58 /Chalice
parent4d3e9b8f2bdb29ec53d1c418a11501bcf6a301d3 (diff)
- Chalice-to-C# compiler now supports channels
Diffstat (limited to 'Chalice')
-rw-r--r--Chalice/src/Chalice.cs23
-rw-r--r--Chalice/src/ChaliceToCSharp.scala30
2 files changed, 53 insertions, 0 deletions
diff --git a/Chalice/src/Chalice.cs b/Chalice/src/Chalice.cs
index f4a804e7..4865db69 100644
--- a/Chalice/src/Chalice.cs
+++ b/Chalice/src/Chalice.cs
@@ -5,6 +5,7 @@
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
+using System.Threading;
namespace Chalice
{
@@ -64,6 +65,28 @@ namespace Chalice
return l;
}
}
+
+ public class ChannelBuffer<E> {
+ private LinkedList<E> contents = new LinkedList<E>();
+
+ public void Add(E e) {
+ lock(this) {
+ contents.AddFirst(e);
+ Monitor.Pulse(this);
+ }
+ }
+
+ public E Remove() {
+ lock(this) {
+ while(contents.Count == 0){
+ Monitor.Wait(this);
+ }
+ E e = contents.Last.Value;
+ contents.RemoveLast();
+ return e;
+ }
+ }
+ }
public class ChalicePrint {
public void Int(int x) {
diff --git a/Chalice/src/ChaliceToCSharp.scala b/Chalice/src/ChaliceToCSharp.scala
index feef3195..2bc7f2fe 100644
--- a/Chalice/src/ChaliceToCSharp.scala
+++ b/Chalice/src/ChaliceToCSharp.scala
@@ -13,9 +13,35 @@ class ChaliceToCSharp {
def convertTopLevelDecl(decl: TopLevelDecl): String = {
decl match {
case cl: Class => convertClass(cl)
+ case ch: Channel => convertChannel(ch)
}
}
+ def convertChannel(ch: Channel): String = {
+ "public class " + ch.channelId + " {" + nl +
+ indentMore {
+ indent + "class " + ch.channelId + "_args " + "{" + nl +
+ indentMore {
+ repsep(ch.parameters map { variable => indent + "internal " + convertType(variable.t) + " " + variable.id + ";"}, nl)
+ } + nl + indent + "}" + nl +
+ indent + "ChannelBuffer<" + ch.channelId + "_args" + ">" + " buffer = new ChannelBuffer<" + ch.channelId + "_args" + ">();" + nl +
+ indent + "public void Send(" + repsep(ch.parameters map { variable => convertType(variable.t) + " " + variable.id }, ", ") + ") {" + nl +
+ indentMore {
+ indent + ch.channelId + "_args " + "entry = new " + ch.channelId + "_args" + "();" + nl +
+ indent + repsep(ch.parameters map { p => "entry." + p.id + " = " + p.id + ";"}, nl) + nl +
+ indent + "buffer.Add(entry);" + nl
+ } +
+ indent + "}" + nl +
+ indent + "public void Receive(" + repsep(ch.parameters map { variable => "out " + convertType(variable.t) + " " + variable.id }, ", ") + ") {" + nl +
+ indentMore {
+ indent + ch.channelId + "_args " + "entry = buffer.Remove();" + nl +
+ indent + repsep(ch.parameters map { p => p.id + " = entry." + p.id + ";"}, nl) + nl
+ } +
+ indent + "}" + nl
+ } +
+ "}" + nl + nl
+ }
+
def convertClass(cl: Class): String = if (cl.IsExternal) "" else {
"public class " + cl.id + " {" + nl +
indentMore { indent + "public " + cl.id + "() " + "{" + nl + indent + "}" } + nl + nl +
@@ -120,6 +146,10 @@ class ChaliceToCSharp {
indent + convertExpression(token) + "." + "del.EndInvoke(" +
repsep(lhs map { x => "out " + x.id}, ", ") +
(if(lhs.length > 0) ", " else "") + convertExpression(token) + "." + "async" + ");" + nl
+ case s@Send(ch, args) =>
+ indent + convertExpression(ch) + ".Send(" + repsep(args map convertExpression, ", ") + ")" + ";" + nl
+ case r@Receive(ch, outs) =>
+ indent + convertExpression(ch) + ".Receive(" + repsep(outs map { out => "out " + convertExpression(out)}, ", ") + ")" + ";" + nl
}
}