1. wikipedia: draft "body" section part I

Phix (body of article)


_tom 
 
please comment directly into the "boxes" like this one 
 
please edit insitu as needed 


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.

_tom 
 
note to self, could weave Wikipedia pages of **simplicity** and **minimalism** into Phix description later on 
 


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

_tom 
the ##number## keyword needs no explaination compared to ##atom##, 
much easier to document! 


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 this one 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 undivisible (the keywords atom and number are synonyms). Thinking about just numbers is much simpler than having extra data-types, integer versus float division, and typecasting.

-- 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. You can be clear about the intent of a variable when needed.

Every compound data-type is a sequence, which is a list of values. Much simpler than declaring 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.

Commonly used languages have many data-types, many rules for using each data-type, and even more rules for mixing values between different data-types.

There are five data-types in total.

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

Less syntax

The Phix keyword...end keyword syntax design is verbose. However, it enables the interpeter to display human-readable error messages, and eliminates the dangling "else" problem. Keywords require fewer syntax rules than indentatation based languages (no need for the pass keyword and extra punctuation as found in Python) and provides more clarity over braces {} (as found in C). Freeform syntax using keywords is easier to read.

-- this is a comment line 
 
-- 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


Routines with specific roles

There are three kinds of routines: procedure, function, and type. A procedure is a routine that packages action, a function is a routine that returns a value, and a type is a routine declares and tests user defined data-types.

-- this is for action 
procedure hello() 
    ? "hello" 
    end procedure 
 
hello() 
--> outputs: hello 
 
-- this returns a value 
function pi() 
    return 3.14 
    end function 
 
    -- use a function in an expression 
    ? pi()*4*4 
    --> outputs: 50.24 
 
    -- explicit discard is needed to ignore return 
    {} = pi() 
 
-- this creates a user data-type 
type positive(number x ) 
    if x >= 0 then return 1 
    else return 0 end if 
    end type 
 
    -- use to declare a variable 
    positive ndx = 5 
 
    -- use to enforce type-checking 
    ndx = -2 
    --> result is: type-check error 

A function declared as part of a class works like a method in other languages.

Having both procedure and function enables the interpreter to display more meaningful error messages.

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu