Jr to JJ(Java) Translation Definition

This file contains the line-by-line Jr to JJ(Java) translation definition.

This definition is driven by the list of Jr Commands and the translations can be done command-by-command. In other words, any Jr command can be translated to the equivalent Java statement without requiring a complex translator and symbol table. This approach is possible because the Jr to Java translation does not require state information (with the one exception of keeping track of the number of slots in a function, routine, or constructor definition...but these commands are consecutive).


HOW TO READ THIS TRANSLATION

When the translation of a production begins with the word "Java", this means that some tokens potentially found in the production require a translation from Jr to Java. For example, the production "Expr" is translated to "JavaExpr" because a few of the Jr tokens require a translation to Java, such as changing the word "and" to the characters "&&". The productions that begin with the word "Java" (JavaPrimitiveTypeCategory, JavaClassTypeCategory, JavaScalarBoolExpr, JavaLhsExpr, JavaFileIORoutineReference, RoutineReferenceOrAssignment, StringConversionAssignment) can be found on the bottom of this file.

Let the translation begin

----------------------------------------------------------
--
-- Box and Boxes Commands
--
----------------------------------------------------------

ClassBoxCommands :=

 Jr:   (1) 'Box' <boxname> 'ofType' PrimitiveTypeCategory ['is' Visibility]
 JJ:       [Visibility] JavaPrimitiveTypeCategory <boxname> ';'

 Jr:   (2) 'Box' <boxname> 'ofClass' ClassTypeCategory ['is' Visibility]
 JJ:       [Visibility] JavaClassTypeCategory <boxname> ';'

ClassBoxesCommands :=

 Jr:   (1) 'Boxes' <boxname> (',' <boxname>)+ 'ofType' PrimitiveTypeCategory ['are' Visibility]
 JJ:       [Visibility] JavaPrimitiveTypeCategory <boxname> (',' <boxname>)+ ';'

 Jr:   (2) 'Boxes' <boxname> (',' <boxname>)+ 'ofClass' ClassTypeCategory ['are' Visibility]
 JJ:       [Visibility] JavaClassTypeCategory <boxname> (',' <boxname>)+ ';'

LocalBoxCommands :=

 Jr:   (1) 'Box' <boxname> 'ofType' PrimitiveTypeCategory
 JJ:       JavaPrimitiveTypeCategory <boxname> ';'

 Jr:   (2) 'Box' <boxname> 'ofClass' ClassTypeCategory
 JJ:       JavaClassTypeCategory <boxname> ';'

LocalBoxesCommands :=

 Jr:   (1) 'Boxes' <boxname> (',' <boxname>)+ 'ofType' PrimitiveTypeCategory
 JJ:       JavaPrimitiveTypeCategory <boxname> (',' <boxname>)+ ';'

 Jr:   (2) 'Boxes' <boxname> (',' <boxname>)+ 'ofClass' ClassTypeCategory
 JJ:       JavaClassTypeCategory <boxname> (',' <boxname>)+ ';'

----------------------------------------------------------
--
-- Import Command
--
----------------------------------------------------------

ImportCommand
 Jr:   'Import' ImportLib
 JJ:   // 'Import' ImportLib
  or
       import ImportLib ';'

----------------------------------------------------------
--
-- Class and EndClass Commands
--
----------------------------------------------------------

ClassCommand := 
 Jr:   'Class' <classname>
 JJ:   'class' <classname> '{'

EndClassCommand := 
 Jr:   'EndClass' <classname>
 JJ:   '}'

----------------------------------------------------------
--
-- Constant Command
--
----------------------------------------------------------

ConstantCommand :=
 Jr:   'Constant' <constantname> '=' Value 'ofType' ScalarPrimitiveType ['is' Visibility]
 JJ:   [Visibility] final JavaScalarPrimitiveType <constantname> '=' Value ';'

----------------------------------------------------------
--
-- Invariant and EndInvariant Commands
--
----------------------------------------------------------

InvariantCommand :=
 Jr:   'Invariant'
 JJ:   'private' 'void' 'JJinvariant' '()' '{'

EndInvariantCommand :=
 Jr:   'EndInvariant'
 JJ:   '}'

----------------------------------------------------------
--
-- Check, PreCheck, PostCheck Commands
--
----------------------------------------------------------

CheckCommand :=
 Jr:   'Check' ScalarBoolExpr 'bounce' StringLiteral 
 JJ:   'JJS.check' '(' JavaScalarBoolExpr ',' StringLiteral ')' ';'

PreCheckCommand :=
 Jr:   'Check' ScalarBoolExpr 'bounce' StringLiteral 
 JJ:   'JJS.preCheck' '(' JavaScalarBoolExpr ',' StringLiteral ')' ';'

PostCheckCommand :=
 Jr:   'Check' ScalarBoolExpr 'bounce' StringLiteral 
 JJ:   'JJS.postCheck' '(' JavaScalarBoolExpr ',' StringLiteral ')' ';'


----------------------------------------------------------
--
-- Constructor and EndConstructor Commands
--
----------------------------------------------------------

ConstructorCommand :=
 Jr:   'Constructor' <classname>  '(' SlotDeclList ')' 'is' 'public'
 JJ:   'public' <classname> '(' 
       SlotDeclList  (* See translation of Slot commands *)
       ')' '{'  (* Put at the end of the translation of Slot commands *)

EndConstructorCommand := 
 Jr:   'EndConstructor' <classname>
 JJ:   '}'

----------------------------------------------------------
--
-- Routine and EndRoutine Commands
--
----------------------------------------------------------

RoutineCommand := 
 Jr:   'Routine' <routinename> '(' SlotDeclList ')' ['is' Visibility]
 JJ:   [Visibility] <routinename> '(' 
       SlotDeclList  (* See translation of Slot commands *)
       ')' '{'  (* Put at the end of the translation of Slot commands *)

EndRoutineCommand := 
 Jr:  'EndRoutine' <routinename>
 JJ:   '}'

----------------------------------------------------------
--
-- Function and EndFunction Commands
--
----------------------------------------------------------

FunctionCommand :=

 Jr:   (1) 'Function' <functionname> 'ofType' PrimitiveTypeCategory '(' SlotDeclList ')' ['is' Visibility]
 JJ:       [Visibility] JavaPrimitiveTypeCategory <functionname> '('
           SlotDeclList  (* See translation of Slot commands *)
           ')' '{'  (* Put at the end of the translation of Slot commands *)

 Jr:   (2) 'Function' <functionname> 'ofClass' ClassTypeCategory '(' SlotDeclList ')' ['is' Visibility]
 JJ:       [Visibility] JavaClassTypeCategory <functionname> '('
           SlotDeclList  (* See translation of Slot commands *)
           ')' '{'  (* Put at the end of the translation of Slot commands *)

EndFunctionCommand :=
 Jr:   'EndFunction' <functionname>
 JJ:   '}'

----------------------------------------------------------
--
-- Slot Command
--
----------------------------------------------------------

SlotCommand :=

 Jr:   (1) 'Slot' <slotname> 'ofType' PrimitiveTypeCategory
 JJ:       JavaPrimitiveTypeCategory <boxname> 
            ','      (* Comma if not last slot *)
            ')' '{'  (* Close parenthesis, open curly bracket if last slot *)

 Jr:   (2) 'Slot' <slotname> 'ofClass' ClassTypeCategory
 JJ:       JavaClassTypeCategory <boxname>
            ','      (* Comma if not last slot *)
            ')' '{'  (* Close parenthesis, open curly bracket if last slot *)

----------------------------------------------------------
--
-- Start Command
--
----------------------------------------------------------

StartCommand :=
 Jr:   'Start'
 JJ:   'JJS.start' '(' ')' ';'

----------------------------------------------------------
--
-- If, ElseIf, Else and EndIf Commands
--
----------------------------------------------------------

IfCommand := 
 Jr:   'If' ScalarBoolExpr 'then'
 JJ:   'if' '(' JavaScalarBoolExpr ')' '{'

ElseIfCommand :=
 Jr:   'ElseIf' ScalarBoolExpr 'then'
 JJ:   '}' 'else' 'if' '(' JavaScalarBoolExpr ')' '{'

ElseCommand :=
 Jr:   'Else'
 JJ:   '}' 'else' '{'

EndIfCommand :=
 Jr:   'EndIf'
 JJ:   '}'

----------------------------------------------------------
--
-- Repeat, ExitOn and EndRepeat Commands
--
----------------------------------------------------------

RepeatCommand :=
 Jr:   'Repeat'
 JJ:   'while (true) {'

ExitOnCommand :=
 Jr:   'ExitOn' ScalarBoolExpr
 JJ:   'if' '(' JavaScalarBoolExpr ')' 'break' ';'

EndRepeatCommand :=
 Jr:   'EndRepeat'
 JJ:   '}'

LoopCommand :=
 Jr:   'Loop' <name> 'from' ScalarIntExpr 'while' ScalarBoolExpr 'by' ScalarIntExpr
 JJ:   'for' '(' <name> '=' ScalarIntExpr ';' '(' ScalarBoolExpr ')' ';' <name> '+=' ScalarIntExpr ) '{'

EndLoopCommand :=
 Jr:   'EndLoop'
 JJ:   '}'

----------------------------------------------------------
--
-- Set Command
--
----------------------------------------------------------

SetCommand :=
 Jr:   'Set' LhsExpr '=' Expr
 JJ:   JavaLhsExpr '=' JavaExpr ';'

----------------------------------------------------------
--
-- Call Command
--
----------------------------------------------------------

CallCommand :=

 Jr:   (1) 'Call' <routinename> ['with' '(' Expr (',' Expr)* ')']
 JJ:       <routinename> '(' [JavaExpr (',' JavaExpr)*] ')' ';'

 Jr:   (2) 'Call' <classboxname>.<routinename> ['with' '(' Expr (',' Expr)* ')']
 JJ:       <classboxname>.<routinename> '(' [JavaExpr (',' JavaExpr)*] ')' ';'

----------------------------------------------------------
--
-- NewArray Command
--
----------------------------------------------------------

NewArrayCommand :=

 Jr:   (1) 'NewArray' <arrayname> 'ofType' ScalarPrimitiveType '[' ScalarIntExpr ']'
 JJ:       <arrayname> '=' 'new' JavaScalarPrimitiveType '[' JavaScalarIntExpr ']' ';'

 Jr:   (2) 'NewArray' <arrayname> 'ofClass' ScalarClassType '[' ScalarIntExpr ']'
 JJ:       <arrayname> '=' 'new' JavaScalarClassType '[' JavaScalarIntExpr ']' ';'

----------------------------------------------------------
--
-- New Command
--
----------------------------------------------------------

NewCommand :=
 Jr:   'New' LhsExpr 'ofClass' ScalarClassType ['with' '(' Expr (',' Expr)* ')']
 JJ:   JavaLhsExpr '=' new JavaScalarClassType '(' [JavaExpr (',' JavaExpr)*] ')' ';'

----------------------------------------------------------
--
-- Input, Output and Outputln Commands
--
----------------------------------------------------------

InputCommand :=
 Jr:   'Input' <varname>
 JJ:   <varname> '=' 'JJS.inputInt'     '(' ')' ';'
 JJ:   <varname> '=' 'JJS.inputDouble'  '(' ')' ';'
 JJ:   <varname> '=' 'JJS.inputBoolean' '(' ')' ';'
 JJ:   <varname> '=' 'JJS.inputString'  '(' ')' ';'

OutputCommand :=
 Jr:   'Output' Expr
 JJ:   'JJS.outputInt'     '(' JavaExpr ')' ';'
 JJ:   'JJS.outputDouble'  '(' JavaExpr ')' ';'
 JJ:   'JJS.outputBoolean' '(' JavaExpr ')' ';'
 JJ:   'JJS.outputString'  '(' JavaExpr ')' ';'

OutputlnCommand :=
 Jr:   'Outputln' Expr
 JJ:   'JJS.outputlnInt'     '(' JavaExpr ')' ';'
 JJ:   'JJS.outputlnDouble'  '(' JavaExpr ')' ';'
 JJ:   'JJS.outputlnBoolean' '(' JavaExpr ')' ';'
 JJ:   'JJS.outputlnString'  '(' JavaExpr ')' ';'

----------------------------------------------------------
--
-- Debug and Debugln Commands
--
----------------------------------------------------------

DebugCommand :=
 Jr:   'Debug' Expr
 JJ:   'System.out.print' '(' JavaExpr ')' ';'
 JJ:   'JJS.debugInt'     '(' JavaExpr ')' ';'
 JJ:   'JJS.debugDouble'  '(' JavaExpr ')' ';'
 JJ:   'JJS.debugBoolean' '(' JavaExpr ')' ';'
 JJ:   'JJS.debugString'  '(' JavaExpr ')' ';'

DebuglnCommand :=
 Jr:   'Debugln' Expr
 JJ:   'System.out.println' '(' JavaExpr ')' ';'
 JJ:   'JJS.debuglnInt'     '(' JavaExpr ')' ';'
 JJ:   'JJS.debuglnDouble'  '(' JavaExpr ')' ';'
 JJ:   'JJS.debuglnBoolean' '(' JavaExpr ')' ';'
 JJ:   'JJS.debuglnString'  '(' JavaExpr ')' ';'

----------------------------------------------------------
--
-- TryCall Command
--
----------------------------------------------------------

TryCallCommand := 
 Jr:   'TryCall' FileIORoutineReference 'OnFail' RoutineReferenceOrAssignment
 JJ:   'if' JavaFileIORoutineReference '{}' 'else' '{' JavaRoutineReferenceOrAssignment '}'

----------------------------------------------------------
--
-- TrySet Command
--
----------------------------------------------------------

TrySetCommand :=
 Jr:   'TrySet' StringConversionAssignment 'OnFail' RoutineReferenceOrAssignment
 JJ:   'if' JavaStringConversionAssignment '{}' 'else' '{' JavaRoutineReferenceOrAssignment '}'


==========================================================
==
== Productions that are not Commands that need translation
== from JJ to Java.
==
==========================================================
----------------------------------------------------------
--
-- PrimitiveTypeCategory
--
----------------------------------------------------------

PrimitiveTypeCategory :=

 Jr:   'int'
 JJ:   'int'

 Jr:   'real'
 JJ:   'double'

 Jr:   'bool'
 JJ:   'boolean'

 Jr:   'int[]'
 JJ:   'int[]'

 Jr:   'real[]'
 JJ:   'double[]'

 Jr:   'bool[]'
 JJ:   'boolean[]'

----------------------------------------------------------
--
-- ClassTypeCategory
--
----------------------------------------------------------
ClassTypeCategory :=

 Jr:   'Str'
 JJ:   'String'

 Jr:   <classname>
 JJ:   <classname>

 Jr:   'Str[]'
 JJ:   'String[]'

 Jr:   <classname>'[]'
 JJ:   <classname>'[]'

----------------------------------------------------------
--
-- JavaScalarBoolExpr
--
----------------------------------------------------------
JavaScalarBoolExpr :=
 (* See Expr translation rules *)

----------------------------------------------------------
--
-- LhsExpr
--
----------------------------------------------------------
LhsExpr :=
 (* See Expr translation rules *)

----------------------------------------------------------
--
-- Expr
--
----------------------------------------------------------
Expr :=

 Jr:   <boxname>
 JJ:   <boxname>

 Jr:   <classboxname>.<boxname>
 JJ:   <classboxname>.<boxname>

 Jr:   'and'
 JJ:   '&&'

 Jr:   'or'
 JJ:   '||'

 Jr:   '+'
 JJ:   '+'

 Jr:   '-'
 JJ:   '-'

 Jr:   '*'
 JJ:   '*'

 Jr:   '/'
 JJ:   '/'

 Jr:   '%'
 JJ:   '%'

 Jr:   '<'
 JJ:   '<'

 Jr:   '<='
 JJ:   '<='

 Jr:   '>'
 JJ:   '>'

 Jr:   '>='
 JJ:   '>='

 Jr:   '=='
 JJ:   '=='

 Jr:   '!='
 JJ:   '!='

 Jr:   'not'
 JJ:   '!'

 Jr:   '('
 JJ:   '('

 Jr:   ')'
 JJ:   ')'

 Jr:   '['
 JJ:   '['

 Jr:   ']'
 JJ:   ']'

----------------------------------------------------------
--
-- FileIORoutineReference
--
----------------------------------------------------------
FileIORoutineReference :=
 Jr:   <classboxname>.<routinename> ['with' '(' Expr (',' Expr)* ')']
 JJ:   <classboxname>.<routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

----------------------------------------------------------
--
-- RoutineReferenceOrAssignment
--
----------------------------------------------------------
RoutineReferenceOrAssignment :=
 Jr:   (1) <routinename> ['with' '(' Expr (',' Expr)* ')']
 JJ:       <routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

 Jr:   (2) <classboxname>.<routinename> ['with' '(' Expr (',' Expr)* ')']
 JJ:       <classboxname>.<routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

 Jr:   (3) LhsExpr '=' Expr
 JJ:       JavaLhsExpr '=' JavaExpr

----------------------------------------------------------
--
-- StringConversionAssignment
--
----------------------------------------------------------
StringConversionAssignment :=
 Jr:   (1) <boxname> '=' 'StringToInt' '(' ScalarStrExpr ')'
 JJ:       <boxname> '=' SomeJavaCode '(' JavaScalarStrExpr ')'

 Jr:   (2) <boxname> '=' 'StringToReal' '(' ScalarStrExpr ')'
 JJ:       <boxname> '=' SomeJavaCode '(' JavaScalarStrExpr ')'