Introduction - Phix is Pete’s Self Hosted Hybrid Interpreter/Compiler
Phix has several advantages over other programming languages: (Self compiling is a bonus)
- A remarkably simple, yet flexible and powerful language definition that is easy to learn and use.
- A high-performance interpreter/compiler much faster than most.
- Fully managed dynamic data storage with no requirement to allocate or free memory. Variables can grow or shrink to any size without any manual housekeeping.
- Extensive run-time checking for: out-of-bounds subscripts, uninitialized variables, bad parameter and variable assignments and many more. There are no mysterious machine exceptions -- you always get a full English description of any problem that occurs within your program at run-time, with the exact source line it occurs on, along with a full call-stack listing and a dump of all of your variable values. Programs can be debugged quickly, easily, and more thoroughly.
- A source level debugger allows execution of programs line-by-line. Tracing can begin (or end) on any line or for any condition, under full programmer control.
- A built-in execution profiler identifies which lines of code take up the highest percentage of execution time, or are executed the most often.
- Features of the underlying hardware are usually hidden. Programs are not typically aware of word-lengths, bit-representations, byte-order etc.
- However, phix also satisfies anyone with a deeper curiosity. There is no hidden wall between the language and the operating system to fully understand how things actually work. Phix can create low-level listing files which show the exact addresses and assembly instructions generated, and also includes a program (filedump.exw) that shows every bit and byte in full detail (within reason, for example icons and other such data are shown in raw hex), for Windows PE, Linux ELF, and other format files.
- Self-hosted so there is no need to know another language, or obtain any other development tools, to enhance or fix the language itself. You can easily download, install, and recompile phix in less than two minutes! (See the short webcast at http://phix.is-great.org/tutorial.php, and also Recommended Tools.)
- The Edita programmers editor is freely available, and written in phix with everything you might expect, including multiple and user-definable syntax colouring, multilingual support, intellisense, autocompletion, code folding, integrated help, window painter, full source, and more.
- Standalone executables can be created simply by adding a "-c" flag to the normal interpret command (or via Ctrl F5 if using Edita). (The detailed assembly listing mentioned above is likewise just a "-d".) There are no complicated compiler options to remember and there is no separate linking phase. A simple "format" directive in the source allows for easy cross-compilation to any other supported system.
- Phix programs are naturally generic. The example program shows a single routine that will sort any type of data -- integers, floating-point numbers, strings etc. Phix achieves many of the benefits of object-oriented programming, yet in a much simpler way.
Phix Bitbucker Project RepositoryPhix TutorialExample Programfunction merge_sort(sequence x)
-- put x into ascending order using a recursive merge sort
integer midpoint
sequence merged, first_half, second_half
if length(x)<=1 then
return x -- trivial case
end if
midpoint = floor(length(x)/2)
first_half = merge_sort(x[1..midpoint])
second_half = merge_sort(x[midpoint+1..$])
-- merge the two sorted halves into one
merged = {}
while length(first_half)>0
and length(second_half)>0 do
if first_half[1]<=second_half[1] then
merged = append(merged, first_half[1])
first_half = first_half[2..$]
else
merged = append(merged, second_half[1])
second_half = second_half[2..$]
end if
end while
-- result is the merged data plus any leftovers
return merged & first_half & second_half
end function
sequence list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2}
? merge_sort(list)
The above example delares a function, a sequence, and then invokes the function and displays the results. It also demonstrates how sequences (flexible arrays) are the real powerhouse of phix.
The output from the program is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
merge_sort() will just as easily sort {1.5, -9, 1e6, 100} or {"oranges", "apples", "bananas"} .