Friday, December 08, 2017

Spring4D presentation slides and more

Slides and code for my Spring4D presentation are now online on the Presentations page.

And to the participants of the workshop, here's the answer I promised.

If you want to catch all calls to some function while mocking, you can pass in Arg.IsAny (or call some other function of the TArg type, defined in Spring.Mocking.Matching).

I have added an example to the Mocking project.

mockCalc.Setup.Returns(4).When.AddTwo(2);
mockCalc.Setup.Executes(
  function(const callInfo: TCallInfo): TValue
  begin
    lbLog.Items.Add('Don''t know how to handle ' + callInfo.Method.Name +
     
'(' + IntToStr(callInfo.Args[0].AsInteger) + ')');

  end).When.AddTwo(Arg.IsNotIn([2]));

Or you can pass Args.Any to the When function. In that case, the argument passed to AddTwo is ignored.

mockCalc.Setup.Returns(-1).When(Args.Any).AddTwo(0);


Monday, December 04, 2017

Advent of Code

Last few days I'm having great fun solving problems from Advent of Code 2017 page and so does my daughter (with a bit of help from her dad). I'm using Delphi and she Python so that's also a good practice to brush my multilanguage skills ;)

Go ahead, take a look at the problems. Some are simple, some not so much, but they are all quite interesting.

All my solutions are posted to my GpDelphiCode repository so you can look at and criticise my code. Most of the solutions depend on units from my GpDelphiUnits repo so you'll need that one too.


Friday, December 01, 2017

In the middle of winter, Spring comes to Ljubljana

Indeed, it is snowing for the last three days. Not much, but winter is definitely here.

To add some green to the white surroundings we'll spend the next Friday talking about spring. Or, actually Spring. For Delphi. Also known as Spring4D - definitely the best Delphi collection of programming goodies that you can find around.

If you speak Slovenian language (sorry, almost all of the world population), you're welcome to my workshop about that ingenious library. If not, you'll have to find that information somewhere else, sorry. Or you can learn Slovenian and come to Ljubljana. Where else can you listen about Delphi in a session targeted to only 2 millions of humans?

Programerji smo včasih malo leni in iščemo bližnjice. Odlične programe bi radi napisali s kar najmanj kode. Pri tem nam pomaga tudi fleksibilnost jezika Delphi in odlične programerske knjižnice.

Tokrat si bomo ogledali knjižnico Spring4D, ki prinaša množico malih pripomočkov in bljižnic za pametne programerje - od objektov, ki jih ni treba sproščati, do razredov, ki jih ni treba inicializirati in seznamov, po katerih se lahko sprehajamo, jih filtriramo, povezujemo in še in še.

Za nameček bomo podrobneje obdelali še tehniko Dependency Injection in si - bolj za zabavo, kot zares - ogledali še, kako lahko v Delphiju s tehniko odzivnega programiranja ReactiveX dogodke obravnavamo čisto drugače kakor smo navajeni.


Izkoristite priložnost in spoznajte kako lahko hitreje napišete odlične programe. Vabljeni v petek, 8. decembra, ob 9.00 uri, v predavalnico Obrtne zbornice Vič. 


Sunday, November 05, 2017

Writing a Simple DSL Compiler with Delphi [7. AST Compiler]

This article provides a description of an AST compiler used for my toy language project. If you are new to this series, I would recommend to start reading with this post. At least you should read the previous post, Intermezzo, as it explains some parts of the compiler that I won't touch here.

Please note that this article describes an initial implementation of the compiler. If you want to browse the code while reading the article, make sure that you have switched to branch dsl_v1.

In my toy compiler framework, a compiler (or codegen as it is called internally), is a piece of code that implements the ISimpleDSLCodegen interface. This interface exposes only one function, Generate, which takes an abstract syntax tree and converts it into an object implementing an ISimpleDSLProgram interface which allows you to call any function in a compiled program by name.

type
  TParameters = TArray;
  TFunctionCall = reference to function (const parameters: TParameters): integer;
  ISimpleDSLProgram = interface ['{2B93BEE7-EF20-41F4-B599-4C28131D6655}']
    function  Call(const functionName: string; const params: TParameters;       var return: integer): boolean;
   end;

  ISimpleDSLCodegen = interface ['{C359C174-E324-4709-86EF-EE61AFE3B1FD}']
    function Generate(const ast: ISimpleDSLAST;      
      var
runnable: ISimpleDSLProgram): boolean;
  end;

Thursday, October 19, 2017

OmniThreadLibrary 3.07.4 has been released

Bug fixes:

  • TOmniEnumeratorProvider and TOmniValueEnumeratorProvider support dmoPreserveOrder option. Now you can use PreserveOrder modifier on Parallel.ForEach when input is IEnumerable, IEnumerator, TEnumerator, or TEnumerable.
  • Fixed 64-bit issues in DSiWin32, GpLists, GpStringHash, and GpStuff.

New features:
  • Locked.Value is now both readable and writable property.  
  • Moved 'inline' functions around so that they will be inlined more often.
As usual, you can get OmniThreadLibrary from GitHub (3.07.4HEAD), download the zip, install it with GetIt or with Delphinus.

Tuesday, October 17, 2017

Writing a Simple DSL Compiler with Delphi [Intermezzo]

When I was preparing an article about the compiler part of my toy language project, I found out that the concept of wrapping a whole program into a bunch of anonymous functions (what the compiler does) is exceedingly hard to explain. I had therefore prepare a simplified version of the compiler, written for a very simplified language ... and then I couldn't stop and I added an AST, parser, tokenizer, interpreter and all shebang.

The result of all that is a program introduction.dpr, a self-contained console program which contains a complete (almost trivial) language together with the full documentation, written in a Literate Programming style. Simply put - you can read it from top to bottom like a story.

As an intermezzo and to simplify my explanation of the compiler, I'm posting the whole program here, reformatted as a blog post.

Saturday, October 14, 2017

Writing a Simple DSL Compiler with Delphi [6. AST Dumper]

This article provides a description of a testing tool used for my toy language project. If you are new to this series, I would recommend to start reading with this post

Please note that this article describes an initial implementation of the parser. If you want to browse the code while reading the article, make sure that you have switched to branch dsl_v1.

Now that we have a working tokenizer and parser outputting an AST, we can start working on the compiler. Still, it would be great if we can verify whether the parser output (the AST) makes any sense. In other words, we need unit tests.

Writing unit tests for a tree structure is, however, a very tedious operation. In the next post I'll show a test for a tree with only five nodes and it will already be a process one would rather skip. Luckily, we can do something more fun - we can write a code that recreate the original program from an AST.

Tuesday, October 03, 2017

Writing a Simple DSL Compiler with Delphi [5. Framework]

This article provides a description of a compiler framework used in my toy language project. If you are new to this series, I would recommend to start reading with this post.

We have now a working parser that converts a string of code into an abstract syntax tree - AST. It is, however, not yet time to write about the most interesting piece - the compiler - as we should first do some integration and testing.

Friday, September 29, 2017

Writing a Simple DSL Compiler with Delphi [4. Parser]

This article provides a description of a parser used in my toy language project. If you are new to this series, I would recommend to start reading with this post.

Please note that this article describes an initial implementation of the parser. If you want to browse the code while reading the article, make sure that you have switched to branch dsl_v1.

After a short break I'm returning to my "toy compiler" series. This time I'll describe the working of the parser - the part of the code which reads the input (usually in a tokenized form) and generates internal representation of the program (in my case an abstract syntax tree - AST).

The goal of my project was to study the compilation step and parser was just a necessary evil that I had to deal with. That's why it is written in a pretty primitive manner, without using any improvements like a Pratt parser.

Tuesday, September 19, 2017

Delphi and Linux

I'd just like to remind my Slovenian readers that on 28th this month I'll be having a presentation about RAD Studio and Linux in Ljubljana.

As the presentation will be given in Slovenian language, the rest of my post containing the description of the presentation is written in that language, too.

Vljudno vabljeni na delavnico "RAD Studio in Linux", na kateri si bomo ogledali:

  • Kako namestiti Ubuntu v virtualni računalnik.
  • Kaj storiti, ko namestitev nagaja.
  • Kako pripraviti virtualni računalnik in RAD Studio za delo.
  • Kako napisati konzolno aplikacijo za Windows in Linux, ki streže podatke z uporabo tehnologij FireDAC in DataSnap.
  • Kako napisati grafično aplikacijo za Windows, OS X, iOS, Android, ki prikazuje in spreminja podatke na aplikacijskem strežniku iz prejšnjega koraka.
  • Z nekaj sreče pa še: 
    • Kako to grafično aplikacijo pognati na Linuxu in
    • Kako konzolno aplikacijo spremeniti v pravi Linux "service".
Vstop prost, prosimo vas le, da se vnaprej registrirate, da bomo znali pripraviti zadostno količino kave in prigrizkov ;)

Wednesday, September 06, 2017

Autumn is coming ...

... and with it, a whole bunch of events.

First, I'll be going to IBC 2017 in Amsterdam where we are presenting our products in Hall 2. I'll be there from 15th to 18th, so if anybody wants to meet, contact me.

Immediately after that I'm going to Zegrze (just north of Warsaw) to speak at Zlot Programistów Delphi (Delphi programmers convention) on 21st and 22nd. Never been there before, should be great fun.

A week later (28th) I'll be leading a workshop on Delphi and Linux in Ljubljana. (Slovenian readers - you can sign on now.)

To finish it up, I'll present two all-new topics in ITDevCon in Rome on October 11th and 12th.

All that in four weeks. Should be quite an interesting time ;)

Monday, September 04, 2017

Introducing OmniThreadLibrary Core

Yesterday I wanted to use OmniThreadLibrary as a git submodule in a top secret ;) open source project I'm working on and I was a bit shocked when it turned out that newly cloned OmniThreadLibrary folder is a 83 megs in size. Given that I only need it to support my other project and that I won't do any OTL fixing/development in this submodule, that looked a bit excessive.

So I went ahead and created a core branch which contains only the barebones - the OmniThreadLibrary root folder without any subfolders. If you want to compile such submodule, you'll also need the GpDelphiUnits repo. Together they weight measly 3,5 MB, which is a big improvement over the original 80+ megs.

I'll be keeping core in sync with the latest OTL release, not with the HEAD, so it will also provide a stable platform for all depending repositories.

Friday, September 01, 2017

Writing a Simple DSL Compiler with Delphi [3. Tokenizer]

This article provides a description of a tokenizer used in my toy language project. If you are new to this series, I would recommend to start reading with this post.

Please note that this article describes an initial implementation of the tokenizer. If you want to browse the code while reading the article, make sure that you have switched to branch dsl_v1.

With this article I'm moving into the gritty part of the project - the code which reads the source code and turns it into a beautiful abstract syntax tree. In other words, I'll be talking about the parser.

I must admit that I spent as little time on the parser as I could. After all, my main goal was to convert AST into an executable code, not to parse input. Still, one cannot write a compiler without writing a parser, so ... here I am.

Monday, August 28, 2017

Writing a Simple DSL Compiler with Delphi [2. Abstract Syntax Tree]

This article provides a description of an abstract syntax tree used to represent "The Language". If you are new to this series, I would recommend to start reading with this post. 

Please note that this article describes an initial implementation of the AST. If you want to browse the code while reading the article, make sure that you have switched to branch dsl_v1.

An abstract syntax tree (AST) is, simply put, a symbolic representation of the program in a form of a tree.

While the textual representation of a program is great for us, humans, computers have hard time dealing with it. Because of that, a special part of any interpreter/compiler, called parser, reads the input and converts it into a computer-readable format - AST. This tree can then be used for multiple purposes. We can, for example, feed to into an interpreter which will then run the program for us, or we can feed it into a compiler to generate an executable program or cross-compiler to generate an equivalent program in a different programming language.

Friday, August 25, 2017

Writing a Simple DSL Compiler with Delphi [1. The Language]

Part 1: The Language

This article provides an informal definition of a simple language (a.k.a. "The Language") I am writing a compiler for. If you are new to this series, I would recommend to start reading with this post.

Let's start with a simple example which calculates an i-th Fibonacci number.

 fib(i) {
   if i < 3 {
     return 1
   } else {
     return fib(i-2) + fib(i-1)
   }
 }

The code is quite simple. First two numbers in a Fibonacci sequence are 1 and every other Fibonacci number is a sum of previous two in the sequence.

Wednesday, August 23, 2017

Writing a Simple DSL Compiler with Delphi [0. Introduction]

Part 0: Introduction

Some time ago I was listening to the Hanselminutes podcast where a guy described how he wrote a simple interpreter in Go language (YOU should write an interpreter with Thorsten Ball). I was not really interested in writing an interpreter - I did that a long time ago - but a thought crossed my mind. I asked myself whether I can do something better - write a compiler. (Or, rather, a kinda-compiler. You'll see.)

What I wanted to do is to take a small language, parse it, generate an abstract syntax tree, and then convert this into one large anonymous function calling other anonymous functions calling other anonymous functions and so on and so on. It is hard to explain, so let me try using a very simple example.

Tuesday, August 01, 2017

OmniThreadLibrary 3.07.3

TL;DR: Update OmniThreadLibrary now!

A nasty bug was found in the DSiWin32 library. It causes the DSiTimeGetTime64 function to work incorrectly when called from multiple threads at the same time. As this function is central to time measurement in the OmniThreadLibrary, it was essential to release new, fixed version.

This version also contains two small enhancements.

  • SetTimer method now accepts TProc and TProc timer methods.
  • IOmniTask implements method InvokeOnSelf which can be used to schedule anonymous function execution from a task back to self.  

As usual, you can get OmniThreadLibrary from GitHub (3.07.3HEAD), download the zip, install it with GetIt or with Delphinus.

Damn, multithreading is hard!

Thursday, July 06, 2017

OmniThreadLibrary 3.07.2

This is just a small update which adds few helpful methods. Although the change log mentions a potentially breaking change I don't think this will affect anybody. (And if you are not sure, you can enabled backward-compatible behaviour by adding one line to the code.)

I have also (finally) found some time to work on The book. It is now fully up to date (all OmniThreadLibrary features are documented on its whooping 293 pages) and is only missing one or two introductory chapters.

As usual, you can get OmniThreadLibrary from GitHub (3.07.2, HEAD), download the zip, install it with GetIt or with Delphinus.

Change log follows after the break.

Wednesday, May 24, 2017

RAD Studio 10.2 links

This post contains just a bunch of links to online resources that I mentioned on the today’s presentation.

Monday, May 22, 2017

10.2 Tokyo comes to Slovenia

In two days I’ll present RAD Studio 10.2 Tokyo in Ljubljana.

Click here for more information and to register.

Thursday, May 18, 2017

OmniThreadLibrary 3.07.1

This update brings only few small changes. The biggest of them is support for the Delphi 10.2 Tokyo.

You can get it as a zipped download, git checkout, via Delphinus, and via GetIt.

For more information on OmniThreadLibrary, go to www.omnithreadlibrary.com.

Monday, March 20, 2017

Forward record declaration

Forward declaration is not really a new concept. It was already present in the original Wirth Pascal where it allowed the programmer to do one thing and one thing only – call procedure A from procedure B and procedure B from procedure A. Remember – in ye olde times of good old Pascal we had no interfaces, no classes, no units … just procedures and functions.

Because the code tells more than thousand words, here’s an example (still perfectly valid in modern Object Pascal).

procedure ProcA; forward;

procedure ProcB;
begin
  ProcA;
end;

procedure ProcA;
begin
  ProcB;
end;

Friday, February 17, 2017

OmniThreadLibrary 3.07

New year, new release ;) You can get it as a zipped download, git checkout, via Delphinus, and via GetIt.

For more information on OmniThreadLibrary, go to www.omnithreadlibrary.com.

Tuesday, February 07, 2017

OmniThreadLibrary 3.07 beta + plans for OmniThreadLibrary 4.0

It is almost time for a new release! If you’d like to see what 3.07 will contain – or if you just want to test it before it is out – click here.

[If you are a serious OTL user - and especially if you are using pre-Seattle Delphi - please test your program(s) with the beta release. Thanks!]

I have started work on OTL 4.0 which will (fingers crossed) fully support cross-platorm work. Yup, it will work on all platforms that Delphi can compile for! Multiplatform support was implemented by Sean B. Durkin so if OTL/Mobile is something you were waiting for a long time, go to OmniThreadLibrary-For-Mobile Google+ community and express your gratitude.