pcosmos.ca

le domaine de Philippe Choquette

Accueil Profil Contact Arrière plan
PCASTL
Half-Life
Informatique
OpenGL
Linux
Grow
Elements
Lecture
Liens
 
PCASTL Examples

Page en français

Two States Toggle
Code Segments
Other examples

  page top Two States Toggle

The following code demonstrates one of three ways of defining a function that alternatively displays two results.

# first way
# function definition:
a = function()
{
   print(1)
   if (value(a.childset[1].childset[0].childset[1].childset[0]) == 1)
   {
      a.childset[1].childset[0] = `print(2)'
   } 
   else
   {
      a.childset[1].childset[0] = `print(1)'
   }
   {}   
}
# function calls
a()
a()

In the first way:

  • a.childset[0] would give a reference to the parameter name list of the "a" function. The function definition tree structure is illustrated here.
  • a.childset[1] gives a reference to the statement list of the "a" function.
  • a.childset[1].childset[0] gives a reference to the first statement in the previous list.
  • a.childset[1].childset[0].childset[0] would give a reference to function name node of the "print" call. The function call tree structure is illustrated here.
  • a.childset[1].childset[0].childset[1] gives a reference to the argument list of the "print" call.
  • a.childset[1].childset[0].childset[1].childset[0] gives a reference to the first element of the previous list, ie to the first argument of the "print" call.

One line comments have to begin with # and we end the statement list of the "a" body with {} to prevent the display of the value of the assignation in the "else".

# second way
a = function()
{
   print(1)
   # arglist, value call, operator==, if, stmtlist
   if (value(parent.parent.parent.parent.parent
      .childset[0].childset[1].childset[0]) == 1)
   {
      a.childset[1].childset[0] = `print(2)'
   }
   else
   {
      a.childset[1].childset[0] = `print(1)'
   }
   {}   
}
a()
a()

In the second way:

  • parent gives a reference to the argument list node of the "value" call.
  • parent.parent gives a reference to the "value" call node.
  • parent.parent.parent gives a reference to the operator == node. This node is also the condition node of the "if" statement.
  • parent.parent.parent.parent gives a reference to the "if" node.
  • parent.parent.parent.parent.parent gives a reference to the statement list node of the "a" function.
  • The additional .childset[0] gives a reference to the "print" call node.
  • The additional .childset[1] gives a reference to the argument list of the "print" call.
  • The additional .childset[0] gives a reference to the first argument of the "print" call.
# third way
a = function()
{
   print(1)
   b = parent.parent.childset[0].childset[1].childset[0]
   if (value(b)== 1)
   {
      *b = `2'
   }
   else
   {
      *b = `1'
   }
   {}
}
a()
a()

In the third way, we use the unary operator * meaning that we want the assignation to be done to the reference holded by the variable "b".

If they were not inside a function body, the "else" could not be placed at the beginning of a new line. Placing an "else" at the beginning of a new line would result in a syntax error.

  page top Code Segments

You can use code segments as starting points for genealogical dotted lists.

> info(` "abc" ')
        Node type: code segment
> info(` "abc" '.childset[0])
        Node type: string
        Content: "abc"
> info(` "abc" != "cba" '.childset[0])
        Node type: relational or logical operator
        Number of childs: 2
        Operator: !=
> info(` function(x) x '.childset[0])
        Node type: function definition
        Parameters: "x"

When you assign a code segment to an existing node, you do not have to get the code root using the `{code here}'.childset[0] form. Using the `{code here}' form is enough.

  page top Other examples

PCASTL on Wikipedia (with very basic examples).

A function displaying alternatively three results.

A function generating functions displaying alternatively n results.

A finite state machine. finite automata

 

back to PCASTL