Transcript PPT

SPECS Translator
A Aniruddha
Introduction
SPECS (Significantly Prettier and Easier C++
Syntax)
• Alternate syntactic binding for C++.
• Reduces syntactic errors.
• Includes new syntax for declaration/definition for
types, functions and objects.
• Changes operators and control structures.
• Better syntactic differentiation of dissimilar
constructs.
SPECS syntactic design principles
• Syntactic consistency.
• Syntactic differentiation of dissimilar
constructs.
• Better readability.
Syntactic consistency
Creation of types in C++:
Class Base {…} ;
typedef struct {…} vector;
typedef int (*fp) ();
enum result {…} ;
In SPECS:
type
type
type
type
Base : class {…}
vector : class {[public] …}
fp : ^((void)->int);
result : enum {…}
Syntactic differentiation of dissimilar
constructs
C++ - insufficient differentiation
Declaration of functions and objects:
int (f) (int, char);
int (*fp) (int, char);
In SPECS:
func f : ((int, char) ->
obj fp : ^((int, char) ->
int);
int);
Syntax : class
(pure virtual functions)
SPECS :
type Base : class
{
func print_func : abstract ((& ostream)
-> void) ;
} // Note : no ;
C++ :
Class Base
{
virtual void print_func (ostream&) = 0;
};
Syntax : class
(inheritance and static members)
type Derived : class
inherits public Base
{
obj a : common int;
}
class Derived : public Base
{
static int a;
}
Syntax: class
(access control, constructors, destructors and friends)
type TestClass : class
{
[private]
obj a : int ;
[friend]
func operator << : ((& ostream, &
TestClass) -> & ostream );
[public]
func ctor : (int) ;
func dtor : (void);
}
Syntax: class
(Operators and cast)
type MyInt : class
{
[public]
func operator pre ++ : ((void)-> & MyInt);
func operator post ++ : ((void)-> & MyInt);
func defined_cast : ((void) -> int);
}
class MyInt
{
public:
MyInt & operator ++ ();
// pre
MyInt & operator ++ (int); // Dummy int for post
operator int ();
};
Syntax: Templates
type GenericClass <[class X]> : class
{…}
template <class X> class GenericClass
{…};
Advantages:
• Keyword ‘template’ not required.
• C++:
TemplateClass<(1>2)> // Parens required
SPECS: TemplateClass<[1>2]>
• C++ : array<auto_ptr<X> > // Space required
SPECS: array<[auto_ptr<[X]>]>
Syntax: Operators
Operator
C++
Specs
Assignment
a = b
a += b …
a := b
a +:= b …
Equality
a == b
a = b
address-of
&val
val@
Dereference *ptr
ptr^
Bitwise XOR a ^ b
a ! b
Syntax: Statements
• Loop body must be block.
• switch
– No break required.
– No default fall-through.
– case can have a list of constant-expression.
– case/default must be followed by a block.
Example:
switch (i)
{
case 1,2,3 : { cout << “1/2/3” ;}
case 4: {cout << “4”; }
}
Commonalities with C++
• All inbuilt types are identically named. Literal values
are specified in exactly the same way.
• The operators have the same precedence and
associativity.
• Scope rules, function call resolution, implicit
conversion, access default and control structure
semantics(except switch) are identical.
• The same preprocessor and standard libraries are
used for both languages.
• RTTI, namespaces, exception handling are identical
in syntax and semantics.
Implementation
• Implemented using TXL(Turing Xtender/Tree
Transformation Language).
– Parse : construct parse tree based on input
grammar.
– Transform : based on defined rules.
– Unparse : In-order traverse of parse tree with
formatting.
• Can convert
–
–
–
–
Simple object and type declarations.
Simple Function declaration and definitions.
Assignment statements.
Switch and class syntax.
Thank You