Skip to content

Commit

Permalink
Ajout des opérations arithmétiques.
Browse files Browse the repository at this point in the history
git-svn-id: http://ladystorm.bigmadarea.net/svn/nf11@14 1e871849-ca45-4617-b4d0-c39c463a1ac7
  • Loading branch information
jeremy committed May 5, 2010
1 parent fc31bba commit 0901380
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
29 changes: 18 additions & 11 deletions src/logoparsing/Logo.g
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ tokens {
LC = 'LC' ;
BC = 'BC' ;
VE = 'VE';
PLUS = '+' ;
MOINS = '-' ;
MULT = '*' ;
DIV = '/' ;
}
@lexer::header {
package logoparsing;
Expand All @@ -27,8 +31,7 @@ tokens {
return valide;
}
}
INT : ('0'..'9')+ ;
ADD : ('+' | '-');
INT : ('0'..'9')+ ;
WS : (' '|'\t'|('\r'? '\n'))+ { skip(); } ;

programme : liste_instructions -> ^(PROGRAMME liste_instructions)
Expand All @@ -41,18 +44,22 @@ instruction :
| TD^
| TG^
| REC^
| FCAP^
| FCC^ )
expression
| FPOS^ expression expression
| FCAP^ )
expr
| FPOS^ expr expr
| FCC^ INT
| LC^
| BC^
| VE^
;
expression :
INT ^'+' INT
expr :
sumExpr ;
sumExpr:
multExpr ((PLUS^|MOINS^) multExpr)*
;
exp1 :
INT
multExpr :
atom ((MULT^|DIV^) atom)*
;

atom:
INT | '('! expr ')'!
;
33 changes: 15 additions & 18 deletions src/logoparsing/LogoTree.g
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@ options {
{Log.appendnl("Programme principal");}
;
instruction :
^(AV a = expression) {double m = Double.parseDouble($a.getText()); traceur.avance(m);}
| ^(TD a = INT) {double m = Double.parseDouble($a.getText()); traceur.tourneDroite(m);}
| ^(TG a = INT) {double m = Double.parseDouble($a.getText()); traceur.tourneGauche(m);}
| ^(REC a = INT) {double m = Double.parseDouble($a.getText()); traceur.recule(m);}
| ^(FPOS a = INT b = INT) {double x = Double.parseDouble($a.getText()); double y = Double.parseDouble($b.getText()); traceur.fixePosition(x, y);}
| ^(FCAP a = INT) {double m = Double.parseDouble($a.getText()); traceur.fixeCap(m);}
| ^(FCC a = INT) {int m = Integer.parseInt($a.getText()); traceur.couleur(m);}
| (LC) {traceur.setTrace(false);}
| (BC) {traceur.setTrace(true);}
^(AV a = expr) {traceur.avance($a.v);}
| ^(TD a = expr) {traceur.tourneDroite($a.v);}
| ^(TG a = expr) {traceur.tourneGauche($a.v);}
| ^(REC a = expr) {traceur.recule($a.v);}
| ^(FPOS a = expr b = expr) {traceur.fixePosition($a.v, $b.v);}
| ^(FCAP a = expr) {traceur.fixeCap($a.v);}
| ^(FCC c = INT) {int m = Integer.parseInt($c.getText()); traceur.couleur(m);}
| LC {traceur.setTrace(false);}
| BC {traceur.setTrace(true);}
| VE {traceur.init();}
;
;
expression returns [Double d] :
^('+' a = INT b = INT) {i = Double.parseDouble($a.getText()) + Double.parseDouble($a.getText());}
//expression '+'^ a = exp1 {setVar(getVar() + Double.parseDouble($a.getText())));
| a = INT {i = Double.parseDouble($a.getText());}
expr returns [double v] :
^(PLUS x=expr y=expr) {$v = $x.v + $y.v;}
| ^(MOINS x=expr y=expr) {$v = $x.v - $y.v;}
| ^(MULT x=expr y=expr) {$v = $x.v * $y.v;}
| ^(DIV x=expr y=expr) {$v = $x.v / $y.v;}
| a = INT {$v = Double.parseDouble($a.getText());}
;
//exp1 :
// INT
//;

0 comments on commit 0901380

Please sign in to comment.