From f9cce777f0bbef3b0469e87cc69d6fc98755688d Mon Sep 17 00:00:00 2001 From: schaeff Date: Tue, 29 Jan 2019 11:24:16 +0100 Subject: [PATCH] add doc, example and failing test for out of scope --- shadow.code | 0 zokrates_book/src/concepts/variables.md | 24 ++++++++++++++++++- zokrates_cli/examples/book/for_scope.code | 7 ++++++ .../examples/book/function_scope.code | 7 ++++++ zokrates_cli/examples/book/no_shadowing.code | 7 ++++++ .../examples/error/out_of_for_scope.code | 4 ++++ ...licate_declaration.code => shadowing.code} | 2 +- 7 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 shadow.code create mode 100644 zokrates_cli/examples/book/for_scope.code create mode 100644 zokrates_cli/examples/book/function_scope.code create mode 100644 zokrates_cli/examples/book/no_shadowing.code create mode 100644 zokrates_cli/examples/error/out_of_for_scope.code rename zokrates_cli/examples/error/{duplicate_declaration.code => shadowing.code} (86%) diff --git a/shadow.code b/shadow.code new file mode 100644 index 00000000..e69de29b diff --git a/zokrates_book/src/concepts/variables.md b/zokrates_book/src/concepts/variables.md index 645619ba..dab25416 100644 --- a/zokrates_book/src/concepts/variables.md +++ b/zokrates_book/src/concepts/variables.md @@ -1,4 +1,26 @@ ## Variables Variables can have any name which does not start with a number. Underscores are not allowed in variable names. -Variables are mutable, and always passed by values to functions. \ No newline at end of file +Variables are mutable, and always passed by value to functions. + +### Shadowing + +Shadowing is not allowed. +```zokrates +{{#include ../../../zokrates_cli/examples/book/no_shadowing.code}} +``` + +### Scope + +#### Function + +Functions have their own scope +```zokrates +{{#include ../../../zokrates_cli/examples/book/function_scope.code}} +``` + +#### For-loop +For-loops have their own scope +```zokrates +{{#include ../../../zokrates_cli/examples/book/for_scope.code}} +``` \ No newline at end of file diff --git a/zokrates_cli/examples/book/for_scope.code b/zokrates_cli/examples/book/for_scope.code new file mode 100644 index 00000000..50655615 --- /dev/null +++ b/zokrates_cli/examples/book/for_scope.code @@ -0,0 +1,7 @@ +def main() -> (field): + field a = 0 + for field i in 0..5 do + a = a + i + endfor + // return i <- not allowed + return a \ No newline at end of file diff --git a/zokrates_cli/examples/book/function_scope.code b/zokrates_cli/examples/book/function_scope.code new file mode 100644 index 00000000..d1f060f7 --- /dev/null +++ b/zokrates_cli/examples/book/function_scope.code @@ -0,0 +1,7 @@ +def foo() -> (field): + // return myGlobal <- not allowed + return 42 + +def main() -> (field): + field myGlobal = 42 + return foo() \ No newline at end of file diff --git a/zokrates_cli/examples/book/no_shadowing.code b/zokrates_cli/examples/book/no_shadowing.code new file mode 100644 index 00000000..8d852911 --- /dev/null +++ b/zokrates_cli/examples/book/no_shadowing.code @@ -0,0 +1,7 @@ +def main() -> (field): + field a = 2 + // field a = 3 <- not allowed + for field i in 0..5 do + // field a = 7 <- not allowed + endfor + return a \ No newline at end of file diff --git a/zokrates_cli/examples/error/out_of_for_scope.code b/zokrates_cli/examples/error/out_of_for_scope.code new file mode 100644 index 00000000..b005b833 --- /dev/null +++ b/zokrates_cli/examples/error/out_of_for_scope.code @@ -0,0 +1,4 @@ +def main() -> (field): + for field i in 0..5 do + endfor + return i \ No newline at end of file diff --git a/zokrates_cli/examples/error/duplicate_declaration.code b/zokrates_cli/examples/error/shadowing.code similarity index 86% rename from zokrates_cli/examples/error/duplicate_declaration.code rename to zokrates_cli/examples/error/shadowing.code index 919eb2a0..d3345294 100644 --- a/zokrates_cli/examples/error/duplicate_declaration.code +++ b/zokrates_cli/examples/error/shadowing.code @@ -2,6 +2,6 @@ def foo() -> (field): return 1 def main() -> (field): - bool a + field a = 2 field a = foo() return 1 \ No newline at end of file