2019-09-22 01:59:48 +04:00
|
|
|
// Test while loops in PascaLIGO
|
|
|
|
|
2019-05-13 00:56:22 +04:00
|
|
|
function counter (var n : nat) : nat is block {
|
|
|
|
var i : nat := 0n ;
|
2019-11-06 23:12:25 +04:00
|
|
|
while i < n block {
|
|
|
|
i := i + 1n;
|
2019-05-13 00:56:22 +04:00
|
|
|
}
|
|
|
|
} with i
|
|
|
|
|
2019-09-25 02:47:04 +04:00
|
|
|
function while_sum (var n : nat) : nat is block {
|
2019-05-13 00:56:22 +04:00
|
|
|
var i : nat := 0n ;
|
|
|
|
var r : nat := 0n ;
|
2019-11-06 23:12:25 +04:00
|
|
|
while i < n block {
|
|
|
|
i := i + 1n;
|
|
|
|
r := r + i;
|
2019-05-13 00:56:22 +04:00
|
|
|
}
|
|
|
|
} with r
|
|
|
|
|
2019-10-22 14:12:19 +04:00
|
|
|
function for_sum (var n : nat) : int is block {
|
2019-10-11 20:31:04 +04:00
|
|
|
var acc : int := 0 ;
|
2019-10-22 14:12:19 +04:00
|
|
|
for i := 1 to int(n)
|
2019-11-06 23:12:25 +04:00
|
|
|
begin
|
|
|
|
acc := acc + i;
|
2019-10-11 20:31:04 +04:00
|
|
|
end
|
2019-11-06 23:12:25 +04:00
|
|
|
} with acc
|
2019-10-11 20:31:04 +04:00
|
|
|
|
2019-10-27 17:12:42 +04:00
|
|
|
function for_collection_list (var nee : unit) : (int * string) is block {
|
2019-11-06 23:12:25 +04:00
|
|
|
var acc : int := 0;
|
|
|
|
var st : string := "to";
|
|
|
|
var mylist : list(int) := list 1; 1; 1 end;
|
|
|
|
for x in list mylist
|
|
|
|
begin
|
|
|
|
acc := acc + x;
|
|
|
|
st := st ^ "to";
|
|
|
|
end
|
2019-10-27 17:12:42 +04:00
|
|
|
} with (acc, st)
|
2019-10-22 14:12:19 +04:00
|
|
|
|
2019-10-27 17:12:42 +04:00
|
|
|
function for_collection_set (var nee : unit) : (int * string) is block {
|
2019-11-06 23:12:25 +04:00
|
|
|
var acc : int := 0;
|
|
|
|
var st : string := "to";
|
|
|
|
var myset : set(int) := set 1; 2; 3 end;
|
|
|
|
for x in set myset
|
|
|
|
begin
|
|
|
|
acc := acc + x ;
|
|
|
|
st := st^"to" ;
|
|
|
|
end
|
2019-10-27 14:47:17 +04:00
|
|
|
} with (acc, st)
|
2019-10-15 15:14:00 +04:00
|
|
|
|
2019-10-29 14:57:15 +04:00
|
|
|
function for_collection_if_and_local_var (var nee : unit) : int is block {
|
|
|
|
var acc : int := 0 ;
|
|
|
|
const theone : int = 1 ;
|
|
|
|
var myset : set(int) := set 1 ; 2 ; 3 end ;
|
2019-11-06 23:12:25 +04:00
|
|
|
for x in set myset
|
|
|
|
begin
|
|
|
|
const thetwo : int = 2 ;
|
|
|
|
if x=theone then
|
|
|
|
acc := acc + x
|
|
|
|
else if x=thetwo then
|
|
|
|
acc := acc + thetwo;
|
|
|
|
else
|
|
|
|
acc := acc + 10;
|
|
|
|
end
|
2019-10-29 14:57:15 +04:00
|
|
|
} with acc
|
|
|
|
|
|
|
|
function for_collection_rhs_capture (var nee : unit) : int is block {
|
|
|
|
var acc : int := 0 ;
|
|
|
|
const mybigint : int = 1000 ;
|
|
|
|
var myset : set(int) := set 1 ; 2 ; 3 end ;
|
2019-11-06 23:12:25 +04:00
|
|
|
for x in set myset
|
|
|
|
begin
|
|
|
|
if x=1 then acc := acc + mybigint;
|
|
|
|
else acc := acc + 10;
|
|
|
|
end
|
2019-10-29 14:57:15 +04:00
|
|
|
} with acc
|
|
|
|
|
|
|
|
function for_collection_proc_call (var nee : unit) : int is block {
|
|
|
|
var acc : int := 0 ;
|
|
|
|
var myset : set(int) := set 1 ; 2 ; 3 end ;
|
2019-11-06 23:12:25 +04:00
|
|
|
for x in set myset
|
|
|
|
begin
|
|
|
|
if x=1 then
|
|
|
|
acc := acc + for_collection_rhs_capture(unit);
|
|
|
|
else
|
|
|
|
acc := acc + 10;
|
|
|
|
end
|
2019-10-29 14:57:15 +04:00
|
|
|
} with acc
|
|
|
|
|
|
|
|
function for_collection_comp_with_acc (var nee : unit) : int is block {
|
|
|
|
var myint : int := 0 ;
|
|
|
|
var mylist : list(int) := list 1 ; 10 ; 15 end;
|
2019-11-06 23:12:25 +04:00
|
|
|
for x in list mylist
|
|
|
|
begin
|
|
|
|
if x < myint then skip;
|
|
|
|
else myint := myint + 10
|
|
|
|
end
|
2019-10-29 14:57:15 +04:00
|
|
|
} with myint
|
|
|
|
|
|
|
|
function for_collection_with_patches (var nee : unit) : map(string,int) is block {
|
|
|
|
var myint : int := 12 ;
|
2019-11-06 23:12:25 +04:00
|
|
|
var mylist : list(string) := list "I"; "am"; "foo" end;
|
2019-10-29 14:57:15 +04:00
|
|
|
var mymap : map(string,int) := map end;
|
2019-11-06 23:12:25 +04:00
|
|
|
for x in list mylist
|
|
|
|
begin
|
|
|
|
patch mymap with map [x -> myint];
|
|
|
|
end
|
2019-10-29 14:57:15 +04:00
|
|
|
} with mymap
|
2019-10-28 21:53:40 +04:00
|
|
|
|
|
|
|
function for_collection_empty (var nee : unit) : int is block {
|
|
|
|
var acc : int := 0 ;
|
2019-11-06 23:12:25 +04:00
|
|
|
var myset : set(int) := set 1; 2; 3 end;
|
|
|
|
for x in set myset
|
|
|
|
begin
|
|
|
|
skip
|
|
|
|
end
|
2019-10-28 21:53:40 +04:00
|
|
|
} with acc
|
|
|
|
|
2019-10-29 18:43:00 +04:00
|
|
|
function for_collection_map_kv (var nee : unit) : (int * string) is block {
|
2019-11-06 23:12:25 +04:00
|
|
|
var acc : int := 0;
|
|
|
|
var st : string := "";
|
|
|
|
var mymap : map(string,int) := map "1" -> 1; "2" -> 2; "3" -> 3 end;
|
|
|
|
for k -> v in map mymap
|
|
|
|
begin
|
|
|
|
acc := acc + v;
|
|
|
|
st := st ^ k;
|
|
|
|
end
|
2019-10-29 18:43:00 +04:00
|
|
|
} with (acc, st)
|
|
|
|
|
|
|
|
function for_collection_map_k (var nee : unit) : string is block {
|
|
|
|
var st : string := "" ;
|
|
|
|
var mymap : map(string,int) := map "1" -> 1 ; "2" -> 2 ; "3" -> 3 end ;
|
2019-11-06 23:12:25 +04:00
|
|
|
for k in map mymap
|
|
|
|
begin
|
|
|
|
st := st ^ k;
|
|
|
|
end
|
2019-10-29 18:43:00 +04:00
|
|
|
} with st
|
2019-10-27 19:42:11 +04:00
|
|
|
|
2019-11-15 18:49:50 +04:00
|
|
|
function nested_for_collection (var nee : unit) : (int*string) is block {
|
|
|
|
var myint : int := 0;
|
|
|
|
var mystoo : string := "";
|
|
|
|
var mylist : list(int) := list 1 ; 2 ; 3 end ;
|
|
|
|
var mymap : map(string,string) := map " one" -> "," ; "two" -> " " end ;
|
|
|
|
|
|
|
|
for i in list mylist
|
|
|
|
begin
|
|
|
|
myint := myint + i ;
|
|
|
|
var myset : set(string) := set "1" ; "2" ; "3" end ;
|
|
|
|
for st in set myset
|
|
|
|
begin
|
|
|
|
mystoo := mystoo ^ st ;
|
|
|
|
for k -> v in map mymap
|
|
|
|
begin
|
|
|
|
mystoo := mystoo ^ k ^ v ;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
} with (myint,mystoo)
|
2019-10-29 19:32:28 +04:00
|
|
|
|
2019-05-13 00:56:22 +04:00
|
|
|
function dummy (const n : nat) : nat is block {
|
2019-11-06 23:12:25 +04:00
|
|
|
while False block { skip }
|
2019-05-13 00:56:22 +04:00
|
|
|
} with n
|