python: support for elif

parent 95652a0d
from random import randint
m = randint(0, 100)
print("j'ai choisi un nombre entre 0 et 100")
print("vous devez le trouver")
while True:
x = input("votre choix : ")
print(x)
if x < 0 or x > 100:
print("j'ai dit entre 0 et 100")
elif x < m:
print("trop petit")
elif x > m:
print("trop grand")
else:
print("bravo !")
break
......@@ -23,7 +23,7 @@
let id_or_kwd =
let h = Hashtbl.create 32 in
List.iter (fun (s, tok) -> Hashtbl.add h s tok)
["def", DEF; "if", IF; "else", ELSE;
["def", DEF; "if", IF; "else", ELSE; "elif", ELIF;
"return", RETURN; "print", PRINT; "while", WHILE;
"for", FOR; "in", IN;
"and", AND; "or", OR; "not", NOT;
......
......@@ -98,7 +98,7 @@ let rec expr env {Py_ast.expr_loc = loc; Py_ast.expr_desc = d } = match d with
| Py_ast.Eint s ->
constant ~loc s
| Py_ast.Estring _s ->
assert false (*TODO*)
mk_unit ~loc (*FIXME*)
| Py_ast.Eident id ->
if not (Mstr.mem id.id_str env.vars) then
Loc.errorm ~loc "unbound variable %s" id.id_str;
......
......@@ -63,7 +63,7 @@
%token <string> STRING
%token <Py_ast.binop> CMP
%token <string> IDENT
%token DEF IF ELSE RETURN PRINT WHILE FOR IN AND OR NOT NONE TRUE FALSE
%token DEF IF ELSE ELIF RETURN PRINT WHILE FOR IN AND OR NOT NONE TRUE FALSE
%token FROM IMPORT
%token EOF
%token LEFTPAR RIGHTPAR LEFTSQ RIGHTSQ COMMA EQUAL COLON BEGIN END NEWLINE
......@@ -98,7 +98,7 @@ file:
;
import:
| FROM m=ident IMPORT f=ident NEWLINE
| FROM _m=ident IMPORT _f=ident NEWLINE
{ () (* FIXME: check legal imports *) }
def:
......@@ -186,9 +186,7 @@ stmt: located(stmt_desc) { $1 };
stmt_desc:
| s = simple_stmt NEWLINE
{ s.stmt_desc }
| IF c = expr COLON s = suite
{ Sif (c, s, []) }
| IF c = expr COLON s1 = suite ELSE COLON s2 = suite
| IF c = expr COLON s1 = suite s2=else_branch
{ Sif (c, s1, s2) }
| WHILE e = expr COLON b=loop_body
{ let a, l = b in Swhile (e, a, l) }
......@@ -196,6 +194,15 @@ stmt_desc:
{ let a, l = b in Sfor (x, e, a.loop_invariant, l) }
;
else_branch:
| /* epsilon */
{ [] }
| ELSE COLON s2=suite
{ s2 }
| ELIF c=expr COLON s1=suite s2=else_branch
{ [mk_stmt (floc $startpos $endpos) (Sif (c, s1, s2))] }
loop_body:
| s = simple_stmt NEWLINE
{ empty_annotation, [s] }
......
from random import randint
i = randint(0, 10)
#@ assert 0 <= i <= 10
# def swap(a, i, j):
# #@ requires 0 <= i < len(a) and 0 <= j < len(a)
# t = a[i]
# a[i] = a[j]
# a[j] = t
i = randint(0, 10)
#@ assert 0 <= i <= 10
a = 0
b = 1
while b < 100:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment