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
1.9 KiB

  1. module.exports = genOpt;
  2. function genOpt(ctx, ast) {
  3. if (ast.type == "OP") {
  4. if (ast.op == "=") {
  5. return genOptVarAssignement(ctx, ast);
  6. } else {
  7. error(ctx, ast, "GENOPT -> Invalid operation: " + ast.op);
  8. }
  9. } else if (ast.type == "TEMPLATEDEF") {
  10. return genOptTemplateDef(ctx, ast);
  11. } else {
  12. error(ctx, ast, "GENOPT -> Invalid AST node type: " + ast.type);
  13. }
  14. }
  15. function error(ctx, ast, errStr) {
  16. ctx.error = {
  17. pos: {
  18. first_line: ast.first_line,
  19. first_column: ast.first_column,
  20. last_line: ast.last_line,
  21. last_column: ast.last_column
  22. },
  23. errStr: errStr,
  24. errFile: ctx.fileName,
  25. ast: ast
  26. };
  27. }
  28. function genOptTemplateDef(ctx, ast) {
  29. if (ctx.templates[ast.name]) {
  30. return error(ctx, ast, "Template name already exists: "+ast.name);
  31. }
  32. ctx.templates[ast.name] = {
  33. type: "TEMPLATE",
  34. params: ast.params,
  35. block: ast.block,
  36. fileName: ctx.fileName,
  37. filePath: ctx.filePath
  38. };
  39. }
  40. function genOptVarAssignement(ctx, ast) {
  41. let varName;
  42. if (ast.values[0].type == "DECLARE") {
  43. varName = genOptCode(ctx, ast.values[0]);
  44. if (ctx.error) return;
  45. } else {
  46. varName = ast.values[0];
  47. }
  48. const varContent = getScope(ctx, varName.name, varName.selectors);
  49. if (ctx.error) return;
  50. if ((typeof(varContent) != "object")||(varContent == null)) return error(ctx, ast, "Variable not defined");
  51. if (varContent.type == "COMPONENT") return genOptInstantiateComponet(ctx, varName, ast.values[1]);
  52. if (varContent.type == "SIGNAL") return error(ctx, ast, "Cannot assig to a signal with `=` use <-- or <== ops");
  53. const res = genOpt(ctx, ast.values[1]);
  54. if (ctx.error) return;
  55. setScope(ctx, varName.name, varName.selectors, res);
  56. return v;
  57. }