python: print is now variadic

parent 8032bb84
...@@ -4,8 +4,10 @@ from random import randint ...@@ -4,8 +4,10 @@ from random import randint
m = randint(0, 100) m = randint(0, 100)
print("j'ai choisi un nombre entre 0 et 100") print("j'ai choisi un nombre entre 0 et 100")
print("vous devez le trouver") print("vous devez le trouver")
tentatives = 0
while True: while True:
x = int(input("votre choix : ")) x = int(input("votre choix : "))
tentatives = tentatives + 1
print(x) print(x)
if x < 0 or x > 100: if x < 0 or x > 100:
print("j'ai dit entre 0 et 100") print("j'ai dit entre 0 et 100")
...@@ -15,5 +17,6 @@ while True: ...@@ -15,5 +17,6 @@ while True:
print("trop grand") print("trop grand")
else: else:
print("bravo !") print("bravo !")
print("en", tentatives, "tentatives")
break break
...@@ -49,7 +49,6 @@ and stmt_desc = ...@@ -49,7 +49,6 @@ and stmt_desc =
| Sif of expr * block * block | Sif of expr * block * block
| Sreturn of expr | Sreturn of expr
| Sassign of ident * expr | Sassign of ident * expr
| Sprint of expr
| Swhile of expr * Ptree.loop_annotation * block | Swhile of expr * Ptree.loop_annotation * block
| Sfor of ident * expr * Ptree.invariant * block | Sfor of ident * expr * Ptree.invariant * block
| Seval of expr | Seval of expr
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
let h = Hashtbl.create 32 in let h = Hashtbl.create 32 in
List.iter (fun (s, tok) -> Hashtbl.add h s tok) List.iter (fun (s, tok) -> Hashtbl.add h s tok)
["def", DEF; "if", IF; "else", ELSE; "elif", ELIF; ["def", DEF; "if", IF; "else", ELSE; "elif", ELIF;
"return", RETURN; "print", PRINT; "while", WHILE; "return", RETURN; "while", WHILE;
"for", FOR; "in", IN; "for", FOR; "in", IN;
"and", AND; "or", OR; "not", NOT; "and", AND; "or", OR; "not", NOT;
"True", TRUE; "False", FALSE; "None", NONE; "True", TRUE; "False", FALSE; "None", NONE;
......
...@@ -97,7 +97,7 @@ let add_loop_invariant i a = ...@@ -97,7 +97,7 @@ let add_loop_invariant i a =
let rec has_break s = match s.Py_ast.stmt_desc with let rec has_break s = match s.Py_ast.stmt_desc with
| Py_ast.Sbreak -> true | Py_ast.Sbreak -> true
| Py_ast.Sreturn _ | Py_ast.Sassign _ | Py_ast.Sprint _ | Py_ast.Sreturn _ | Py_ast.Sassign _
| Py_ast.Seval _ | Py_ast.Sset _ | Py_ast.Sassert _ | Py_ast.Seval _ | Py_ast.Sset _ | Py_ast.Sassert _
| Py_ast.Swhile _ -> false | Py_ast.Swhile _ -> false
| Py_ast.Sif (_, bl1, bl2) -> has_breakl bl1 || has_breakl bl2 | Py_ast.Sif (_, bl1, bl2) -> has_breakl bl1 || has_breakl bl2
...@@ -139,6 +139,10 @@ let rec expr env {Py_ast.expr_loc = loc; Py_ast.expr_desc = d } = match d with ...@@ -139,6 +139,10 @@ let rec expr env {Py_ast.expr_loc = loc; Py_ast.expr_desc = d } = match d with
mk_expr ~loc (Eidapp (prefix ~loc "-", [expr env e])) mk_expr ~loc (Eidapp (prefix ~loc "-", [expr env e]))
| Py_ast.Eunop (Py_ast.Unot, e) -> | Py_ast.Eunop (Py_ast.Unot, e) ->
mk_expr ~loc (Eidapp (Qident (mk_id ~loc "not"), [expr env e])) mk_expr ~loc (Eidapp (Qident (mk_id ~loc "not"), [expr env e]))
| Py_ast.Ecall ({id_str="print"}, el) ->
let eval res e =
mk_expr ~loc (Elet (mk_id ~loc "_", Gnone, expr env e, res)) in
List.fold_left eval (mk_unit ~loc) el
| Py_ast.Ecall (id, el) -> | Py_ast.Ecall (id, el) ->
mk_expr ~loc (Eidapp (Qident id, List.map (expr env) el)) mk_expr ~loc (Eidapp (Qident id, List.map (expr env) el))
| Py_ast.Emake (e1, e2) -> (* [e1]*e2 *) | Py_ast.Emake (e1, e2) -> (* [e1]*e2 *)
...@@ -163,8 +167,6 @@ let rec stmt env ({Py_ast.stmt_loc = loc; Py_ast.stmt_desc = d } as s) = ...@@ -163,8 +167,6 @@ let rec stmt env ({Py_ast.stmt_loc = loc; Py_ast.stmt_desc = d } as s) =
match d with match d with
| Py_ast.Seval e -> | Py_ast.Seval e ->
expr env e expr env e
| Py_ast.Sprint e ->
mk_expr ~loc (Elet (mk_id ~loc "_", Gnone, expr env e, mk_unit ~loc))
| Py_ast.Sif (e, s1, s2) -> | Py_ast.Sif (e, s1, s2) ->
mk_expr ~loc (Eif (expr env e, block env ~loc s1, block env ~loc s2)) mk_expr ~loc (Eif (expr env e, block env ~loc s1, block env ~loc s2))
| Py_ast.Sreturn _e -> | Py_ast.Sreturn _e ->
......
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
%token <string> STRING %token <string> STRING
%token <Py_ast.binop> CMP %token <Py_ast.binop> CMP
%token <string> IDENT %token <string> IDENT
%token DEF IF ELSE ELIF RETURN PRINT WHILE FOR IN AND OR NOT NONE TRUE FALSE %token DEF IF ELSE ELIF RETURN WHILE FOR IN AND OR NOT NONE TRUE FALSE
%token FROM IMPORT BREAK %token FROM IMPORT BREAK
%token EOF %token EOF
%token LEFTPAR RIGHTPAR LEFTSQ RIGHTSQ COMMA EQUAL COLON BEGIN END NEWLINE %token LEFTPAR RIGHTPAR LEFTSQ RIGHTSQ COMMA EQUAL COLON BEGIN END NEWLINE
...@@ -232,8 +232,6 @@ simple_stmt_desc: ...@@ -232,8 +232,6 @@ simple_stmt_desc:
{ Sassign (id, e) } { Sassign (id, e) }
| e1 = expr LEFTSQ e2 = expr RIGHTSQ EQUAL e3 = expr | e1 = expr LEFTSQ e2 = expr RIGHTSQ EQUAL e3 = expr
{ Sset (e1, e2, e3) } { Sset (e1, e2, e3) }
| PRINT e = expr
{ Sprint e }
| k=assertion_kind t = term | k=assertion_kind t = term
{ Sassert (k, t) } { Sassert (k, t) }
| e = expr | e = expr
......
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