Call: +44 (0)1904 557620 Call
Blog

Pete Finnigan's Oracle Security Weblog

This is the weblog for Pete Finnigan. Pete works in the area of Oracle security and he specialises in auditing Oracle databases for security issues. This weblog is aimed squarely at those interested in the security of their Oracle databases.

[Previous entry: "Can we Add C Style Pointers to PL/SQL?"] [Next entry: "Protect Your PL/SQL"]

Extreme PL/SQL - An Interpreter for a Simple Language

I talked at a high level a few weeks ago about Extreme PL/SQL and gave a brief look at an interpreter I have been creating for a simple language based on BASIC.

I have been keeping notes in a Word document currently running over 100 pages and over 35 sections/chapters/articles. I plan to release each of those chapters as separate blogs in a blog series. I have been writing all of these notes as I develop the language and the interpreter and of course the design and tests and content changes. So, i am unsure at this stage whether to release all the articles "as-is" or update them to reflect the changes that have occurred as we progressed.

I will decide soon!

The language is fairly simple and have these features:

  • Define any number of numeric variables such as "var" or "x" or...

  • Expressions include "+, -, /, *", variables brackets

  • KEYWORDS such as IF, THEN, FI, LET, PRINT, GOTO, REM and END


The language is simple and does not include loops or ELSE in the IF statement or procedures - YET!

We can implement all of the above with LET, GOTO and IF anyway. Writing loops, ELSE or procedures with GOTO, IF and LET makes the code more complex and hard to write and understand but can be done.

I wanted to get a simple version of a language working first and then we can extend it and add more language constructs and features.

The original plan was to write a VM for a CPU in PL/SQL and indeed I have written that already in anticipation but I have not written an assembler yet to convert assembly language to binary instructions. This binary stream would then be executed in the VM of the CPU. Finally the interpreter would be converted to a compiler and would emit assembly language instructions for the VM. The tool chain would be BASIC => compiler => assembly language => assemble => binary => execute the binary in the VM CPU.

Currently the simple BASIC language is interpreted and having looked at the speed, I suspect the binary would not run massively faster than the interpreter. The VM is a CPU written in PL/SQL and the interpreter executes the simple BASIC. We compare at run time executing BASIC or binary in a PL/SQL program. There is additional complexity of course in compiling the BASIC to assembler and then assembling the assembly language and running it. If the goal is to run a program then probably there is not a massive difference in speed. I will explain a lot more in the detailed write up. One option is to make the BASIC execute faster by reducing its size so it parses and executes faster. We will explore that also in the detailed write up.

The language is not BASIC and is a simple implementation of some of the original BASIC from the 1960s. It will change as I add features to it so I am going to call it PFCLScript.

A traditional starter program is "hello World". Here it is in PFCLScript:

declare
lv_prog varchar2(32767):=q'[
PRINT "Hello, World!!"
]';
begin
pfcl_int.init(true,1);
pfcl_int.run(lv_prog);
end;
/

And here it is running:

SQL> @compiler
Hello, World!!

Start Time : 17-JUL-24 08.14.09.484629 AM
End Time : 17-JUL-24 08.14.09.562677 AM
Elapsed Seconds : +000000 00:00:00.078048000
SQL>

OK, lets try another favourite for testing new languages or learning to program in a language; the Fibonacci sequence where the Fibonacci number is less than 20:

declare
lv_prog varchar2(32767):=q'[
LET m=20
LET x=1
LET y=1
:30 IF x>m THEN GOTO :20 FI
PRINT x
LET x=x+y
IF y>m THEN GOTO :20 FI
PRINT y
LET y=x+y
GOTO :30
:20 END
]';
begin
--
pfcl_int.init(true,1);
--
pfcl_int.run(lv_prog);
--
end;
/

And running shows:

SQL> @compiler
1
1
2
3
5
8
13

Start Time : 17-JUL-24 10.55.13.109739 AM
End Time : 17-JUL-24 10.55.16.933322 AM
Elapsed Seconds : +000000 00:00:03.823583000
SQL>

That is the correct answer.

I will decide whether to update the notes I have already created for many articles to reflect the later versions of the interpreter or release them as they were written. I am also going to continue to add some features to the language and the interpreter and also decide whether to convert to a compiler or not.

Watch out for more and commend via social media and also please consider following me on my social media accounts.


#oracleace #sym_42 #oracle #database #23c #23ai #securecode #plsql #extreme #interpreter #compiler #assembler #vm #cpu #pfclscript