1 / 40

PtiLoo

Ajout à faire pour 2010 - version 18/11/2014 05:35 "animation" pour passe1(AST) et passe 2(resolve-name) avec: double déclaration (err passe 1) non déclaration ou erreur de type (err passe 2) comparaison entre: instruction et exp (AST only) bloc (AST + portée) declaration (AST + TS)

Download Presentation

PtiLoo

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ajout à faire pour 2010 - version 18/11/2014 05:35 • "animation" pour passe1(AST) et passe 2(resolve-name) avec: • double déclaration (err passe 1) • non déclaration ou erreur de type (err passe 2) • comparaison entre: • instruction et exp (AST only) • bloc (AST + portée) • declaration (AST + TS) • méthode, classe (AST + TS + portée) PtiLoo Compléments de Cours pour réaliser les Extensions version 18/11/2014 05:35

  2. ratio.loo (1) extern int printf (string, ...); class rational { int num, denom; rational (int n, int d) { num = n; denom = d; } void print (string s) { printf ("%s = %d / %d\n", s, num, denom); } rational add (rational o) { rational r; r = new rational (num * o.denom + o.num * denom, denom * o.denom); return r; } }

  3. ratio.loo (2) class safe_rat extends rational { bool ok; safe_rat (int n, int d) { super (n, d); if (d == 0) { n = 0; ok = false; } else ok = true; } rational add (rational o) { rational r; if (ok) r = super.add (o); else r = new safe_rat (0, 0); return r; } safe_rat safe_add (safe_rat o) { safe_rat r; if (ok && o.ok) r = new safe_rat (num * o.denom + o.num * denom, denom * o.denom); else r = new safe_rat (0, 0); return r; } }

  4. ratio.loo (3) main () { rational r1, r2, r3, r4, r5; r1 = new rational (1, 2); r2 = new rational (1, 4); r3 = new rational (1, 8); r4 = r3.add (r2.add (r1.add (new rational (1, 16)))); r5 = r3.add (r2).add (r1).add (new rational (1, 16)); r1.print ("r1"); r2.print ("r2"); r3.print ("r3"); r4.print ("r4"); r5.print ("r5"); safe_rat s1, s2, s3, s4, s5; s1 = new safe_rat (1, 0); s2 = new safe_rat (3, 4); s3 = new safe_rat (1, s4 = s2.safe_add (s3); s5 = s1.safe_add (s3); s1.print ("s1"); s2.print ("s2"); s3.print ("s3"); s4.print ("s4"); s5.print ("s5"); r5 = s2.add (r1); r5.print ("r5"); r5 = s2.safe_add (s1); r5.print ("r5"); r5 = s2.add (s1); r5.print ("r5"); r2 = s2; r3 = r2.add (s3); r3.print ("r3"); r1 = s1; r4 = r1.add (s4); r4.print ("r4"); // division par 0 printf ("%s %s\n", r5.type_name (), s5.type_name ()); }

  5. ptiloo.cc try { symbol_table.init(); yyparse (); if (!errors) { cerr << "analyse lexico-syntaxique et declarations OK\n"; for (ClassList::const_iterator p = classes.begin(); p != classes.end(); ++p) (*p)->analyse () ; main_function->analyse (); if (! errors) cerr << "analyse semantique OK\n"; // production du code C cout << "#include <run_time.h>\n"; if (!externals.empty()) { cout << "\n/*********** definition des fonctions externes ************/\n\n"; for (ExternalList::const_iterator p = externals.begin (); p != externals.end(); ++p) (*p)->produce_declaration(); } for (ClassList::const_iterator p = classes.begin (); p != classes.end(); ++p) (*p)->produce_definition (); cout << "\n/*********************************************************/\n\n"; main_function->produce_definition (); cerr << "production de code OK\n"; } } else { res = 1; cerr << "erreurs a la compilation, code non produit\n"; } }

  6. PtiLoo PROLOGUE version 18/11/2014 05:35

  7. ptiloo.cc ("shunté" pour le Prologue) try { // **** symbol_table.init(); yyparse (); if (!errors) { cerr << "analyse lexico-syntaxique et declarations OK\n"; *** Shunt PFZ for (ClassList::const_iterator p = classes.begin(); p != classes.end(); ++p) (*p)->analyse () ; main_function->analyse (); if (! errors) cerr << "analyse semantique OK\n"; // production du code C cout << "#include <run_time.h>\n"; if (!externals.empty()) { cout << "\n/*********** definition des fonctions externes ************/\n\n"; for (ExternalList::const_iterator p = externals.begin (); p != externals.end(); ++p) (*p)->produce_declaration(); } for (ClassList::const_iterator p = classes.begin (); p != classes.end(); ++p) (*p)->produce_definition (); cout << "\n/*********************************************************/\n\n"; main_function->produce_definition (); cerr << "production de code OK\n"; } } /**** Shunt PFZ ***/ else { res = 1; cerr << "erreurs a la compilation, code non produit\n"; } }

  8. .loo vers SA en XML + CSS + DTD Essais/*.loo Essais/*.xml Essais/mainLoo.css Essais/looSA.dtd . ratio.loo ratio.xml mainLoo.css looSA.dtd

  9. PtiLoo Extensions version 18/11/2014 05:35

  10. Nom: Prénom: Groupe: PtiLoo - Ext # 26/11/2007

  11. E#0 : simple.loo => simple.c int main () { int x = 0; int y = 0; int z = 0; int t = 0; int q = 0; int r = 0; x = 1; y = 2; z = 3; t = 4; printf ("%d %d %d %d\n", x, y, z, t); if (t - z == y - x) { int q = 0; int r = 0; q = t - y; r = z - x; printf ("%d %d\n", q, r); } q = t + x; r = y + z; printf ("%d %d\n", q, r); } Déclarations en début de bloc main () { int x; int y; x = 1; // pas d'initialisation int x = 1 pour l'instant y = 2; int z; int t; z = 3; t = 4; printf ("%d %d %d %d\n", x, y, z, t); if (t - z == y - x) { // int t interdit car double le t global int q; int r; q = t - y; r = z - x; printf ("%d %d\n", q, r); } int q; int r; // autorise car ne doublent pas les q et r précédents q = t + x; r = y + z; printf ("%d %d\n", q, r); } Affectations à "leur place" Déclarations en début de bloc Affectations à "leur place"

  12. PtiLoo Extension #1 Operateur "++" et "--" Opérateur "!" & Fonction externe _FACT

  13. E#0=>E#1 : oper.loo => oper.c class A {}; main () { int i, j; string s; A a; j = i++; j = --i; // erreurs : // printf++; // --A; // a++; // --s; } typedef struct _A* A; /************ définition de la classe A ************/ struct _A { _META* _type; }; A _A_A (A); _METH _A__VTBL[] = { (_METH) &_Object_type_name }; _META _A__META = { "A", &_Object__META, _A__VTBL }; /****méthodes de A ******/ A _A_A (A this) { _Object_Object ( (Object) this ); this->_type = &_A__META; { } return this; } int main () { int i = 0; int j = 0; string s = 0; A a = 0; j = i++; j = --i; } Affectations à "leur place"

  14. Extensions PtiLoo: opérateurs ++ et -- ALS AST Sem

  15. Extensions PtiLoo: opérateurs ++ et -- ALS AST ?? … type entier ??… Sem

  16. Extensions PtiLoo: opérateur ! et fonction Xterne ALS AST Sem

  17. Extensions PtiLoo: opérateur ! et fonction Xterne ALS AST Sem

  18. PtiLoo Extension #2 Initialisation des Variables Initialisation des Attributs de Classe

  19. E#0 : Un Souci évité grâce à la Règle Java ! int main () { int x = 0; int y = 0; int z = 0; int t = 0; int q = 0; int r = 0; x = 1; y = 2; z = 3; printf ("%d %d %d %d\n", x, y, z, t); if (t - z == y - x) { int t = 0; int q = 0; int r = 0; t = 4; // erreur de t q = t - y; r = z - x; printf ("%d %d\n", q, r); } q = t + x; r = y + z; printf ("%d %d\n", q, r); } Déclarations en début de bloc main () { int x; int y; x = 1; // pas d'initialisation int x = 1 pour l'instant y = 2; int z; int t; z = 3; // t = 4; printf ("%d %d %d %d\n", x, y, z, t); if (t - z == y - x) { t = 4; int t ; // heureusement interdit int q; int r; q = t - y; r = z - x; printf ("%d %d\n", q, r); } int q; int r; // autorise car ne doublent pas les q et r précédents q = t + x; r = y + z; printf ("%d %d\n", q, r); } Affectations à "leur place" Déclarations en début de bloc Affectations à "leur place"

  20. E#2(variables: sol1) : init.loo => init.c Initialisations à "leur place" int main () { int i = 1; i = 2; { int j = i; i = j + 1; printf ("%d %d\n", i, j); { A a = _A_A (_NEW (A), 100); printf ("%d %d %d\n", _OCHECK (a, A)->_A_x, _OCHECK (a, A)->_A_j, _OCHECK (a, A)->_A_k ); { B b = _B_B (_NEW (B), 200, 300); printf ("%d %d %d %d %d\n", _OCHECK (b, B)->_A_x, _OCHECK (b, B)->_A_j, _OCHECK (b, B)->_A_k, _OCHECK (b, B)->_B_m, _OCHECK (b, B)->_B_n ); } } } } main () { int i = 1; i = 2; int j = i; i = j + 1; printf ("%d %d\n", i, j); A a = new A (100); printf ("%d %d %d\n", a.x, a.j, a.k); B b = new B (200, 300); printf ("%d %d %d %d %d\n", b.x, b.j, b.k, b.m, b.n); } dans des "nouveaux blocs"

  21. E#2(variables: sol2) : init.loo => init.c Déclarations en début de bloc int main () { int i = 0; int j = 0; A a ; B b ; i = 1; i = 2; j = i; i = j + 1; printf ("%d %d\n", i, j); a = _A_A (_NEW (A), 100); printf ("%d %d %d\n", _OCHECK (a, A)->_A_x, _OCHECK (a, A)->_A_j, _OCHECK (a, A)->_A_k ); b = _B_B (_NEW (B), 200, 300); printf ("%d %d %d %d %d\n", _OCHECK (b, B)->_A_x, _OCHECK (b, B)->_A_j, _OCHECK (b, B)->_A_k, _OCHECK (b, B)->_B_m, _OCHECK (b, B)->_B_n ); } main () { int i = 1; i = 2; int j = i; i = j + 1; printf ("%d %d\n", i, j); A a = new A (100); printf ("%d %d %d\n", a.x, a.j, a.k); B b = new B (200, 300); printf ("%d %d %d %d %d\n", b.x, b.j, b.k, b.m, b.n); } Initialisations à "leur place"

  22. Extensions PtiLoo: initialisation des déclarations Sol1 ALS AST Sem

  23. E#0 => E#2 variables : ptiloo.yacc Sol1 static LocalVariable* declare_variable(const char* no, const Type& ty) { LocalVariable* va = new LocalVariable(no, ty, yylineno); current_block->add (*va); symbol_table.declare (no, va, yylineno); return va; } %type< > %% declvars : type IDENT { declare_variable ($2, *$1); $$ = $1; } | declvars ',' IDENT { declare_variable ($3, *$1); $$ = $1; } => Erreurs en phase syntaxique Expression * init_opt

  24. E#0 => E#2 (Variables) : entity.h Sol1 class Variable : public Entity { // classe abstraite des variables public : Variable (const string& cn, const Type& ty, int li) : Entity (cn, li), vtype (ty){ } const Type& type () const { return vtype; } void analyse (); protected : const Type& vtype; }; class LocalVariable : public Variable { public : LocalVariable (const string& cn, const Type& ty, int li) : Variable (cn, ty, li) { } void produce_definition () const; }; typedef autolist<LocalVariable> LocalVariableList; Expression * init_opt

  25. E#0 => E#2(Variables) : stat.h void analyse (const Type&, bool); int compute_temporaries (); void generate (const Type&) const;// declare_locals + generate_body void declare_locals () const; void produce_body (const Type&) const; protected : BlockStat* embedding; int line; BlockScope scope; LocalVariableList locals; StatementList body; }; class BlockStat : public Statement { // l'instruction { ... } public : …. void add (LocalVariable& v) { locals.push_back (&v); } void add (Statement& s) { body.push_back (&s); } …. Sol1

  26. E#0 => E#2 (Variables) : stat_pro.cc Sol1 void BlockStat :: declare_locals () const { bool first = true; for (LocalVariableList::const_iterator p=locals.begin(); p=locals.end();++p) {if (first) first=false; (*p)-> produce_definition(); } } void BlockStat :: produce_body (const Type& res) const { bool first = true; for (StatementList::const_iterator p=body.begin(); p=body.end();++p) (*p)-> generate(res); } void BlockStat :: generate (const Type& res) const { declare_locals() ; produce_body(res); }

  27. E#0 => E#2 (Variables) : stat_ana.cc void BlockStat :: analyse (const Type& res, bool in_constr) const { symbol_table.enter_scope(const-cast<BlockScope&>(scope)); for (StatementList::const_iterator p=body.begin(); p=body.end();++p) (*p)-> analyse(res, in_constr); symbol_table.leave_scope(); } analyse les déclarations ici ? ou dans la classe Variable ? ( => entity.ana.cc)

  28. E#0 => E#2(Variables): entity_ana.cc, entity_pro.cc stat_pro.cc Sol1 void Variable::analyse (bool in_constr) { …. } void LocalVariable::produce_definition () const { cout << vtype.c_name << ' ' << c_name << " = 0 "; ……. cout <<";" ; } void BlockStat::generate () const { for ( ) { // traiter la StatementList du bloc (body) if (necessaire) cout << '{' ; // ouvrir un extra bloc ……. } ……. for ( ) { // fermer tous les extras blocs ouverts cout << '}' ; } }

  29. E#2(attributs): .loo => .c typedef struct _A* A ; s truct _A { _META* _type; int _A_ax; string _A_as; }; A _A_A (A this, int aa) { _Object_Object ((Object) this); this->_type = &_A__META; this->_A_ax = 50; this->_A_as = "Hello"; this->_A_ax = aa; printf ("\n\t%s\n", this->_A_as); return this; } typedef struct _B* B; struct _B { _META* _type; int _A_ax; string _A_as; int _B_bx; }; class A { int ax = 50; string as = "Hello" ; A (int aa) { ax = aa; printf ("\n\t%s\n", as); } } class B extends A { int bx; B (int bb) { super (bb); bx = bb; } } code C partiel

  30. E#0 => E#2:attributs: Suivez la piste …

  31. E#0 => E#2 (Attributs): entity.h & entity.cc class ClassType : public Type { friend class Constructor; public : void add_attribute (const char*, const Type&, int); class Attribute : public Variable { public : Attribute (const string& cn, const Type& ty, const ClassType& cl, int li) : Variable (cn, ty, li), klass (cl) { } ~Attribute () { } const ClassType& attribute_class () const { return klass; } void produce_definition () const; protected : const ClassType& klass; }; Expression* ClassType::add_attribute (const char*, const Type&, int) { new Attribute() ; … };

  32. E#0 => E#2 (Attributs) : entity_ana.cc void ClassType::analyse () { // construire un constructeur implicite pour faciliter la production de code if (is_defined()) { current_class = this; for (autolist<Attribute>::iterator it = pattrs.begin(); it != pattrs.end(); ++it) (*it)->analyse(true); symbol_table.enter_scope (*scope); // analyser les methodes et le constructeur constr->analyse (); for (autolist<Method>::iterator it = pmeths.begin(); it != pmeths.end(); ++it) (*it)->analyse(); symbol_table.leave_scope (); current_class = 0; } else name_error ("classe non définie", c_name, line); }

  33. E#0 => E#2(Attributs) : entity_pro.cc void ClassType::produce_attribute_initialization () const { …….. } void Attribute::produce_initialization () const { cout << "this->" << c_name << c_name << " = "; …….. cout <<";" ; }

  34. PtiLoo Extension #3 Instruction "for"

  35. E#3 : for.loo => for.c main() { int x; int y; int i; x = 0; for (int k = 0; k < 10; k = k + 1) x = x + k; for (int k = 0; k < 10; k = k + 1) { x = x + k; y = y + x; } for (int p = 1, q = 10; p < q; p = p + 1, q = q - 1) { x = x + p; y = y - q;} } Déclarations à "leur place" des variables de boucle int main () { int x = 0; int y = 0; int i = 0; x = 0; { int k = 0; while (k < 10){ x = x + k; k = k + 1; } } { int k = 0; while (k < 10){ {x = x + k;y = y + x;} k = k + 1; } } {int p = 1;int q = 10; while (p < q){ {x = x + p; y = y - q; } p = p + 1;q = q - 1; } } } dans des "extras blocs"

  36. Extensions PtiLoo: boucle "for" ALS Sol1 AST Sem

  37. Extensions PtiLoo: boucle "for" ALS AST Sem Sol2

  38. PtiLoo Extension #4 Instruction "break" et Etiquettes

  39. E#4 : break.loo => break.c AA : while (i < 100) { if (i == 50) break; j = j + 1; k = i * j; if (k < 1000) BB : while (k > 0) if (k == 10) break AA; else if (k == 11) break BB; else k = k - 1; else k = 0; i = i + 1; } { while (i < 100) { if (i == 50) break; j = j + 1; k = i * j; if (k < 1000) { while (k > 0) if (k == 10) goto _L1; else if (k == 11) goto _L2; else k = k - 1; _L2 : ; } else k = 0; i = i + 1; } _L1 : ; } dans des "nouveaux blocs" Structure de bloc <=> pile d'étiquettes Etiquettes en fin de bloc

  40. Extensions PtiLoo: "break" et étiquettes ALS Sol1 AST Sem

More Related