// TODO: Осознать, что тут происходит private static void TableEach(Evaluator eval, StackFrame frame) { var args = frame.args; var(dict, func) = StructureUtils.Split2(args); Context dictionary = GetDictionary(dict); Function proc = func?.value as Function; if (dictionary == null) { throw new ArgumentException("First argument must be table!"); } if (proc == null) { throw new ArgumentException("Second argument must be procedure!"); } switch (frame.state.value) { case "-eval-sexp-body-": var list = dictionary .Select(pair => StructureUtils.List(new Atom(AtomType.String, pair.Key), pair.Value)) .ToArray(); frame.temp1 = StructureUtils.List(list); frame.state = new Atom("-built-in-table-each-"); break; case "-built-in-table-each-": if (eval.HaveReturn()) { frame.temp2 = StructureUtils.BuildListContainer(frame.temp2, eval.TakeReturn()); } if (frame.temp1 != null) { var pair = frame.temp1.atom; frame.temp1 = frame.temp1.next; var newFrame = eval.CreateFrame( "-eval-sexp-args-", new Atom(func, pair), frame.context); newFrame.function = func; newFrame.args = frame.temp1.atom; } else { eval.SetReturn(null); frame.state = new Atom("-eval-sexp-body-"); } break; } }
private static void ReadDirectory(Evaluator eval, StackFrame frame) { Atom args = frame.args; Atom path = args?.atom; if (path == null || !path.IsString) { throw new ArgumentException("Path must be string!"); } string directory = (string)path.value; Atom pattern = args?.next?.atom; Atom mode = args?.next?.next?.atom; SearchOption option = ArgUtils.GetEnum <SearchOption>(mode, 3); string[] dirs = null; if (pattern == null) { dirs = Directory.GetDirectories(directory); } else { dirs = Directory.GetDirectories(directory, (string)pattern.value, option); } string[] files = null; if (pattern == null) { files = Directory.GetFiles(directory); } else { files = Directory.GetFiles(directory, (string)pattern.value, option); } Atom[] elements = new Atom[dirs.Length + files.Length]; for (int i = 0; i < dirs.Length; i++) { elements[i] = new Atom(AtomType.String, dirs[i]); } for (int i = 0; i < files.Length; i++) { elements[i + dirs.Length] = new Atom(AtomType.String, files[i]); } eval.Return(StructureUtils.List(elements)); }
private static void Each(Evaluator eval, StackFrame frame) { (var list, var proc, var skip) = StructureUtils.Split3(frame.args); bool skipNull = (bool?)skip?.value ?? false; switch (frame.state.value) { case "-eval-sexp-body-": frame.temp1 = list; frame.state = new Atom("-built-in-each-"); break; case "-built-in-each-": if (eval.HaveReturn()) { frame.temp2 = StructureUtils.BuildListContainer(frame.temp2, eval.TakeReturn()); } if (frame.temp1 != null) { while (skipNull && frame.temp1.atom == null) { frame.temp1 = frame.temp1.next; } var subExpression = new Atom(frame.temp1.atom, null); var newFrame = eval.CreateFrame( "-eval-sexp-body-", StructureUtils.List(proc, frame.temp1.atom), frame.context); newFrame.function = proc; newFrame.args = subExpression; frame.temp1 = frame.temp1.next; } else { eval.SetReturn(null); frame.state = new Atom("-eval-sexp-body-"); } break; } }
private static void ReadLines(Evaluator eval, StackFrame frame) { Atom path = frame.args?.atom; if (path == null || !path.IsString) { throw new ArgumentException("Path must be string!"); } string file = (string)path.value; string[] lines = File.ReadAllLines(file, System.Text.Encoding.UTF8); Atom[] atoms = new Atom[lines.Length]; for (int i = 0; i < lines.Length; i++) { atoms[i] = new Atom(AtomType.String, lines[i]); } eval.Return(StructureUtils.List(atoms)); }