.Open The Programming Language OJ . Status This is the first rough draft of a new language. It need critical proof reading, correcting, and improving. . Goals OJ is designed to be a very simple structured language with syntax not unlike C, C++ and Java. The name is yet another of the Java/Coffee puns: Orange Juice. In theory if you take a correct OJ program and embed it in a main method of an appropriately named class in a file with the correct name then if can be compiled and run as a Java program. Similar translations could handle C and C++. In practice the I/O ($in and $out) may have to be translated for some languages. . Note Conventionally OJ programs are pout in files with suffix ".oj". . Example 1 .As_is // A program that inputs a number and outputs its square .As_is out("Number="); .As_is int number; .As_is number = in(); .As_is int square; .As_is square = number * number; .As_is out( square ); . Example 2 .As_is //Inputting and squaring many positive numbers .As_is int x; .As_is out("Input a series of numbers greater than 0\n"); .As_is x=in();//read ahead .As_is while( x > 0 ) .As_is { .As_is out(x); out(" squared is "); out(x*x); out("\n"); .As_is x=in(); .As_is } . Notation This description uses the XBNF/MATHS notation to define and describe the OJ language. XBNF has a set of predefined terms like digit, letter, quotes, backslash, etc. plus the BNF "or" (|) and an "Any number of"(#) meta-symbol. . Lexemes An OJ program is defined in terms of lexemes like $Strings, $Integers, $Variables, and some reserved words: .As_is if, while, in, out, int, else and symbols: .As_is = == <= >= < > != * + / % - ( ) { } Ends of lines, tabs, and other extra whitespace can be used to improve the readability of an OJ program. They are ignored except as separating lexemes. . Comments An OJ comment is optional and can be put after any statement. The copmment is terminated at the end of the line. comment::= "//" any_thing. Comments are removed in a lexical scan and replaced by an end of line. They are not shown in the $Syntax below. .Open Syntax . Programs An OJ program is a sequence of statements. Unlike most structured languages it is not a block and so needs no special heading or ending syntax. program::= #$statement. An empty program does nothing of course. A program with a single statement executes it and stops. With two or more statements the first statement is executed, and when it finishes the rest of the program is executed (as if it was a program). . Statements OJ provides the minimum set of control statements using a C/C++/Java syntax plus assignments, declarations, and output. statement::= $control_statement | $assignment | $declaration | $output_statement. . Assignment Statements An assignment statement changes the value of a previously declared variable to the value found be evaluating an expression. assignment::= $variable "=" $expression ";". .As_is square = number * number; . Declarations A declaration introduces a new variable that can hold a one integer value at a time. declaration::= "int " $variable";" .As_is int number; .As_is int square; . Control Structures There are the only two structures that you need to right a program: while and if-then-else: control_statement::= $while_statement | $if_statement. A while statement introduces a loop with a condition and a body: while_statement ::= "while(" $condition ")" $body. .As_is while( i > 0 ) { i = i - 1 ; } body::= $empty_statement | "{" #$statement "}". .As_is ; .As_is { out(x*x); x=in(); } An if statement selects one of two branches depending on the truth-value of a condition. if_statement::= "if(" $condition ")" $body "else" $body. .As_is if ( a>b) { out(a); } else { out (b); } .As_is if (a%2 == 0){out("even");} else ; empty_statement::= ";" . Expressions Expressions are evaluated to produce integer values that can be output or stored in a predeclared variable by an assignment. expression ::= $term #($add_operator $term). .As_is b*b - 4*a*c .As_is x*x - y*y add_operator ::= "+" | "-". term ::= $factor #($mult_operator $factor). .As_is b*b .As_is 4*a*c .As_is (x - y)* ( x + y) mult_operator::=("*" | "/" | "%" ). factor::= $variable | $integer | "(" $expression ")" | $input_expression. .As_is 4 .As_is a .As_is b .As_is (b*b - 4*a*c) .As_is (x-y) . Conditions A condition compares two expressions. Its value (true or false) determines what happens next in an if_statement or a while_statement: condition::= $expression $relation $expression. relation::= "==" | "!=" | "<=" | ">=" | "<" | ">". . Integers The only data type is called 'int'. This is is implemented as a 16 bit signed integer. It has the operations of addition, subtraction, multiplication, division, and remainder. integer::= digit #digit. . Input/Output There is a special output statement and a special input expression: output_statement::= "out(" ($expression | $string) ");". Sends the value of the expression, or the content of the string to the user. input_statement::= "in()". Inputs the next integer form the user and returns the value. .Close Syntax . Strings OJ has C/C++/Java string constants but (like Algol 60) not much can be done with them. You can output them with an $output_statement. string::= $quotes #$string_element $quotes. string_element::= char~$special_character | backslash $special_character | $control_char. special_character::= backslash | quotes. control_character::= backslash ( "n" | "t" ), representing a newline and tab respectively. . Variables variable::= letter #(letter | digit). . Formal Semantics To Be Done using the UML. .Close The Programming Language OJ