[5] | 1 | /* |
---|
| 2 | |
---|
| 3 | This is a test app for the ps10 lexer/parser. |
---|
| 4 | |
---|
| 5 | Cass Everitt |
---|
| 6 | 07-06-01 |
---|
| 7 | |
---|
| 8 | */ |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <state_to_nvparse_text.h> |
---|
| 11 | #include "nvparse_errors.h" |
---|
| 12 | |
---|
| 13 | #include <string> |
---|
| 14 | #include <GL/glut.h> |
---|
| 15 | |
---|
| 16 | #define GLH_EXT_SINGLE_FILE |
---|
| 17 | #include <glh_extensions.h> |
---|
| 18 | |
---|
| 19 | using namespace std; |
---|
| 20 | |
---|
| 21 | bool ps10_init(char *); |
---|
| 22 | int ps10_parse(void); |
---|
| 23 | |
---|
| 24 | nvparse_errors errors; |
---|
| 25 | int line_number; |
---|
| 26 | char * myin = 0; |
---|
| 27 | |
---|
| 28 | char * const * const nvparse_get_errors() |
---|
| 29 | { |
---|
| 30 | return errors.get_errors(); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | char * const * const nvparse_print_errors(FILE * errfp) |
---|
| 34 | { |
---|
| 35 | for (char * const * ep = nvparse_get_errors(); *ep; ep++) |
---|
| 36 | { |
---|
| 37 | const char * errstr = *ep; |
---|
| 38 | fprintf(errfp, "%s\n", errstr); |
---|
| 39 | } |
---|
| 40 | return nvparse_get_errors(); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | char * read_input_file(string filename) |
---|
| 45 | { |
---|
| 46 | char buf[256]; |
---|
| 47 | FILE * fp = fopen(filename.c_str(), "rb"); |
---|
| 48 | if(! fp) |
---|
| 49 | return 0; |
---|
| 50 | string result; |
---|
| 51 | while(fgets(buf, 255, fp) > 0) |
---|
| 52 | { |
---|
| 53 | result += buf; |
---|
| 54 | } |
---|
| 55 | fclose(fp); |
---|
| 56 | char * r = new char[result.size()+1]; |
---|
| 57 | strcpy(r, result.c_str()); |
---|
| 58 | return r; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | int main(int argc, char **argv) |
---|
| 63 | { |
---|
| 64 | /* |
---|
| 65 | char test_string[] = |
---|
| 66 | "Ps.1.0 \n\n\n" |
---|
| 67 | "def c0, 0.4, 1, 0, .2 ; diffuse \n" |
---|
| 68 | "def c7, .3, .2, .1, 0 \n \n" |
---|
| 69 | " \n" |
---|
| 70 | "add r0, t0_bias, r1_bx2 \n" |
---|
| 71 | "cnd r0, r0.a, t0, r1 \n" |
---|
| 72 | "dp3 r0, t0, r1 \n" |
---|
| 73 | "lrp r0, t0.a, t0, r1 \n" |
---|
| 74 | "mad r0, t0.a, t0, r1 \n" |
---|
| 75 | "mov r0.a, -r1.a \n" |
---|
| 76 | "mul r0, t0, r1 \n" |
---|
| 77 | "sub r0, t0, r1 \n" |
---|
| 78 | ; |
---|
| 79 | */ |
---|
| 80 | /* |
---|
| 81 | char test_string[] = |
---|
| 82 | "Ps.1.0 \n" |
---|
| 83 | "def c0, 0.4, 1, 0, .2 \n" |
---|
| 84 | "def c7, .3, .2, .1, 0 \n" |
---|
| 85 | "add r0, t0_bias, r1_bx2 \n" |
---|
| 86 | "cnd r0, r0.a, t0, r1 \n" |
---|
| 87 | "dp3 r0, t0, r1 \n" |
---|
| 88 | "lrp r0, t0.a, t0, r1 \n" |
---|
| 89 | "mad r0, t0.a, t0, r1 \n" |
---|
| 90 | "mov r0.a, -r1.a \n" |
---|
| 91 | "mul r0, t0, r1 \n" |
---|
| 92 | "sub r0, t0, r1 \n" |
---|
| 93 | ; |
---|
| 94 | */ |
---|
| 95 | char test_string[] = |
---|
| 96 | "ps.1.1\n" |
---|
| 97 | "tex t0\n" |
---|
| 98 | "mul r1, t0, c0\n" |
---|
| 99 | "mul r0, r1, v0\n" |
---|
| 100 | "// End of program\n" |
---|
| 101 | ; |
---|
| 102 | |
---|
| 103 | glutInit(&argc, argv); |
---|
| 104 | glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB); |
---|
| 105 | glutCreateWindow("test window"); |
---|
| 106 | |
---|
| 107 | char * str; |
---|
| 108 | if(argc == 2) |
---|
| 109 | { |
---|
| 110 | str = read_input_file(argv[1]); |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | str = test_string; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | line_number = 1; |
---|
| 118 | |
---|
| 119 | ps10_init(str); |
---|
| 120 | |
---|
| 121 | glNewList(GL_COMPILE_AND_EXECUTE, 1); |
---|
| 122 | ps10_parse(); |
---|
| 123 | glEndList(); |
---|
| 124 | |
---|
| 125 | char * err = (char *) gluErrorString(glGetError()); |
---|
| 126 | |
---|
| 127 | errors.set("spacer..."); |
---|
| 128 | nvparse_print_errors(stderr); |
---|
| 129 | |
---|
| 130 | fprintf(stdout, "\n\nnvparse output: \n\n"); |
---|
| 131 | fprintf(stdout, state_to_rc10() ); |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | return 0; |
---|
| 135 | } |
---|