skulpt_ast.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var $builtinmodule = function(name)
  2. {
  3. var mod = {};
  4. var parses = {};
  5. mod.count_components = new Sk.builtin.func(function(source, component) {
  6. Sk.builtin.pyCheckArgs("parse", arguments, 2, 2);
  7. Sk.builtin.pyCheckType("source", "string", Sk.builtin.checkString(source));
  8. Sk.builtin.pyCheckType("component", "string", Sk.builtin.checkString(component));
  9. source = source.v;
  10. component = component.v;
  11. if (!(source in parses)) {
  12. var parse = Sk.parse("__main__", source);
  13. parses[source] = Sk.astFromParse(parse.cst, "__main__", parse.flags);
  14. }
  15. var ast = parses[source];
  16. ast_list = (new NodeVisitor()).recursive_walk(ast);
  17. var count = 0;
  18. for (var i = 0, len = ast_list.length; i < len; i = i+1) {
  19. if (ast_list[i]._astname == component) {
  20. count = count+1;
  21. }
  22. }
  23. return Sk.ffi.remapToPy(count);
  24. });
  25. return mod;
  26. }