glsl_parser_test.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include "glsl_parser.h"
  6. #include "glsl_ast.h"
  7. void error_cb(const char *str, int lineno, int first_col, int last_col)
  8. {
  9. fprintf(stderr, "GLSL parse error line %d(%d-%d): %s\n", lineno, first_col, last_col, str);
  10. }
  11. void parse_file(struct glsl_parse_context *context, FILE *f)
  12. {
  13. bool error = glsl_parse_file(context, f);
  14. if (!error && context->root) {
  15. printf("\nAST tree:\n\n");
  16. glsl_ast_print(context->root, 0);
  17. printf("\nRegenerated GLSL:\n\n");
  18. char *out = glsl_ast_generate_glsl(context->root);
  19. printf("%s", out);
  20. free(out);
  21. }
  22. }
  23. int main(int argc, char **argv, char **envp)
  24. {
  25. struct glsl_parse_context context;
  26. glsl_parse_context_init(&context);
  27. glsl_parse_set_error_cb(&context, error_cb);
  28. if (argc == 1) {
  29. parse_file(&context, stdin);
  30. }
  31. else {
  32. int i;
  33. for (i = 1; i < argc; i++) {
  34. FILE *f = fopen(argv[i], "rt");
  35. if (!f) {
  36. fprintf(stderr, "Couldn't open file %s: %s\n", argv[i], strerror(errno));
  37. continue;
  38. }
  39. printf("Input file: %s\n", argv[i]);
  40. parse_file(&context, f);
  41. fclose(f);
  42. }
  43. }
  44. glsl_parse_context_destroy(&context);
  45. }