Embedded programming language for Zig
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement `dolist`

IamPyu 69a33d2c ae93d418

+25 -1
+21 -1
src/stdlib/core.zig
··· 26 26 .{ "while", try Value.initNativeMacro(scope, @"while") }, 27 27 .{ "let", try Value.initNativeMacro(scope, let) }, 28 28 .{ "lambda", try Value.initNativeMacro(scope, lambda) }, 29 + .{ "dolist", try Value.initNativeMacro(scope, dolist) }, 29 30 30 31 .{ "eval", try Value.initNativeFunc(scope, eval) }, 31 32 .{ "define", try Value.initNativeFunc(scope, define) }, ··· 180 181 try scope.setException(sb.written()); 181 182 return RuntimeError.InvalidArguments; 182 183 } 183 - // TODO: implement dolist 184 + 185 + const var_name = switch (args[0].expr) { 186 + .sym => |s| s, 187 + else => { 188 + try util.invalidType(&sb, .sym, args[0].getType()); 189 + try scope.setException(sb.written()); 190 + return RuntimeError.InvalidArguments; 191 + }, 192 + }; 193 + var iter = Value.Iter.init(try scope.evalExpr(args[1])); 194 + const body = args[2..]; 195 + 196 + var eval_scope = try scope.branch(); 197 + defer eval_scope.deinit(); 198 + while (iter.next()) |val| { 199 + try eval_scope.insert(var_name, val); 200 + _ = try eval_scope.evalAst(body); 201 + } 202 + 203 + return scope.getRoot().createAst(&.{try Value.initNil(scope.getRoot())}); 184 204 } 185 205 186 206 // -- FUNCTIONS --
+4
src/test/dolist.zexa
··· 1 + (define 'my-list (list 1 2 3 4 5)) 2 + 3 + (dolist elem my-list 4 + (print (format "element: %{}" elem)))