Pastey Phix wiki #1

Phix (programming language)


Phix is a programming language, created by Pete Lomax. Phix adopted the atom|sequence|object data-type design found in the Euphoria programming language. Phix is a self-hosting hybrid interpreter|compiler; Phix can interpret, compile, and re-compile itself. The design of Phix is based on three tenets: simplicity, clarity, and easy debugging.


Overview

Phix is suitable for educational, general, high-level scripting, and text processing. Phix is available for Linux and Windows platforms and in 32 bit and 64 bit versions.

Phix is simple because of:

  • two distinct data-types: number and sequence
  • keyword...end keyword based syntax
  • generic operators and routines
  • symmetrical indexing/slicing of sequences

Phix is simple

"Compactness is the property that a design can fit inside a human being's head." --The Art of Unix Programming.

Fewer data-types

Every value is an object. You could program using just one object data-type.

An object is either a number (an atomic value) or a sequence (a list of values).

            object 
      -------------------- 
              | 
      number  |  sequence 

A number is "atomic" in being indivisible (the keywords atom and number are synonyms). Thinking about just numbers is simple. ( Learning about many data-types, integer division vs regular division, and type conversions is much more complicated.)

-- examples of number values 
atom a 
a = 1     -- integer 
a = 3.14  -- float 
a = 'a'   -- ascii character 
 
?  'a' + 32  -- integer arithmetic 
             // 129 
 
?  3.14 * 5  -- float arithmetic 
             // 15.7 

Phix recognizes integer as a subset of number.

Every compound data-type is a sequence, which is a list of values. (Much simpler than learning about arrays, immutable strings, tuples, and lists.) A sequence may be used for text, numbers, and mixed data. Sequences are dynamic and flexible.

-- examples of sequence values 
sequence s 
s = "hello"                    -- text 
s = {1,2,4}                    -- list of numbers 
s = { {2,"cats"}, {4,"dogs"} } -- nested and mixed values 

Phix recognizes string as a subset of sequence. A string is a flat sequence of integers. This matches the UTF8 specification for text.

There are five builtin data-types in total.

            object 
      -------------------- 
              | 
      number  |  sequence 
         |    |     | 
      integer |  string 
 

Less syntax

The Phix keyword...end keyword syntax design is verbose. However, it enables the interpreter to display human-readable error messages, and eliminates the dangling "else" problem. Keywords provide more clarity over indentation based languages (Much simpler than requiring extra punctuation and keywords.) or over brace {} based languages. Free-form syntax using keywords has clarity and is easier to read.

Line comments may be -- or //, while /* */ are for block comments:

-- use `?` to output a value 
 
            // the output you see is: 
? "Phix"    // "Phix" 
? sqrt(9)   // 9 
 
/* 

    in this example -- is used for descriptions 
                    // is used to show output 
*/ 
-- a loop 
number a = 4.4 
while a<6 do 
    ? a 
    a += 1 
end while 
 
-- output is: 
// 4.4 
// 5.4 
 
--  an iteration 
sequence s = "hello" 
for i=1 to length(s) do 
    puts(1, s[i] & "\n" ) 
end for 
 
-- output is: 
// h 
// e 
// l 
// l 
// o 
 
-- a branch 
number x = -3 
if x < 0 then 
    ? "x is negative" 
elsif x > 0 then 
    ? "x is positive" 
else 
    ? "x is zero" 
end if 
 
-- output is: 
// x is negative 

https://en.wikipedia.org/wiki/Dangling_else