validator.py 609 B

1234567891011121314151617181920212223242526
  1. from __future__ import annotations
  2. import src.zelkelc.ast.nodes as nodes
  3. import src.zelkelc.validator.scope as scope
  4. class SemanticCheckFailed(Exception):
  5. pass
  6. def validate_ast(tree: list[nodes.Node]) -> scope.Scope:
  7. checker = scope.ScopeChecker()
  8. global_scope, errors = checker.check(tree)
  9. if len(errors) > 0:
  10. pretty_errors = "\n".join(f"- {err.message}" for err in errors)
  11. raise SemanticCheckFailed(f"AST semantic validation failed:\n{pretty_errors}")
  12. return global_scope
  13. class Validator:
  14. scope: scope.Scope
  15. def __init__(self, tree: list[nodes.Node]):
  16. self.scope = validate_ast(tree)