web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.4 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.program = exports.expression = exports.statement = exports.statements = exports.smart = void 0;
  6. var t = _interopRequireWildcard(require("@babel/types"));
  7. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  8. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  9. function makeStatementFormatter(fn) {
  10. return {
  11. code: str => `/* @babel/template */;\n${str}`,
  12. validate: () => {},
  13. unwrap: ast => {
  14. return fn(ast.program.body.slice(1));
  15. }
  16. };
  17. }
  18. const smart = makeStatementFormatter(body => {
  19. if (body.length > 1) {
  20. return body;
  21. } else {
  22. return body[0];
  23. }
  24. });
  25. exports.smart = smart;
  26. const statements = makeStatementFormatter(body => body);
  27. exports.statements = statements;
  28. const statement = makeStatementFormatter(body => {
  29. if (body.length === 0) {
  30. throw new Error("Found nothing to return.");
  31. }
  32. if (body.length > 1) {
  33. throw new Error("Found multiple statements but wanted one");
  34. }
  35. return body[0];
  36. });
  37. exports.statement = statement;
  38. const expression = {
  39. code: str => `(\n${str}\n)`,
  40. validate: ast => {
  41. if (ast.program.body.length > 1) {
  42. throw new Error("Found multiple statements but wanted one");
  43. }
  44. if (expression.unwrap(ast).start === 0) {
  45. throw new Error("Parse result included parens.");
  46. }
  47. },
  48. unwrap: ({
  49. program
  50. }) => {
  51. const [stmt] = program.body;
  52. t.assertExpressionStatement(stmt);
  53. return stmt.expression;
  54. }
  55. };
  56. exports.expression = expression;
  57. const program = {
  58. code: str => str,
  59. validate: () => {},
  60. unwrap: ast => ast.program
  61. };
  62. exports.program = program;