.Open 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 Date Event .Row Dec 20 2007 Preliminary draft proposal .Row Mar 16 2007 Author creates an improved SOOP2 .Row Apr 07 2009 Small 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: .As_is {"Hello, World!"!;} More examples below: .See Extended Examples .Open Syntax and Informal Semantics . Lexemes The following strings are important parts of the language that are used in more complicated syntactic structures. 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. operator::= $symbol #(symbol). symbol::="!" | "@" | "#" | ... . vertical_bar::="|". semicolon::=";". backslash::="\\". quotes::="\"". . Constants constant ::= $number | $string | $Boolean | $null. Constants represent objects that are members of predefined classes. These are listed below. (Predefined Classes): following .Set .Key Number: Integer values, understands arithmetic operations like addition and subtraction. .Key String: Finite sequences of characters, understands how to be concatenated, etc. .Key Boolean: True or false, understands conjunction, disjunction, negation, etc. .Key Compound: has many parts with "@" and "#" to access them. .Key Null: has no knowledge or know how except how to accept data and methods. .Close.Set 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. number ::=$O(sign) $N(digit). .As_is 42 .As_is -1 .As_is +41 string ::=quotes #(char ~ quotes) quotes. .As_is "hello world" Boolean ::= "true" | "false". null ::= "new". symbolic_id::=$N($symbolic_character) | $symbol. symbolic_character::="+" | "-" | "*" | "/" | "=" |"<" | ">" | "!" | "%" | "^" | $backslash | "&" | "?". string::= $quotes #($char~$quotes) $quotes. . Classes and Programs class_definition ::= $class_id "=" "{" #($method | $data_field) "}". .As_is Widgets={type=""; setType{type=$@1;}}; class_id::=$identifier. .As_is 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 .As_is 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: .As_is {type=""; setType {type=$@1; } }; method::= $message $program. .As_is count{ n=n+1;} .As_is setType{type=$@1;} message::=$identifier. data_field::= $variable "=" $initial_value $semicolon. .As_is type=""; .As_is count=1; initial_value::=$expression. . Programs program::= "{" $scenario # $extension "}". Here is a simple one scenario program describing a simple calculation .As_is {delta = b*b - 4 * a * c; } Here is another .As_is {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. .As_is {delta==0; equalRoots; | delta>0; realRoots; | delta<0; complexRoots; }; 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. scenario::= #($command | $method) $O($value | $repeat | $empty). value::=$expression. repeat::="repeat". empty::="". command::=$O($labeled) $expression $semicolon. .As_is x=1+2; 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 expression::=$O($object) #($message $O($parameter) | $program). .As_is new .As_is n*3+1 .As_is n odd .As_is (3,4,1,2)@2 .As_is x>0 .As_is 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 object::=$variable | $constant | $this | $formal_parameter | $compound. this ::= "this". 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. compound::= $list | $program. Compound objects have an $at and an $number method. at ::= "@". This method selects parts of the compound. .As_is (2,3,4)@1 == 2 .As_is {a=1; b=2;}@a == 1 .Table i c@i .Row i>0 i'th part of c .Row i==0 the whole of c .Row i<0 all parts of c without the i'th part .Close.Table .As_is (2,3,4)@1 == 2 .As_is (2,3,4)@0 == (2,3,4) .As_is (2,3,4)@(-1) == (2,3) number::= "#". .As_is (2,3,4)# == 3 parameter::=$object. list::="(" $L($command)")". message::=$identifier|$operator. . Extended Examples Here is an object that knows how to count things: .As_is theCount= new { value=0; counts{value=value+1;}; get{value}; } and here is how it might be used .As_is theCount counts; .As_is theCount counts; .As_is theCount get; The last action returns the number 2. The following is an object designed to handle loops that count down to zero .As_is countDown= new { value=0; from(value=$@1;); loop{value=value-1; value -1>-1}; } It is used like this .As_is countDown.from(20); { counDown.loop ; ... ; repeat } The following creates a new object with two data items and two methods: .As_is 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". .As_is widget addKnob; widget setID ("ax123c"); The following shows an extension of Widget: .As_is 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 .As_is Hailstone={n==1; | n even; n=n/2; repeat| n=n*3+1; repeat} .As_is sum={s=0; n=1; { n<=$@#; s=s+$@n; repeat }; s} .As_is {d>0; d=d-a; | d=d+b; } .As_is {n>0; n=n-1; oneStep; repeat} .As_is Bresenham={a=?;b=?; d=?; n=?; .As_is {n>0; n=n-1; .As_is {d>0; d=d-a; diagonal; .As_is | d=d+b; axis; .As_is } .As_is repeat .As_is } .As_is } An incomplete class for handling fractions like 1/2 and 37/48: .As_is Fraction={numerator=0; denominator=1; .As_is setNumerator{numerator=$;}; .As_is setDenominator{denominator=$;}; .As_is set{numerator=$@1;denominator=$@2;}; .As_is *{new Fraction set (numerator * $@numerator, denominator * $@denominator)}; .As_is . . . .As_is } .Close Syntax .Open Predefined Messages .Table Symbol Meaning Class Parameter Produces .Row + addition Number Number Number .Row + concatenation String Number Number .Row - subtraction Number Number Number .Row * multiplication Number Number Number .Row / division Number Number Number .Row odd parity Number - Boolean .Row even parity Number - Boolean .Row ^ conjunction Boolean Boolean Boolean .Row \/ disjunction Boolean Boolean Boolean .Row ' negation Boolean Boolean Boolean .Row ! output Number, String, Boolean, .Row == equality All same Boolean .Row <> inequality All same Boolean .Row < less than All same Boolean .Row > less than All same Boolean .Row # number parts in Compound - Number .Row @ part Compound Number any .Row . . . .Close.Table .Open Semantics .Close .Close . XBNF Syntax Meta-Language O::=`Optional`. N::=`one or more`. char::=`ASCII code character`. #::=`any number of including none`.