The Green Language Grammar 159

The Green Language Grammar 159

The Green Language Jos´ede Oliveira Guimar˜aes Departamento de Computa¸c˜ao UFSCar S˜ao Carlos - SP Brasil e-mail: [email protected] 16 de julho de 2018 arXiv:1306.5930v1 [cs.PL] 25 Jun 2013 2 Sum´ario 1 Introduction 7 2 Basic Elements 9 2.1 Comments........................................ 9 2.2 BasicTypes...................................... ... 9 2.3 LiteralValues................................... ..... 10 2.4 Identifiers ...................................... 12 2.5 Assignments..................................... 12 2.6 ControlStatements............................... ...... 12 2.7 LoopStatements .................................. 13 3 Classes and Methods 15 3.1 Syntax.......................................... 15 3.2 MessageSend..................................... 17 3.3 self............................................ 17 3.4 MethodOverloading ............................... ..... 17 3.5 Assertions...................................... 17 3.6 AbstractClasses ................................. ..... 19 3.7 Why Green Does Not Support Operator Overloading ? . ........... 20 3.8 ExpandedVariables ............................... ..... 20 4 Class Objects 23 4.1 ClassesasObjects ................................ ..... 23 4.2 ConstantDeclaration. ....... 25 4.3 EnumeratedConstants. ...... 26 4.4 WheretheProgramExecutionStarts. ......... 26 5 Inheritance 27 5.1 Introduction.................................... ..... 27 5.2 The Any and AnyClass Classes .............................. 28 5.3 Method Look-up and super ................................ 30 5.4 The subclass Section ................................... 30 5.5 nil .............................................. 32 5.6 AbstractClassesandInheritance . .......... 32 5.7 Abstract Classes, Assertions, and Inheritance . ............... 33 3 4 SUMARIO´ 6 Arrays 35 6.1 Introduction.................................... ..... 35 6.2 MethodsofArrayClasses .. .. .. .. .. .. .. .. ...... 36 6.3 Initialization and Creation of Arrays . ............ 38 6.4 ArraysandInheritance. ....... 39 6.5 Methods with Variable Number of Parameters . ........... 40 6.6 ExpandedArrays.................................. 41 7 The Green Type System 43 7.1 TypesandSubtypes ................................ 43 7.2 RulesforTypeChecking. .. .. .. .. .. .. .. .. ...... 45 7.3 DiscussiononTypes ............................... ..... 47 7.4 AssertionsandSubclassing . ........ 48 7.5 Constructors,Inheritance,andTypes. ............ 50 7.6 TypesandOverloadedMethods. ....... 51 7.7 AssertionsandSubtyping . ....... 52 8 The Basic Classes 55 8.1 TheClasses ...................................... 55 8.2 WrapperClasses .................................. 71 9 Strings 75 10 Class Objects and Subtyping 83 10.1 TypesandClassObjects. ....... 83 10.2 Delegation and Class Objects . ......... 84 10.3 MethodsAddedtoClassObjects . ........ 87 11 Exceptions 91 11.1Definition ...................................... 91 11.2 HierarchyofExceptions . ........ 94 11.3 TypingExceptions ............................... ...... 95 11.4 FixingErrorswithCatchObjects. ..........102 11.5 Comparison with Other Exception Systems . ...........104 11.6 Exception and Meta-Level Programming . ...........105 11.7 Static Type Correctness of Exceptions . ............106 11.8 Exceptions and Enhancing of Objects at Runtime . .............107 11.9 AssertionsandExceptions . .........111 11.10SubtypingandExceptions . .........111 12 Shells and Dynamic Extensions: Metaprogramming in Green 115 12.1Shells ......................................... 115 12.2 A High-Level View of Shell Implementation . ............118 12.3 ShellInheritance ............................... .......119 12.4 ShellInitialization . .........119 12.5 ReflectiveShellClasses. .........121 12.6 ShellsandClassObjects. ........121 12.7 Method interceptAll ...................................121 12.8 DynamicExtension. .. .. .. .. .. .. .. .. .. 123 SUMARIO´ 5 13 Standard Class Objects 127 13.1Introduction................................... 127 13.2Rainbow........................................ 128 13.3TheClassObjects ................................ 129 14 Parameterized Classes 133 15 The Standard Library 137 16 Introspective Reflection 153 A The Green Language Grammar 159 B The Introspective Reflection Library 163 C The Exception Library 187 D The Main Green Classes 197 6 SUMARIO´ Cap´ıtulo 1 Introduction This Report defines the Green Language, an object-oriented language being designed at the Com- puter Science Department of the Federal University of S˜ao Carlos (Universidade Federal de S˜ao Carlos - UFSCar). Language Green separates the subtype from the subclass hierarchy and sup- ports garbage collection, classes as first-class objects, parameterized classes, introspective reflection and a kind of run-time metaobjects called shells. Green will support compile and link-time metaobjects and will have a special module system. These features have not been completely defined and are not discussed in this report. This is not an introduction to object-oriented concepts. We assume the reader knows at least one object-oriented language well. 7 8 CAP´ITULO 1. INTRODUCTION Cap´ıtulo 2 Basic Elements 2.1 Comments In Green, anything between /* and */ is a comment. Nested comments are allowed. So the line, i = 10; /* comment /* i = 5; old code */ still a comment */ is legal and contains one assignment. There is another way to specify a comment: anything after // till the end of the line is a comment. 2.2 Basic Types The language basic types are shown in the table below together with the operators they support. The comparison operators are supported by all types. basic type operators char ++ -- boolean and or not xor byte + - * / % & | ^ ~ ++ -- << >> integer + - * / % & | ^ ~ ++ -- << >> long + - * / % & | ^ ~ ++ -- << >> real + - * / ++ -- double + - * / ++ -- +, -, *, and / are the arithmetic operators. The remainder of division of p by t is given by “p%t”. Operators “&”, “|”, and “^” are the bitwise “and”, “or”, and “xor” (exclusive or). “~” is the bit to bit complement. Operators ++ and -- are only applied to variables and are prefixed. They increase (++) or decrease (--) their operands by one and do not return a value, unlike C++. The operators << and >> are right and left shift of bits. In an expression k << n k may be byte, integer, or long and n may be byte or integer. The Green compiler translates source code into C. The table below shows the mapping between Green and C basic types. 9 10 CAP´ITULO 2. BASIC ELEMENTS Green C char signed char boolean char byte unsigned char integer int long long real float double double So, the semantics of the Green basic types depends on the semantics of the basic types in the C compiler and machine used. We hope to fix the size of integer in 32 bits and introduce 16-bit short integers. Chapter 8 discusses other features of basic types. Table 2.1 shows the precedence of the operators. Operators in the same box have the same precedence. Unary operators and the assignment operator are right associative. The operators << >> < <= > >= == <> are neither left or right associative. That means expressions like (a << 5 << 1) == b == c are illegal. Every other operator is left associative. The higher in the table, the higher the prece- dence. Any similarities between this table and one of Stroustrup book [17] is not a coincidence, although the operator precedence order in Green is rather different from C++. Operators and and or finish their execution as soon as possible. Then, in ok = false and good(); ok = true or good(); the good method would not be called. 2.3 Literal Values A character constant should be enclosed between ’ as in language C. All escape characters of C are valid in Green. The boolean type has two pre-defined values: true and false. These are constants that can be cast to 1 and 0, respectively. A byte value is a literal integer with a postfixed “b”. For example, 2b, 32b, 255b are byte values. Any literal number without a floating point or exponent is an integer literal number that can be postfixed with an i: 3200i, -1i. long values must be postfixed with an upper case L: 3L, 3000000L Any literal number with a floating point or an exponent is a real number. Example: 1.1 320E+5 1e-10 1r A real number may have a trailing “r” as in 1r above. A double real value must have a “d” at its end: 1d 2.1e-10d 445.076d The rules for valid numbers (byte, integer, long, real and double) are the rules of the C compiler used. 2.3. LITERAL VALUES 11 () method call [] subscripting . method/variable selection ~ bitwise complement + unary plus - unary minus not logical not ++ increment -- decrement << shift left >> shift right & bitwise and ^ bitwise xor | bitwise or / divide & multiply % remainder + binary plus - binary minus == equal <> not equal < less than <= less than or equal > greater than >= greater than or equal and logical and xor exclusive or or logical or = assignment Figura 2.1: Operator precedence table 12 CAP´ITULO 2. BASIC ELEMENTS No automatic conversion is made among values of the different basic types. Any conversion must be explicitly made. For example, to assign a real variable r to an integer variable i one should write: i = integer.cast(r); This subject is further discussed in Chapter 8. 2.4 Identifiers An identifier is a sequence of one letter followed by any number of letters, digits, and underscore (“ ”). There is no limit for the size of an identifier, although there may be problems when translating long names into C source code. Upper and lower case are considered different. We

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    220 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us