[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng] / [R J Botting] / [Samples] / SOOP
[Index] [Contents] [Source Text] [About] [Notation] [Copyright] [Comment/Contact] [Search ]
Thu Apr 29 17:22:48 PDT 2010

Contents


    SOOP -- Simple Object-Oriented Programming

      Author -- R J Botting

      Date -- Tue Apr 7 10:57:44 PDT 2009

      Status -- Second Draft Proposal

      There are many deliberate omissions, stupid ideas, and even typographical errors to fix.

      History


      Table
      DateEvent
      Dec 20 2007Preliminary draft proposal
      Mar 16 2007Author creates an improved SOOP2
      Apr 07 2009Small Corrections ready for CS320 students.

      (Close Table)

      Goal -- The Simplest Possible Object Oriented Language

      SOOP is more object-based than object-oriented because classes take a back seat to objects.

      Example program:

       	{"Hello, World!"!;}

      More examples below: [ Extended Examples ]

      Syntax and Informal Semantics

        Lexemes

        The following strings are important parts of the language that are used in more complicated syntactic structures.
      1. identifier::= letter #(letter|digit|"_"). Letter and digit are defined in XBNF as the normal ASCII letters A-Z and a-z, and the decimal digits 0-9.
      2. operator::= symbol #(symbol).
      3. symbol::="!" | "@" | "#" | ... .
      4. vertical_bar::="|".
      5. semicolon::=";".
      6. backslash::="\\".
      7. quotes::="\"".

        Constants

      8. constant::= number | string | Boolean | null. Constants represent objects that are members of predefined classes. These are listed below.
        (Predefined Classes): following

          Number: Integer values, understands arithmetic operations like addition and subtraction.

          String: Finite sequences of characters, understands how to be concatenated, etc.

          Boolean: True or false, understands conjunction, disjunction, negation, etc.

          Compound: has many parts with "@" and "#" to access them.

          Null: has no knowledge or know how except how to accept data and methods.

        Note: all objects know how to add data and methods to derive new objects with new behaviors. They also know how to output themselves. Predefined methods are listed later.

      9. number::=O(sign) N(digit).
         		42
         		-1
         		+41
      10. string::=quotes #(char ~ quotes) quotes.
         		"hello world"
      11. Boolean::= "true" | "false".
      12. null::= "new".

      13. symbolic_id::=N(symbolic_character) | symbol.
      14. symbolic_character::="+" | "-" | "*" | "/" | "=" |"<" | ">" | "!" | "%" | "^" | backslash | "&" | "?".

      15. string::= quotes #(char~quotes) quotes.

        Classes and Programs

      16. class_definition::= class_id "=" "{" #(method | data_field) "}".
        	Widgets={type=""; setType{type=$@1;}};
      17. class_id::=identifier.
         	Widget

        A class is a list of new data and methods that can be added to an existing object. Al objects know how to extend themselves with new methods and data. So

         	new Widget
        creates a null object and adds an item of data (type) and a method to change it.

        A class is a collection of such methods and data fields. Here is a single an example with one data field and one method:

        	{type=""; setType {type=$@1; } };

      18. method::= message program.
         	count{ n=n+1;}
         	setType{type=$@1;}
      19. message::=identifier.

      20. data_field::= variable "=" initial_value semicolon.
         	type="";
         	count=1;
      21. initial_value::=expression.

        Programs

      22. program::= "{" scenario # extension "}".

        Here is a simple one scenario program describing a simple calculation

         	{delta = b*b - 4 * a * c; }
        Here is another
         	{tmp=a; a=b; b=tmp; }
        A scenario is, in general, a sequence of commands and methods. A program has many scenarios that are tried in turn. The later scenarios are called extensions -- see below.

        Here is a three scenario program which is part of solving a quadratic equation. It has a main scenario and two extensions.

        	{delta==0; equalRoots; | delta>0; realRoots;  | delta<0; complexRoots;  };

      23. extension::= vertical_bar scenario.

        A program executes a series of scenarios until one of them completes. A scenario can fail if an expression produces "false" as a final result or if all sub-scenarios fail.

        If a scenario fails then the following "extension" is tried in turn.

      24. scenario::= #(command | method) O(value | repeat | empty).
      25. value::=expression.
      26. repeat::="repeat".
      27. empty::="".
      28. command::=O(labeled) expression semicolon.
         	x=1+2;
      29. labeled::= variable "=".

        Control Structures

        SOOP does not need special control structures like if-then-else and while. A programer can use the above "program" structure to express them.


        (while_loop): { condition; body; repeat | }
        (if_then_else_selection): { condition; ifTrue; | ifFalse; }
        (for_loop): { index=first; {condition; body; index=next; repeat | }; }

        Expressions

      30. expression::=O(object) #(message O(parameter) | program).
         	new
         	n*3+1
         	n odd
         	(3,4,1,2)@2
         	x>0
         	aStudent drops( aClass )

        If the first object is omitted then the object 'this' is assumed as the default. This has the effect of (1) allowing subprogram calls inside a class, (2) treating data items in an object as variables.

        Objects

      31. object::=variable | constant | this | formal_parameter | compound.
      32. this::= "this".
      33. formal_parameter::="$" .

        Methods and Messages

        Methods in SOOP have a single formal parameter that is symbolized by a dollar sign. However, the actual parameter is nearly always a compound object. Often they are lists and so strings like "$@1" refer to the first object in the parameter.
      34. compound::= list | program. Compound objects have an at and an number method.
      35. at::= "@". This method selects parts of the compound.
         	(2,3,4)@1 == 2
         	{a=1; b=2;}@a == 1

        Table
        ic@i
        i>0i'th part of c
        i==0the whole of c
        i<0all parts of c without the i'th part

        (Close Table)
         	(2,3,4)@1 == 2
         	(2,3,4)@0 == (2,3,4)
         	(2,3,4)@(-1) == (2,3)

      36. number::= "#".
         	(2,3,4)# == 3

      37. parameter::=object.

      38. list::="(" L(command)")".

      39. message::=identifier|operator.

        Extended Examples

        Here is an object that knows how to count things:
         	theCount= new { value=0; counts{value=value+1;}; get{value}; }
        and here is how it might be used
        	theCount counts;
         	theCount counts;
         	theCount get;
        The last action returns the number 2.

        The following is an object designed to handle loops that count down to zero

         	countDown= new { value=0; from(value=$@1;); loop{value=value-1; value -1>-1}; }

        It is used like this

         	countDown.from(20); { counDown.loop ; ... ; repeat }

        The following creates a new object with two data items and two methods:

        	widget= new {knobs=0; name=""; addKnob{knobs=knobs+1;}; setID{name=$@1;}};

        The following applies the methods to the object to make a Widget with one knob and name "ax123c".

        	widget addKnob; widget setID ("ax123c");

        The following shows an extension of Widget:

        	wodget= widget{type=""; setType{type=$@1;}};
        Extends the widget object by adding a type attribute and an operation to operate on it.

        Conditions and loops

         	Hailstone={n==1; | n even; n=n/2; repeat| n=n*3+1; repeat}

         	sum={s=0; n=1; { n<=$@#; s=s+$@n; repeat }; s}

         	{d>0; d=d-a;  | d=d+b; }

         	{n>0; n=n-1; oneStep; repeat}

         	Bresenham={a=?;b=?; d=?; n=?;
         		{n>0;  n=n-1;
         			{d>0; d=d-a; diagonal;
         			| d=d+b; axis;
        			}
         		 repeat
         		}
         	}

        An incomplete class for handling fractions like 1/2 and 37/48:

         Fraction={numerator=0; denominator=1;
         	setNumerator{numerator=$;};
         	setDenominator{denominator=$;};
         	set{numerator=$@1;denominator=$@2;};
         	*{new Fraction set (numerator * $@numerator, denominator * $@denominator)};
         	. . .
         }

      . . . . . . . . . ( end of section Syntax) <<Contents | End>>

      Predefined Messages


        Table
        SymbolMeaningClassParameterProduces
        +additionNumberNumberNumber
        +concatenationStringNumberNumber
        -subtractionNumberNumberNumber
        *multiplicationNumberNumberNumber
        /divisionNumberNumberNumber
        oddparityNumber-Boolean
        evenparityNumber-Boolean
        ^conjunctionBooleanBooleanBoolean
        \/disjunctionBooleanBooleanBoolean
        'negationBooleanBooleanBoolean
        !outputNumber, String, Boolean,
        ==equalityAllsameBoolean
        <>inequalityAllsameBoolean
        <less thanAllsameBoolean
        >less thanAllsameBoolean
        #number parts inCompound-Number
        @partCompoundNumberany
        . . .

        (Close Table)

        Semantics

      XBNF Syntax Meta-Language

    1. O::=Optional.
    2. N::=one or more.
    3. char::=ASCII code character.
    4. #::=any number of including none.

    End