Skip to content

Commit

Permalink
new parameter definition model:
Browse files Browse the repository at this point in the history
 - arguments are no longer accessed by index
 - ref/out and params modifiers are supported directly
 - enables addition of custom attributes on parameters in the future

--HG--
branch : parameters
  • Loading branch information
ssimek committed Oct 8, 2009
1 parent f0feb7d commit 3d64355
Show file tree
Hide file tree
Showing 30 changed files with 1,238 additions and 499 deletions.
6 changes: 3 additions & 3 deletions Examples/01_HelloWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public static void GenHello3(AssemblyGen ag)
{
TypeGen Hello3 = ag.Public.Class("Hello3");
{
CodeGen g = Hello3.Public.Static.Method(typeof(void), "Main", typeof(string[]));
CodeGen g = Hello3.Public.Static.Method(typeof(void), "Main").Parameter(typeof(string[]), "args");
{
Operand args = g.Arg(0, "args");
Operand args = g.Arg("args");
g.WriteLine("Hello, World!");
g.WriteLine("You entered the following {0} command line arguments:",
g.WriteLine("You entered the following {0} command line arguments:",
args.ArrayLength());
Operand i = g.Local();
g.For(i.Assign(0), i < args.ArrayLength(), i.Increment());
Expand Down
4 changes: 2 additions & 2 deletions Examples/02_CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static void GenCmdLine2(AssemblyGen ag)
{
TypeGen CommandLine2 = ag.Public.Class("CommandLine2");
{
CodeGen g = CommandLine2.Public.Static.Method(typeof(void), "Main", typeof(string[]));
CodeGen g = CommandLine2.Public.Static.Method(typeof(void), "Main").Parameter(typeof(string[]), "args");
{
Operand args = g.Arg(0);
Operand args = g.Arg("args");
g.WriteLine("Number of command line parameters = {0}",
args.Property("Length"));
Operand s = g.ForEach(typeof(string), args);
Expand Down
28 changes: 16 additions & 12 deletions Examples/04_Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public static void GenShapeTest(AssemblyGen ag)

PropertyGen Id = Shape.Public.SimpleProperty(myId, "Id");

CodeGen g = Shape.Public.Constructor(typeof(string));
CodeGen g = Shape.Public.Constructor().Parameter(typeof(string), "s");
{
g.Assign(Id, g.Arg(0, "s")); // calling the set accessor of the Id property
g.Assign(Id, g.Arg("s")); // calling the set accessor of the Id property
}

// Area is a read-only property - only a get accessor is needed:
Expand All @@ -101,10 +101,10 @@ public static void GenShapeTest(AssemblyGen ag)
{
FieldGen mySide = Square.Private.Field(typeof(int), "mySide");

CodeGen g = Square.Public.Constructor(typeof(int), typeof(string));
CodeGen g = Square.Public.Constructor().Parameter(typeof(int), "side").Parameter(typeof(string), "id");
{
g.InvokeBase(g.Arg(1, "id"));
g.Assign(mySide, g.Arg(0, "side"));
g.InvokeBase(g.Arg("id"));
g.Assign(mySide, g.Arg("side"));
}

PropertyGen Area = Square.Public.Override.Property(typeof(double), "Area");
Expand All @@ -119,10 +119,10 @@ public static void GenShapeTest(AssemblyGen ag)
{
FieldGen myRadius = Circle.Private.Field(typeof(int), "myRadius");

CodeGen g = Circle.Public.Constructor(typeof(int), typeof(string));
CodeGen g = Circle.Public.Constructor().Parameter(typeof(int), "radius").Parameter(typeof(string), "id");
{
g.InvokeBase(g.Arg(1, "id"));
g.Assign(myRadius, g.Arg(0, "radius"));
g.InvokeBase(g.Arg("id"));
g.Assign(myRadius, g.Arg("radius"));
}

PropertyGen Area = Circle.Public.Override.Property(typeof(double), "Area");
Expand All @@ -138,11 +138,15 @@ public static void GenShapeTest(AssemblyGen ag)
FieldGen myWidth = Rectangle.Private.Field(typeof(int), "myWidth");
FieldGen myHeight = Rectangle.Private.Field(typeof(int), "myHeight");

CodeGen g = Rectangle.Public.Constructor(typeof(int), typeof(int), typeof(string));
CodeGen g = Rectangle.Public.Constructor()
.Parameter(typeof(int), "width")
.Parameter(typeof(int), "height")
.Parameter(typeof(string), "id")
;
{
g.InvokeBase(g.Arg(2, "id"));
g.Assign(myWidth, g.Arg(0, "width"));
g.Assign(myHeight, g.Arg(1, "height"));
g.InvokeBase(g.Arg("id"));
g.Assign(myWidth, g.Arg("width"));
g.Assign(myHeight, g.Arg("height"));
}

PropertyGen Area = Rectangle.Public.Override.Property(typeof(double), "Area");
Expand Down
12 changes: 6 additions & 6 deletions Examples/05_Versioning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,33 @@ public static void GenVersioning(AssemblyGen ag)
{
TypeGen MyBase = ag.Public.Class("MyBase");
{
MyBase.Public.Virtual.Method(typeof(string), "Meth1").Code
MyBase.Public.Virtual.Method(typeof(string), "Meth1").GetCode()
.Return("MyBase-Meth1");

MyBase.Public.Virtual.Method(typeof(string), "Meth2").Code
MyBase.Public.Virtual.Method(typeof(string), "Meth2").GetCode()
.Return("MyBase-Meth2");

MyBase.Public.Virtual.Method(typeof(string), "Meth3").Code
MyBase.Public.Virtual.Method(typeof(string), "Meth3").GetCode()
.Return("MyBase-Meth3");
}

TypeGen MyDerived = ag.Class("MyDerived", MyBase);
{
// Overrides the virtual method Meth1 using the override keyword:
MyDerived.Public.Override.Method(typeof(string), "Meth1").Code
MyDerived.Public.Override.Method(typeof(string), "Meth1").GetCode()
.Return("MyDerived-Meth1");

// Explicitly hide the virtual method Meth2 using the new
// keyword:
// remark: new is not supported/required in RunSharp
MyDerived.Public.Method(typeof(string), "Meth2").Code
MyDerived.Public.Method(typeof(string), "Meth2").GetCode()
.Return("MyDerived-Meth2");

// Because no keyword is specified in the following declaration
// a warning will be issued to alert the programmer that
// the method hides the inherited member MyBase.Meth3():
// remark: this warning is not supported in RunSharp
MyDerived.Public.Method(typeof(string), "Meth3").Code
MyDerived.Public.Method(typeof(string), "Meth3").GetCode()
.Return("MyDerived-Meth3");

CodeGen g = MyDerived.Public.Static.Method(typeof(void), "Main");
Expand Down
11 changes: 7 additions & 4 deletions Examples/06_CollectionClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public static void GenTokens2(AssemblyGen ag)
{
FieldGen elements = Tokens.Private.Field(typeof(string[]), "elements");

CodeGen g = Tokens.Constructor(typeof(string), typeof(char[]));
CodeGen g = Tokens.Constructor()
.Parameter(typeof(string), "source")
.Parameter(typeof(char[]), "delimiters")
;
{
g.Assign(elements, g.Arg(0, "source").Invoke("Split", g.Arg(1, "delimiters")));
g.Assign(elements, g.Arg("source").Invoke("Split", g.Arg("delimiters")));
}

// Inner class implements IEnumerator interface:
Expand All @@ -49,9 +52,9 @@ public static void GenTokens2(AssemblyGen ag)
FieldGen position = TokenEnumerator.Field(typeof(int), "position", -1);
FieldGen t = TokenEnumerator.Field(Tokens, "t");

g = TokenEnumerator.Public.Constructor(Tokens);
g = TokenEnumerator.Public.Constructor().Parameter(Tokens, "tokens");
{
g.Assign(t, g.Arg(0));
g.Assign(t, g.Arg("tokens"));
}

g = TokenEnumerator.Public.Method(typeof(bool), "MoveNext");
Expand Down
10 changes: 5 additions & 5 deletions Examples/07_Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void GenStruct1(AssemblyGen ag)

PropertyGen X = SimpleStruct.Public.Property(typeof(int), "X");
{
X.Getter().Code.Return(xval);
X.Getter().GetCode().Return(xval);
g = X.Setter();
{
g.If(g.PropertyValue() < 100);
Expand Down Expand Up @@ -85,14 +85,14 @@ public static void GenStruct2(AssemblyGen ag)

TypeGen TestClass = ag.Class("TestClass");
{
CodeGen g = TestClass.Public.Static.Method(typeof(void), "structtaker", TheStruct);
CodeGen g = TestClass.Public.Static.Method(typeof(void), "structtaker").Parameter(TheStruct, "s");
{
g.Assign(g.Arg(0, "s").Field("x"), 5);
g.Assign(g.Arg("s").Field("x"), 5);
}

g = TestClass.Public.Static.Method(typeof(void), "classtaker", TheClass);
g = TestClass.Public.Static.Method(typeof(void), "classtaker").Parameter(TheClass, "c");
{
g.Assign(g.Arg(0, "c").Field("x"), 5);
g.Assign(g.Arg("c").Field("x"), 5);
}

g = TestClass.Public.Static.Method(typeof(void), "Main");
Expand Down
16 changes: 8 additions & 8 deletions Examples/08_Indexers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public static void GenIndexer(AssemblyGen ag)
// used to access the file.

// Create a new FileByteArray encapsulating a particular file.
CodeGen g = FileByteArray.Public.Constructor(typeof(string));
CodeGen g = FileByteArray.Public.Constructor().Parameter(typeof(string), "fileName");
{
g.Assign(stream, Exp.New(typeof(FileStream), g.Arg(0, "fileName"), FileMode.Open));
g.Assign(stream, Exp.New(typeof(FileStream), g.Arg("fileName"), FileMode.Open));
}

// Close the stream. This should be the last thing done
Expand All @@ -57,37 +57,37 @@ public static void GenIndexer(AssemblyGen ag)
}

// Indexer to provide read/write access to the file.
PropertyGen Item = FileByteArray.Public.Indexer(typeof(byte), typeof(long)); // long is a 64-bit integer
PropertyGen Item = FileByteArray.Public.Indexer(typeof(byte)).Index(typeof(long), "index"); // long is a 64-bit integer
{
// Read one byte at offset index and return it.
g = Item.Getter();
{
Operand buffer = g.Local(Exp.NewArray(typeof(byte), 1));
g.Invoke(stream, "Seek", g.Arg(0, "index"), SeekOrigin.Begin);
g.Invoke(stream, "Seek", g.Arg("index"), SeekOrigin.Begin);
g.Invoke(stream, "Read", buffer, 0, 1);
g.Return(buffer[0]);
}
// Write one byte at offset index and return it.
g = Item.Setter();
{
Operand buffer = g.Local(Exp.NewInitializedArray(typeof(byte), g.PropertyValue()));
g.Invoke(stream, "Seek", g.Arg(0, "index"), SeekOrigin.Begin);
g.Invoke(stream, "Seek", g.Arg("index"), SeekOrigin.Begin);
g.Invoke(stream, "Write", buffer, 0, 1);
}
}

// Get the total length of the file.
FileByteArray.Public.Property(typeof(long), "Length").Getter().Code
FileByteArray.Public.Property(typeof(long), "Length").Getter().GetCode()
.Return(stream.Invoke("Seek", 0, SeekOrigin.End));
}

// Demonstrate the FileByteArray class.
// Reverses the bytes in a file.
TypeGen Reverse = ag.Public.Class("Reverse");
{
CodeGen g = Reverse.Public.Static.Method(typeof(void), "Main", typeof(string[]));
CodeGen g = Reverse.Public.Static.Method(typeof(void), "Main").Parameter(typeof(string[]), "args");
{
Operand args = g.Arg(0, "args");
Operand args = g.Arg("args");

// Check for arguments.
g.If(args.ArrayLength() != 1);
Expand Down
38 changes: 21 additions & 17 deletions Examples/09_IndexedProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,25 @@ public static void GenIndexedProperty(AssemblyGen ag)
FieldGen document = WordCollection.ReadOnly.Field(Document, "document"); // The containing document
Operand document_TextArray = document.Field("TextArray"); // example of a saved expression - it is always re-evaluated when used

g = WordCollection.Internal.Constructor(Document);
g = WordCollection.Internal.Constructor().Parameter(Document, "d");
{
g.Assign(document, g.Arg(0, "d"));
g.Assign(document, g.Arg("d"));
}

// Helper function -- search character array "text", starting at
// character "begin", for word number "wordCount." Returns false
// if there are less than wordCount words.Sets "start" and
// length" to the position and length of the word within text:
// remark: there is no equivalent to 'out' in RunSharp, only 'ref' can be used
g = WordCollection.Private.Method(typeof(bool), "GetWord", typeof(char[]), typeof(int), typeof(int),
typeof(int).MakeByRefType(), typeof(int).MakeByRefType());
g = WordCollection.Private.Method(typeof(bool), "GetWord")
.Parameter(typeof(char[]), "text")
.Parameter(typeof(int), "begin")
.Parameter(typeof(int), "wordCount")
.Out.Parameter(typeof(int), "start")
.Out.Parameter(typeof(int), "length")
;
{
Operand text = g.Arg(0, "text"), begin = g.Arg(1, "begin"), wordCount = g.Arg(2, "wordCount"),
start = g.Arg(3, "start"), length = g.Arg(4, "length");
Operand text = g.Arg("text"), begin = g.Arg("begin"), wordCount = g.Arg("wordCount"),
start = g.Arg("start"), length = g.Arg("length");

Operand end = g.Local(text.ArrayLength());
Operand count = g.Local(0);
Expand Down Expand Up @@ -103,11 +107,11 @@ public static void GenIndexedProperty(AssemblyGen ag)
}

// Indexer to get and set words of the containing document:
PropertyGen Item = WordCollection.Public.Indexer(typeof(string), typeof(int));
PropertyGen Item = WordCollection.Public.Indexer(typeof(string)).Index(typeof(int), "index");
{
g = Item.Getter();
{
Operand index = g.Arg(0, "index");
Operand index = g.Arg("index");

Operand start = g.Local(0), length = g.Local(0);

Expand All @@ -123,7 +127,7 @@ public static void GenIndexedProperty(AssemblyGen ag)
}
g = Item.Setter();
{
Operand index = g.Arg(0, "index");
Operand index = g.Arg("index");
Operand value = g.PropertyValue();

Operand start = g.Local(0), length = g.Local(0);
Expand Down Expand Up @@ -185,21 +189,21 @@ public static void GenIndexedProperty(AssemblyGen ag)
FieldGen document = CharacterCollection.ReadOnly.Field(Document, "document"); // The containing document
Operand document_TextArray = document.Field("TextArray");

g = CharacterCollection.Internal.Constructor(Document);
g = CharacterCollection.Internal.Constructor().Parameter(Document, "d");
{
g.Assign(document, g.Arg(0, "d"));
g.Assign(document, g.Arg("d"));
}

// Indexer to get and set characters in the containing document:
PropertyGen Item = CharacterCollection.Public.Indexer(typeof(char), typeof(int));
PropertyGen Item = CharacterCollection.Public.Indexer(typeof(char)).Index(typeof(int), "index");
{
g = Item.Getter();
{
g.Return(document_TextArray[g.Arg(0, "index")]);
g.Return(document_TextArray[g.Arg("index")]);
}
g = Item.Setter();
{
g.Assign(document_TextArray[g.Arg(0, "index")], g.PropertyValue());
g.Assign(document_TextArray[g.Arg("index")], g.PropertyValue());
}
}

Expand All @@ -215,9 +219,9 @@ public static void GenIndexedProperty(AssemblyGen ag)
FieldGen Words = Document.Public.ReadOnly.Field(WordCollection, "Words");
FieldGen Characters = Document.Public.ReadOnly.Field(CharacterCollection, "Characters");

g = Document.Public.Constructor(typeof(string));
g = Document.Public.Constructor().Parameter(typeof(string), "initialText");
{
g.Assign(TextArray, g.Arg(0, "initialText").Invoke("ToCharArray"));
g.Assign(TextArray, g.Arg("initialText").Invoke("ToCharArray"));
g.Assign(Words, Exp.New(WordCollection, g.This()));
g.Assign(Characters, Exp.New(CharacterCollection, g.This()));
}
Expand Down
Loading

0 comments on commit 3d64355

Please sign in to comment.