Posts tagged with 'prog'

Instiki

Today and tomorrow, I'm on holiday from work. After having a pretty lazy weekend, I've decided to make my short holiday productive. I've installed Common Lisp and Ruby on Rails on my iBook, and have been playing around with Lisp, getting to know its syntax.

I've been cleaning up various directories full of text files on my iBook and have come to the conclusion that I have so many notes scattered around the place that I'm probably losing track of some things. The solution? A wiki. I'm playing with Instiki. It seems to meet my requirements, and was pretty simple to set up on OS X. Here are the steps I took:

  1. Installing Ruby 1.8.5 from here
  2. Installing rubygems from here
  3. sudo gem install rails --include-dependencies
  4. sudo gem install sqlite3-ruby --include-dependencies
  5. unzip instiki 0.12.0
  6. Run ./instiki

Then, pointing a browser at http://localhost, you'll be prompted to specify some details about the Wiki, and then be forwarded to the wiki's homepage:

Instiki

tags: apps, devel, lisp, osx, prog, ruby, web

Fast StringReader for .NET

While driving home from work today, I was thinking of how to write an algorithm in C# 2.0 that takes a string representing a multi-line text file, and return the lines, one at a time.

I know Mono's System.IO.StringReader class can do this, but I was thinking there must be a more efficient way of doing it. Here's what I came up with:

 public class LineReader
 {
     private string _source;
 
     public LineReader(string src)
     {
         _source = src;
     }
 
     public IEnumerator GetEnumerator()
     {
         bool finished=false;
         int nextChar=0;
         int readTo;
         string nextLine;
         while(!finished) {
             readTo = _source.IndexOf ('\n', nextChar);
             if (readTo==-1){
                 readTo=_source.Length-1;
                 finished=true;
                 if (readTo<nextChar){
                     yield break;
                 }
             }
             nextLine = _source.Substring(nextChar,readTo-nextChar);
             nextChar = readTo+1;
             yield return nextLine;
         }
     }
 }
 

Timing tests show that with the following test harness running on my 1.33GHz iBook G4 running Mono 1.2.5, parsing a 1000 line text file 1000 times takes 2.7 seconds on average using this algorithm and 3.4 seconds on average using StringReader.

 public static void Main(string[] args)
 {
     StreamReader sr = new StreamReader("sample.text");
     string test = sr.ReadToEnd();
     System.DateTime start = DateTime.Now;
     for (int i=0;i<1000;i++){
         LineReader lineReader = new LineReader(test);
         foreach (string s in lineReader){
         }
     }
     System.DateTime finish = DateTime.Now;
     double seconds = (((double)finish.Ticks) - ((double)start.Ticks)) / 10000000;
     Console.WriteLine("{0} seconds",seconds);
 }
 

Yes, StringReader handles both LF and CRLF line endings, and has a PushBack method to allow jumping back to the previous line, but for pure speed of parsing standard text with LF line endings, this algorithm seems to be around 20% faster than StringReader, and 30% faster than using Split to turn large string into an array of one string per line.

Update on 14th November 2007:
I ran the same tests on Windows XP with Microsoft .NET 2.0, on a dual core 3GHz PC, and produced this graph which compares various ways of splitting the large string into one string per line:

Graph

kick it on DotNetKicks.com

Wildcat BZR Source Available

The source of Wildcat BZR is now available on Launchpad and there is a homepage which has a list of features, screenshots and a roadmap. It has been tested on Windows XP and OS X. It should also work on Linux. The external diff and merge support is somewhat limited at the moment. Everything is explained on the project page.

Wildcat BZR on Windows

I have re-worked the algorithm for reading the directory structure and Bazaar status of files in Wildcat BZR, and verified that it works correctly on Windows XP:

Wildcat BZR

I started writing a Bazaar client this weekend. After two days, here's what I've got:

Currently supported features are:

  • Init
  • Commit
  • Add
  • Pull
  • Merge
  • Push
  • Commit
  • Merge using an external merge tool
  • I've only tested it on OS X at the moment, but it should be able to work on any platform that supports wxWidgets (Linux, Windows, Mac, etc). There are a few things I want to clean up in the application before I release a 0.1 version on the Internet:

    • Dialogs like the commit and add ones need their layout made nicer
    • At the moment, only Apple's FileMerge is supported as an external merge tool. I plan on getting SourceGear's DiffMerge working as well.
    • I need to add diffing and history support
    • I need to change the way it walks the directory tree, as it's slow with large projects

Wildcat COBOL 0.1.13 Introduces Sequential File Support

It's that time again - a new version of the Wildcat COBOL Compiler for Mono and .NET is out. Version 0.1.13 introduces experimental support for reading sequential data files, and making use of level 88 data definitions. I'll explain these advances in some more detail...

Consider the following excerpt from the fileread.cbl program which is included in the version 0.1.13 ZIP file (click to enlarge):

example from fileread.cbl

This code opens a file, and reads the first line. This data is stored in the StudentFile record which has been declared in the FILE SECTION of the DATA DIVISION. The code then loops until EndOfStudentFile evaulates to be true. EndOfStudentFile was declared as a level 88 member of the StudentFile record. Level 88 is special in that it can be used to store and test for certain conditions. Here, it is used to indicate whether or not the end of the sequential data file has been reached.

Inside the loop, the DISPLAY statement prints out the values of some parts of the StudentFile record, then the next record is read from the file, until the end of the file has been reached. When the end is reached, EndOfStudentFile is set to true, and the loop exits.

The READ statement can have "AT END" and "NOT AT END" sections inside it, which specify code to execute when each of those conditions is true.

This diagram shows the logic the compiler uses when compiling READ statements:

The code used in this example is based on Introduction to Sequential Files from the University of Limerick.

HIGH VALUES

Tonight I started implementing file I/O in the Wildcat COBOL Compiler for .NET. One of the examples I was testing my implementation against used the HIGH-VALUES keyword. I decided to implement this before continuing with file I/O as it provided a way for this particular example to deal with a boolean value. I derived a test program from the example, and tested in both the Wildcat COBOL compiler and in OpenCOBOL. Thankfully, both produced the same result:

Wildcat COBOL vs OpenCOBOL

This functionality will be available in version 0.1.13 of the Wildcat COBOL compiler.

tags: cobol, prog

I CAN HAS A .NET COMPILR?

A colleague pointed me in the direction of lolcode - a programming language based on the lolcats. I found many compilers/interpreters on the website, and the one that really caught my eye was LOLCode.NET - a lolcode compiler for Microsoft's .NET runtime.

I just *had* to try that on Mono. Here's a screenshot of it working perfectly under Mono 1.2.4 on Mac OS X:

LOLCode.NET running under Mono on OS X

Update:
You can get the source code via SVN by following the instructions here. If you're trying to compile it on a Unix-like system (eg Linux, OS X), you'll need to place this Makefile in your lolcode-dot-net directory, and run make.

kick it on DotNetKicks.com

Serious bug in String.IsNullOrEmpty in the .NET 2.0 CLR

I've read a few reports which have mentioned a problem with .NET's String.IsNullOrEmpty static method where under certain circumstances it can cause a null reference exception at runtime. Bill at MSMVPS.COM has as article with some sample source code that can be used to test this problem.

I've done some experimentation and found that it only occurs when a program is compiler in release mode and run outside of the development environment on a PC with .NET 2.0 installed. Debug builds are not affected and neither are computers running Mono.

It looks like this may be a bug in the optimizations performed by Microsoft's .NET CLR or JIT on Windows. Microsoft have posted an article addressing this issue and have stated that it will be fixed in the Orcas CLR beta 1.

Needless to say, the .NET developer community are shocked that a flaw in the CLR with such serious consequences as this will not be fixed in the current version, but instead people will need to upgrade to a new and relatively untested technology preview.

kick it on DotNetKicks.com

More COBOL Updates

Version 0.1.12 of the Wildcat COBOL compiler is out now, with bug fixes in the handling of complex IF statements, and support for all relation operators.

Pieter Baele has been working on a COBOL syntax highlighting bundle for TextMate and I have started putting together a COBOL syntax highlighting definition file for Smultron.

COBOL Syntax Highlighting on OS X

To make Smultron use this syntax highlighting, you will need to put this file into /Applications/Smultron.app/Contents/Resources/Syntax Definitions and add the following text into /Applications/Smultron.app/Contents/Resources/SyntaxDefinitions.plist:

<dict>
  <key>name</key>
  <string>COBOL</string>
  <key>file</key>
  <string>cobol</string>
  <key>extensions</key>
  <string>cbl cob</string>
</dict>

tags: apps, cobol, osx, prog

Wildcat COBOL 0.1.10 Released

This release now includes support for use of AND and OR in IF statements. The amount of language features remaining to be implemented before the compiler is actually useful is getting smaller :-)

As always, the latest release if available here.

The release includes updates to the NUnit tests for testing if statements like this:

Unit testing an IF statement in COBOL

Writing the rest of the IF statement unit tests will be boring, but will ensure that future changes don't break anything. I also plan on making sure the rest of the NUnit Assert methods work, as I've not used many of them yet in COBOL.

Wildcat COBOL Cocoa Application Support

Today I compiled and ran my first Cocoa# application in COBOL - probably the first time anyone's ever had a working Cocoa# application written in COBOL.

The Wildcat COBOL Compiler now supports referencing packages like mcs does, which made this strange mix of technologies possible. It feels strange running a COBOL .NET GUI application on OS X.

The updated compiler and source is available here and the example is available here. Here's a screenshot of my Cocoa# test application running:

Cocoa# Test App

Compiling if Statements

I hadn't realised that explaining how a COBOL "IF x AND y" statement translates into CIL code differs from a simple "IF x" one from the compiler's point of view could be the subject of a multi-thousand word essay. Sitting at 500 words of bullet points and code snippets here, with no solution yet. This is *NOT* over-documenting - it's barely touching on the complexities of implementing conditional statements in a compiler for a high-level language. I just know that the solution will be *MUCH* simpler than the explanation though.

tags: cobol, prog

Towers of Hanoi with Wildcat COBOL .NET

Tonight I got the Towers of Hanoi from kernelthread.com compiling with the Wildcat COBOL .NET compiler. I've packaged up version 0.1.6 of the compiler which can be downloaded from here, and the release notes are here.

NUnit Tests with COBOL .NET

After adding experimental support for using .NET classes and attributes in COBOL source code, and some playing around with System.Reflection, I was able to get the Wildcat COBOL Compiler to compile a program that contains an NUnit test, and run it from NUnit's command line utility.

More information about running NUnit tests for COBOL programs, including example source code and GUI screenshots, is available here.

NUnit with COBOL .NET

I had to add a couple of new things to the COBOL syntax, but kept them in line with what Fujitsu have already done in their NETCOBOL implementation for .NET.

Here is the program I wrote - you can see the simple NUnit test at the end:

000010 IDENTIFICATION DIVISION.
000020 PROGRAM-ID. FRAMEWORK-TEST.
000030 AUTHOR. SANDY DUNLOP.
000040*Testing .NET COBOL Extensions
000050
000060 ENVIRONMENT DIVISION.
000070 CONFIGURATION SECTION.
000080 ATTRIBUTES "TestFixture".
000090 REPOSITORY.
000100     CLASS SYS-STRING AS "System.String"
000125     CLASS SYS-CONSOLE AS "System.Console"
000136     CLASS NUNIT-ASSERT AS "NUnit.Framework.Assert"
000140 DATA DIVISION.
000150 WORKING-STORAGE SECTION.
000160 77 NET-STRING OBJECT REFERENCE SYS-STRING.
000170
000180 PROCEDURE DIVISION.
000190 MAIN-PARAGRAPH.
000200     MOVE "Test Prog" TO NET-STRING
000210     INVOKE SYS-CONSOLE "WriteLine"
000220         USING BY VALUE NET-STRING
000210     STOP RUN.
000230 TEST-PARAGRAPH WITH ATTRIBUTES "Test".
000240     MOVE "teststring" TO NET-STRING
000250     INVOKE NUNIT-ASSERT "AreEqual"
000260         USING BY VALUE "teststring"
000270         USING BY VALUE NET-STRING
000280 END PROGRAM FRAMEWORK-TEST.

I plan on writing some NUnit tests for the compiler now, and making it all available online over the coming week.

A VB Programmer's Rant About C Sharp

One thing I've heard VB programmers moan about with C# is the compiler error message they often see:

Parser.cs(398,4): error CS1002: Expecting `;' Compilation failed: 1 error(s), 0 warnings make: *** [Parser.dll] Error 1

I've heard people say "Why can't it just add a semi-colon if it knows it should be there?"

Here's one reason: because there shouldn't be one - the compiler didn't expect the user to be dumb enough to miss out a bracket from their code, and got confused when it read the open brace on the next line...

Missing Bracket

tags: prog

COBOL .Net Compiling with Types

In my limited spare time, I've managed to add integer and string types to my COBOL compiler for .Net, and also add the ability to type stuff in at the command line. Numbers, strings and things typed in at the command line can now be set as the values of existing variables. The next step will include floating point numbers, the COBOL equivalent of structs, and maybe some maths.

Click the image to watch the flash movie...

Programming Multiple Precision maths and Encryption

Since last Tuesday afternoon, I've been learning about and implementing a secure key exchange and encryption algorithm, using Blowfish and the Diffie-Hellman key exchange. I've been doing this in C, using the freely available BigDigits library for dealing with large prime numbers. I've found the BigDigits library to be very handy, and it seems to be quite efficient. I've written my own functions to wrap up Blowfish in a way that character buffers can be passed from one machine to another over the network, with encryption that's transparent to the application (at the socket level). Next, I'd like to get the same thing working in C#, but I discovered today that the C# library provided be the people who wrote BigDigits actually just imports Win32 DLL functions and presents them in a nice .Net-friendly way. I'm intending my code to run on Mono as well as Microsoft .Net, so I think writing the algorithms in pure C# is the approach I will be taking. |