Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, November 09, 2009

Regex for Tracing

I've been working on SwinGame, and one of the tasks has been to add tracing code to the core to make it easier to debug problems. This involves adding TraceEnter and Trace exit calls to each procedure in the code.

To avoid having to do this by hand I am using a Regular Expression in TextMate to find procedures in the code, and replace them with a version that includes the injected trace enter and exit code. The regex I am using is shown below. It will work with a maximum depth of four begin/end groups... so its not completely automatic, but is a good start.

(function |procedure )(.*?)(;(\n|.)*?begin)((\n|(begin(\n|(begin(\n|(begin(\n|(begin(\n|.)*?end)|.)*?end)|.)*?end)|.)*?end)|.)*?)(\n end;)
This is then replaced using the following replace text. The $1 values are the groups that are matched in the regular expression. This enables me to place the procedure name, parameters and return type in the traced output ($2). It also ensures that all of the code from the program is output in the right spot.
$1$2$3
{$IFDEF TRACE}
TraceEnter('sgCamera', '$2', '');
{$ENDIF}
$5
{$IFDEF TRACE}
TraceExit('sgCamera', '$2', '');
{$ENDIF}
$15
This regular expression converts the following code...
function VectorFrom(x, y: Single): Vector; overload;
begin
result := VectorFrom(x, y, false);
end;
into this code which now has the tracing details added.
function VectorFrom(x, y: Single): Vector; overload;
begin
{$IFDEF TRACE}
TraceEnter('sgCamera', 'VectorFrom(x, y: Single): Vector', '');
{$ENDIF}

result := VectorFrom(x, y, false);

{$IFDEF TRACE}
TraceExit('sgCamera', 'VectorFrom(x, y: Single): Vector', '');
{$ENDIF}
end;
This should save me some time... though a real parser trace injector would be great!

Monday, December 01, 2008

Plans for APS

Next year I will be teaching Algorithmic Problem Solving (APS) in first semester, a subject that I have developed and worked on over a few years. At the start of this year I trialled portfolio assessment with some success in this subject, though I improved on this in semester 2 with Object Oriented Programming. So what am I going to do differently next year?

Greater focus on the portfolio assessment: The portfolio assessment was good in this subject last year, but the focus can be even better. I found that this helped the students focus on what was important in OOP, the intended learning outcomes. My plan is to provide assessed learning activities during the semester, the resulting work will then be included in the students' portfolios. 

Simpler book: The first few times I ran this subject we used the language manual that comes with the language we are using. Its very dry, and not presented in an order that is useful for learning. This year I tried providing my own details, but didn't really get that right. So next year I will be providing a new book.

The book is what I am focusing on now. This will use a syntax based approach to learning the language, along with clear descriptions and illustrations to help students understand the programming abstractions. At this stage I have the following sections for the book:
  1. Programs - introduces the Program abstraction and a large number of new "programming language" terms such as identifier, keyword, procedure, procedure call, expression, literal, etc.
  2. Creating Procedures - focuses on creating your own procedures. 
  3. Storing and Using data - variables, and the assignment statement.
  4. Passing Data Around - introduces parameters (both in, out, and in/out).
  5. Calculating Values - covers functions and function calls.
  6. External Libraries - introduces units, shows how to use external units, and provides an example function from the SysUtils unit.
  7. User Input - Up to this stage we will have been using literal values, but now all of the framework is in place to understand user input. This includes ReadLn, as well as reading command line arguments.
  8. Branching - Indicates the change from programming "infrastructure" to control flow, and algorithm design.
  9. Looping - For, while, repeat, etc...
  10. Data abstractions - now the focus changes to the programming abstractions for data. This will include arrays, records and pointers.
  11. Creating Libraries - Lastly onto creating your own programming libraries
My idea for this is to have each section as self contained as possible. Knowledge will be built up without the need for any "magic". This is the reason for delaying user input... this requires functions and out parameters, with library use being necessary if you want to convert values from strings to integers for example.

These 11 chapters will be covered in the first five weeks of the semester. Week one will cover 1 & 2, week two will be 3 & 4, week three on 5, 6 & 7, week four on 8 & 9, and week five on 10 & 11. This leaves seven weeks of the semester to learn about how these abstractions are used to create programs. 

Along with this I am aiming to keep the explanation of each concept or part of the syntax down to a single page, where possible. This is enough room to convey the important aspects, but not too much that the end result is overwhelming. It should also help to create a useful reference, where each idea has its own page. So far I am up to chapter 5 which start at page 39, so there isn't too may pages per chapter.

Let me know what you think. I'll post up some chapters later...


Wednesday, March 05, 2008

Python is intriguing...

I share an office with Clinton who has lately become somewhat of a Python convert. Today I started to play with Python for the first time. Unlike "normal" people I didn't start with Python in Python... I started by embedding it within a Pascal program. Embedding Python within Pascal was really simple - I am totally amazed! With less than 20 lines of Pascal code you can embed a Python interpreter and have it load and run python modules.

The code for this will be placed on Google Code once I have it a little farther along.

Why am I doing this? We are looking at providing the ability to run Scripts within SwinGame, its looking like we should have something interesting soon!

Thursday, May 10, 2007

To Thread or Not To Thread

Today I have been asked a few threading questions from students studying at Swinburne. Usually these were about the "How to" do something with threads, when I think the real question was should threads be used at all?

From what I know these students are developing a torrent like application and network protocol. The issue is that they need to accept data from a number of peers at the same time. Initially this appears to suggest the need for threads... but are they really needed?

In this case I think not. The project indicate potentially thousands of peers... a threaded client with one thread per client will kill the machine with context switches before any real work can be done. So what is the answer? Non-Blocking IO.

With Non-Blocking IO you dont need to block, waiting for data to arrive from a peer. Rather you keep a list of connected peers and then loop through those that have data currently available. This can all be performed on a single thread, giving good performance and throughput.

Java offers a number a library of non-blocking IO utilities. Have a look at the NBTimeServer.java example. This shows a time server that accepts connections using non-blocking IO. A quick look through the Java API and you will find the SelectableChannel and the SelectionKey class. Using the register methods, and the various SelectionKey options you can loop through only those connections that actually have data waiting to be processed.

There is a good article, with a good source code example at http://tim.owlmountain.com/tutorials/NonBlockingIo.htm though there is an interesting note at the start.
As suggested in the article, you could also look at MINA the Multipurpose Infrastructure for Network Applications. If you use MINA just stick with the basic "getting started" code and build on top of that. There is no real need to worry about fiddling with the thread model. But... check with the subject convener first...

Tuesday, May 08, 2007

Programming 2D Games

Are you interested in programming 2d games? Got a PC, Mac, or linux box? That all you need with the new SwinGame API.

The SwinGame API was developed initially by Daniel Chambers as a Windows API for developing small games. Over the last few weeks I have been modifying the code to extend its features, and to make it cross platform. The latest version is still in testing, but is worth looking at if you are interested in creating a 2D game.

SwinGame features:

  • Window drawing is double buffered
  • Create a Window of any size
  • Load and display images of various formats including bmp, png, and jpg
  • Works with 32 bit images, including alpha blending
  • Load true type fonts
  • Read and write text
  • Draw lines, and shapes
  • Sprite management routines
  • Load and play sounds in various formats including wav, ogg, and mp3
  • Detect collisions between sprites, bitmaps, and areas of the screen
  • Perform pixel level collision detection
  • Vector manipulation routines
  • Use matrices to manipulate game vectors
There are a couple of games in the process of being built by the 1st year PSD students at the moment, and I will make these available for download from SwinBrain as soon as they are complete.

Tuesday, March 27, 2007

Empty your Mind

Today one of the PSD students showed me what he had started for the game he was developing for Algorithmic Problem Solving. He had started on the game yesterday, and it already looks quite impressive. Basically this is going to be a scrolling space arcade game. So far he has the weapon firing in a number of shot combinations. The screenshot below shows the largest fire pattern.



Looks like some of the students are having fun with this assignment. The SwinGameAPI is a real hit, making this possible without having to worry about many complexities.

Keep the games coming...