File tree Expand file tree Collapse file tree 4 files changed +34
-2
lines changed Expand file tree Collapse file tree 4 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,8 @@ This language is still currently experimental with many more features to come.
20
20
* [ Variables] ( examples/variable.ls ) .
21
21
* [ Conditionals] ( examples/conditionals.ls ) .
22
22
* [ Logical operators] ( examples/logical.ls ) .
23
- * Loops.
24
- * Closures.
23
+ * [ Loops] ( examples/loops.ls ) .
24
+ * [ Closures] ( examples/closure.ls ) .
25
25
* [ Recursion] ( examples/recursion.ls ) .
26
26
* Blocks.
27
27
* Printing.
Original file line number Diff line number Diff line change
1
+ // closure wraps function and keeps one variable named counter for internal
2
+ // state .
3
+ func Closure() {
4
+ var counter = 0 ;
5
+
6
+ func Increment() {
7
+ counter = counter + 1 ;
8
+ return counter ;
9
+ }
10
+
11
+ return Increment ;
12
+ }
13
+
14
+ // Gets a function from the closure back .
15
+ var counter = Closure();
16
+
17
+ // Calling counter() increments the counter .
18
+ print counter();
19
+ print counter();
Original file line number Diff line number Diff line change
1
+ var x = 0 ;
2
+
3
+ // your typical while loop .
4
+ while (x < 10 ) {
5
+ print x ;
6
+ x = x + 1 ;
7
+ }
8
+
9
+ // Your typical for loop that does the same thing as the while .
10
+ for (var y = 0 ; y < 10 ; y = y + 1 ) {
11
+ print y ;
12
+ }
Original file line number Diff line number Diff line change @@ -330,6 +330,7 @@ void Resolver::ResolveLocalVariable(
330
330
for (int pos = scope_stack_.size () - 1 ; pos >= 0 ; pos--) {
331
331
if (scope_stack_[pos].contains (variable_name.Lexeme )) {
332
332
interpreter_->Resolve (expression, scope_stack_.size () - 1 - pos);
333
+ scope_stack_[pos][variable_name.Lexeme ].Used = true ;
333
334
return ;
334
335
}
335
336
}
You can’t perform that action at this time.
0 commit comments