Created
November 4, 2012 12:56
-
-
Save joseanpg/4011807 to your computer and use it in GitHub Desktop.
Understanding cool-tree.aps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Datatype -> | |
class Datatype_class : public tree_node | |
constructor_of_Datatype -> | |
class constructor_class : public Datatype_class { | |
... | |
constructor_class(...) | |
Datatype constructor( par1, par2, ... ) { | |
return new constructor_class(par1, par2, ...); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module COOL where | |
data Symbol = Symbol { id :: Int | |
, str :: String | |
} | |
data Program = Program { classes :: Classes} | |
data Class_ = Class_ { name :: Symbol | |
, parent :: Symbol | |
, features :: Features | |
, filename :: Symbol | |
} | |
data Feature = Method { name :: Symbol | |
, formals :: Formals | |
, return_type :: Symbol | |
, expr :: Expression | |
} | |
| Attr { name :: Symbol | |
, type_decl :: Symbol | |
, init :: Expression | |
} | |
data Formal = Formal { name :: Symbol | |
, type_decl: Symbol | |
} | |
data Case = Branch { name :: Symbol | |
, type_decl :: Symbol | |
, expr :: Expression | |
} | |
data Expression = Assign { name :: Symbol | |
, expr :: Expression | |
} | |
| Static_dispatch { expr :: Expression | |
, type_name :: Symbol | |
, name :: Symbol | |
, actual :: Expressions | |
| Dispatch { expr :: Expression | |
, name :: Symbol | |
, actual :: Expressions | |
| Cond { pred :: Expression | |
, then_exp :: Expression | |
, else_exp :: Expression | |
| Loop { pred :: Expression | |
, body :: Expression | |
} | |
| Typcase { expr :: Expression | |
, cases :: Cases | |
} | |
| Block { body :: Expressions } | |
| Let { identifier :: Symbol | |
, type_decl :: Symbol | |
, init :: Expression | |
, body :: Expression | |
} | |
| Plus { e1 :: Expression | |
, e2 :: Expression | |
} | |
| Sub { e1 :: Expression | |
, e2 :: Expression | |
} | |
| Mul { e1 :: Expression | |
, e2: Expression | |
} | |
| Divide { e1 :: Expression | |
, e2: Expression | |
} | |
| Neg { e1 :: Expression } | |
| Lt { e1 :: Expression | |
, e2 :: Expression | |
} | |
| Eq { e1 :: Expression | |
, e2 :: Expression | |
} | |
| Leq { e1 :: Expression | |
, e2 :: Expression | |
} | |
| Comp { e1 :: Expression} | |
| Int_const { token :: Symbol } | |
| Bool_const { val :: Boolean } | |
| String_const { token :: Symbol } | |
| New_ { type_name :: Symbol } | |
| Isvoid { e1 :: Expression } | |
| No_expr | |
| Object { name :: Symbol } | |
type Classes = [Class_] | |
type Features = [Feature] | |
type Formals = [Formal] | |
type Expressions = [Expression] | |
type Cases = [Case] | |
{- | |
-- cool-tree.aps | |
-- A (typed) tree language for Cool abstract syntax trees | |
module COOL[] begin | |
type Symbol; | |
type Boolean; | |
phylum Program; | |
phylum Class_; | |
phylum Classes = LIST[Class_]; | |
phylum Feature; | |
phylum Features = LIST[Feature]; | |
phylum Formal; | |
phylum Formals = LIST[Formal]; | |
phylum Expression; | |
phylum Expressions = LIST[Expression]; | |
phylum Case; | |
phylum Cases = LIST[Case]; | |
constructor program(classes : Classes) : Program; | |
constructor class_(name : Symbol; parent: Symbol; | |
features : Features; filename : Symbol): Class_; | |
-- Features: | |
constructor method(name : Symbol; | |
formals : Formals; | |
return_type : Symbol; | |
expr: Expression) : Feature; | |
constructor attr(name, type_decl : Symbol; | |
init : Expression) : Feature; | |
-- Formals | |
constructor formal(name, type_decl: Symbol) : Formal; | |
-- Case | |
constructor branch(name, type_decl: Symbol; expr: Expression): Case; | |
-- Expressions | |
constructor assign(name : Symbol; expr : Expression) : Expression; | |
constructor static_dispatch(expr: Expression; | |
type_name : Symbol; | |
name : Symbol; | |
actual : Expressions) : Expression; | |
constructor dispatch(expr : Expression; | |
name : Symbol; | |
actual : Expressions) : Expression; | |
constructor cond(pred, then_exp, else_exp : Expression): Expression; | |
constructor loop(pred, body: Expression) : Expression; | |
constructor typcase(expr: Expression; cases: Cases): Expression; | |
constructor block(body: Expressions) : Expression; | |
constructor let(identifier, type_decl: Symbol; | |
init, body: Expression): Expression; | |
constructor plus(e1, e2: Expression) : Expression; | |
constructor sub(e1, e2: Expression) : Expression; | |
constructor mul(e1, e2: Expression) : Expression; | |
constructor divide(e1, e2: Expression) : Expression; | |
constructor neg(e1: Expression) : Expression; | |
constructor lt(e1, e2: Expression) : Expression; | |
constructor eq(e1, e2: Expression) : Expression; | |
constructor leq(e1, e2: Expression) : Expression; | |
constructor comp(e1: Expression) : Expression; | |
constructor int_const(token: Symbol) : Expression; | |
constructor bool_const(val: Boolean) : Expression; | |
constructor string_const(token: Symbol) : Expression; | |
constructor new_(type_name: Symbol): Expression; | |
constructor isvoid(e1: Expression): Expression; | |
constructor no_expr(): Expression; | |
constructor object(name: Symbol): Expression; | |
end; | |
-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
phylum Feature; | |
constructor method(name : Symbol; | |
formals : Formals; | |
return_type : Symbol; | |
expr: Expression) : Feature; | |
constructor attr(name, type_decl : Symbol; | |
init : Expression) : Feature; | |
-- APS is translated to C++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Example | |
data Feature = Method { name :: Symbol | |
, formals :: Formals | |
, return_type :: Symbol | |
, expr :: Expression | |
} | |
| Attr { name :: Symbol | |
, type_decl :: Symbol | |
, init :: Expression | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************************* | |
cool-tree.h | |
*************************************************************************************/ | |
// define simple phylum - Feature | |
typedef class Feature_class *Feature; | |
class Feature_class : public tree_node { | |
public: | |
tree_node *copy() { return copy_Feature(); } | |
virtual Feature copy_Feature() = 0; | |
#ifdef Feature_EXTRAS | |
Feature_EXTRAS | |
#endif | |
}; | |
/* ... */ | |
// define constructor - method | |
class method_class : public Feature_class { | |
protected: | |
Symbol name; | |
Formals formals; | |
Symbol return_type; | |
Expression expr; | |
public: | |
method_class(Symbol a1, Formals a2, Symbol a3, Expression a4) { | |
name = a1; | |
formals = a2; | |
return_type = a3; | |
expr = a4; | |
} | |
Feature copy_Feature(); | |
void dump(ostream& stream, int n); | |
#ifdef Feature_SHARED_EXTRAS | |
Feature_SHARED_EXTRAS | |
#endif | |
#ifdef method_EXTRAS | |
method_EXTRAS | |
#endif | |
}; | |
// define constructor - attr | |
class attr_class : public Feature_class { | |
protected: | |
Symbol name; | |
Symbol type_decl; | |
Expression init; | |
public: | |
attr_class(Symbol a1, Symbol a2, Expression a3) { | |
name = a1; | |
type_decl = a2; | |
init = a3; | |
} | |
Feature copy_Feature(); | |
void dump(ostream& stream, int n); | |
#ifdef Feature_SHARED_EXTRAS | |
Feature_SHARED_EXTRAS | |
#endif | |
#ifdef attr_EXTRAS | |
attr_EXTRAS | |
#endif | |
}; | |
/************************************************************************************* | |
cool-tree.cc fragment | |
*************************************************************************************/ | |
Feature method(Symbol name, Formals formals, Symbol return_type, Expression expr) | |
{ | |
return new method_class(name, formals, return_type, expr); | |
} | |
Feature attr(Symbol name, Symbol type_decl, Expression init) | |
{ | |
return new attr_class(name, type_decl, init); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment