Ragel State Machine Compiler

Ragel is a State Machine Compiler that supports generating code from Ragel’s regular expressions. Ragel provides code generation for C, C++, Objective-C, D, Java, and Ruby. Regular expressions and finite automata can be used in protocol analysis, data parsing, lexical analysis, and input validation. Implementing Ragel’s C code is very easy. Here is an atoi implementation for C’s standard library. It is several times faster than C standard library’s implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Convert a string to an integer.
 */
 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
%%{
	machine atoi;
	write data;
}%%
 
long long atoi( char *str )
{
	char *p = str, *pe = str + strlen( str );
	int cs;
	long long val = 0;
	bool neg = false;
 
	%%{
		action see_neg {
			neg = true;
		}
 
		action add_digit {
			val = val * 10 + (fc - '0');
		}
 
		main :=
			( '-'@see_neg | '+' )? ( digit @add_digit )+
			'\n';
 
		# Initialize and execute.
		write init;
		write exec;
	}%%
 
	if ( neg )
		val = -1 * val;
 
	if ( cs < atoi_first_final )
		fprintf( stderr, "atoi: there was an error\n" );
 
	return val;
};
 
#define BUFSIZE 1024
 
int main()
{
	char buf[BUFSIZE];
	while ( fgets( buf, sizeof(buf), stdin ) != 0 ) {
		long long value = atoi( buf );
		printf( "%lld\n", value );
	}
	return 0;
}

What OO is and not

This is a small snippet in defense for OO, against Why OO Sucks.

When we talk about Object-Oriented something, we may discuss in several different aspect, such as Object-Oriented Analysis (OOA), Object-Oriented Design (OOD), or Object-Oriented Programming (OOP). OO is such an overwhelming adjective to emphasize the Object-ed way of thinking. Briefly speaking, the Object-Oriented way is to model the world with objects, including states and behaviours. A real-world entity has data and the ability to interact with itself or other objects. Such an entity can be abstracted using a set of states to represent its data, and a set of methods to model its interactions. An object will also maintain something invariant. Cats have tail. Dogs have tail. Cats do eat. Dogs do eat. They are all animals. The invariant in an object determines what class it belongs to.

OOP is a programming language derivative of the OO principle. Like other programming language paradigms, OOP is both the thinking process of creating executable code (such as object files and Java classes) and the management process of assembling executable parts (code reuse, linking). In a pure OOP environment, these two processes are fully object-oriented. Other paradigms include procedural programming paradigm, functional programming paradigm, logic programming paradigm, etc. Procedural paradigm is a flow-based model with input, buffer, data manipulators, and output. Functional paradigm is a to model everything into functions as in mathematics, therefore, every entity in FP is a function. These paradigms are just different approaches defined in the same problem scope. For average people, the Object-Oriented approach is heretofore the best way to describe the world to the computer, as speaking to human beings. Mathematicians may favour in functions. People with business education may be fond of flow charts.

As we can speak English, Chinese, French, Italian, or even Greek or Latin, we do understand different languages. However, the most efficient way to convey our thoughts is our mother tongue. OO is good enough to be English, although it may not be your mother tongue.

First experience on GWT

My last project involves GWT, Google Web Toolkit, which is a developer’s killer app to create Javascript-based web applications. GWT is more a presentation-layer SDK. While it involves some server aspects, such as a RPC interface to integrate with the client-side communication, a larger portion of GWT’s user API includes a Javascript runtime emulation layer of Java core API, a set of DOM manipulation API, and a library of widgets. Two GWT’s most powerful functions are GWT’s hosted mode, which enables us to debug GWT apps in a Java environment, and GWT’s Java-Javascript compiler, which translates Java source code into Javascript code, therefore, the production piece of GWT apps can run entirely in the browser. GWT will deal with most browser-compatibility issues. Developers can concentrate on developing good apps, instead of fighting with Firefox-IE-Safari-Opera battles.

Developing in GWT is very similar to writing Javascript apps with pure-Javascript libraries, except for GWT’s better capability of debugging, type-safe grammar checking, and a small portion of JDK. GWT utilizes HTML and CSS to manage layouts and styles. It can also programmatically use various layout managers (panels) to place widgets that represent different kinds of UI elements on a web page.

 

Course Selector

Course Selector

This is an example application I have been working. GWT uses a DockPanel to divide the page into five locations, the north (1 & 2), the west (1 & 2), the centre, the east (1 & 2), and the south (1 & 2). These locations may include one single widget respectively. However, Panels can be added to contain more than one widget. Panels are used everywhere in GWT. A widget can be decorated by placing it into a DecoratorPanel. Pop-up contents can be wrapped into a PopupPanel. Vertically or horizontally distributed widgets can be placed into a VerticalPanel or a HorizontalPanel respectively.

The following series of articles will discuss the development of this example app.

The example app’s source repository is located at: http://github.com/upei/calendar-app/

This website uses a Hackadelic PlugIn, Hackadelic Sliding Notes 1.6.4.

Supported by Webinit Consulting