public static void GenBreakContinue(AssemblyGen ag) { CodeGen g = ag.Class("Test").Static.Void("Main"); g.WriteLine("Break test:"); Operand i = g.Local(); g.For(i.Assign(1), i <= 100, i.Increment()); { g.If(i == 5); { g.Break(); } g.End(); g.WriteLine(i); } g.End(); g.WriteLine("Continue test:"); g.For(i.Assign(1), i <= 10, i.Increment()); { g.If(i > 3 && i < 8); { g.Continue(); } g.End(); g.WriteLine(i); } g.End(); }
// example based on the MSDN Arrays Sample (arrays.cs) public static void GenArrays(AssemblyGen ag) { TypeGen DeclareArraysSample = ag.Class("DecalreArraysSample"); { CodeGen g = DeclareArraysSample.Public.Static.Void("Main"); { // Single-dimensional array Operand numbers = g.Local(Exp.NewArray <int>(5)); // Multidimensional array Operand names = g.Local(Exp.NewArray <string>(5, 4)); // Array-of-arrays (jagged array) Operand scores = g.Local(Exp.NewArray <byte[]>(5)); // Create the jagged array Operand i = g.Local(); g.For(i.Assign(0), i < scores.ArrayLength(), i.Increment()); { g.Assign(scores[i], Exp.NewArray <byte>(i + 3)); } g.End(); // Print length of each row g.For(i.Assign(0), i < scores.ArrayLength(), i.Increment()); { g.WriteLine("Length of row {0} is {1}", i, scores[i].ArrayLength()); } g.End(); } } }
void EmitRead_CreateInstance(SerializerCodeGen g, Local valueArr, Local[] lengths, Operand optionalOutOldFirstDimLength, Local outResult) { using (g.ctx.StartDebugBlockAuto(this)) { if (AppendToCollection) { g.If(valueArr.AsOperand != null); { g.AssignAdd(lengths[0], optionalOutOldFirstDimLength.Assign(valueArr.AsOperand.Invoke("GetLength", 0))); } g.End(); } g.Assign(outResult, g.ExpressionFactory.NewArray(_itemType, lengths.Select(l => (Operand)l.AsOperand).ToArray())); g.Reader.NoteObject(outResult); if (AppendToCollection) { g.If(valueArr.AsOperand != null && optionalOutOldFirstDimLength > 0); { g.Invoke(typeof(Array), "Copy", valueArr, outResult, valueArr.AsOperand.Property("Length")); } g.End(); } } }
public static void GenHello3(AssemblyGen ag) { TypeGen Hello3 = ag.Public.Class("Hello3"); { CodeGen g = Hello3.Public.Static.Method(typeof(void), "Main").Parameter(typeof(string[]), "args"); { Operand args = g.Arg("args"); g.WriteLine("Hello, World!"); 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()); { g.WriteLine("{0}", args[i]); } g.End(); } } }
public static void GenIndexer(AssemblyGen ag) { // Class to provide access to a large file // as if it were a byte array. TypeGen FileByteArray = ag.Public.Class("FileByteArray"); { FieldGen stream = FileByteArray.Field <Stream>("stream"); // Holds the underlying stream // used to access the file. // Create a new FileByteArray encapsulating a particular file. CodeGen g = FileByteArray.Public.Constructor().Parameter <string>("fileName"); { g.Assign(stream, Exp.New <FileStream>(g.Arg("fileName"), FileMode.Open)); } // Close the stream. This should be the last thing done // when you are finished. g = FileByteArray.Public.Void("Close"); { g.Invoke(stream, "Close"); g.Assign(stream, null); } // Indexer to provide read/write access to the file. PropertyGen Item = FileByteArray.Public.Indexer <byte>().Index <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 <byte>(1)); 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 <byte>(g.PropertyValue())); 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 <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.Void("Main").Parameter <string[]>("args"); { Operand args = g.Arg("args"); // Check for arguments. g.If(args.ArrayLength() != 1); { g.WriteLine("Usage : Indexer <filename>"); g.Return(); } g.End(); // Check for file existence g.If(!Static.Invoke(typeof(File), "Exists", args[0])); { g.WriteLine("File " + args[0] + " not found."); g.Return(); } g.End(); Operand file = g.Local(Exp.New(FileByteArray, args[0])); Operand len = g.Local(file.Property("Length")); // Swap bytes in the file to reverse it. Operand i = g.Local <long>(); g.For(i.Assign(0), i < len / 2, i.Increment()); { Operand t = g.Local(); // Note that indexing the "file" variable invokes the // indexer on the FileByteStream class, which reads // and writes the bytes in the file. g.Assign(t, file[i]); g.Assign(file[i], file[len - i - 1]); g.Assign(file[len - i - 1], t); } g.End(); g.Invoke(file, "Close"); } } }
// example based on the MSDN Indexed Properties Sample (indexedproperty.cs) public static void GenIndexedProperty(AssemblyGen ag) { CodeGen g; TypeGen Document = ag.Public.Class("Document"); { FieldGen TextArray = Document.Private.Field(typeof(char[]), "TextArray"); // The text of the document. // Type allowing the document to be viewed like an array of words: TypeGen WordCollection = Document.Public.Class("WordCollection"); { 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().Parameter(Document, "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: 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("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); Operand inWord = g.Local(-1); g.Assign(start, length.Assign(0)); Operand i = g.Local(); g.For(i.Assign(begin), i <= end, i.Increment()); { Operand isLetter = g.Local(i < end && Static.Invoke(typeof(char), "IsLetterOrDigit", text[i])); g.If(inWord >= 0); { g.If(!isLetter); { g.If(count.PostIncrement() == wordCount); { g.Assign(start, inWord); g.Assign(length, i - inWord); g.Return(true); } g.End(); g.Assign(inWord, -1); } g.End(); } g.Else(); { g.If(isLetter); { g.Assign(inWord, i); } g.End(); } g.End(); } g.End(); g.Return(false); } // Indexer to get and set words of the containing document: PropertyGen Item = WordCollection.Public.Indexer(typeof(string)).Index(typeof(int), "index"); { g = Item.Getter(); { Operand index = g.Arg("index"); Operand start = g.Local(0), length = g.Local(0); g.If(g.This().Invoke("GetWord", document_TextArray, 0, index, start.Ref(), length.Ref())); { g.Return(Exp.New(typeof(string), document_TextArray, start, length)); } g.Else(); { g.Throw(Exp.New(typeof(IndexOutOfRangeException))); } g.End(); } g = Item.Setter(); { Operand index = g.Arg("index"); Operand value = g.PropertyValue(); Operand start = g.Local(0), length = g.Local(0); g.If(g.This().Invoke("GetWord", document_TextArray, 0, index, start.Ref(), length.Ref())); { // Replace the word at start/length with the // string "value": g.If(length == value.Property("Length")); { g.Invoke(typeof(Array), "Copy", value.Invoke("ToCharArray"), 0, document_TextArray, start, length); } g.Else(); { Operand newText = g.Local(Exp.NewArray(typeof(char), document_TextArray.ArrayLength() + value.Property("Length") - length)); g.Invoke(typeof(Array), "Copy", document_TextArray, 0, newText, 0, start); g.Invoke(typeof(Array), "Copy", value.Invoke("ToCharArray"), 0, newText, start, value.Property("Length")); g.Invoke(typeof(Array), "Copy", document_TextArray, start + length, newText, start + value.Property("Length"), document_TextArray.ArrayLength() - start - length); g.Assign(document_TextArray, newText); } g.End(); } g.Else(); { g.Throw(Exp.New(typeof(IndexOutOfRangeException))); } g.End(); } } // Get the count of words in the containing document: g = WordCollection.Public.Property(typeof(int), "Count").Getter(); { Operand count = g.Local(0), start = g.Local(0), length = g.Local(0); g.While(g.This().Invoke("GetWord", document_TextArray, start + length, 0, start.Ref(), length.Ref())); { g.Increment(count); } g.End(); g.Return(count); } } // Type allowing the document to be viewed like an "array" // of characters: TypeGen CharacterCollection = Document.Public.Class("CharacterCollection"); { FieldGen document = CharacterCollection.ReadOnly.Field(Document, "document"); // The containing document Operand document_TextArray = document.Field("TextArray"); g = CharacterCollection.Internal.Constructor().Parameter(Document, "d"); { g.Assign(document, g.Arg("d")); } // Indexer to get and set characters in the containing document: PropertyGen Item = CharacterCollection.Public.Indexer(typeof(char)).Index(typeof(int), "index"); { g = Item.Getter(); { g.Return(document_TextArray[g.Arg("index")]); } g = Item.Setter(); { g.Assign(document_TextArray[g.Arg("index")], g.PropertyValue()); } } // Get the count of characters in the containing document: g = CharacterCollection.Public.Property(typeof(int), "Count").Getter(); { g.Return(document_TextArray.ArrayLength()); } } // Because the types of the fields have indexers, // these fields appear as "indexed properties": FieldGen Words = Document.Public.ReadOnly.Field(WordCollection, "Words"); FieldGen Characters = Document.Public.ReadOnly.Field(CharacterCollection, "Characters"); g = Document.Public.Constructor().Parameter(typeof(string), "initialText"); { g.Assign(TextArray, g.Arg("initialText").Invoke("ToCharArray")); g.Assign(Words, Exp.New(WordCollection, g.This())); g.Assign(Characters, Exp.New(CharacterCollection, g.This())); } g = Document.Public.Property(typeof(string), "Text").Getter(); { g.Return(Exp.New(typeof(string), TextArray)); } } TypeGen Test = ag.Class("Test"); { g = Test.Public.Static.Method(typeof(void), "Main"); { Operand d = g.Local(Exp.New(Document, "peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?")); // Change word "peter" to "penelope": Operand i = g.Local(); g.For(i.Assign(0), i < d.Field("Words").Property("Count"), i.Increment()); { g.If(d.Field("Words")[i] == "peter"); { g.Assign(d.Field("Words")[i], "penelope"); } g.End(); } g.End(); // Change character "p" to "P" g.For(i.Assign(0), i < d.Field("Characters").Property("Count"), i.Increment()); { g.If(d.Field("Characters")[i] == 'p'); { g.Assign(d.Field("Characters")[i], 'P'); } g.End(); } g.End(); g.WriteLine(d.Property("Text")); } } }
public Operand AssignOperandFromOperator(string oper, Operand lval, Operand rval, bool scoped) { return(oper == "=" ? lval.Assign(rval, scoped) : new Operand()); }