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

Contents


    The Programming Language Juice

      Status

      This is the first rough draft of a new language. It need critical proof reading, correcting, and improving.

      Goals

      Juice 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 Juice 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 Juice programs are put in files with suffix ".jui".

      Example 1

       		// A program that inputs a number and outputs its square
       		out("Number=");
       		int number;
       		number = in();
       		int square;
       		square = number * number;
       		out( square );

      Example 2

       		//Inputting and squaring many positive numbers
       		int x;
       		out("Input a series of numbers greater than 0\n");
       		x=in();//read ahead
       		while( x > 0 )
       		{
       			out(x); out(" squared is "); out(x*x); out("\n");
       			x=in();
       		}

      Notation

      This description uses the XBNF/MATHS notation to define and describe the Juice 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

      A Juice program is defined in terms of lexemes like Strings, Integers, Variables, and some reserved words:
       	if, while, in, out, int, else
      and symbols:
       	= == <= >= < > != * + / % - ( ) { }

      Ends of lines, tabs, and other extra whitespace can be used to improve the readability of a Juice program. They are ignored except as separating lexemes.

      Comments

      A Juice comment is optional and can be put after any statement. The copmment is terminated at the end of the line.
    1. 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. The have no effect on the execution of the program and so are not mentioned in Semantics.

      Syntax

        Programs

        A Juice program is a sequence of zero or more statements. Unlike most structured languages it is not a block and so needs no special heading or ending syntax.

      1. program::= empty | statement program. An empty program does nothing. A program with one statement is followed by another program. The first statement is executed and then the program that follows it.

        Statements

        Juice provides the minimum set statements using a C/C++/Java syntax:

      2. 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.
      3. assignment::= variable "=" expression ";".
         	square = number * number;

        Declarations

        A declaration introduces a new variable that can hold a one integer value at a time.
      4. declaration::= "int " variable";"
         	int number;
         	int square;

        Control Structures

        There are the only two structures that you need to right a program: while and if-then-else:
      5. control_statement::= while_statement | if_statement.

        A while statement introduces a loop with a condition and a body:

      6. while_statement::= "while(" condition ")" body.
         		while( i > 0 ) { i = i - 1 ; }
      7. body::= empty_statement | "{" #statement "}".
        		;
        		{ out(x*x); x=in(); }

        An if statement selects one of two branches depending on the truth-value of a condition.

      8. if_statement::= "if(" condition ")" body "else" body.
        		if ( a>b) { out(a); } else { out (b); }
        		if (a%2 == 0){out("even");} else ;

      9. empty_statement::= ";"

        Expressions

        Expressions are evaluated to produce integer values that can be output or stored in a predeclared variable by an assignment.
      10. expression::= term #(add_operator term).
         		b*b - 4*a*c
         		x*x - y*y
      11. add_operator::= "+" | "-".

      12. term::= factor #(mult_operator factor).
         		b*b
         		4*a*c
         		(x - y)* ( x + y)
      13. mult_operator::=("*" | "/" | "%" ).

      14. factor::= variable | integer | "(" expression ")" | input_expression.
         		4
         		a
        		b
         		(b*b - 4*a*c)
         		(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:
      15. condition::= expression relation expression.
      16. relation::= "==" | "!=" | "<=" | ">=" | "<" | ">".
         		4*a*c <= b * b

        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 constants are written in decimal:
      17. integer::= digit #digit.
         		1234

        Input/Output

        There is a special output statement and a special input expression:
      18. output_statement::= "out(" (expression | string) ");".
         		out( "Hello, World!\n");
         		out( 4*a*c-b*b);
        This sends the value of the expression, or the content of the string to the user.

      19. input_statement::= "in()". This is a expression rather than a statement. It Inputs the next integer form the user and returns the value.
         		in()
         		in()+4

        Strings

        Juice 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.
      20. string::= quotes #string_element quotes.
      21. string_element::= char~special_character | backslash special_character | control_char.
      22. special_character::= backslash | quotes.
      23. control_character::= backslash ( "n" | "t" ), representing a newline and tab respectively.

        Variables

      24. variable::= letter #(letter | digit). A variable strats with a letter and can have any number of letters and digits,but it can not be a reserved word.

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

      Formal Semantics

      To Be Done using the UML.

    . . . . . . . . . ( end of section The Programming Language Juice) <<Contents | End>>

End