├── .gitignore ├── Makefile ├── README.md ├── TODO ├── configure ├── design └── sswitch.sxc ├── doc ├── MACROS ├── NOTES └── WIP.md ├── examples ├── Makefile ├── broken │ ├── fire.c │ └── hash-djb2.sxc ├── fire.sxc ├── hash-djb2.sxc ├── hello.sxc ├── linear-lisp │ ├── README.md │ ├── interp.lisp │ ├── paper.html │ └── paper.txt ├── struct.sxc └── words.txt ├── install.sh ├── main.lisp ├── profile ├── rd ├── run-tests.sh ├── sxc ├── sxc-pretty ├── sxc.sh ├── sxcc ├── sxcc.sh ├── tests ├── adder.sxc ├── adder.sxc.out ├── array-initialization.sxc ├── array-initialization.sxc.out ├── bf.sxc ├── bf.sxc.in ├── bf.sxc.out ├── block.sxc ├── block.sxc.out ├── break.sxc ├── break.sxc.out ├── comma.sxc ├── comma.sxc.out ├── dowhile.sxc ├── dowhile.sxc.out ├── equals.sxc ├── equals.sxc.out ├── goto.sxc ├── goto.sxc.out ├── hello.sxc ├── hello.sxc.out ├── k_and_r │ ├── p12.sxc │ ├── p12.sxc.out │ ├── p13.sxc │ ├── p13.sxc.out │ ├── p15.sxc │ ├── p15.sxc.out │ ├── p16.sxc │ ├── p16.sxc.in │ ├── p16.sxc.out │ ├── p17.sxc │ ├── p17.sxc.in │ ├── p17.sxc.out │ ├── p18-1.sxc │ ├── p18-1.sxc.in │ ├── p18-1.sxc.out │ ├── p18-2.sxc │ ├── p18-2.sxc.in │ ├── p18-2.sxc.out │ ├── p19.sxc │ ├── p19.sxc.in │ ├── p19.sxc.out │ ├── p20.sxc │ ├── p20.sxc.in │ ├── p20.sxc.out │ ├── p22.sxc │ ├── p22.sxc.in │ ├── p22.sxc.out │ ├── p24.sxc │ ├── p24.sxc.out │ ├── p29.sxc │ ├── p29.sxc.in │ ├── p29.sxc.out │ ├── p32.sxc │ ├── p32.sxc.in │ ├── p32.sxc.out │ ├── p59.sxc │ ├── p59.sxc.in │ ├── p59.sxc.out │ ├── p69.sxc │ ├── p69.sxc.in │ ├── p69.sxc.out │ ├── p76.part1.sxc │ ├── p76.part2.sxc │ ├── p76.part3.sxc │ ├── p76.part4.sxc │ ├── p76.sxc │ ├── p76.sxc.in │ ├── p76.sxc.out │ ├── p9.sxc │ └── p9.sxc.out ├── pointers.sxc └── pointers.sxc.out ├── to-translate ├── README.md ├── anser │ ├── Makefile │ ├── README.md │ ├── TODO │ ├── anser.h │ ├── sha256 │ ├── sha256.cpp │ ├── tests │ └── tests.cpp ├── woc.c └── xp32 │ ├── LICENCE │ ├── Makefile │ ├── README │ ├── XP32.exe │ ├── __.SYMDEF SORTED │ ├── _depend │ ├── angle.h │ ├── camera.c │ ├── camera.h │ ├── fbanim.c │ ├── fbanim.h │ ├── fire.c │ ├── fire.h │ ├── font.c │ ├── font.h │ ├── goo.c │ ├── goo.h │ ├── line.c │ ├── line.h │ ├── list.c │ ├── list.h │ ├── main.c │ ├── matrix.c │ ├── matrix.h │ ├── one.c │ ├── random.c │ ├── random.h │ ├── scalar.h │ ├── screen.c │ ├── screen.h │ ├── seq.h │ ├── smoke.c │ ├── smoke.h │ ├── terrain.c │ ├── terrain.h │ ├── three.c │ ├── two.c │ ├── vector.c │ ├── vector.h │ ├── window.c │ └── window.h └── typed-cl.lisp /.gitignore: -------------------------------------------------------------------------------- 1 | a.out 2 | x 3 | x.c 4 | *~ 5 | *.o 6 | <<<<<<< HEAD 7 | old 8 | ======= 9 | *.dSYM/ 10 | sxc 11 | >>>>>>> 65a83c77f32091d5742036358263b5481d161f84 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: sxc 2 | 3 | sxc: main.lisp 4 | sbcl --no-userinit --load main.lisp --eval "(sb-ext:save-lisp-and-die \"sxc\" :toplevel 'main :executable t)" 5 | # echo "(load \"main.lisp\")(system::save \"./sxc\")" | gcl # DFW 6 | # echo "(load \"main.lisp\")(ext:saveinitmem \"./sxc.clisp\" :init-function #'sxc::main :executable t)" | clisp # DFW 7 | 8 | fire: examples/fire.sxc sxc 9 | ./sxc examples/fire.sxc > /tmp/fire.c && gcc -o /tmp/fire /tmp/fire.c -lGL -lglut && /tmp/fire 10 | # ./sxcc.sh fire.sxc -framework GLUT -framework OpenGL 11 | 12 | test: 13 | ./run-tests.sh 14 | 15 | clean: 16 | rm -f a.out sxc fire && ./rd 17 | find tests -name '*.c' -exec rm {} \; 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sxc (S-Expression C) is a C like language described by parenthesized 2 | expressions for minimal syntax and the option of using macros. 3 | 4 | EXAMPLE: 5 | 6 | $ ./sxcc.sh hello.sxc 7 | $ ./a.out 8 | Hello, World! 9 | $ make fire # TODO: make fire come out 10 | $ C-c # Control-C to get your terminal back 11 | 12 | sxc - S-Expression C 13 | -------------------- 14 | 15 | sxc is an s-expression based language source transpiler for C. By 16 | providing an S-expression based syntax, sxc will attempt to add 17 | Lisp-like macro and code generation capabilities to the C language (WIP). 18 | 19 | Motivation 20 | ---------- 21 | 22 | Lisp as a language uses lists as data structures for it's code 23 | represenation and is a language designed to work with lists. This 24 | combination has allowed for the creation of 'macros', or programs that 25 | take an input program, analyze it and then transform or generate 26 | arbitrary code from it. This gives great power to the programmer to 27 | create new abstraction and control structures that are not available 28 | in the original language. 29 | 30 | sxc makes C a programmable language, just like a Lisp, with a the 31 | mental programming model of C. 32 | 33 | Features 34 | -------- 35 | 36 | - transpilation from .sxc to .c sources 37 | - source line gdb debugging of original sources 38 | 39 | Building 40 | -------- 41 | 42 | Install sbcl from http://sbcl.org. 43 | 44 | Type make. Output will be in ./sxc. 45 | 46 | Running 47 | ------- 48 | 49 | Use the wrapper scripts ./sxc.sh and ./sxcc.sh to compile and build an 50 | executable from one or more .sxc files: 51 | 52 | sxcc main.sxc lib.sxc 53 | 54 | The result goes into ./a.out. 55 | 56 | All options to sxcc are passed to the compiler other than file names. 57 | 58 | Examples 59 | -------- 60 | 61 | The following is a basic "Hello World" program: 62 | 63 | // 64 | // hello.sxc 65 | // 66 | 67 | (#include ) 68 | 69 | (int main ((int argc) (char (** argv))) 70 | (printf "Hello, World!\n") 71 | 72 | (return 0)) 73 | 74 | This can be compiled and run using the following: 75 | 76 | ./sxcc.sh -o hello hello.sxc && ./hello 77 | 78 | Future Design 79 | -- 80 | 81 | Future Work - Macros 82 | -------------------- 83 | 84 | A more complex example is adding a new control structure to the 85 | language, like a 'string switch', which is like a standard C switch 86 | but works with strings as arguments: 87 | 88 | // 89 | // sswitch - string switch 90 | // 91 | // a switch statement that works with strings 92 | // 93 | (macro sswitch (val &body cases) 94 | (labels ((genswitch (cases) 95 | (let* ((case (car cases)) 96 | (cmd (first case)) 97 | (val (second case)) 98 | (statements (cddr case)) 99 | condition) 100 | (when case 101 | (unless (listp val) 102 | (setf val (list val))) 103 | (mapcar (lambda (v) 104 | (setf condition (append conditiion `((strcmp ,v ,val) &&)))) 105 | val) 106 | (setf condition (append condition '(1))) 107 | `(if ,condition 108 | ,@statements 109 | (else 110 | ,@(genswitch (cdr cases)))))))) 111 | (genswitch cases)) 112 | 113 | 114 | (int main ((int argc) (char (** argv))) 115 | (sswitch ([] argv 1) 116 | (case ("a" "c") 117 | (printf "The value is \"a\" or \"c\"\n")) 118 | (case "d" 119 | (goto e-label)) 120 | (case "b" 121 | (printf "The value is \"b\"\n")) 122 | (case "e" 123 | (: e-label) 124 | (printf "The value is \"d\" or \"e\"\n")) 125 | (default 126 | (printf "The value is neither \"a\", \"b\", \"c\", \"d\", or \"e\"\n"))) 127 | (return 0)) 128 | 129 | Syntax 130 | ------ 131 | 132 | SEE ./tests/ FOR MORE SYNTAX EXAMPLES. 133 | 134 | -- 135 | 136 | The basic syntax of sxc follows that of the Lisp family of languages. 137 | 138 | The first argument to an s-expression is the function or keyword to be 139 | called or used, followed by it's arguments. 140 | 141 | There are a some exceptions for convenience: 142 | 143 | ++x - pre-increment on variables 144 | x++ - post-increment on variables 145 | 146 | This is due to the fact that all symbols are passed through directly 147 | to the C compiler, and you will find there are no exceptions to handle 148 | this in the sxc code. 149 | 150 | Also, for array operations: 151 | 152 | (*?++ ...) - post increment on pointers (*x++) 153 | (++?* ...) - pre increment on pointers (*(++x)) 154 | 155 | (*?-- ...) - post decrement on pointers (*x--) 156 | (--?* ...) - pre decrement on pointers (*(--x)) 157 | 158 | These are handled specially by sxc in the code. 159 | 160 | Utilities 161 | --------- 162 | 163 | A wrapper script 'sxcc.sh' is provided to simplify building and 164 | compilation of program sources into an executable. 165 | 166 | TODO: 167 | -- 168 | 169 | You can find some things to translate in to SXC in the to-translate/ directory. 170 | 171 | -- 172 | Burton Samograd 173 | busfactor1@icloud.com 174 | 2018 175 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | -- handle lispy operations 2 | (+ a b c ...) 3 | 4 | -- macros 5 | (macro (args ...) (code (CL))) 6 | 7 | -- macros 8 | - design/sswitch.sxc for an example 9 | 10 | -- add (inline-c "int x = 0;") 11 | 12 | DONE -- add (struct name 13 | type1 field 14 | type2 field ...) 15 | 16 | -- Allow for nested struct definitions 17 | (struct point 18 | int x 19 | int y 20 | (struct data 21 | int a 22 | int b) d) -------------------------------------------------------------------------------- /configure: -------------------------------------------------------------------------------- 1 | sudo apt install indent 2 | sudo apt install freeglut3-dev 3 | -------------------------------------------------------------------------------- /design/sswitch.sxc: -------------------------------------------------------------------------------- 1 | // sswitch - string switch 2 | // 3 | // a switch statement that works with strings 4 | // 5 | (macro sswitch (val &body cases) 6 | (labels ((genswitch (cases) 7 | (let* ((case (car cases)) 8 | (cmd (first case)) 9 | (val (second case)) 10 | (statements (cddr case)) 11 | condition) 12 | (when case 13 | (unless (listp val) 14 | (setf val (list val))) 15 | (mapcar (lambda (v) 16 | (setf condition (append conditiion `((strcmp ,v ,val) &&)))) 17 | val) 18 | (setf condition (append condition '(1))) 19 | `(if ,condition 20 | ,@statements 21 | (else 22 | ,@(genswitch (cdr cases)))))))) 23 | (genswitch cases)) 24 | 25 | 26 | (int main ((int argc) (char (** argv))) 27 | (sswitch ([] argv 1) 28 | (case ("a" "c") 29 | (printf "The value is \"a\" or \"c\"\n")) 30 | (case "b" 31 | (printf "The value is \"b\"\n")) 32 | (case "d" 33 | (goto fallthrough-e)) 34 | (case "e" 35 | (: fallthrough-e) 36 | (printf "The value is \"d\" or \"e\"\n")) 37 | (default 38 | (printf "The value is neither \"a\", \"b\", \"c\", \"d\", or \"e\"\n"))) 39 | (return 0)) 40 | -------------------------------------------------------------------------------- /doc/MACROS: -------------------------------------------------------------------------------- 1 | -- this isn't common lisp so we don't have to support backquoting and commas 2 | 3 | -- instead we could use shell script like expansion things maybe 4 | 5 | -------------------------------------------------------------------------------- /doc/NOTES: -------------------------------------------------------------------------------- 1 | -- to compile *pointer++ you must enter it either literally as the 2 | symbol '*pointer++' or as '(*pointer ++)' or using the function 3 | (*?++ pointer). Using the *?++ function is preferred so as to not 4 | need symbol creation in the code generating program. Same for 5 | *pointer--. 6 | -------------------------------------------------------------------------------- /doc/WIP.md: -------------------------------------------------------------------------------- 1 | NOTE: sxc is WIP (Work In Progress) 2 | 3 | TODO: make macros work 4 | 5 | This project was written over 5 years ago by my past self, and was 6 | found weeks ago by my current self, and this is a note, telling my 7 | future self that I should verify my work prior to publishing. 8 | 9 | When I found this project, after looking through the sources and 10 | remembering how far I'd gotten with the project, assumed that all the 11 | code I had written did actually work. 12 | 13 | I was wrong. 14 | 15 | Unfortunately, the main example, sswitch.sxc was but a dream. Macros 16 | have not yet been implememted in sxc as of the end of 2016. 17 | 18 | I apologize for the inconvenience should you have expected more from 19 | this project at its release. 20 | 21 | So did I. 22 | 23 | Cursory examination of the problem (adding Common Lisp macros to sxc) 24 | proved to show that this is not a simple problem to handle in Lisps. 25 | I call it the 'printed representation' problem. It comes not from 26 | Lisp itself, but from the combination of Lisps that are currently 27 | available for use. 28 | 29 | How does one describe this problem? 30 | 31 | I found it first in something I created using Emacs Lisp. It was an 32 | elegant solution to the programming problem of web development, 33 | elegantly combined together into a nested sea of S-Expressions 34 | combining a number of code generation languages. One of those 35 | languages was a compiler that was run as an external program. 36 | 37 | There was a macro written in emacs lisp that took the printed 38 | representation of it's argument S-Expressions and passed them to the 39 | external compiler. This is where the problem reared it's head, but in 40 | subtly strange ways. Days later, I came to the realization what the 41 | actual problem was. 42 | 43 | Lisp has many times. Read time, compile time and run time. It has 44 | little syntax, for convenience, like ''' and '`' and ','. At read 45 | time, the syntax is transformed away and replaced by internal 46 | identifiers, like so: 47 | 48 | ```lisp 49 | 'x => (quote x) 50 | `(+ ,x ,@y) => (quasiquote (+ (unquote x) (unquote-splice x))) 51 | ``` 52 | 53 | By the time the S-Expression has been READ by the reader, the syntax 54 | has mostly been stripped. The problem... 55 | 56 | We need to jack the reader. 57 | 58 | I'll explain more later. 59 | 60 | -- 61 | Dec 25, 2017 -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | all: fire struct hash-djb2 2 | 3 | fire: fire.sxc 4 | sxcc -g -o fire fire.sxc -framework GLUT -framework OpenGL 5 | 6 | hash-djb2: hash-djb2.sxc 7 | sxcc -g -o hash-djb2 hash-djb2.sxc 8 | 9 | struct: struct.sxc 10 | sxcc -g -o struct struct.sxc 11 | 12 | clean: 13 | rm -f fire struct hash-djb2 -------------------------------------------------------------------------------- /examples/broken/fire.c: -------------------------------------------------------------------------------- 1 | #line 2 "fire.sxc" 2 | #include 3 | #line 3 "fire.sxc" 4 | #include 5 | #line 4 "fire.sxc" 6 | #include 7 | #line 5 "fire.sxc" 8 | #include 9 | #line 6 "fire.sxc" 10 | #include 11 | #line 7 "fire.sxc" 12 | #include 13 | #line 8 "fire.sxc" 14 | #include 15 | #line 10 "fire.sxc" 16 | float (g_spin ) = (0.0 ); 17 | #line 11 "fire.sxc" 18 | int (g_width ) = (250 ); 19 | #line 12 "fire.sxc" 20 | int 21 | (g_height ) = (250 ); 22 | #line 14 "fire.sxc" 23 | unsigned char 24 | pixels [320 ][240 ][4 ]; 25 | #line 16 "fire.sxc" 26 | void f_keyboard( 27 | #line 16 "fire.sxc" 28 | unsigned char key , #line 16 "fire.sxc" 29 | int x , #line 16 "fire.sxc" 30 | int y ) { 31 | #line 17 "fire.sxc" 32 | exit(0 ); 33 | 34 | } 35 | #line 19 "fire.sxc" 36 | #line 19 "fire.sxc" 37 | double gettime(void ) {#line 20 "fire.sxc" 38 | double #line 20 "fire.sxc" 39 | (now ) = (#line 20 "fire.sxc" 40 | time(0 )); 41 | #line 21 "fire.sxc" 42 | struct timeval now_tv ; 43 | #line 22 "fire.sxc" 44 | now +=#line 22 "fire.sxc" 45 | ((#line 22 "fire.sxc" 46 | ((double )#line 22 "fire.sxc" 47 | now_tv.tv_usec) ) / (1000000.0 )); 48 | #line 23 "fire.sxc" 49 | return(now ); 50 | 51 | } 52 | #line 25 "fire.sxc" 53 | #line 25 "fire.sxc" 54 | void f_display(void ) {#line 26 "fire.sxc" 55 | double #line 26 "fire.sxc" 56 | (now ) = (#line 26 "fire.sxc" 57 | gettime()), then ; 58 | #line 27 "fire.sxc" 59 | glClear(#line 27 "fire.sxc" 60 | ((GL_COLOR_BUFFER_BIT ) | (GL_DEPTH_BUFFER_BIT ))); 61 | #line 28 "fire.sxc" 62 | glRasterPos3f(200 , 100 , 0 ); 63 | #line 30 "fire.sxc" 64 | glPushMatrix(); 65 | #line 31 "fire.sxc" 66 | glTranslatef(#line 31 "fire.sxc" 67 | ((g_width ) / (2 )), #line 31 "fire.sxc" 68 | ((g_height ) / (2 )), 0 ); 69 | #line 32 "fire.sxc" 70 | glRotatef(g_spin , 0 , 0 , 1 ); 71 | #line 33 "fire.sxc" 72 | glColor3f(1 , 1 , 1 ); 73 | #line 34 "fire.sxc" 74 | glRectf(-25 , -25 , 25 , 25 ); 75 | #line 35 "fire.sxc" 76 | glPopMatrix(); 77 | #line 36 "fire.sxc" 78 | (then ) = (#line 36 "fire.sxc" 79 | gettime()); 80 | #line 37 "fire.sxc" 81 | int #line 37 "fire.sxc" 82 | (sleepytime ) = (#line 37 "fire.sxc" 83 | ((#line 37 "fire.sxc" 84 | ((#line 37 "fire.sxc" 85 | ((1.0 ) / (60 ))) - (#line 37 "fire.sxc" 86 | ((then ) - (now ))))) * (1000000 ))); 87 | #line 38 "fire.sxc" 88 | printf("%d\n", sleepytime ); 89 | #line 39 "fire.sxc" 90 | fflush(stdout ); 91 | #line 40 "fire.sxc" 92 | usleep(sleepytime ); 93 | #line 41 "fire.sxc" 94 | glutSwapBuffers(); 95 | 96 | } 97 | #line 43 "fire.sxc" 98 | #line 43 "fire.sxc" 99 | void f_idle(void ) {#line 44 "fire.sxc" 100 | g_spin +=2.0 ; 101 | #line 45 "fire.sxc" 102 | if(#line 45 "fire.sxc" 103 | ((g_spin ) > (360.0 ))) {#line 46 "fire.sxc" 104 | g_spin -=360.0 ; 105 | }; 106 | #line 47 "fire.sxc" 107 | glutPostRedisplay(); 108 | 109 | } 110 | #line 49 "fire.sxc" 111 | #line 49 "fire.sxc" 112 | void f_mouse(#line 49 "fire.sxc" 113 | int button , #line 49 "fire.sxc" 114 | int state , #line 49 "fire.sxc" 115 | int x , #line 49 "fire.sxc" 116 | int y ) { 117 | #line 50 "fire.sxc" 118 | switch (button ) { 119 | case GLUT_LEFT_BUTTON : { 120 | #line 52 "fire.sxc" 121 | if(#line 52 "fire.sxc" 122 | state ==GLUT_DOWN ) {#line 53 "fire.sxc" 123 | glutIdleFunc(NULL ); 124 | break ; 125 | }; 126 | } 127 | case GLUT_RIGHT_BUTTON : { 128 | #line 56 "fire.sxc" 129 | if(#line 56 "fire.sxc" 130 | state ==GLUT_DOWN ) {#line 57 "fire.sxc" 131 | glutIdleFunc(f_idle ); 132 | break ; 133 | }; 134 | } 135 | } 136 | ; 137 | 138 | } 139 | #line 60 "fire.sxc" 140 | #line 60 "fire.sxc" 141 | void f_reshape(#line 60 "fire.sxc" 142 | int width , #line 60 "fire.sxc" 143 | int height ) { 144 | #line 61 "fire.sxc" 145 | (g_width ) = (width ); 146 | #line 62 "fire.sxc" 147 | (g_height ) = (height ); 148 | #line 63 "fire.sxc" 149 | glViewport(0 , 0 , g_width , g_height ); 150 | #line 64 "fire.sxc" 151 | glMatrixMode(GL_PROJECTION ); 152 | #line 65 "fire.sxc" 153 | glLoadIdentity(); 154 | #line 66 "fire.sxc" 155 | glOrtho(0 , g_width , 0 , g_height , -1 , 1 ); 156 | #line 67 "fire.sxc" 157 | glMatrixMode(GL_MODELVIEW ); 158 | 159 | } 160 | #line 69 "fire.sxc" 161 | #line 69 "fire.sxc" 162 | int main(#line 69 "fire.sxc" 163 | int argc , #line 69 "fire.sxc" 164 | char #line 69 "fire.sxc" 165 | (**argv )) { 166 | #line 70 "fire.sxc" 167 | glutInit(#line 70 "fire.sxc" 168 | (&(argc )), argv ); 169 | #line 72 "fire.sxc" 170 | int i , j , k , color ; 171 | #line 73 "fire.sxc" 172 | for(#line 73 "fire.sxc" 173 | (i ) = (0 ), #line 73 "fire.sxc" 174 | (color ) = (0 ); #line 73 "fire.sxc" 175 | ((i ) < (320 )); #line 73 "fire.sxc" 176 | (i ++), #line 73 "fire.sxc" 177 | (color ++)) { 178 | #line 74 "fire.sxc" 179 | for(#line 74 "fire.sxc" 180 | (j ) = (0 ); #line 74 "fire.sxc" 181 | ((j ) < (240 )); #line 74 "fire.sxc" 182 | (j ++)) { 183 | #line 75 "fire.sxc" 184 | for(#line 75 "fire.sxc" 185 | (k ) = (0 ); #line 75 "fire.sxc" 186 | ((k ) < (4 )); #line 75 "fire.sxc" 187 | (k ++)) { 188 | #line 76 "fire.sxc" 189 | (#line 76 "fire.sxc" 190 | (pixels [i ][j ][k ])) = (color ); 191 | }; 192 | }; 193 | }; 194 | #line 78 "fire.sxc" 195 | glClearColor(0 , 0 , 0 , 0 ); 196 | #line 79 "fire.sxc" 197 | glShadeModel(GL_FLAT ); 198 | #line 81 "fire.sxc" 199 | glutInitWindowPosition(0 , 0 ); 200 | #line 82 "fire.sxc" 201 | glutInitWindowSize(g_width , g_height ); 202 | #line 83 "fire.sxc" 203 | glutInitDisplayMode(#line 83 "fire.sxc" 204 | ((GLUT_RGBA )| (GLUT_DOUBLE ) |(GLUT_DEPTH ))); 205 | #line 84 "fire.sxc" 206 | int #line 84 "fire.sxc" 207 | (window ) = (#line 84 "fire.sxc" 208 | glutCreateWindow("sxc-fire")); 209 | #line 85 "fire.sxc" 210 | (window ) = (window ); 211 | #line 87 "fire.sxc" 212 | glutDisplayFunc(f_display ); 213 | #line 88 "fire.sxc" 214 | glutIdleFunc(f_idle ); 215 | #line 89 "fire.sxc" 216 | glutKeyboardFunc(f_keyboard ); 217 | #line 90 "fire.sxc" 218 | glutMouseFunc(f_mouse ); 219 | #line 91 "fire.sxc" 220 | glutReshapeFunc(f_reshape ); 221 | #line 93 "fire.sxc" 222 | glutPostRedisplay(); 223 | #line 94 "fire.sxc" 224 | glutMainLoop(); 225 | #line 96 "fire.sxc" 226 | return(0 ); 227 | 228 | } 229 | -------------------------------------------------------------------------------- /examples/broken/hash-djb2.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#include ) 3 | (#include ) 4 | 5 | /* 6 | 7 | djb2 8 | 9 | this algorithm (k=33) was first reported by dan bernstein many years 10 | ago in comp.lang.c. another version of this algorithm (now favored by 11 | bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i] 12 | 13 | the magic of 14 | number 33 (why it works better than many other constants, prime or 15 | not) has never been adequately explained. 16 | 17 | */ 18 | 19 | (int djb2 (((unsigned char) (* s)) (int a) (int b)) 20 | (var (unsigned long) (= hash a)) 21 | (var int c) 22 | 23 | (while (= c (*?++ s)) 24 | (= hash (+ (* hash b) c))) 25 | 26 | (return hash)) 27 | 28 | (int sort ((int* pa) (int* pb)) 29 | (var int (= a *pa)) 30 | (var int (= b *pb)) 31 | (if (< a b) 32 | (return -1) 33 | (else (if (> a b) 34 | (return 1) 35 | (else (return 0)))))) 36 | 37 | (int main ((int argc) (char (** argv))) 38 | (var (* FILE) (= file (fopen "words.txt" "r"))) 39 | (var char ([] word 128)) 40 | (var (* char) ([] words 354985)) 41 | (var (unsigned long) ([] hashes 354985)) 42 | (var (unsigned long) ([] counts 354985)) 43 | (var int (= nwords 0)) 44 | (var int (= nhashes 0)) 45 | 46 | (while (> (fscanf file "%s\n" word) 0) 47 | (= ([] hashes nwords) 0) 48 | (= ([] counts nwords) 0) 49 | (= ([] words (nwords ++)) (strdup word))) 50 | 51 | (var int i b) 52 | (for (= b 0) (< b 256) (b ++) 53 | (= nhashes 0) 54 | (memset hashes 0 (sizeof hashes)) 55 | (memset counts 0 (sizeof counts)) 56 | 57 | (for (= i 0) (< i nwords) (i ++) 58 | (var (unsigned long) hash) 59 | (= hash (djb2 (cast (* (unsigned char)) ([] words i)) 5381 b)) 60 | (= ([] hashes (nhashes ++)) hash)) 61 | 62 | (qsort hashes nhashes (sizeof ([] hashes 0)) sort) 63 | 64 | (var int (= total 1)) 65 | 66 | (for (= i 0) (< i nhashes) (i ++) 67 | (if (== ([] hashes i) ([] hashes (+ i 1))) 68 | continue) 69 | (total ++)) 70 | 71 | (printf "b: %d collisions: %d\n" b (- 354985 total)))) 72 | 73 | /* 74 | $ ./a.out | sort -rnk 4 75 | b: 0 collisions: 354953 76 | b: 1 collisions: 352788 77 | b: 128 collisions: 290754 78 | b: 64 collisions: 253515 79 | b: 192 collisions: 253491 80 | b: 2 collisions: 236075 81 | b: 32 collisions: 186630 82 | b: 160 collisions: 186513 83 | b: 224 collisions: 186459 84 | b: 96 collisions: 186440 85 | b: 16 collisions: 115047 86 | b: 80 collisions: 114854 87 | b: 208 collisions: 114563 88 | b: 240 collisions: 113265 89 | b: 144 collisions: 113190 90 | b: 176 collisions: 113161 91 | b: 48 collisions: 112967 92 | b: 112 collisions: 112848 93 | b: 3 collisions: 102283 94 | b: 4 collisions: 54276 95 | b: 8 collisions: 44477 96 | b: 24 collisions: 36635 97 | b: 88 collisions: 35781 98 | b: 216 collisions: 35777 99 | b: 152 collisions: 35773 100 | b: 248 collisions: 35534 101 | b: 120 collisions: 35449 102 | b: 184 collisions: 35377 103 | b: 56 collisions: 35357 104 | b: 136 collisions: 35346 105 | b: 72 collisions: 35317 106 | b: 200 collisions: 35314 107 | b: 168 collisions: 35256 108 | b: 104 collisions: 35252 109 | b: 40 collisions: 35251 110 | b: 232 collisions: 35211 111 | b: 5 collisions: 28918 112 | b: 6 collisions: 17870 113 | b: 7 collisions: 12656 114 | b: 10 collisions: 6349 115 | b: 9 collisions: 5736 116 | b: 12 collisions: 4908 117 | b: 14 collisions: 3383 118 | b: 20 collisions: 3368 119 | b: 11 collisions: 3090 120 | b: 13 collisions: 2414 121 | b: 15 collisions: 1921 122 | b: 212 collisions: 1743 123 | b: 188 collisions: 1631 124 | b: 84 collisions: 1535 125 | b: 148 collisions: 1517 126 | b: 124 collisions: 1495 127 | b: 252 collisions: 1492 128 | b: 76 collisions: 1459 129 | b: 68 collisions: 1447 130 | b: 140 collisions: 1446 131 | b: 60 collisions: 1444 132 | b: 52 collisions: 1435 133 | b: 132 collisions: 1434 134 | b: 116 collisions: 1433 135 | b: 204 collisions: 1427 136 | b: 36 collisions: 1424 137 | b: 164 collisions: 1415 138 | b: 196 collisions: 1412 139 | b: 180 collisions: 1410 140 | b: 244 collisions: 1399 141 | b: 220 collisions: 1398 142 | b: 100 collisions: 1392 143 | b: 28 collisions: 1391 144 | b: 228 collisions: 1389 145 | b: 92 collisions: 1378 146 | b: 156 collisions: 1374 147 | b: 172 collisions: 1359 148 | b: 108 collisions: 1337 149 | b: 236 collisions: 1336 150 | b: 44 collisions: 1332 151 | b: 17 collisions: 1143 152 | b: 18 collisions: 977 153 | b: 19 collisions: 620 154 | b: 21 collisions: 303 155 | b: 22 collisions: 255 156 | b: 23 collisions: 145 157 | b: 31 collisions: 114 158 | b: 255 collisions: 108 159 | b: 62 collisions: 97 160 | b: 58 collisions: 67 161 | b: 25 collisions: 66 162 | b: 29 collisions: 64 163 | b: 66 collisions: 60 164 | b: 77 collisions: 59 165 | b: 33 collisions: 59 166 | b: 34 collisions: 58 167 | b: 73 collisions: 55 168 | b: 69 collisions: 49 169 | b: 26 collisions: 49 170 | b: 78 collisions: 45 171 | b: 63 collisions: 43 172 | b: 39 collisions: 43 173 | b: 218 collisions: 43 174 | b: 35 collisions: 41 175 | b: 75 collisions: 39 176 | b: 61 collisions: 39 177 | b: 70 collisions: 35 178 | b: 71 collisions: 31 179 | b: 59 collisions: 31 180 | b: 38 collisions: 31 181 | b: 178 collisions: 31 182 | b: 82 collisions: 30 183 | b: 130 collisions: 30 184 | b: 93 collisions: 29 185 | b: 41 collisions: 29 186 | b: 190 collisions: 29 187 | b: 106 collisions: 29 188 | b: 74 collisions: 28 189 | b: 65 collisions: 28 190 | b: 50 collisions: 28 191 | b: 170 collisions: 27 192 | b: 30 collisions: 26 193 | b: 27 collisions: 26 194 | b: 194 collisions: 26 195 | b: 127 collisions: 26 196 | b: 54 collisions: 25 197 | b: 226 collisions: 25 198 | b: 174 collisions: 25 199 | b: 154 collisions: 25 200 | b: 131 collisions: 25 201 | b: 202 collisions: 24 202 | b: 123 collisions: 24 203 | b: 49 collisions: 23 204 | b: 147 collisions: 23 205 | b: 57 collisions: 22 206 | b: 55 collisions: 22 207 | b: 251 collisions: 22 208 | b: 177 collisions: 22 209 | b: 137 collisions: 22 210 | b: 46 collisions: 21 211 | b: 238 collisions: 21 212 | b: 165 collisions: 21 213 | b: 162 collisions: 21 214 | b: 150 collisions: 21 215 | b: 139 collisions: 21 216 | b: 79 collisions: 20 217 | b: 246 collisions: 20 218 | b: 245 collisions: 20 219 | b: 235 collisions: 20 220 | b: 234 collisions: 20 221 | b: 222 collisions: 20 222 | b: 185 collisions: 20 223 | b: 171 collisions: 20 224 | b: 157 collisions: 20 225 | b: 141 collisions: 20 226 | b: 125 collisions: 20 227 | b: 122 collisions: 20 228 | b: 105 collisions: 20 229 | b: 86 collisions: 19 230 | b: 53 collisions: 19 231 | b: 51 collisions: 19 232 | b: 231 collisions: 19 233 | b: 230 collisions: 19 234 | b: 217 collisions: 19 235 | b: 214 collisions: 19 236 | b: 209 collisions: 19 237 | b: 166 collisions: 19 238 | b: 151 collisions: 19 239 | b: 126 collisions: 19 240 | b: 119 collisions: 19 241 | b: 113 collisions: 19 242 | b: 94 collisions: 18 243 | b: 42 collisions: 18 244 | b: 207 collisions: 18 245 | b: 153 collisions: 18 246 | b: 114 collisions: 18 247 | b: 102 collisions: 18 248 | b: 101 collisions: 18 249 | b: 97 collisions: 17 250 | b: 90 collisions: 17 251 | b: 87 collisions: 17 252 | b: 67 collisions: 17 253 | b: 254 collisions: 17 254 | b: 253 collisions: 17 255 | b: 247 collisions: 17 256 | b: 225 collisions: 17 257 | b: 210 collisions: 17 258 | b: 198 collisions: 17 259 | b: 195 collisions: 17 260 | b: 149 collisions: 17 261 | b: 146 collisions: 17 262 | b: 142 collisions: 17 263 | b: 135 collisions: 17 264 | b: 111 collisions: 17 265 | b: 95 collisions: 16 266 | b: 85 collisions: 16 267 | b: 81 collisions: 16 268 | b: 45 collisions: 16 269 | b: 237 collisions: 16 270 | b: 213 collisions: 16 271 | b: 206 collisions: 16 272 | b: 182 collisions: 16 273 | b: 175 collisions: 16 274 | b: 155 collisions: 16 275 | b: 143 collisions: 16 276 | b: 118 collisions: 16 277 | b: 250 collisions: 15 278 | b: 223 collisions: 15 279 | b: 203 collisions: 15 280 | b: 191 collisions: 15 281 | b: 167 collisions: 15 282 | b: 138 collisions: 15 283 | b: 134 collisions: 15 284 | b: 121 collisions: 15 285 | b: 110 collisions: 15 286 | b: 109 collisions: 15 287 | b: 99 collisions: 14 288 | b: 91 collisions: 14 289 | b: 43 collisions: 14 290 | b: 37 collisions: 14 291 | b: 215 collisions: 14 292 | b: 186 collisions: 14 293 | b: 183 collisions: 14 294 | b: 181 collisions: 14 295 | b: 163 collisions: 14 296 | b: 145 collisions: 14 297 | b: 107 collisions: 14 298 | b: 239 collisions: 13 299 | b: 229 collisions: 13 300 | b: 219 collisions: 13 301 | b: 211 collisions: 13 302 | b: 201 collisions: 13 303 | b: 199 collisions: 13 304 | b: 197 collisions: 13 305 | b: 187 collisions: 13 306 | b: 169 collisions: 13 307 | b: 158 collisions: 13 308 | b: 133 collisions: 13 309 | b: 129 collisions: 13 310 | b: 117 collisions: 13 311 | b: 115 collisions: 13 312 | b: 47 collisions: 12 313 | b: 242 collisions: 12 314 | b: 233 collisions: 12 315 | b: 227 collisions: 12 316 | b: 221 collisions: 12 317 | b: 205 collisions: 12 318 | b: 193 collisions: 12 319 | b: 189 collisions: 12 320 | b: 173 collisions: 12 321 | b: 103 collisions: 12 322 | b: 89 collisions: 11 323 | b: 249 collisions: 11 324 | b: 241 collisions: 11 325 | b: 179 collisions: 11 326 | b: 161 collisions: 11 327 | b: 98 collisions: 10 328 | b: 243 collisions: 10 329 | b: 83 collisions: 9 330 | b: 159 collisions: 7 331 | $ # the least number of collisions in 354985 english words is with the constant 159. 332 | */ -------------------------------------------------------------------------------- /examples/fire.sxc: -------------------------------------------------------------------------------- 1 | // -*- mode: lisp -*- 2 | // fire.sxc 3 | // 4 | // An OpenGL Fire Demo. 5 | // 6 | // TODO: make work (crashes on startup) 7 | // 8 | (#include ) 9 | (#include ) 10 | (#include ) 11 | (#include ) 12 | (#include ) 13 | (#include ) 14 | (#include ) 15 | 16 | (var float (= g_spin 0.0)) 17 | (var int (= g_width 250)) 18 | (var int (= g_height 250)) 19 | 20 | (var (unsigned char) ([] pixels 320 240 4)) 21 | 22 | (void f_keyboard (((unsigned char) key) (int x) (int y)) 23 | (exit 0)) 24 | 25 | (double gettime (void) 26 | (var double (= now (time 0))) 27 | (var struct timeval now_tv) 28 | (+= now (/ (cast double (. now_tv tv_usec)) 1000000.0)) 29 | (return now)) 30 | 31 | (void f_display (void) 32 | (var double (= now (gettime)) then) 33 | (glClear (| GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)) 34 | (glRasterPos3f 200 100 0) 35 | //(glDrawPixels 320 240 GL_RGBA GL_UNSIGNED_BYTE pixels) 36 | (glPushMatrix) 37 | (glTranslatef (/ g_width 2) (/ g_height 2) 0) 38 | (glRotatef g_spin 0 0 1) 39 | (glColor3f 1 1 1) 40 | (glRectf -25 -25 25 25) 41 | (glPopMatrix) 42 | (= then (gettime)) 43 | (var int (= sleepytime (* (- (/ 1.0 60) (- then now)) 1000000))) 44 | (printf "%d\n" sleepytime) 45 | (fflush stdout) 46 | (usleep sleepytime) 47 | (glutSwapBuffers)) 48 | 49 | (void f_idle (void) 50 | (+= g_spin 2.0) 51 | (if (> g_spin 360.0) 52 | (-= g_spin 360.0)) 53 | (glutPostRedisplay)) 54 | 55 | (void f_mouse ((int button) (int state) (int x) (int y)) 56 | (switch button 57 | (GLUT_LEFT_BUTTON 58 | (if (== state GLUT_DOWN) 59 | (glutIdleFunc NULL) 60 | break)) 61 | (GLUT_RIGHT_BUTTON 62 | (if (== state GLUT_DOWN) 63 | (glutIdleFunc f_idle) 64 | break)))) 65 | 66 | (void f_reshape ((int width) (int height)) 67 | (= g_width width) 68 | (= g_height height) 69 | (glViewport 0 0 g_width g_height) 70 | (glMatrixMode GL_PROJECTION) 71 | (glLoadIdentity) 72 | (glOrtho 0 g_width 0 g_height -1 1) 73 | (glMatrixMode GL_MODELVIEW)) 74 | 75 | (int main ((int argc) (char (** argv))) 76 | (glutInit (& argc) argv) 77 | 78 | (var int i j k color) 79 | (for ((= i 0) (= color 0)) (< i 320) ((i ++)(color ++)) 80 | (for (= j 0) (< j 240) (j ++) 81 | (for (= k 0) (< k 4) (k ++) 82 | (= ([] pixels i j k) color)))) 83 | 84 | (glClearColor 0 0 0 0) 85 | (glShadeModel GL_FLAT) 86 | 87 | (glutInitWindowPosition 0 0) 88 | (glutInitWindowSize g_width g_height) 89 | (glutInitDisplayMode (| GLUT_RGBA GLUT_DOUBLE GLUT_DEPTH)) 90 | (var int (= window (glutCreateWindow "sxc-fire"))) 91 | (= window window) 92 | 93 | (glutDisplayFunc f_display) 94 | (glutIdleFunc f_idle) 95 | (glutKeyboardFunc f_keyboard) 96 | (glutMouseFunc f_mouse) 97 | (glutReshapeFunc f_reshape) 98 | 99 | (glutPostRedisplay) 100 | (glutMainLoop) 101 | 102 | (return 0)) 103 | -------------------------------------------------------------------------------- /examples/hash-djb2.sxc: -------------------------------------------------------------------------------- 1 | /* -*- mode: lisp -*- 2 | * 3 | * hash-djb2.sxc 4 | * 5 | * This example is meant to test Bernstein's hash algorithm 6 | * djb2 for collisions using values of k != 33. 7 | * 8 | * Burton Samograd 9 | * 2018 10 | */ 11 | 12 | (#include ) 13 | (#include ) 14 | (#include ) 15 | /* 16 | 17 | djb2 - Dan Bernstein's Hash Function 18 | 19 | this algorithm (k=33) was first reported by dan bernstein many years 20 | ago in comp.lang.c. another version of this algorithm (now favored by 21 | bernstein) uses xor: 22 | 23 | hash(i) = hash(i - 1) * 33 ^ str[i] 24 | 25 | the magic of number 33 (why it works better than many other constants, 26 | prime or not) has never been adequately explained. 27 | 28 | */ 29 | 30 | (int djb2 (((unsigned char) (* s)) (int a) (int b)) 31 | (var (unsigned long) (= hash a)) 32 | (var int c) 33 | 34 | (while (= c (*?++ s)) 35 | (= hash (+ (* hash b) c))) 36 | 37 | (return hash)) 38 | 39 | /* 40 | void swap(int *xp, int *yp) 41 | { 42 | int temp = *xp; 43 | *xp = *yp; 44 | *yp = temp; 45 | } 46 | 47 | // A function to implement bubble sort 48 | void bubbleSort(int arr[], int n) 49 | { 50 | int i, j; 51 | for (i = 0; i < n-1; i++) 52 | 53 | // Last i elements are already in place 54 | for (j = 0; j < n-i-1; j++) 55 | if (arr[j] > arr[j+1]) 56 | swap(&arr[j], &arr[j+1]); 57 | } 58 | */ 59 | 60 | (void swap (((unsigned long) (* a)) ((unsigned long) (* b))) 61 | (var (unsigned long) (= tmp (* a))) 62 | (= (* a) (* b)) 63 | (= (* b) tmp)) 64 | 65 | (void sort (((unsigned long) (* a)) (int n)) 66 | (var int i) 67 | (var int j) 68 | (for (= i 0) (< i (- n 1)) (++ i) 69 | (if (> ([] a j) ([] a (+ j 1))) 70 | (swap (& ([] a j)) (& ([] a (+ j 1))))))) 71 | 72 | (void debug ((char (* message))) 73 | (puts message) 74 | (fflush stdout)) 75 | 76 | 77 | (int main ((int argc) (char (** argv))) 78 | (var (* FILE) file) 79 | (var char ([] word 128)) 80 | (var (unsigned long *) 81 | (= hashes (malloc (* 354985 (sizeof (unsigned long)))))) 82 | (var (unsigned long *) 83 | (= counts (malloc (* 354985 (sizeof (unsigned long)))))) 84 | (var (char **) 85 | (= words (malloc (* 354985 (sizeof (char **)))))) 86 | (var int (= nwords 0)) 87 | (var int (= nhashes 0)) 88 | 89 | (debug "hash collision detector") 90 | 91 | (= file (fopen "./words.txt" "r")) 92 | (if (! file) 93 | (debug "error opening ./words.txt") 94 | (exit 1)) 95 | 96 | (debug "reading words...") 97 | (while (> (fscanf file "%s\n" word) 0) 98 | (= ([] hashes nwords) 0) 99 | (= ([] counts nwords) 0) 100 | (= ([] words (nwords ++)) (strdup word))) 101 | 102 | (var int i b) 103 | (for (= b 0) (< b 256) (b ++) 104 | (= nhashes 0) 105 | (memset hashes 0 (sizeof (* hashes))) 106 | (memset counts 0 (sizeof (* counts))) 107 | 108 | (for (= i 0) (< i nwords) (i ++) 109 | (var (unsigned long) hash) 110 | (= hash (djb2 (cast (* (unsigned char)) ([] words i)) 5381 b)) 111 | (= ([] hashes (nhashes ++)) hash)) 112 | 113 | (debug "; sorting hashes") // it crashes if you remove this 114 | (sort hashes nhashes) 115 | 116 | (var int (= total 1)) 117 | 118 | (for (= i 0) (< i nhashes) (i ++) 119 | (if (== ([] hashes i) ([] hashes (+ i 1))) 120 | continue) 121 | (total ++)) 122 | 123 | (printf "b: %d collisions: %d\n" b (- 354985 total)))) 124 | -------------------------------------------------------------------------------- /examples/hello.sxc: -------------------------------------------------------------------------------- 1 | // hello.sxc - the first program 2 | // 3 | // Burton Samograd 4 | // 2018 5 | 6 | (#include ) 7 | (int main ((int argc) (char (** argv))) 8 | (printf "Hello, World!\n")) 9 | 10 | -------------------------------------------------------------------------------- /examples/linear-lisp/README.md: -------------------------------------------------------------------------------- 1 | Linear Lisp 2 | ----------- 3 | 4 | An implementation of a Linear Lisp, as described by Baker: 5 | 6 | http://home.pipeline.com/~hbaker1/LinearLisp.html 7 | 8 | Abstract 9 | 10 | Linear logic has been proposed as one solution to the problem of 11 | garbage collection and providing efficient "update-in-place" 12 | capabilities within a more functional language. Linear logic conserves 13 | accessibility, and hence provides a mechanical metaphor which is more 14 | appropriate for a distributed-memory parallel processor in which 15 | copying is explicit. However, linear logic's lack of sharing may 16 | introduce significant inefficiencies of its own. 17 | 18 | We show an efficient implementation of linear logic called Linear Lisp 19 | that runs within a constant factor of non-linear logic. This Linear 20 | Lisp allows RPLACX operations, and manages storage as safely as a 21 | non-linear Lisp, but does not need a garbage collector. Since it 22 | offers assignments but no sharing, it occupies a twilight zone between 23 | functional languages and imperative languages. Our Linear Lisp Machine 24 | offers many of the same capabilities as combinator/graph reduction 25 | machines, but without their copying and garbage collection problems. 26 | 27 | WIP 28 | 29 | -- 30 | Burton Samograd 31 | 32 | 2018 -------------------------------------------------------------------------------- /examples/linear-lisp/interp.lisp: -------------------------------------------------------------------------------- 1 | ;; 2 | ;; meta-circular interpreter 3 | ;; 4 | ;; From paper.txt 5 | ;; 6 | ;; Todo: 7 | ;; 8 | ;; Implement or find implementation of dlet* (destructuring let*) 9 | ;; For some reason it's not in the code...must have been in some 10 | ;; fancy lisp Baker was used to. 11 | ;; 12 | 13 | (defmacro free (x) `(setq ,x nil)) ; just for testing... 14 | 15 | (defmacro mif (be te ee) 16 | (let ((vs (car (freevars be)))) 17 | `(if-function 18 | #'(lambda () ,be) 19 | #'(lambda ,vs ,te) 20 | #'(lambda ,vs ,ee)))) 21 | 22 | (defun if-function (be te ee) 23 | (dlet* (((pval . stuff) (funcall be))) 24 | (if pval (apply te stuff) (apply ee stuff)))) 25 | 26 | (defmacro mcond (&rest clauses) 27 | (dlet* ((((be . rest) . clauses) clauses)) 28 | (if (eq be 't) `(progn ,@rest) 29 | `(mif ,be (progn ,@rest) 30 | (mcond ,@clauses))))) 31 | 32 | (defun matom (x) `(,(atom x) ,x)) 33 | 34 | (defun meq (x y) `(,(eq x y) ,x)) 35 | 36 | (defun meq2 (x y) `(,(eq x y) ,x ,y)) 37 | 38 | (defun msymbolp (x) `(,(symbolp x) ,x)) 39 | 40 | (defun mnull (x) `(,(null x) ,x)) 41 | 42 | (defun mcopy (x) ; Pedagogical non-primitive copy function. 43 | (mif (matom x) `(,x ,@x) ; primitive is allowed to copy symbol refs. 44 | (dlet* (((carx . cdrx) x) 45 | ((ncar1 . ncar2) (mcopy carx)) 46 | ((ncdr1 . ncdr2) (mcopy cdrx))) 47 | `((,ncar1 ,@ncdr1) . (,ncar2 ,@ncdr2))))) 48 | 49 | (defun massoc (x e) ; return binding, else nil. 50 | (mif (mnull e) (progn (free x) `(nil ,@e)) 51 | (dlet* ((((var . val) . reste) e)) 52 | (mif (meq2 x var) (progn (free x) `((,var ,@val) ,@reste)) 53 | (dlet* (((nbinding . nreste) (massoc x reste))) 54 | `(,nbinding . ((,var ,@val) ,@nreste))))))) 55 | 56 | (defun mevprogn (xl e) 57 | (dlet* (((x . xl) xl)) 58 | (mif (mnull xl) (progn (assert (null xl)) (meval x e)) 59 | (dlet* (((xval . ne) (meval x e)) 60 | ((xlval . nne) (mevprogn xl ne))) 61 | (free xval) 62 | `(,xlval ,@nne))))) 63 | 64 | (defun meval (x e) 65 | (mcond 66 | ((msymbolp x) (dlet* ((((var . val) . ne) (massoc x e))) 67 | (free var) 68 | `(,val ,@ne))) 69 | ((matom x) `(,x ,@e)) 70 | (t (dlet* ((fn . args) x)) 71 | (mcond 72 | ((meq fn 'progn) (free fn) (mevprogn args e)) 73 | ((meq fn 'function) (free fn) 74 | (dlet* (((lambda) args) 75 | ((nlambda . lambda) (mcopy lambda)) 76 | ((fvars . bvars) (freevars `(function ,nlambda) nil nil)) 77 | ((e1 . e2) (split fvars e))) 78 | `((funarg ,lambda ,e1) ,@e2))) 79 | ((meq fn 'funcall) (free fn) 80 | (dlet* ((((nfn . nargs) . ne) (mevlis args e))) 81 | `(,(mapply nfn nargs) ,@ne))) 82 | (t (dlet* (((nargs . ne) (mevlis args e))) 83 | `(,(mapply (symbol-function fn) nargs) ,@ne)))))))) 84 | 85 | (defun mapply (fn args) 86 | (mif (matom fn) (apply fn args) 87 | (dlet* (((ffn . rfn) fn)) 88 | (mcond 89 | ((meq ffn 'lambda) (free ffn) 90 | (dlet* (((bvlist . body) rfn) 91 | ((v . ne) (mevprogn body (mpairlis bvlist args nil)))) 92 | (assert (null ne)) 93 | v)) 94 | ((meq ffn 'funarg) (free ffn) 95 | (dlet* ((((lambda bvlist . body) ce) rfn) 96 | ((v . ne) (mevprogn body (mpairlis bvlist args ce)))) 97 | (free lambda) (assert (null ne)) 98 | v)) 99 | (t (error "mapply: bad fn ~S" fn)))))) 100 | 101 | (defun mpairlis (vars vals e) 102 | (mif (mnull vars) (progn (assert (null vals)) e) 103 | (dlet* (((var . vars) vars) 104 | ((val . vals) vals)) 105 | `((,var ,@val) ,@(mpairlis vars vals e))))) 106 | 107 | (defun mevlis (args e) 108 | (mif (mnull args) (progn (assert (null args)) `(nil ,@e)) 109 | (dlet* (((x . args) args) 110 | ((xval . e) (meval x e)) 111 | ((argvals . e) (mevlis args e))) 112 | `((,xval ,@argvals) ,@e)))) 113 | 114 | (defun split (vars e) ; split env. into 2 segments. 115 | (mif (mnull vars) (progn (assert (null vars)) `(nil ,@e)) 116 | (dlet* (((var . nvars) vars) 117 | ((binding . ne) (massoc var e)) 118 | ((e1 . e2) (split nvars ne))) 119 | `((,binding ,@e1) ,@e2)))) 120 | 121 | (defun mmember (x ls) ; return truth value & rest of list. 122 | (mif (mnull ls) (progn (free x) `(nil ,@ls)) 123 | (dlet* (((carls . ls) ls)) 124 | (mif (meq2 x carls) (progn (free x) `(,carls ,@ls)) 125 | (dlet* (((tval . rest) (mmember x ls))) 126 | `(,tval . (,carls ,@rest))))))) 127 | 128 | (defun freevars (x bvars fvars) ; return new fvars and new bvars. 129 | (mcond 130 | ((msymbolp x) 131 | (dlet* (((x1 . x2) (mcopy x)) 132 | ((x2 . x3) (mcopy x2)) 133 | ((p1val . nbvars) (mmember x1 bvars)) 134 | ((p2val . nfvars) (mmember x2 fvars))) 135 | (mif p1val (progn (free x3) `(,nfvars ,@nbvars)) 136 | (mif p2val (progn (free x3) `(,nfvars ,@nbvars)) 137 | `((,x ,@nfvars) ,@nbvars))))) 138 | ((matom x) (free x) `(,fvars ,@bvars)) 139 | (t (dlet* (((fn . args) x)) 140 | (mcond 141 | ((meq fn 'function) (free fn) 142 | (dlet* ((((lambda bvlist . body)) args)) 143 | (free lambda) 144 | (freelistvars body `(,@bvlist ,@bvars) fvars))) 145 | (t (freelistvars `(,fn ,@args) bvars fvars))))))) 146 | 147 | (defun freelistvars (xl bvars fvars) 148 | (mif (mnull xl) (progn (assert (null xl)) `(,fvars ,@bvars)) 149 | (dlet* (((x . xl) xl) 150 | ((nfvars . nbvars) (freelistvars xl bvars fvars))) 151 | (freevars x nbvars nfvars)))) 152 | -------------------------------------------------------------------------------- /examples/linear-lisp/paper.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burtonsamograd/sxc/0ae95779f3deb5cc37b2f7aaa4b7af0e84ee2138/examples/linear-lisp/paper.html -------------------------------------------------------------------------------- /examples/struct.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (struct data 4 | int a 5 | int b) 6 | 7 | (struct point 8 | int x 9 | int y 10 | (struct data) d 11 | ) 12 | 13 | (int main () 14 | (printf "struct")) 15 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | for i in sxc sxcc sxc.sh sxcc.sh; do echo ln -s $(pwd)/$i /usr/local/bin/$i; done 2 | -------------------------------------------------------------------------------- /profile: -------------------------------------------------------------------------------- 1 | PATH=`pwd`:$PATH 2 | -------------------------------------------------------------------------------- /rd: -------------------------------------------------------------------------------- 1 | find . -name '*~' -exec rm {} \; 2 | -------------------------------------------------------------------------------- /run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | PWD=$(pwd) 4 | 5 | for i in $(find tests -name '*.sxc' | grep -v -e 'part[0-9]' | sort -n); do 6 | echo -n $i... 7 | { ./sxcc.sh -I$(PWD) -Wall $i && 8 | { [ -f $i.in ] && 9 | { ./a.out < $i.in | cmp $i.out - ; } || 10 | { ./a.out | cmp $i.out - ; } 11 | } && 12 | echo "passed!" 13 | } || { echo -e "\n*** failed: Compilation results of $i\n\n" && ./sxc.sh $i ; break ; } 14 | done 15 | 16 | rm a.out 17 | -------------------------------------------------------------------------------- /sxc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burtonsamograd/sxc/0ae95779f3deb5cc37b2f7aaa4b7af0e84ee2138/sxc -------------------------------------------------------------------------------- /sxc-pretty: -------------------------------------------------------------------------------- 1 | sxc.sh -------------------------------------------------------------------------------- /sxc.sh: -------------------------------------------------------------------------------- 1 | ${SXC-sxc} $1 | indent 2 | -------------------------------------------------------------------------------- /sxcc: -------------------------------------------------------------------------------- 1 | sxcc.sh -------------------------------------------------------------------------------- /sxcc.sh: -------------------------------------------------------------------------------- 1 | 2 | sxc=${SXC-sxc}.sh 3 | delete_temp_file=true 4 | verbose=false 5 | 6 | tempfile () { 7 | dir=$1 8 | head=$2 9 | tail=$3 10 | 11 | name=$dir/$head-${RANDOM}$tail; 12 | mkdir -p $(dirname $name) 13 | touch $name 14 | echo $name 15 | } 16 | 17 | verbose () { 18 | if [[ $verbose == true ]]; then 19 | echo $* 20 | fi 21 | } 22 | 23 | print_help() { 24 | echo "sxcc.sh: compile and pass files to CC" 25 | echo "Options:" 26 | echo 27 | echo "--keep-temp-file: do not delete temporary C files after compilation with sxc" 28 | echo "--verbose: print some internal information about sxcc.sh" 29 | echo "--help: this message" 30 | } 31 | 32 | args="" 33 | sxc_files="" 34 | num_files=0 35 | # iterate over args, handling sxcc options, keeping a list of .sxc files and keeping a list of other args 36 | for i in $*; do 37 | case $i in 38 | --keep-temp-file) 39 | delete_temp_file=false;; 40 | --help) 41 | print_help;; 42 | --verbose) 43 | verbose=true;; 44 | *.sxc) 45 | sxc_files="$sxc_files $i" 46 | : $((num_files++));; 47 | *) 48 | args="$args $i";; 49 | esac 50 | done 51 | 52 | # check for no input files and return error if so 53 | if [[ -z $sxc_files ]]; then 54 | echo "sxcc: no input files" 1>&2 55 | exit 1 56 | fi 57 | 58 | # generate c file names from sxc file names 59 | c_files="" 60 | for file in $sxc_files; do 61 | c_file=$(tempfile . $file .c) 62 | c_files="$c_files $c_file" 63 | done 64 | 65 | verbose "args: $args" 66 | verbose "sxc_files: $sxc_files" 67 | verbose "c_files: $c_files" 68 | 69 | # convert the files variables to arrays 70 | sxc_files=( $sxc_files ) 71 | c_files=( $c_files ) 72 | 73 | # compile sxc files to c files 74 | for ((i=0; i<$num_files; i++)); do 75 | $sxc ${sxc_files[i]} > ${c_files[i]} 76 | done 77 | 78 | cc_cmd="${CC-gcc} $args ${c_files[*]}" 79 | verbose "cc_cmd: $cc_cmd" 80 | $cc_cmd 81 | 82 | # delete temp files unless told not to 83 | if [[ $delete_temp_files == true ]]; then 84 | rm -f ${c_files[*]} 85 | fi 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /tests/adder.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | (printf "%d+%d=%d\n" 1 2 (+ 1 2)) 5 | (printf "%d*%d=%d\n" 1 2 (* 1 2)) 6 | (printf "((2 + 3) * 5)=%d\n" (* (+ 2 3) 5)) 7 | 8 | (return 0)) -------------------------------------------------------------------------------- /tests/adder.sxc.out: -------------------------------------------------------------------------------- 1 | 1+2=3 2 | 1*2=2 3 | ((2 + 3) * 5)=25 4 | -------------------------------------------------------------------------------- /tests/array-initialization.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | (var int i 5 | (= ([] x) ({,} 1 2 3 4 5 6 7 8 9 10)) 6 | (= nx (/ (sizeof x) (sizeof ([] x 0))))) 7 | 8 | (for (= i 0) (< i nx) (i ++) 9 | (printf "%d\n" ([] x i))) 10 | 11 | (return 0)) 12 | -------------------------------------------------------------------------------- /tests/array-initialization.sxc.out: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 9 | 9 10 | 10 11 | -------------------------------------------------------------------------------- /tests/bf.sxc: -------------------------------------------------------------------------------- 1 | /* bf.sxc - a simple brainfuck interpreter */ 2 | (#include ) 3 | (#include ) 4 | (#include ) 5 | 6 | (var char ([] code 32768)) 7 | (var (unsigned char) ([] m 37268)) 8 | (var (unsigned char) (= (* dp) m) (= (* dendp) (- (+ m (sizeof m)) 1))) 9 | 10 | (int main ((int argc) (char (** argv))) 11 | (var char c ([] (* sp) 255) (= (* ip) code)) 12 | (var int (= si 0)) 13 | 14 | (var FILE (= (* f) stdin)) 15 | 16 | /* strip whitespace and comments */ 17 | (while (!= (= c (getc f)) EOF) 18 | (if (strchr "<>+-.,[]" c) 19 | (= (*?++ ip) c))) // note how to perform *ip++ is (*?++ ip) 20 | 21 | (= (*?++ ip) 0) // see above comment 22 | (= ip code) 23 | 24 | (while (!= (= c *ip++) 0) 25 | (switch c 26 | ('> // shift to next cell 27 | (dp ++) 28 | /* wraparound */ 29 | (if (> dp dendp) 30 | (= dp m)) 31 | break) 32 | ('< /* shift to previous cell */ 33 | (dp --) 34 | /* wraparound */ 35 | (if (< dp m) 36 | (= dp dendp)) 37 | break) 38 | ('+ /* add 1 to current cell */ 39 | ((* dp) ++) 40 | break) 41 | ('- /* sub 1 from current cell */ 42 | ((* dp) --) 43 | break) 44 | ('. /* print the current cell */ 45 | (putchar (* dp)) 46 | break) 47 | (', /* read character into current cell */ 48 | (= (* dp) (getchar)) 49 | break) 50 | ('[ /* begin loop */ 51 | /* if current cell not equal to zero */ 52 | (if (* dp) 53 | /* save loop start */ 54 | (= ([] sp (si ++)) (- ip 1)) 55 | (else 56 | (var int (= count 1)) 57 | (while (&& count (* ip)) 58 | (if (== (* ip) ']) 59 | (-- count) 60 | (else (if (== (* ip) '[) 61 | (count ++)))) 62 | (ip ++)))) 63 | break) 64 | ('] /* end loop */ 65 | (= ip ([] sp (-- si))) 66 | break))) 67 | (return 0)) 68 | -------------------------------------------------------------------------------- /tests/bf.sxc.in: -------------------------------------------------------------------------------- 1 | #ASCII value of 0 2 | ++++++++++++++++++++++++++++++++++++++++++++++++ 3 | > 4 | #Loop counter variable 5 | ++++++++++ 6 | [ 7 | < . + > 8 | - 9 | ] 10 | #H 11 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 12 | ++++++++++++++++++++++ 13 | . 14 | > 15 | #e 16 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 17 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 18 | + 19 | . 20 | > 21 | #l 22 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 23 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 24 | ++++++++ 25 | . 26 | > 27 | #l 28 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 29 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 30 | ++++++++ 31 | . 32 | > 33 | #o 34 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 35 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 36 | +++++++++++ 37 | . 38 | > 39 | #SPACE 40 | ++++++++++++++++++++++++++++++++ 41 | . 42 | > 43 | #w 44 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 45 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 46 | +++++++++++++++++++ 47 | . 48 | > 49 | #o 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 51 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 52 | +++++++++++ 53 | . 54 | > 55 | #r 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 57 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 58 | ++++++++++++++ 59 | . 60 | > 61 | #l 62 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 63 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 64 | ++++++++ 65 | . 66 | > 67 | #d 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 69 | ++++++++++++++++++++++++++++++++++++++++++++++++++ 70 | . 71 | -------------------------------------------------------------------------------- /tests/bf.sxc.out: -------------------------------------------------------------------------------- 1 | 0123456789Hello world -------------------------------------------------------------------------------- /tests/block.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | // block scoping test 4 | (int main ((int argc) (char (** argv))) 5 | (var int (= x 10)) 6 | ({} 7 | (var int (= x 20)) 8 | (printf "x: %d\n" x)) 9 | (printf "x: %d\n" x) 10 | (return 0)) 11 | -------------------------------------------------------------------------------- /tests/block.sxc.out: -------------------------------------------------------------------------------- 1 | x: 20 2 | x: 10 3 | -------------------------------------------------------------------------------- /tests/break.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#include ) 3 | (#include ) 4 | 5 | /* trim: remove trailing blanks, tabs, newlines */ 6 | (int trim ((char ([] s))) 7 | (var int n) 8 | 9 | (for (= n (- (strlen s) 1)) (>= n 0) (n --) 10 | (if (&& (!= ([] s n) ' ) 11 | (!= ([] s n) '\t) 12 | (!= ([] s n) '\n)) 13 | break) 14 | (= ([] s n) '\0)) 15 | (return n)) 16 | 17 | (int main ((int argc) (char (** argv))) 18 | (var char (= (* s) (strdup "testing\t\n \t\n"))) 19 | (trim s) 20 | (printf "'%s'\n" s) 21 | (free s) 22 | (= s (strdup " \t \n")) 23 | (trim s) 24 | (printf "'%s'\n" s) 25 | (free s) 26 | (= s (strdup "")) 27 | (trim s) 28 | (printf "'%s'\n" s) 29 | (free s) 30 | (return 0)) -------------------------------------------------------------------------------- /tests/break.sxc.out: -------------------------------------------------------------------------------- 1 | 'testing' 2 | '' 3 | '' 4 | -------------------------------------------------------------------------------- /tests/comma.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#include ) 3 | (#include ) 4 | // from p63 of K&R 5 | 6 | (int main () 7 | (var int c i j) 8 | (var char (= (* string) "testing")) 9 | (var char (= (* s) (malloc (strlen string)))) 10 | (strcpy s string) 11 | // reverse the string s 12 | (for (, (= i 0) (= j (- (strlen s) 1))) (< i j) (, (i ++) (j --)) 13 | (= c (, ([] s i) (= ([] s i) ([] s j)) (= ([] s j) c)))) 14 | (printf "%s\n" s) 15 | (return 0)) 16 | -------------------------------------------------------------------------------- /tests/comma.sxc.out: -------------------------------------------------------------------------------- 1 | gnitset 2 | -------------------------------------------------------------------------------- /tests/dowhile.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#include ) 3 | 4 | /* reverse: reverse string s in place */ 5 | (void reverse ((char ([] s))) 6 | (var int c i j) 7 | 8 | (for ((= i 0) (= j (- (strlen s) 1))) (< i j) ((i ++) (j --)) 9 | (= c ([] s i)) 10 | (= ([] s i) ([] s j)) 11 | (= ([] s j) c))) 12 | 13 | /* itoa: convert n characters to s */ 14 | (void itoa ((int n) (char ([] s))) 15 | (var int i sign) 16 | 17 | (if (< (= sign n) 0) /* record sign */ 18 | /* make n positive */ 19 | (= n (- n))) 20 | 21 | (= i 0) 22 | 23 | (do 24 | (= ([] s (i ++)) (+ (% n 10) '0)) 25 | (while (> (/= n 10) 0))) 26 | 27 | (if (< sign 0) 28 | (= ([] s (i ++)) '-)) 29 | 30 | (= ([] s i) '\0) 31 | (reverse s)) 32 | 33 | (int main ((int argc) (char (** argv))) 34 | (var char ([] s 20)) 35 | (itoa 1234567890 s) 36 | (printf "%s\n" s) 37 | (return 0)) 38 | -------------------------------------------------------------------------------- /tests/dowhile.sxc.out: -------------------------------------------------------------------------------- 1 | 1234567890 2 | -------------------------------------------------------------------------------- /tests/equals.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | (var int a x (= y 1) (= z 0)) 5 | (= a x (, y z)) 6 | (return 0)) 7 | -------------------------------------------------------------------------------- /tests/equals.sxc.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burtonsamograd/sxc/0ae95779f3deb5cc37b2f7aaa4b7af0e84ee2138/tests/equals.sxc.out -------------------------------------------------------------------------------- /tests/goto.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | 5 | (puts "before loop") 6 | 7 | (: before_loop) 8 | 9 | (if 1 10 | (goto after_loop) 11 | (else (if 0 // should indent this way, TODO: figure out how to do this in emacs (indent back to if level) 12 | (goto before_loop)))) 13 | 14 | (while 1 15 | (puts "infinite loop")) 16 | 17 | (if 0 18 | (goto before_loop)) 19 | 20 | (: after_loop) 21 | 22 | (puts "after loop") 23 | 24 | (return 0)) -------------------------------------------------------------------------------- /tests/goto.sxc.out: -------------------------------------------------------------------------------- 1 | before loop 2 | after loop 3 | -------------------------------------------------------------------------------- /tests/hello.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | (printf "Hello, World!\n") 5 | 6 | (return 0)) 7 | -------------------------------------------------------------------------------- /tests/hello.sxc.out: -------------------------------------------------------------------------------- 1 | Hello, World! 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p12.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | // print Fhrenheit-Celsius table 4 | // for fahr = 0, 20, ..., 300 floating-point version 5 | (int main ((int argc) (char (** argv))) 6 | (var float fahr celsius) 7 | (var int lower upper step) 8 | 9 | /* comment */ 10 | (= lower 0) 11 | (= upper 300) 12 | (= step 20) 13 | 14 | /* comment */ 15 | (= fahr lower) 16 | 17 | /* comment */ 18 | (while (<= fahr upper) 19 | (= celsius /* testing */ (* (/ 5.0 9.0) (- fahr 32.0))) 20 | (printf "%3.0f %6.1f\n" fahr celsius) 21 | (= fahr (+ fahr step))) 22 | (return 0)) 23 | 24 | /* /* comment */ */ 25 | -------------------------------------------------------------------------------- /tests/k_and_r/p12.sxc.out: -------------------------------------------------------------------------------- 1 | 0 -17.8 2 | 20 -6.7 3 | 40 4.4 4 | 60 15.6 5 | 80 26.7 6 | 100 37.8 7 | 120 48.9 8 | 140 60.0 9 | 160 71.1 10 | 180 82.2 11 | 200 93.3 12 | 220 104.4 13 | 240 115.6 14 | 260 126.7 15 | 280 137.8 16 | 300 148.9 17 | -------------------------------------------------------------------------------- /tests/k_and_r/p13.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | // print Fahrenheit-Celcius table 4 | (int main ((int argc) (char (** argv))) 5 | (var int fahr) 6 | (for (= fahr 0) (<= fahr 200) (= fahr (+ fahr 20)) 7 | (printf "%3d %6.1f\n" fahr (* (/ 5.0 9.0) (- fahr 32)))) 8 | (return 0)) 9 | -------------------------------------------------------------------------------- /tests/k_and_r/p13.sxc.out: -------------------------------------------------------------------------------- 1 | 0 -17.8 2 | 20 -6.7 3 | 40 4.4 4 | 60 15.6 5 | 80 26.7 6 | 100 37.8 7 | 120 48.9 8 | 140 60.0 9 | 160 71.1 10 | 180 82.2 11 | 200 93.3 12 | -------------------------------------------------------------------------------- /tests/k_and_r/p15.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (#define LOWER 0) // lower limit of table 4 | (#define UPPER 200) // upper limit 5 | (#define STEP 20) // step size 6 | 7 | (int main ((int argc) (char (** argv))) 8 | (var int fahr) 9 | 10 | (for (= fahr LOWER) (<= fahr UPPER) (= fahr (+ fahr STEP)) 11 | (printf "%3d %6.1f\n" fahr (* (/ 5.0 9.0) (- fahr 32)))) 12 | (return 0)) 13 | -------------------------------------------------------------------------------- /tests/k_and_r/p15.sxc.out: -------------------------------------------------------------------------------- 1 | 0 -17.8 2 | 20 -6.7 3 | 40 4.4 4 | 60 15.6 5 | 80 26.7 6 | 100 37.8 7 | 120 48.9 8 | 140 60.0 9 | 160 71.1 10 | 180 82.2 11 | 200 93.3 12 | -------------------------------------------------------------------------------- /tests/k_and_r/p16.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | // copy input to output; 1st version 4 | (int main ((int argc) (char (** argv))) 5 | (var int c) 6 | (= c (getchar)) 7 | (while (!= c EOF) 8 | (putchar c) 9 | (= c (getchar))) 10 | (return 0)) -------------------------------------------------------------------------------- /tests/k_and_r/p16.sxc.in: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p16.sxc.out: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p17.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | (var int c) 5 | 6 | (while (!= (= c (getchar)) EOF) 7 | (putchar c)) 8 | (return 0)) -------------------------------------------------------------------------------- /tests/k_and_r/p17.sxc.in: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p17.sxc.out: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p18-1.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | // count characters in input; 1st version 4 | (int main ((int argc) (char (** argv))) 5 | (var long nc) 6 | 7 | (= nc 0) 8 | 9 | (while (!= (getchar) EOF) 10 | (++ nc)) 11 | (printf "%ld\n" nc) 12 | 13 | (return 0)) 14 | -------------------------------------------------------------------------------- /tests/k_and_r/p18-1.sxc.in: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p18-1.sxc.out: -------------------------------------------------------------------------------- 1 | 15 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p18-2.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | (var double nc) 5 | 6 | (for (= nc 0) (!= (getchar) EOF) (nc ++)) 7 | 8 | (printf "%.0f\n" nc) 9 | 10 | (return 0)) 11 | -------------------------------------------------------------------------------- /tests/k_and_r/p18-2.sxc.in: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p18-2.sxc.out: -------------------------------------------------------------------------------- 1 | 15 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p19.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main ((int argc) (char (** argv))) 4 | (var int c nl) 5 | 6 | (= nl 0) 7 | 8 | (while (!= (= c (getchar)) EOF) 9 | (if (== c '\n) 10 | (++ nl))) 11 | 12 | (printf "%d\n" nl) 13 | 14 | (return 0)) -------------------------------------------------------------------------------- /tests/k_and_r/p19.sxc.in: -------------------------------------------------------------------------------- 1 | this is a test 2 | this is a test 2 3 | this is a test 3 4 | -------------------------------------------------------------------------------- /tests/k_and_r/p19.sxc.out: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p20.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (#define IN 1) // inside a word 4 | (#define OUT 0) // outside a word 5 | 6 | // count words, lines and characters in input 7 | (int main ((int argc) (char (** argv))) 8 | (var int c nl nw nc state) 9 | 10 | (= state OUT) 11 | (= nl nw nc 0) 12 | 13 | (while (!= (= c (getchar)) EOF) 14 | (++ nc) 15 | (if (== c '\n) 16 | (++ nl)) 17 | (if (|| (== c ' ) (== c '\n) (== c '\t)) 18 | (= state OUT) 19 | (else (if (== state OUT) 20 | (= state IN) 21 | (++ nw))))) 22 | (printf "%d %d %d\n" nl nw nc) 23 | 24 | (return 0)) 25 | 26 | -------------------------------------------------------------------------------- /tests/k_and_r/p20.sxc.in: -------------------------------------------------------------------------------- 1 | this 2 | is 3 | a 4 | test 5 | again -------------------------------------------------------------------------------- /tests/k_and_r/p20.sxc.out: -------------------------------------------------------------------------------- 1 | 4 5 20 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p22.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int main () 4 | (var int c i nwhite nother) 5 | (var int ([] ndigit 10)) 6 | 7 | (= nwhite nother 0) 8 | 9 | (for (= i 0) (< i 10) (++ i) 10 | (= ([] ndigit i) 0)) 11 | 12 | (while (!= (= c (getchar)) EOF) 13 | (if (&& (>= c '0) (<= c '9)) 14 | (++ ([] ndigit (- c '0))) 15 | (else 16 | (if (|| (== c ' ) (== c '\n) (== c '\t)) 17 | (++ nwhite) 18 | (else 19 | (++ nother)))))) 20 | 21 | (printf "digits =") 22 | 23 | (for (= i 0) (< i 10) (++ i) 24 | (printf " %d" ([] ndigit i))) 25 | (printf ", white space = %d, other = %d\n" nwhite nother) 26 | 27 | (return 0)) 28 | 29 | 30 | -------------------------------------------------------------------------------- /tests/k_and_r/p22.sxc.in: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* count digits, white space, others */ 4 | main () 5 | { 6 | int c, i, nwhite, nother; 7 | int ndigit[10]; 8 | 9 | nwhite = nother = 0; 10 | (for (i = 0; i < 10; ++i) 11 | ndigit[i] = 0; 12 | 13 | while ((c = getchar()) != EOF) 14 | if (c == '0' && c = '9') 15 | ++ndigit[c-'0']; 16 | else if(c == ' ' || c = '\n' || c == '\t') 17 | ++nwhite; 18 | else 19 | ++nother; 20 | 21 | printf("digits ="); 22 | for (i = 0; i < 10; ++i) 23 | printf(" %d", ndigit[i]); 24 | printf(", white space = %d, other = %d\n"), 25 | nwhite, nother); 26 | } -------------------------------------------------------------------------------- /tests/k_and_r/p22.sxc.out: -------------------------------------------------------------------------------- 1 | digits = 9 3 0 0 0 0 0 0 0 1, white space = 159, other = 345 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p24.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (int power ((int m) (int n))) 4 | 5 | // test power function 6 | (int main () 7 | (var int i) 8 | 9 | (for (= i 0) (< i 10) (++ i) 10 | (printf "%d %d %d\n" i (power 2 i) (power -3 i))) 11 | 12 | (return 0)) 13 | 14 | // power: raise base to the n-th power; n >= 0 15 | (int power ((int base) (int n)) 16 | (var int i p) 17 | 18 | (= p 1) 19 | 20 | (for (= i 1) (<= i n) (++ i) 21 | (= p (* p base))) 22 | 23 | (return p)) 24 | -------------------------------------------------------------------------------- /tests/k_and_r/p24.sxc.out: -------------------------------------------------------------------------------- 1 | 0 1 1 2 | 1 2 -3 3 | 2 4 9 4 | 3 8 -27 5 | 4 16 81 6 | 5 32 -243 7 | 6 64 729 8 | 7 128 -2187 9 | 8 256 6561 10 | 9 512 -19683 11 | -------------------------------------------------------------------------------- /tests/k_and_r/p29.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#define MAXLINE 1000) // maximum input line length 3 | 4 | (int _getline ((char ([] line)) (int maxline))) // need the underscore since GLIBC defines getline 5 | (void copy ((char ([] to)) (char ([] from)))) 6 | 7 | (int main () 8 | (var int len) // current line length 9 | (var int max) // maximum length seen so far 10 | (var char ([] line MAXLINE)) // current input line 11 | (var char ([] longest MAXLINE)) // longest line saved here 12 | 13 | (= max 0) 14 | (while (> (= len (_getline line MAXLINE)) 0) 15 | (if (> len max) 16 | (= max len) 17 | (copy longest line))) 18 | 19 | (if (> max 0) // there was a line 20 | (printf "%s" longest)) 21 | 22 | (return 0)) 23 | 24 | // _getline: read a line into s, return length 25 | (int _getline ((char ([] s)) (int lim)) 26 | (var int c i) 27 | 28 | (for (= i 0) (&& (< i (- lim 1)) (!= (= c (getchar)) EOF) (!= c '\n)) (++ i) 29 | (= ([] s i) c)) 30 | (if (== c '\n) 31 | (= ([] s i) c) 32 | (++ i)) 33 | 34 | (= ([] s i) '\0) 35 | 36 | (return i)) 37 | 38 | // copy: copy 'from' int o 'to'; assume it is big enough 39 | (void copy ((char ([] to)) (char ([] from))) 40 | (var int i) 41 | 42 | (= i 0) 43 | 44 | (while (!= (= ([] to i) ([] from i)) '\0) 45 | (++ i))) 46 | -------------------------------------------------------------------------------- /tests/k_and_r/p29.sxc.in: -------------------------------------------------------------------------------- 1 | WELL, PRINCE, Genoa and Lucca are now no more than private estates of 2 | the Bonaparte family. No, I warn you, that if you do not tell me we 3 | are at war, if you again allow yourself to palliate all the infamies 4 | and atrocities of this Antichrist (upon my word, I believe he is), I 5 | don't know you in future, you are no longer my friend, no longer my 6 | faithful slave, as you say. There, how do you do, how do you do? I see 7 | I'm scaring you, sit down and talk to me." -------------------------------------------------------------------------------- /tests/k_and_r/p29.sxc.out: -------------------------------------------------------------------------------- 1 | faithful slave, as you say. There, how do you do, how do you do? I see 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p32.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | (#define MAXLINE 1000) // maximum input line size 4 | 5 | (var int max) // maximum lenth seen so far 6 | (var char ([] line MAXLINE)) // current input line 7 | (var char ([] longest MAXLINE)) // longest line saved here 8 | 9 | (int _getline (void)) 10 | (void copy (void)) 11 | 12 | // print longest input line// specialized version 13 | (int main () 14 | (var int len) 15 | (extern int max) 16 | (extern char ([] longest)) 17 | 18 | (= max 0) 19 | (while (> (= len (_getline)) 0) 20 | (if (> len max) 21 | (= max len) 22 | (copy))) 23 | (if (> max 0) // there was a line 24 | (printf "%s" longest)) 25 | (return 0)) 26 | 27 | // getline: specialized version 28 | (int _getline (void) 29 | (var int c i) 30 | (extern char ([] line)) 31 | 32 | (for (= i 0) (&& (< i (- MAXLINE 1)) (!= (= c (getchar)) EOF) (!= c '\n)) (++ i) 33 | (= ([] line i) c)) 34 | 35 | (if (== c '\n) 36 | (= ([] line i) c) 37 | (++ i)) 38 | (= ([] line i) '\0) 39 | (return i)) 40 | 41 | // copy: specialized version 42 | (void copy (void) 43 | (var int i) 44 | (extern char ([] line) ([] longest)) 45 | 46 | (= i 0) 47 | 48 | (while (!= (= ([] longest i) ([] line i)) '\0) 49 | (++ i))) -------------------------------------------------------------------------------- /tests/k_and_r/p32.sxc.in: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLINE 1000 3 | int max; 4 | char (line[MAXLINE]); 5 | char (longest[MAXLINE]); 6 | int _getline (void); 7 | void copy (void); 8 | int 9 | main () 10 | { 11 | int len; 12 | extern int max; 13 | extern char longest[]; 14 | (max) = (0); 15 | while ((((len) = (_getline ())) > (0))) 16 | { 17 | if (((len) > (max))) 18 | { 19 | (max) = (len); 20 | copy (); 21 | }; 22 | }; 23 | if (((max) > (0))) 24 | { 25 | printf ("%s", longest); 26 | }; 27 | return (0); 28 | 29 | } 30 | 31 | int 32 | _getline (void) 33 | { 34 | int c, i; 35 | extern char line[]; 36 | for ((i) = (0); 37 | ((((i) < (((MAXLINE) - (1))))) && ((((c) = (getchar ())) != (EOF))) 38 | && (((c) != ('\n')))); ++(i)) 39 | { 40 | ((line[i])) = (c); 41 | }; 42 | if (((c) == ('\n'))) 43 | { 44 | ((line[i])) = (c); 45 | ++(i); 46 | }; 47 | ((line[i])) = ('\0'); 48 | return (i); 49 | 50 | } 51 | 52 | void 53 | copy (void) 54 | { 55 | int i; 56 | extern char line[], longest[]; 57 | (i) = (0); 58 | while (((((longest[i])) = ((line[i]))) != ('\0'))) 59 | { 60 | ++(i); 61 | }; 62 | 63 | } 64 | -------------------------------------------------------------------------------- /tests/k_and_r/p32.sxc.out: -------------------------------------------------------------------------------- 1 | ((((i) < (((MAXLINE) - (1))))) && ((((c) = (getchar ())) != (EOF))) 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p59.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | // count digits, white space, others 4 | (int main () 5 | (var int c i nwhite nother ([] ndigit 10)) 6 | 7 | (= nwhite nother 0) 8 | 9 | (for (= i 0) (< i 10) (i ++) 10 | (= ([] ndigit i) 0)) 11 | 12 | (while (!= (= c (getchar)) EOF) 13 | (switch c 14 | (('0 '1 '2 '3 '4 '5 '6 '7 '8 '9) 15 | (([] ndigit (- c '0)) ++) 16 | break) 17 | ((' '\n '\t) 18 | (nwhite ++) 19 | break) 20 | (default 21 | (nother ++) 22 | break))) 23 | (printf "digits =") 24 | (for (= i 0) (< i 10) (i ++) 25 | (printf " %d" ([] ndigit i))) 26 | (printf ", white space = %d, other = %d\n" nwhite nother) 27 | (return 0)) 28 | -------------------------------------------------------------------------------- /tests/k_and_r/p59.sxc.in: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* count digits, white space, others */ 4 | main () 5 | { 6 | int c, i, nwhite, nother; 7 | int ndigit[10]; 8 | 9 | nwhite = nother = 0; 10 | (for (i = 0; i < 10; ++i) 11 | ndigit[i] = 0; 12 | 13 | while ((c = getchar()) != EOF) 14 | if (c == '0' && c = '9') 15 | ++ndigit[c-'0']; 16 | else if(c == ' ' || c = '\n' || c == '\t') 17 | ++nwhite; 18 | else 19 | ++nother; 20 | 21 | printf("digits ="); 22 | for (i = 0; i < 10; ++i) 23 | printf(" %d", ndigit[i]); 24 | printf(", white space = %d, other = %d\n"), 25 | nwhite, nother); 26 | } -------------------------------------------------------------------------------- /tests/k_and_r/p59.sxc.out: -------------------------------------------------------------------------------- 1 | digits = 9 3 0 0 0 0 0 0 0 1, white space = 159, other = 345 2 | -------------------------------------------------------------------------------- /tests/k_and_r/p69.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#define MAXLINE 1000) /* maximum input line length */ 3 | 4 | (int _getline ((char ([] line)) (int max))) 5 | (int strindex ((char ([] source)) (char ([] searchfor)))) 6 | 7 | (var char (= ([] pattern) "ould")) 8 | 9 | (int main () 10 | (var char ([] line MAXLINE)) 11 | (var int (= found 0)) 12 | 13 | (while (> (_getline line MAXLINE) 0) 14 | (if (>= (strindex line pattern) 0) 15 | (printf "%s\n" line) 16 | (found ++))) 17 | 18 | (return found)) 19 | 20 | /* getline: get line into s, return length */ 21 | (int _getline ((char ([] s)) (int lim)) 22 | (var int c i) 23 | 24 | (= i 0) 25 | (while (&& (> (-- lim) 0) 26 | (!= (= c (getchar)) EOF) 27 | (!= c '\n)) 28 | (= ([] s (i ++)) c)) 29 | (= ([] s i) '\0) 30 | (return i)) 31 | 32 | /* strindex: return index of t in s, -1 if none */ 33 | (int strindex ((char ([] s)) (char ([] t))) 34 | (var int i j k) 35 | 36 | (for (= i 0) (!= ([] s i) '\0) (i ++) 37 | (for ((= j i) (= k 0)) (&& (!= ([] t k) '\0) (== ([] s j) ([] t k))) ((j ++) (k ++))) 38 | (if (&& (> k 0) (== ([] t k) '\0)) 39 | (return i))) 40 | (return -1)) 41 | -------------------------------------------------------------------------------- /tests/k_and_r/p69.sxc.in: -------------------------------------------------------------------------------- 1 | would 2 | could 3 | should 4 | won't 5 | because 6 | i 7 | can -------------------------------------------------------------------------------- /tests/k_and_r/p69.sxc.out: -------------------------------------------------------------------------------- 1 | would 2 | could 3 | should 4 | -------------------------------------------------------------------------------- /tests/k_and_r/p76.part1.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#include ) 3 | 4 | (#define MAXOP 100) /* max size of operand or operator */ 5 | (#define NUMBER '0) /* signal that a number was found */ 6 | 7 | (int getop (([] char))) 8 | (void push ((double))) 9 | (double pop (void)) 10 | 11 | (int main () 12 | (var int type) 13 | (var double op2) 14 | (var char ([] s MAXOP)) 15 | 16 | (while (!= (= type (getop s)) EOF) 17 | (fflush stdout) 18 | (switch type 19 | (NUMBER 20 | (printf "xx %s\n" s) 21 | (var float (= number (atof s))) 22 | (push number) 23 | break) 24 | ('+ 25 | (push (+ (pop) (pop))) 26 | break) 27 | ('* 28 | (push (* (pop) (pop))) 29 | break) 30 | ('- 31 | (= op2 (pop)) 32 | (push (- (pop) op2)) 33 | break) 34 | ('/ 35 | (= op2 (pop)) 36 | (if (!= op2 0.0) 37 | (push (/ (pop) op2)) 38 | (else 39 | (printf "error: zero divisor\n"))) 40 | break) 41 | ('\n 42 | (printf "\t%.8g\n" (pop)) 43 | break) 44 | (EOF 45 | (exit 0)) 46 | (default 47 | (printf "error: unknown command %s\n" s) 48 | break))) 49 | (return 0)) 50 | -------------------------------------------------------------------------------- /tests/k_and_r/p76.part2.sxc: -------------------------------------------------------------------------------- 1 | /* push/pop functions */ 2 | (#define MAXVAL 100) 3 | 4 | (var int (= sp 0)) 5 | (var double ([] val MAXVAL)) 6 | 7 | /* push: push f onto value stack */ 8 | (void push ((double f)) 9 | (if (< sp MAXVAL) 10 | (= ([] val (sp ++)) f) 11 | (else 12 | (printf "error: stack full, can't push %g\n" f)))) 13 | 14 | /* pop: pop and return top value from stack */ 15 | (double pop (void) 16 | (if (> sp 0) 17 | (return ([] val (-- sp))) 18 | (else 19 | (printf "error: stack empty\n"))) 20 | (return 0.0)) 21 | 22 | -------------------------------------------------------------------------------- /tests/k_and_r/p76.part3.sxc: -------------------------------------------------------------------------------- 1 | /* getop */ 2 | 3 | (#include ) 4 | 5 | (int getch (void)) 6 | (void ungetch ((int))) 7 | 8 | /* getop: get next operator or numeric operand */ 9 | (int getop ((char ([] s))) 10 | (var int i c) 11 | 12 | (do 13 | (= c (getch)) 14 | (while (|| (== (= ([] s 0) c) ' ) (== c '\t)))) 15 | 16 | (= ([] s 1) '\0) 17 | (if (&& (! (isdigit c)) (!= c '.)) 18 | (return c)) /* not a number */ 19 | 20 | (= i 0) 21 | (if (isdigit c) /* collect integer part */ 22 | (while (isdigit (= ([] s ++i) c (getch))))) 23 | 24 | (if (== c '.) 25 | (while (isdigit (= ([] s ++i) c (getch))))) 26 | 27 | (= ([] s i) '\0) 28 | 29 | (if (!= c EOF) 30 | (ungetch c)) 31 | 32 | (printf "%d\n" i) 33 | (printf "%s\n" s) 34 | 35 | (return NUMBER)) 36 | -------------------------------------------------------------------------------- /tests/k_and_r/p76.part4.sxc: -------------------------------------------------------------------------------- 1 | (#define BUFSIZE 100) 2 | 3 | (var char ([] buf BUFSIZE)) /* buffer got ungetch */ 4 | (var int (= bufp 0)) /* next free position in buf */ 5 | 6 | (int getch (void) /* get a (possibly pushed back) character */ 7 | (return (? (> bufp 0) ([] buf (-- bufp)) (getchar)))) 8 | 9 | (void ungetch ((int c)) /* push character back on input */ 10 | (if (>= bufp BUFSIZE) 11 | (printf "ungetch: too many characters") 12 | (else 13 | (= ([] buf (bufp ++)) c)))) 14 | 15 | -------------------------------------------------------------------------------- /tests/k_and_r/p76.sxc: -------------------------------------------------------------------------------- 1 | /* a reverse polish calculator slighly modified to be in one file */ 2 | (#include "./tests/k_and_r/p76.part1.sxc") 3 | (#include "./tests/k_and_r/p76.part2.sxc") 4 | (#include "./tests/k_and_r/p76.part3.sxc") 5 | (#include "./tests/k_and_r/p76.part4.sxc") 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /tests/k_and_r/p76.sxc.in: -------------------------------------------------------------------------------- 1 | 1 1 + 2 | 2 2 + 3 | 2 2 - 4 | 10 1 / 5 | 10 2 / 6 | 10 10 * 7 | -------------------------------------------------------------------------------- /tests/k_and_r/p76.sxc.out: -------------------------------------------------------------------------------- 1 | 2 2 | 4 3 | 0 4 | 10 5 | 5 6 | 100 -------------------------------------------------------------------------------- /tests/k_and_r/p9.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | 3 | // print Fahrenheit-Celcius trable 4 | // for fahr = 0, 20, ..., 300 5 | (int main ((int argc) (char (** argv))) 6 | (var int fahr celsius) 7 | (var int lower upper step) 8 | 9 | (= lower 0) 10 | (= upper 300) 11 | (= step 20) 12 | 13 | (= fahr lower) 14 | 15 | (while (<= fahr upper) 16 | (= celsius (/ (* 5 (- fahr 32)) 9)) 17 | (printf "%d\t%d\n" fahr celsius) 18 | (= fahr (+ fahr step))) 19 | 20 | (return 0)) -------------------------------------------------------------------------------- /tests/k_and_r/p9.sxc.out: -------------------------------------------------------------------------------- 1 | 0 -17 2 | 20 -6 3 | 40 4 4 | 60 15 5 | 80 26 6 | 100 37 7 | 120 48 8 | 140 60 9 | 160 71 10 | 180 82 11 | 200 93 12 | 220 104 13 | 240 115 14 | 260 126 15 | 280 137 16 | 300 148 17 | -------------------------------------------------------------------------------- /tests/pointers.sxc: -------------------------------------------------------------------------------- 1 | (#include ) 2 | (#include ) 3 | 4 | (int main ((int argc) (char (** argv))) 5 | (var int (= (* x) 0)) 6 | (= x (malloc (sizeof x))) 7 | (= (* x) 42) 8 | (printf "x: %d\n" (* x)) 9 | (return 0)) 10 | -------------------------------------------------------------------------------- /tests/pointers.sxc.out: -------------------------------------------------------------------------------- 1 | x: 42 2 | -------------------------------------------------------------------------------- /to-translate/README.md: -------------------------------------------------------------------------------- 1 | This directory contains C source code that could be translated into 2 | sxc code and put into the example/ or test/ directories. -------------------------------------------------------------------------------- /to-translate/anser/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -g --std=c++0x 2 | 3 | all: tests sha256 4 | 5 | sha256: sha256.cpp anser.h 6 | g++ $(CFLAGS) -D__MAIN__ -g -o sha256 sha256.cpp 7 | 8 | tests: tests.cpp sha256.cpp anser.h 9 | g++ $(CFLAGS) -o tests tests.cpp sha256.cpp 10 | 11 | clean: 12 | rm -rf $? *~ *.dSYM *.o tests sha256 13 | -------------------------------------------------------------------------------- /to-translate/anser/TODO: -------------------------------------------------------------------------------- 1 | -- implement - (negate) on busses (2's compliment) 2 | -- implement - (subtract) on busses 3 | 4 | -- implment / (divide) on busses (hard?) 5 | 6 | -- analyze why 'y = x * x' does not work in reverse -------------------------------------------------------------------------------- /to-translate/anser/sha256: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burtonsamograd/sxc/0ae95779f3deb5cc37b2f7aaa4b7af0e84ee2138/to-translate/anser/sha256 -------------------------------------------------------------------------------- /to-translate/anser/sha256.cpp: -------------------------------------------------------------------------------- 1 | // sha256.cpp 2 | // 3 | // An implementation of SHA256 in anser. 4 | // 5 | // Burton Samograd 6 | // License: AGPL 7 | // 2018 8 | 9 | 10 | #include "anser.h" 11 | 12 | #ifndef __ANSER_SHA256_H__ 13 | #define __ANSER_SHA256_H__ 14 | 15 | static Bus& ror32(Bus& a, int n) { 16 | return a >>= n; 17 | } 18 | 19 | static inline Bus& Ch(Bus& x, Bus& y, Bus& z) 20 | { 21 | return z ^ (x & (y ^ z)); 22 | } 23 | 24 | static inline Bus& Maj(Bus& x, Bus& y, Bus& z) 25 | { 26 | return (x & y) | (z & (x | y)); 27 | } 28 | 29 | #define e0(x) (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22)) 30 | #define e1(x) (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25)) 31 | #define s0(x) (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3)) 32 | #define s1(x) (ror32(x,17) ^ ror32(x,19) ^ (x >> 10)) 33 | 34 | static inline void BLEND_OP(int I, Bus W[]) 35 | { 36 | W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16]; 37 | } 38 | 39 | static Bus* sha256_transform(Bus state[8], const Bus input[16]) 40 | { 41 | Bus a, b, c, d, e, f, g, h, t1, t2; 42 | Bus W[64]; 43 | int i; 44 | 45 | /* load the input */ 46 | for (i = 0; i < 16; i++) 47 | W[i] = input[i]; 48 | 49 | /* now blend */ 50 | for (i = 16; i < 64; i++) 51 | BLEND_OP(i, W); 52 | 53 | /* load the state into our registers */ 54 | a=state[0]; b=state[1]; c=state[2]; d=state[3]; 55 | e=state[4]; f=state[5]; g=state[6]; h=state[7]; 56 | 57 | /* now iterate */ 58 | t1 = h + e1(e) + Ch(e,f,g) + 0x428a2f98 + W[ 0]; 59 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 60 | t1 = g + e1(d) + Ch(d,e,f) + 0x71374491 + W[ 1]; 61 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 62 | t1 = f + e1(c) + Ch(c,d,e) + 0xb5c0fbcf + W[ 2]; 63 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 64 | t1 = e + e1(b) + Ch(b,c,d) + 0xe9b5dba5 + W[ 3]; 65 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 66 | t1 = d + e1(a) + Ch(a,b,c) + 0x3956c25b + W[ 4]; 67 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 68 | t1 = c + e1(h) + Ch(h,a,b) + 0x59f111f1 + W[ 5]; 69 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 70 | t1 = b + e1(g) + Ch(g,h,a) + 0x923f82a4 + W[ 6]; 71 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 72 | t1 = a + e1(f) + Ch(f,g,h) + 0xab1c5ed5 + W[ 7]; 73 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 74 | 75 | t1 = h + e1(e) + Ch(e,f,g) + 0xd807aa98 + W[ 8]; 76 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 77 | t1 = g + e1(d) + Ch(d,e,f) + 0x12835b01 + W[ 9]; 78 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 79 | t1 = f + e1(c) + Ch(c,d,e) + 0x243185be + W[10]; 80 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 81 | t1 = e + e1(b) + Ch(b,c,d) + 0x550c7dc3 + W[11]; 82 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 83 | t1 = d + e1(a) + Ch(a,b,c) + 0x72be5d74 + W[12]; 84 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 85 | t1 = c + e1(h) + Ch(h,a,b) + 0x80deb1fe + W[13]; 86 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 87 | t1 = b + e1(g) + Ch(g,h,a) + 0x9bdc06a7 + W[14]; 88 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 89 | t1 = a + e1(f) + Ch(f,g,h) + 0xc19bf174 + W[15]; 90 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 91 | 92 | t1 = h + e1(e) + Ch(e,f,g) + 0xe49b69c1 + W[16]; 93 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 94 | t1 = g + e1(d) + Ch(d,e,f) + 0xefbe4786 + W[17]; 95 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 96 | t1 = f + e1(c) + Ch(c,d,e) + 0x0fc19dc6 + W[18]; 97 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 98 | t1 = e + e1(b) + Ch(b,c,d) + 0x240ca1cc + W[19]; 99 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 100 | t1 = d + e1(a) + Ch(a,b,c) + 0x2de92c6f + W[20]; 101 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 102 | t1 = c + e1(h) + Ch(h,a,b) + 0x4a7484aa + W[21]; 103 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 104 | t1 = b + e1(g) + Ch(g,h,a) + 0x5cb0a9dc + W[22]; 105 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 106 | t1 = a + e1(f) + Ch(f,g,h) + 0x76f988da + W[23]; 107 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 108 | 109 | t1 = h + e1(e) + Ch(e,f,g) + 0x983e5152 + W[24]; 110 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 111 | t1 = g + e1(d) + Ch(d,e,f) + 0xa831c66d + W[25]; 112 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 113 | t1 = f + e1(c) + Ch(c,d,e) + 0xb00327c8 + W[26]; 114 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 115 | t1 = e + e1(b) + Ch(b,c,d) + 0xbf597fc7 + W[27]; 116 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 117 | t1 = d + e1(a) + Ch(a,b,c) + 0xc6e00bf3 + W[28]; 118 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 119 | t1 = c + e1(h) + Ch(h,a,b) + 0xd5a79147 + W[29]; 120 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 121 | t1 = b + e1(g) + Ch(g,h,a) + 0x06ca6351 + W[30]; 122 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 123 | t1 = a + e1(f) + Ch(f,g,h) + 0x14292967 + W[31]; 124 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 125 | 126 | t1 = h + e1(e) + Ch(e,f,g) + 0x27b70a85 + W[32]; 127 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 128 | t1 = g + e1(d) + Ch(d,e,f) + 0x2e1b2138 + W[33]; 129 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 130 | t1 = f + e1(c) + Ch(c,d,e) + 0x4d2c6dfc + W[34]; 131 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 132 | t1 = e + e1(b) + Ch(b,c,d) + 0x53380d13 + W[35]; 133 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 134 | t1 = d + e1(a) + Ch(a,b,c) + 0x650a7354 + W[36]; 135 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 136 | t1 = c + e1(h) + Ch(h,a,b) + 0x766a0abb + W[37]; 137 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 138 | t1 = b + e1(g) + Ch(g,h,a) + 0x81c2c92e + W[38]; 139 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 140 | t1 = a + e1(f) + Ch(f,g,h) + 0x92722c85 + W[39]; 141 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 142 | 143 | t1 = h + e1(e) + Ch(e,f,g) + 0xa2bfe8a1 + W[40]; 144 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 145 | t1 = g + e1(d) + Ch(d,e,f) + 0xa81a664b + W[41]; 146 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 147 | t1 = f + e1(c) + Ch(c,d,e) + 0xc24b8b70 + W[42]; 148 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 149 | t1 = e + e1(b) + Ch(b,c,d) + 0xc76c51a3 + W[43]; 150 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 151 | t1 = d + e1(a) + Ch(a,b,c) + 0xd192e819 + W[44]; 152 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 153 | t1 = c + e1(h) + Ch(h,a,b) + 0xd6990624 + W[45]; 154 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 155 | t1 = b + e1(g) + Ch(g,h,a) + 0xf40e3585 + W[46]; 156 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 157 | t1 = a + e1(f) + Ch(f,g,h) + 0x106aa070 + W[47]; 158 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 159 | 160 | t1 = h + e1(e) + Ch(e,f,g) + 0x19a4c116 + W[48]; 161 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 162 | t1 = g + e1(d) + Ch(d,e,f) + 0x1e376c08 + W[49]; 163 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 164 | t1 = f + e1(c) + Ch(c,d,e) + 0x2748774c + W[50]; 165 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 166 | t1 = e + e1(b) + Ch(b,c,d) + 0x34b0bcb5 + W[51]; 167 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 168 | t1 = d + e1(a) + Ch(a,b,c) + 0x391c0cb3 + W[52]; 169 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 170 | t1 = c + e1(h) + Ch(h,a,b) + 0x4ed8aa4a + W[53]; 171 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 172 | t1 = b + e1(g) + Ch(g,h,a) + 0x5b9cca4f + W[54]; 173 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 174 | t1 = a + e1(f) + Ch(f,g,h) + 0x682e6ff3 + W[55]; 175 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 176 | 177 | t1 = h + e1(e) + Ch(e,f,g) + 0x748f82ee + W[56]; 178 | t2 = e0(a) + Maj(a,b,c); d=d+t1; h=t1+t2; 179 | t1 = g + e1(d) + Ch(d,e,f) + 0x78a5636f + W[57]; 180 | t2 = e0(h) + Maj(h,a,b); c=c+t1; g=t1+t2; 181 | t1 = f + e1(c) + Ch(c,d,e) + 0x84c87814 + W[58]; 182 | t2 = e0(g) + Maj(g,h,a); b=b+t1; f=t1+t2; 183 | t1 = e + e1(b) + Ch(b,c,d) + 0x8cc70208 + W[59]; 184 | t2 = e0(f) + Maj(f,g,h); a=a+t1; e=t1+t2; 185 | t1 = d + e1(a) + Ch(a,b,c) + 0x90befffa + W[60]; 186 | t2 = e0(e) + Maj(e,f,g); h=h+t1; d=t1+t2; 187 | t1 = c + e1(h) + Ch(h,a,b) + 0xa4506ceb + W[61]; 188 | t2 = e0(d) + Maj(d,e,f); g=g+t1; c=t1+t2; 189 | t1 = b + e1(g) + Ch(g,h,a) + 0xbef9a3f7 + W[62]; 190 | t2 = e0(c) + Maj(c,d,e); f=f+t1; b=t1+t2; 191 | t1 = a + e1(f) + Ch(f,g,h) + 0xc67178f2 + W[63]; 192 | t2 = e0(b) + Maj(b,c,d); e=e+t1; a=t1+t2; 193 | 194 | Bus *retval = new Bus[8]; 195 | retval[0] = state[0] + a; 196 | retval[1] = state[1] + b; 197 | retval[2] = state[2] + c; 198 | retval[3] = state[3] + d; 199 | retval[4] = state[4] + e; 200 | retval[5] = state[5] + f; 201 | retval[6] = state[6] + g; 202 | retval[7] = state[7] + h; 203 | 204 | return retval; 205 | } 206 | 207 | const uint32_t sha256_init_state[8] = { 208 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 209 | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 210 | }; 211 | 212 | #ifdef __MAIN__ 213 | int main(int argc, char** argv) { 214 | Bus input[16]; 215 | Bus initstate[8]; 216 | 217 | for(int i = 0; i < 8; i++) { 218 | initstate[i].set(sha256_init_state[i]); 219 | } 220 | 221 | Bus* hash = sha256_transform(initstate, input); 222 | 223 | // SHA256 the null string 224 | input[0].set(1 << 31); 225 | for(int i = 1; i < 16; i++) { 226 | input[i].set(0); 227 | } 228 | 229 | for(int i = 0; i < 8; i++) { 230 | std::cerr << std::hex << hash[i].get(); 231 | } 232 | std::cerr << std::endl; 233 | } 234 | #else /* __MAIN__ */ 235 | /* void test() { #DFW to remove warning 236 | Bus state[8]; 237 | const Bus input[16]; 238 | sha256_transform(state, input); 239 | } 240 | */ 241 | #endif // __MAIN__ 242 | 243 | #endif /* __ANSER_SHA256_H__ */ 244 | 245 | -------------------------------------------------------------------------------- /to-translate/anser/tests: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burtonsamograd/sxc/0ae95779f3deb5cc37b2f7aaa4b7af0e84ee2138/to-translate/anser/tests -------------------------------------------------------------------------------- /to-translate/anser/tests.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "anser.h" 4 | #include "sha256.cpp" 5 | 6 | void traverse(Wire* w, std::function fop) { 7 | for(unsigned int i = 0; i < w->size(); i++) { 8 | Op* op = w->nth(i); 9 | if(fop) fop(op); 10 | 11 | for(int j = 0; j < op->outputs(); j++) { 12 | std::cout << op->outputs() << std::endl; 13 | Wire* w2 = op->output(j); 14 | traverse(w2, fop); 15 | } 16 | } 17 | } 18 | 19 | void traverse_op(Op* o) { 20 | for(int i = 0; i < o->inputs(); i++) { 21 | std::cout << 'o' << o << " -- " << o->input(i) << std::endl; 22 | 23 | } 24 | for(int i = 0; i < o->outputs(); i++) { 25 | std::cout << 'o' << o << " -- " << o->output(i) << std::endl; 26 | 27 | } 28 | } 29 | 30 | unsigned long 31 | djb2_orig(unsigned char *str) 32 | { 33 | unsigned long hash = 5381; 34 | int c; 35 | 36 | while ((c = *str++)) 37 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 38 | 39 | return hash; 40 | } 41 | 42 | Bus* 43 | djb2_anser(unsigned char *str) 44 | { 45 | Bus* hash = new Bus(); 46 | 47 | hash->set(5381); 48 | 49 | int c; 50 | while ((c = (*str++))) { 51 | *hash = ((*hash << 5) + *hash) + c; /* hash * 33 + c */ 52 | } 53 | 54 | return hash; 55 | } 56 | 57 | Bus* 58 | djb2_anser2(Bus input[16]) 59 | { 60 | Bus* hashn = new Bus, *hash; 61 | hashn->set(5381); 62 | hash = hashn; 63 | for(int i = 0; i < 16; i++) { 64 | *hashn = ((*hash << 5) + *hash) + input[i]; /* hash * 33 + c */ 65 | hash = hashn; 66 | } 67 | 68 | return hashn; 69 | } 70 | 71 | int main(int argc, char** argv) { 72 | 73 | Bus a, b, c; 74 | 75 | c = a + b; 76 | 77 | c.set(3); 78 | a.set(1); 79 | 80 | assert(b.get() == 2); 81 | 82 | unsigned char* string = (unsigned char*)"This is a test."; 83 | unsigned int hash = djb2_orig(string); 84 | Bus* hash2 = djb2_anser(string); 85 | std::cerr << std::hex << hash << std::endl; 86 | std::cerr << std::hex << hash2->get() << std::endl; 87 | assert(hash == hash2->get()); 88 | 89 | 90 | int ntests = 3; 91 | 92 | for(int i=0; i < ntests; i++) { 93 | uint32_t x = random(); 94 | uint32_t y = random(); 95 | uint32_t z; 96 | Bus a, b; 97 | 98 | Bus sum = a + b; 99 | a.set(x); b.set(y); 100 | z = sum.get(); 101 | std::cerr << a.toString() << std::endl << b.toString() << std::endl << sum.toString() << std::endl << x << " " << y << " " << z << " " << (x + y == z) << " " << (z - (x + y)) << std::endl; 102 | assert(z == x + y); 103 | } 104 | 105 | for(int i=0; i < ntests; i++) { 106 | uint32_t x = random(); 107 | uint32_t n = random() % 32; 108 | uint32_t z; 109 | 110 | Bus a, b; 111 | a = b >> n; 112 | b.set(x); 113 | z = a.get(); 114 | std::cerr << a.toString() << std::endl << b.toString() << std::endl << x << " " << n << " " << ((x >> n) == z) << std::endl; 115 | assert(z == (x >> n)); 116 | } 117 | 118 | for(int i=0; i < ntests; i++) { 119 | uint32_t x = random(); 120 | uint32_t y = random(); 121 | uint32_t z; 122 | 123 | Bus a, b; 124 | std::cerr << "------\n"; 125 | Bus exp = a ^ b; 126 | a.set(x); 127 | b.set(y); 128 | std::cerr << a.toString() << " " << b.toString() << " " << exp.toString() << std::endl; 129 | z = exp.get(); 130 | std::cerr << a.toString() << std::endl << b.toString() << std::endl << exp.toString() << std::endl << x << " " << y << " " << z << std::endl; 131 | std::cerr << (x^y) << " " << ((x ^ y) == z) << std::endl; 132 | assert(z == (x ^ y)); 133 | } 134 | 135 | { 136 | // DJB2 Hash 137 | unsigned char* string = (unsigned char*)"0123456789012345"; 138 | 139 | Bus input[16]; 140 | 141 | Bus* output = djb2_anser2(input); 142 | 143 | unsigned int hash = djb2_orig(string); 144 | 145 | for(int i = 0; i < 16; i++) { 146 | input[i].set(string[i]); 147 | } 148 | 149 | std::cerr << "------\n"; 150 | std::cerr << std::hex << hash << std::endl; 151 | std::cerr << std::hex << output->get() << std::endl; 152 | assert(hash == output->get()); 153 | } 154 | 155 | { 156 | // Multiply 157 | Bus a, b, c; 158 | 159 | c = a * b; 160 | 161 | a.set(1); 162 | b.set(2); 163 | assert(c.get() == 2); 164 | 165 | 166 | // Division 167 | Bus d, e, f; 168 | 169 | d = e * f; 170 | 171 | d.set(10); 172 | e.set(5); 173 | assert(f.get() == 2); 174 | } 175 | 176 | { 177 | // Subtraction 178 | Bus a, b, c; 179 | 180 | c = a + b; 181 | 182 | /* 183 | a.set(100); 184 | c.set(80); 185 | 186 | assert(b.get() == -20); 187 | */ 188 | 189 | // Subtraction 190 | Bus d, e, f; 191 | 192 | f = d + e; 193 | 194 | d.set(100); 195 | e.set(-20); 196 | 197 | assert(f.get() == 80); 198 | } 199 | 200 | { 201 | // Farenheight to Celcius 202 | Bus farenheight, celcius; 203 | 204 | Bus constant, a, b; 205 | 206 | b = a * constant; 207 | a.set(5); 208 | b.set(9); 209 | 210 | farenheight = celcius * constant + 32; 211 | 212 | celcius.set(0); 213 | assert(farenheight.get() == 32); 214 | } 215 | 216 | { 217 | // Celcius to Farenheight 218 | Bus farenheight, celcius; 219 | 220 | Bus constant, a, b; 221 | 222 | b = a * constant; 223 | a.set(5); 224 | b.set(9); 225 | 226 | farenheight = celcius * constant + 32; 227 | 228 | farenheight.set(32); 229 | assert(celcius.get() == 0); 230 | } 231 | 232 | { 233 | // And gate 234 | Wire a, b, c; 235 | And x(&a, &b, &c); 236 | 237 | assert(a.size() == 1); 238 | assert(b.size() == 1); 239 | assert(c.size() == 1); 240 | 241 | assert(a.nth(0) == &x); 242 | assert(b.nth(0) == &x); 243 | assert(c.nth(0) == &x); 244 | 245 | assert(x.input(0) == &a); 246 | assert(x.input(1) == &b); 247 | assert(x.output(0) == &c); 248 | } 249 | 250 | { 251 | ops.clear(); 252 | // Traverse and 253 | Wire a, b, c; 254 | And x(&a, &b, &c); 255 | 256 | //assert(x.get() == 2); // doesn't work, needs analysis 257 | } 258 | 259 | { 260 | ops.clear(); 261 | // Half adder 262 | Wire a, b, s, c; 263 | HalfAdder x(&a, &b, &s, &c); 264 | 265 | for(auto op : ops) { 266 | traverse_op(op); 267 | } 268 | 269 | //assert(x.get() == 2); // doesn't work, needs analysis 270 | } 271 | 272 | { 273 | // SHA256 test 274 | Bus input[16]; 275 | Bus initstate[8]; 276 | 277 | for(int i = 0; i < 8; i++) { 278 | initstate[i].set(sha256_init_state[i]); 279 | } 280 | 281 | Bus* hash = sha256_transform(initstate, input); 282 | 283 | // SHA256 the null string 284 | input[0].set(1 << 31); 285 | for(int i = 1; i < 16; i++) { 286 | input[i].set(0); 287 | } 288 | 289 | for(int i = 0; i < 8; i++) { 290 | std::cerr << std::hex << hash[i].get(); 291 | } 292 | std::cerr << std::endl; 293 | } 294 | } 295 | -------------------------------------------------------------------------------- /to-translate/woc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * woc - word occurance 3 | * 4 | * usage: woc [words...] -- [files or directories...] 5 | * 6 | * Count the occurances of words... in files... printing the score to 7 | * standard out. If a word starts with '-' then reduce the count by 1 8 | * for each occurance of the given word. 9 | * 10 | * If a directory is specified after the -- then all the files in the 11 | * directory are scanned. 12 | * 13 | * Author: Burton Samograd 2012 14 | * License: AGPL 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | int count_occurances(char** words, int num_words, char* filename) { 28 | FILE* file = fopen(filename, "r"); 29 | if(!file) { 30 | fprintf(stderr, "fopen: %s: ", filename); perror(""); 31 | exit(1); 32 | } 33 | fseek(file, 0, SEEK_END); 34 | int size = ftell(file); 35 | 36 | char* str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fileno(file), 0); 37 | if(!str) { 38 | perror("mmap: "); 39 | exit(1); 40 | } 41 | 42 | int cur_word, i, match_count = 0; 43 | for(cur_word = 0; cur_word < num_words; cur_word++) { 44 | char* word = words[cur_word]; 45 | int inc; 46 | if(*word == '-') { 47 | word++; 48 | inc = -1; 49 | } else { 50 | inc = 1; 51 | } 52 | int wordlen = strlen(word); 53 | 54 | for(i = 0; i < size; i++) { 55 | if(toupper(str[i]) == *word) { 56 | int j; 57 | for(j = 1; word[j] == toupper(str[i+j]) && j < wordlen; j++); 58 | if(j == wordlen) { 59 | match_count += inc; 60 | } 61 | } 62 | } 63 | } 64 | 65 | munmap(str, size); 66 | fclose(file); 67 | 68 | return match_count; 69 | } 70 | 71 | void woc(char* file, char** words, int num_words) { 72 | int match_count; 73 | /* see if a directory is specified */ 74 | DIR* dir = opendir(file); 75 | if(dir) { 76 | /* we got a directory */ 77 | int filesilen = strlen(file); 78 | if(file[filesilen-1] == '/') { 79 | /* strip off trailing / if present */ 80 | file[filesilen-1] = '\0'; 81 | } 82 | struct dirent* entry; 83 | char path[PATH_MAX]; 84 | while((entry = readdir(dir))) { 85 | /* traverse the files in the directory */ 86 | struct stat buf; 87 | if(!(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))) { 88 | snprintf(path, PATH_MAX, "%s/%s", file, entry->d_name); 89 | stat(path, &buf); 90 | if(S_ISDIR(buf.st_mode)) { 91 | /* Recurse subdirectories */ 92 | woc(path, words, num_words); 93 | } else { 94 | /* do match count */ 95 | match_count = count_occurances(words, num_words, path); 96 | if(match_count > 0) { 97 | fprintf(stdout, "%d\t%s\n", match_count, path); 98 | } 99 | } 100 | } 101 | } 102 | closedir(dir); 103 | } else { 104 | /* we have a file specified, do match count */ 105 | match_count = count_occurances(words, num_words, file); 106 | if(match_count > 0) { 107 | fprintf(stdout, "%d\t%s\n", match_count, file); 108 | } 109 | } 110 | } 111 | 112 | void usage(void) { 113 | fprintf(stderr, "usage: woc [words...] -- [files or directories...]\n"); 114 | fprintf(stderr, "count the number of occurances of words in files or files in directories\n"); 115 | } 116 | 117 | int main(int argc, char **argv) { 118 | int help_requested = 0, i, j; 119 | int words_start = 1, num_words = 0; 120 | int files_start = -1, num_files = 0; 121 | for(i = 1; i < argc; i++) { 122 | if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { 123 | help_requested = 1; 124 | break; 125 | } else if(!strcmp(argv[i], "--")) { 126 | files_start = i+1; 127 | continue; 128 | } 129 | 130 | if(files_start > 0) { 131 | num_files++; 132 | } else { 133 | num_words++; 134 | } 135 | } 136 | 137 | if(argc < 2 || help_requested) { 138 | usage(); 139 | exit(1); 140 | } 141 | 142 | char use_defaults = num_files == 0; 143 | if(num_files == 0) { 144 | num_files++; 145 | } 146 | char* words[num_words]; 147 | char* files[num_files]; 148 | if(use_defaults) { 149 | files[0] = "."; 150 | } 151 | for(i = words_start; i < words_start + num_words; i++) { 152 | /* uppercase word */ 153 | for(j = 0; j < strlen(argv[i]); j++) { 154 | argv[i][j] = toupper(argv[i][j]); 155 | } 156 | words[i-words_start] = argv[i]; 157 | } 158 | if(!use_defaults) { 159 | for(i = files_start; i < files_start + num_files; i++) { 160 | files[i-files_start] = argv[i]; 161 | } 162 | } 163 | for(i = 0; i < num_files; i++) { 164 | woc(files[i], words, num_words); 165 | } 166 | return 0; 167 | } 168 | -------------------------------------------------------------------------------- /to-translate/xp32/Makefile: -------------------------------------------------------------------------------- 1 | TARGET = XP32.exe 2 | TARGETDIR = . 3 | SRCDIR = $(TARGETDIR) 4 | INCDIR = $(TARGETDIR) 5 | 6 | CC = gcc 7 | LD = gcc 8 | 9 | CFLAGS = -c -Wall -g -O4 10 | LDFLAGS = -o$(TARGET) -O4 11 | OBJS = main.o \ 12 | line.o \ 13 | screen.o \ 14 | goo.o \ 15 | font.o \ 16 | fbanim.o \ 17 | list.o \ 18 | window.o \ 19 | random.o \ 20 | one.o \ 21 | two.o \ 22 | three.o \ 23 | vector.o \ 24 | matrix.o \ 25 | camera.o \ 26 | terrain.o \ 27 | smoke.o \ 28 | fire.o 29 | 30 | LIBS = -lm 31 | 32 | $(TARGET): $(DEP) $(OBJS) 33 | $(LD) $(LDFLAGS) $(OBJS) $(ASMOBJS) $(LIBS) 34 | 35 | dep: _depend 36 | _depend: 37 | gcc -MM $(SRCDIR)/*.c > _depend 38 | 39 | clean: 40 | -rm *.o _depend 41 | 42 | -include _depend 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /to-translate/xp32/README: -------------------------------------------------------------------------------- 1 | README for XP32 Graphics Demo 2 | ----------------------------- 3 | 4 | Description: 5 | 6 | XP32 is an MSDOS/VGA based graphics demo. Running time is about 2 7 | minutes. Effects include gooey plasma-like thing, animated matte 8 | transistions, 3D vertex rendering, crazy spinning thing, smoke and 9 | fire. 10 | 11 | Build Requirements: 12 | 13 | Current Version of DJGPP GNU C compiler for MSDOS. 14 | GNU make for MSDOS. 15 | 16 | These packages are available for free download from ftp.gnu.org in 17 | /pub/gnu, and oak.oakland.edu in /pub/djgpp (I think). 18 | 19 | Build Instructions: 20 | 21 | Extract the source files from the archive (.tar.gz or .zip) into 22 | an appropriate directory. 23 | 24 | Type 'make' to build the program. 25 | 26 | Running Instructions: 27 | 28 | To run this program, you require a program called go32.exe. This 29 | is a free DOS extender that is required for the program to run. 30 | It must be located either in the directory of the executable or 31 | the in a directory contained in you system PATH. go32.exe is 32 | included in the compiled archive for your convenience. 33 | 34 | This program was originally written in DOS, and to the best 35 | knowledge of the author, does not run in a Win95 DOS box. I have 36 | not tested it in a Win98 DOS box, not have I tested it with the 37 | newer versions of the go32.exe DOS extender, which may have more 38 | favorable results. If you have any problems, try restarting in 39 | DOS mode and running. 40 | 41 | Questions or Comments?: 42 | 43 | Feel free to contact me if you have any questions or comments 44 | about anything contained in this archive. 45 | 46 | Burt Samograd 47 | burt@conspiracy-software.com 48 | http://www.conspiracy-software.com 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /to-translate/xp32/XP32.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/burtonsamograd/sxc/0ae95779f3deb5cc37b2f7aaa4b7af0e84ee2138/to-translate/xp32/XP32.exe -------------------------------------------------------------------------------- /to-translate/xp32/__.SYMDEF SORTED: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /to-translate/xp32/_depend: -------------------------------------------------------------------------------- 1 | camera.o: ./camera.c ./camera.h ./scalar.h ./matrix.h ./vector.h \ 2 | ./angle.h ./window.h 3 | fire.o: ./fire.c ./window.h ./screen.h ./random.h 4 | font.o: ./font.c ./screen.h ./font.h ./window.h ./fbanim.h 5 | goo.o: ./goo.c ./window.h ./screen.h ./random.h 6 | line.o: ./line.c ./window.h ./line.h 7 | list.o: ./list.c ./list.h 8 | matrix.o: ./matrix.c ./vector.h ./scalar.h ./matrix.h ./angle.h 9 | one.o: ./one.c ./seq.h ./goo.h ./window.h ./font.h ./fbanim.h ./screen.h 10 | random.o: ./random.c 11 | smoke.o: ./smoke.c ./screen.h ./window.h ./random.h 12 | terrain.o: ./terrain.c ./terrain.h ./random.h 13 | three.o: ./three.c ./fbanim.h ./screen.h ./window.h ./smoke.h ./font.h \ 14 | ./fire.h ./goo.h 15 | vector.o: ./vector.c ./vector.h ./scalar.h 16 | window.o: ./window.c ./window.h 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /to-translate/xp32/angle.h: -------------------------------------------------------------------------------- 1 | /* 2 | * angle.h 3 | * 4 | * Angle defintion and trigonometric functions 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | #include 25 | 26 | #ifndef _ANGLE_H 27 | #define _ANGLE_H 28 | 29 | /* Define floats as sclars for now */ 30 | typedef double ANGLE; 31 | 32 | #define DEG_TO_ANGLE(x) (((x)*M_PI)/180.0) 33 | 34 | /* Soon to be lookup tables, but this is fine for now */ 35 | #define SIN(x) sin(x) 36 | #define COS(x) cos(x) 37 | #define TAN(x) tan(x) 38 | 39 | #endif 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /to-translate/xp32/camera.c: -------------------------------------------------------------------------------- 1 | /* 2 | * camera.c 3 | * 4 | * View volume and projection matrix functions. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | /* C Includes */ 25 | #include 26 | #include 27 | 28 | /* Local includes */ 29 | #include "camera.h" 30 | #include "scalar.h" 31 | #include "angle.h" 32 | #include "matrix.h" 33 | #include "vector.h" 34 | #include "window.h" 35 | 36 | #define PROJ_RATIO 2.0f 37 | 38 | CAMERA Camera; 39 | MATRIX CameraMatrix; 40 | 41 | /* Initialize the camera system variables */ 42 | void InitCamera(void) 43 | { 44 | Camera.NearZ = 0.0; 45 | Camera.FarZ = 50.0; 46 | 47 | Camera.Origin.x = 0.0; 48 | Camera.Origin.y = 0.0; 49 | Camera.Origin.z = 0.0; 50 | 51 | Camera.Direction.x = 0.0; 52 | Camera.Direction.y = 0.0; 53 | Camera.Direction.z = 1.0; 54 | 55 | Camera.Up.x = 0.0; 56 | Camera.Up.y = -1.0; 57 | Camera.Up.z = 0.0; 58 | 59 | Camera.FOV = DEG_TO_ANGLE(60); 60 | SetCameraFocalLength(5); 61 | } 62 | 63 | /* Return a pointer to the camera matrix */ 64 | MATRIX *GetCameraMatrix(void) 65 | { 66 | return &CameraMatrix; 67 | } 68 | 69 | /* Set the near Z clipping plane */ 70 | void SetCameraNearZ(SCALAR z) 71 | { 72 | Camera.NearZ = z; 73 | } 74 | 75 | /* Set the far Z clipping plane */ 76 | void SetCameraFarZ(SCALAR z) 77 | { 78 | Camera.FarZ = z; 79 | } 80 | 81 | /* Set the origin (view point) of the camera */ 82 | void SetCameraOrigin(VECTOR *v) 83 | { 84 | Camera.Origin = *v; 85 | } 86 | 87 | /* Set the look at point for the vector */ 88 | /* This is an alternate for setting the direction 89 | vector for the camera */ 90 | void SetCameraViewPoint(VECTOR *v) 91 | { 92 | VECTOR Temp; 93 | 94 | Temp = *v; 95 | 96 | /* dv = look at point - origin */ 97 | AddVector(&Temp, &Camera.Origin); 98 | 99 | /* Set the direction vector */ 100 | NormVector(&Temp); 101 | Camera.Direction = Temp; 102 | } 103 | 104 | /* Set the direction vector for the camera */ 105 | void SetCameraDirection(VECTOR *v) 106 | { 107 | NormVector(v); 108 | Camera.Direction = *v; 109 | } 110 | 111 | /* Set the up vector for the camera */ 112 | void SetCameraUp(VECTOR *v) 113 | { 114 | Camera.Up = *v; 115 | } 116 | 117 | /* Set the focal lenght for the vector */ 118 | void SetCameraFocalLength(SCALAR fl) 119 | { 120 | Camera.FocalLength = fl; 121 | } 122 | 123 | /* Set the field of view angle for the camera */ 124 | void SetCameraFieldOfView(ANGLE fov) 125 | { 126 | Camera.FOV = fov; 127 | } 128 | 129 | /* Set the camera rendering window */ 130 | void SetCameraWindow(WINDOW Win) 131 | { 132 | Camera.Window = Win; 133 | } 134 | 135 | /* Update dependant camera variables */ 136 | void UpdateCameraVariables(void) 137 | { 138 | Camera.AspectRatio = (SCALAR)GetWinWidth(Camera.Window)/(SCALAR)GetWinHeight(Camera.Window); 139 | Camera.WorldWidth = Camera.FocalLength*TAN(Camera.FOV/2)*2; 140 | Camera.WorldHeight = Camera.WorldWidth*Camera.AspectRatio; 141 | Camera.HalfScreenWidth = ((SCALAR)GetWinWidth(Camera.Window))/2; 142 | Camera.HalfScreenHeight = ((SCALAR)GetWinHeight(Camera.Window))/2; 143 | Camera.ProjectionRatio = -Camera.HalfScreenWidth * PROJ_RATIO; 144 | } 145 | 146 | /* Create the camera matrix using the current view 147 | volume settings */ 148 | void CreateCameraMatrix(void) 149 | { 150 | UnitMatrix(&CameraMatrix); 151 | NegVector(&Camera.Origin); 152 | TransMatrix(&CameraMatrix, &Camera.Origin); 153 | NegVector(&Camera.Origin); 154 | } 155 | 156 | /**********************************/ 157 | /* View volume clipping functions */ 158 | /**********************************/ 159 | 160 | /* Clip the given point to the current view volume */ 161 | /* This will return TRUE if the point is in the view 162 | volume, or FALSE if it is not */ 163 | int ClipPoint(VECTOR *o, VECTOR *v) 164 | { 165 | return -1; 166 | } 167 | 168 | /* Clip the given line to the current view volume */ 169 | /* This function will return TRUE if the the whole line 170 | is contained in the view volume. If a portion of the 171 | line is contained in the view volume, the routine will 172 | modify v1 and v2, such that the resulting line is 173 | contained within the view volume, and will return TRUE. 174 | If niether point is contained in the view volume, the 175 | function will return FALSE */ 176 | int ClipLine(VECTOR *v1, VECTOR *v2) 177 | { 178 | return -1; 179 | 180 | } 181 | 182 | /**********************************/ 183 | /* Rendering projection functions */ 184 | /**********************************/ 185 | 186 | /* Project a single point into screen space using the 187 | current view volume */ 188 | void ProjectPoint(VECTOR *o, VECTOR *v) 189 | { 190 | SCALAR Temp = Camera.ProjectionRatio/v->z; 191 | 192 | /* Transform the point */ 193 | /* MatrixMultVec(o, &CameraMatrix, v);*/ 194 | 195 | /* Perspective project the point to the view plane */ 196 | o->x = (v->x*Temp) + Camera.HalfScreenWidth; 197 | o->y = (v->y*Temp) + Camera.HalfScreenHeight; 198 | o->z = v->z; 199 | 200 | } 201 | 202 | /* Project an array of points into screen space using 203 | the current view volume */ 204 | void Project(VECTOR *v, long Num) 205 | { 206 | } 207 | 208 | 209 | 210 | 211 | 212 | -------------------------------------------------------------------------------- /to-translate/xp32/camera.h: -------------------------------------------------------------------------------- 1 | /* 2 | * camera.h 3 | * 4 | * View volume and projection matrix functions. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | #include "scalar.h" 25 | #include "matrix.h" 26 | #include "vector.h" 27 | #include "window.h" 28 | 29 | #ifndef _CAMERA_H 30 | #define _CAMERA_H 31 | 32 | /* Camera structure */ 33 | typedef struct tagCAMERA 34 | { 35 | SCALAR NearZ, FarZ; 36 | VECTOR Origin; 37 | VECTOR Direction; 38 | VECTOR Up; 39 | SCALAR FocalLength; 40 | ANGLE FOV; 41 | WINDOW Window; 42 | SCALAR ProjectionRatio; 43 | SCALAR HalfScreenWidth; 44 | SCALAR HalfScreenHeight; 45 | SCALAR WorldWidth; 46 | SCALAR WorldHeight; 47 | SCALAR AspectRatio; 48 | } CAMERA; 49 | 50 | /* Externs */ 51 | extern CAMERA Camera; 52 | extern MATRIX CameraMatrix; 53 | 54 | /* Initialize the camera system */ 55 | void InitCamera(void); 56 | 57 | /* Projection view volume setting functions */ 58 | void SetCameraNearZ(SCALAR z); 59 | void SetCameraFarZ(SCALAR z); 60 | void SetCameraOrigin(VECTOR *v); 61 | void SetCameraViewPoint(VECTOR *v); 62 | void SetCameraDirection(VECTOR *v); 63 | void SetCameraUp(VECTOR *v); 64 | void SetCameraFocalLength(SCALAR fl); 65 | void SetCameraFieldOfView(ANGLE fov); 66 | void SetCameraWindow(WINDOW Window); 67 | 68 | void UpdateCameraVariables(void); 69 | 70 | /* Create the projection matrix */ 71 | void CreateCameraMatrix(void); 72 | 73 | /* View volume clipping functions */ 74 | int ClipPoint(VECTOR *o, VECTOR *v); 75 | int ClipLine(VECTOR *v1, VECTOR *v2); 76 | 77 | /* Rendering projection functions */ 78 | void ProjectPoint(VECTOR *o, VECTOR *v); 79 | void Project(VECTOR *v, long Num); 80 | 81 | /* Get a pointer to the camera matrix */ 82 | MATRIX *GetCameraMatrix(void); 83 | 84 | #endif 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /to-translate/xp32/fbanim.c: -------------------------------------------------------------------------------- 1 | /* 2 | * fbanim.c 3 | * 4 | * Frame based animation system. This is a basic run until yield 5 | * multitasking system that uses the frame rate as the cycle timer. 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 | * 21 | * 22 | * Burt Samograd 23 | * Copywrite 1997 24 | * 25 | * TODO: 26 | * Need to add support for passing parameters to procs. 27 | */ 28 | 29 | /* C Includes */ 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | /* DJGPP Includes */ 38 | #ifdef DOS 39 | #include 40 | #endif /* DOS */ 41 | 42 | /* Local includes */ 43 | #include "fbanim.h" 44 | #include "list.h" 45 | #include "screen.h" 46 | 47 | /* Proc structure definition */ 48 | typedef struct tagPROC 49 | { 50 | PROCESS Func; /* Pointer to the function to 51 | be called to exec the process */ 52 | unsigned long StartFrame; /* The frame that the process 53 | will begin execution */ 54 | unsigned long EndFrame; /* The last frame that the process 55 | will execute in */ 56 | unsigned long NextFrame; /* The next frame the process will 57 | execute in */ 58 | unsigned long Period; /* The number of frames between 59 | process execution */ 60 | PRIORITY Priority; /* Governs how the process is placed 61 | on the queue */ 62 | unsigned long Arg; /* Argument to be passed to the proc 63 | when called */ 64 | } PROC; 65 | 66 | /* Module global variables */ 67 | static unsigned long FrameCounter; 68 | static HLIST ReadyQueue; 69 | static HLIST ExecutionQueue; 70 | static int RunFlag; 71 | static PROC *CurProc; 72 | 73 | 74 | /* Module function prototypes */ 75 | static int AddProcToQueue(HLIST List, PROC *Proc); 76 | static void RunProc(PROC *Proc); 77 | static int Reschedual(void); 78 | 79 | /* Initialize the animation system */ 80 | int InitFrameBasedAnimSystem(void) 81 | { 82 | /* Allocate the lists for the module */ 83 | ReadyQueue = ListCreate(); 84 | ExecutionQueue = ListCreate(); 85 | if(ReadyQueue == NULL || ExecutionQueue == NULL) return -1; 86 | 87 | /* Initialize some varaibles */ 88 | RunFlag = 0; 89 | 90 | return 0; 91 | } 92 | 93 | /* Run the system based on the contents of the current queue */ 94 | int RunFrameBasedAnimSystem(void) 95 | { 96 | /* Set the run flag */ 97 | RunFlag = 1; 98 | 99 | /* Perform a proc list update */ 100 | if(Reschedual()) return -1; 101 | 102 | /* Do until break */ 103 | while(RunFlag) 104 | { 105 | CurProc = (PROC*)ListFirst(ExecutionQueue); 106 | while(CurProc) 107 | { 108 | /* Run the procedure */ 109 | RunProc(CurProc); 110 | 111 | /* If we have gone too long, end this sequence of procs and 112 | go to the next frame */ 113 | #if 0 114 | if(inp(0x3da /* VGA_STATUS_REGISTER*/ ) & 0x08) assert(0); 115 | #endif 116 | /* Get the next proc to be run */ 117 | CurProc = ListNext(ExecutionQueue); 118 | } 119 | IncFrameCounter(); 120 | /* WaitForVerticalBlank(0);*/ 121 | if(Reschedual()) break; 122 | } 123 | 124 | return 0; 125 | } 126 | 127 | /* Stop the frame based anim system on the next context switch */ 128 | int StopFrameBasedAnimSystem(void) 129 | { 130 | RunFlag = 0; 131 | 132 | return 0; 133 | } 134 | 135 | /* Frame counter routines */ 136 | unsigned long ResetFrameCounter(void) 137 | { 138 | return FrameCounter=0; 139 | } 140 | unsigned long IncFrameCounter(void) 141 | { 142 | return ++FrameCounter; 143 | } 144 | unsigned long GetFrameCounter(void) 145 | { 146 | return FrameCounter; 147 | } 148 | void SetFrameCounter(unsigned long New) 149 | { 150 | FrameCounter = New; 151 | } 152 | 153 | /* Insert a procedure that takes no arguments into the proc queue */ 154 | int InsertProc(unsigned long StartFrame, unsigned long EndFrame, 155 | unsigned long Period, PRIORITY Priority, 156 | void *Func, unsigned long Arg) 157 | { 158 | PROC *NewProc; 159 | 160 | /* FIX ME!! The system will block if a process is added *while running* 161 | and the start frame is less than or equal to the current frame */ 162 | 163 | /* Create the new proc structure */ 164 | NewProc = (PROC*)malloc(sizeof(*NewProc)); 165 | if(NewProc == NULL) return -1; 166 | 167 | NewProc->StartFrame = StartFrame; 168 | NewProc->EndFrame = EndFrame; 169 | NewProc->NextFrame = StartFrame; 170 | NewProc->Period = Period; 171 | NewProc->Priority = Priority; 172 | NewProc->Func = Func; 173 | NewProc->Arg = Arg; 174 | 175 | return AddProcToQueue(ReadyQueue, NewProc); 176 | } 177 | 178 | /* Insert a process into the ready queue when the system is running. 179 | This function is not required, but gives more robust interface. 180 | If the start frame is not correct when inserting a process, the system 181 | can block (which will be fixed later). This function will insert a 182 | procedure 1 quantum after the current frame */ 183 | int DynamicInsertProc(unsigned long EndFrame, 184 | unsigned long Period, PRIORITY Priority, 185 | void *Func, unsigned long Arg) 186 | { 187 | return InsertProc(FrameCounter+1, EndFrame, Period, Priority, Func, Arg); 188 | } 189 | 190 | /* Add the given procedure to the proc queue */ 191 | int AddProcToQueue(HLIST List, PROC *Proc) 192 | { 193 | PROC *TempProc; 194 | 195 | /* Get the first procedure on the proc queue */ 196 | TempProc = ListFirst(List); 197 | 198 | /* Sort by starting frame. Stop sorting as soon as a proc 199 | with the same next frame number is reached */ 200 | while((TempProc != NULL) && (TempProc->NextFrame < Proc->NextFrame)) 201 | { 202 | TempProc = ListNext(List); 203 | } 204 | 205 | /* Sort by priority. If there are any proc's with the same priority 206 | as this one, place them in sequential in the order they were 207 | inserted. Stop as soon as a proc with a lower priority is 208 | found. */ 209 | if((TempProc != NULL) && (TempProc->NextFrame == Proc->NextFrame)) 210 | { 211 | while((TempProc != NULL) && (TempProc->Priority >= Proc->Priority) 212 | && (TempProc->NextFrame == Proc->NextFrame)) 213 | { 214 | TempProc = ListNext(List); 215 | } 216 | } 217 | 218 | /* Add the new procedure to the proc queue */ 219 | /* If TempProc is NULL, this means that we ran off the end of 220 | the list. In this case the proc is added to the end. If the 221 | end of the list was not reached, the proc is inserted before 222 | the last item that it was compared to (the one which caused the 223 | conditional to fail). */ 224 | if(TempProc == NULL) 225 | return ListAppend(List, Proc); 226 | else 227 | return ListInsert(List, Proc); 228 | } 229 | 230 | /* Run the given procedure based on its procedure definition structure */ 231 | static void RunProc(PROC *Proc) 232 | { 233 | Proc->Func(Proc->Arg); 234 | Proc->NextFrame = FrameCounter+Proc->Period; 235 | } 236 | 237 | /* Update the execution list for the next frame */ 238 | static int Reschedual(void) 239 | { 240 | PROC *ExecProc; 241 | PROC *ReadyProc; 242 | 243 | /* Prune the execution list by removing any procedures that 244 | are not to be run on the next frame */ 245 | ExecProc = (PROC*)ListFirst(ExecutionQueue); 246 | while(ExecProc != NULL) 247 | { 248 | 249 | if(ExecProc->NextFrame != FrameCounter || 250 | ExecProc->EndFrame < FrameCounter) 251 | { 252 | ListRemove(ExecutionQueue); 253 | AddProcToQueue(ReadyQueue, ExecProc); 254 | ExecProc = ListCurr(ExecutionQueue); 255 | } 256 | else 257 | { 258 | ExecProc = ListNext(ExecutionQueue); 259 | } 260 | } 261 | 262 | /* Add the procs in the queue to the proc list */ 263 | ReadyProc = (PROC*)ListFirst(ReadyQueue); 264 | while(ReadyProc != NULL) 265 | { 266 | /* If this proc has expired, remove it from the ready queue */ 267 | if(ReadyProc->EndFrame < FrameCounter) 268 | { 269 | PROC *Temp = ListRemove(ReadyQueue); 270 | free(Temp); 271 | } 272 | /* If this proc is to be run, add it to the execution queue */ 273 | else if(ReadyProc->NextFrame == FrameCounter) 274 | { 275 | ListRemove(ReadyQueue); 276 | AddProcToQueue(ExecutionQueue, ReadyProc); 277 | } 278 | /* If this frame has a next frame greater than the current frame, 279 | diregard the rest of the list, since all procs are sorted by 280 | their next frame */ 281 | else if(ReadyProc->NextFrame > FrameCounter) 282 | { 283 | break; 284 | } 285 | ReadyProc = ListCurr(ReadyQueue); 286 | } 287 | 288 | /* If the proc list is empty, then return a non-zero value */ 289 | return ((ListCount(ExecutionQueue) == 0) && 290 | (ListCount(ReadyQueue) == 0)); 291 | } 292 | 293 | /* Stop running the current proceduree */ 294 | void KillCurProc(void) 295 | { 296 | CurProc->EndFrame = FrameCounter; 297 | } 298 | 299 | /* Set the value of the current processes arg value */ 300 | void SetCurProcArg(unsigned long Arg) 301 | { 302 | CurProc->Arg = Arg; 303 | } 304 | 305 | /* Set the period of the current proc */ 306 | void SetPeriod(unsigned long Arg) 307 | { 308 | CurProc->Period = Arg; 309 | 310 | } 311 | 312 | /* Kill all of the procedures in the execution and ready queues */ 313 | void KillAllProcs(void) 314 | { 315 | PROC *Proc; 316 | 317 | Proc = ListFirst(ExecutionQueue); 318 | while(Proc) 319 | { 320 | Proc->EndFrame = FrameCounter; 321 | Proc = ListNext(ExecutionQueue); 322 | } 323 | 324 | Proc = ListFirst(ReadyQueue); 325 | while(Proc) 326 | { 327 | Proc->EndFrame = FrameCounter; 328 | Proc = ListNext(ReadyQueue); 329 | } 330 | Reschedual(); 331 | } 332 | 333 | -------------------------------------------------------------------------------- /to-translate/xp32/fbanim.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fbanim.h 3 | * 4 | * Frame based animation system. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | #ifndef _FBANIM_H 25 | #define _FBANIM_H 26 | 27 | typedef enum {NULLPROC=0, LOW=16, MED=32, HIGH=64} PRIORITY; 28 | typedef void(*PROCESS)(unsigned long Arg); 29 | 30 | /* Initialize the animation system */ 31 | int InitFrameBasedAnimSystem(void); 32 | 33 | /* Run the system based on the current proc queue */ 34 | int RunFrameBasedAnimSystem(void); 35 | 36 | /* Stop the system at any time and return */ 37 | int StopFrameBasedAnimSystem(void); 38 | 39 | /* Frame counter routines. Each returns the current 40 | value of the frame counter */ 41 | unsigned long ResetFrameCounter(void); 42 | unsigned long IncFrameCounter(void); 43 | unsigned long GetFrameCounter(void); 44 | void SetFrameCounter(unsigned long New); 45 | 46 | /* Process insertion functions */ 47 | int InsertProc(unsigned long StartFrame, unsigned long EndFrame, 48 | unsigned long Period, PRIORITY Priority, 49 | void *Func, unsigned long Arg); 50 | 51 | /* Insert a proc while the system is running */ 52 | int DynamicInsertProc(unsigned long EndFrame, 53 | unsigned long Period, PRIORITY Priority, 54 | void *Func, unsigned long Arg); 55 | 56 | /* Process information routines for the currently running process */ 57 | unsigned long GetStartFrame(void); 58 | unsigned long GetEndFrame(void); 59 | unsigned long GetNextFrame(void); 60 | unsigned long GetPeriod(void); 61 | void SetPeriod(unsigned long Arg); 62 | void SetCurProcArg(unsigned long Arg); 63 | 64 | void KillCurProc(void); 65 | void KillAllProcs(void); 66 | 67 | #endif 68 | 69 | 70 | -------------------------------------------------------------------------------- /to-translate/xp32/fire.c: -------------------------------------------------------------------------------- 1 | /* 2 | * fire.c 3 | * 4 | * Fire routine. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | /* C Includes */ 25 | #include 26 | #include 27 | #include 28 | 29 | /* Local includes */ 30 | #include "window.h" 31 | #include "screen.h" 32 | #include "random.h" 33 | 34 | /* Module constant defines */ 35 | #define RANDLT_SIZE 1024 36 | 37 | /* Local function definitions */ 38 | void InitFirePalette(void); 39 | unsigned char* DoTripleLines(unsigned char *Screen, int Offset); 40 | unsigned char* DoDoubleLines(unsigned char *Screen, int Offset); 41 | unsigned char* DoSingleLines(unsigned char *Screen, int Offset); 42 | void DoRandomLines(unsigned char *Screen, int Offset); 43 | void FirePostBlt(WINDOW Win); 44 | void FirePreBlt(WINDOW Win); 45 | 46 | /* Module global variables */ 47 | static WINDOW Window; 48 | static int Pitch, PitchTwo, PitchThree, PitchFour, PitchSix; 49 | static int TripleLineValue, DoubleLineValue, SingleLineValue, RandomLineValue; 50 | static int Offset; 51 | static unsigned char *RandLT; 52 | 53 | /* Initialize the fire system */ 54 | /* This routine expects an offscreen window structure */ 55 | void InitFire(unsigned long Arg) 56 | { 57 | long DrawBufferPitch; 58 | unsigned long Temp, i; 59 | 60 | /* Save the pointer to the window structure */ 61 | Window = (WINDOW)Arg; 62 | 63 | /* Set up the palette */ 64 | InitFirePalette(); 65 | 66 | /* Precalc the pitch multipliers */ 67 | DrawBufferPitch = GetWinPitch(Window); 68 | Pitch = DrawBufferPitch; 69 | PitchTwo = DrawBufferPitch*2; 70 | PitchThree = DrawBufferPitch*3; 71 | PitchFour = DrawBufferPitch*4; 72 | PitchSix = DrawBufferPitch*6; 73 | 74 | /* Create the random look up table */ 75 | RandLT = (unsigned char *)malloc(sizeof(unsigned char)*RANDLT_SIZE); 76 | assert(RandLT); 77 | for(i=0;i191 <255) with groups of four pixels */ 81 | Temp = Random()%64 + /*191;*/ GetWinHeight(Window); 82 | Temp |= Temp<<8; 83 | Temp |= Temp<<16; 84 | 85 | ((long*)RandLT)[i] = Temp; 86 | } 87 | 88 | 89 | TripleLineValue = ((double)GetWinHeight(Window)*57.0/200.0); 90 | DoubleLineValue = ((double)GetWinHeight(Window)*10.0/200.0); 91 | SingleLineValue = ((double)GetWinHeight(Window)*7.0/200.0); 92 | RandomLineValue = GetWinHeight(Window)-TripleLineValue*3 -DoubleLineValue*2-SingleLineValue; 93 | 94 | Offset = GetWinWidth(Window); 95 | 96 | return; 97 | } 98 | 99 | /* Cleanup memory */ 100 | void CleanupFire(unsigned long Arg) 101 | { 102 | /* Free the lookup table memory */ 103 | if(RandLT) 104 | free(RandLT); 105 | RandLT = NULL; 106 | } 107 | 108 | /* Update the fire screen */ 109 | /* This is done through a combination of scan tripling, doubling and 110 | regular evaluation of pixel averaging for the fire effect */ 111 | void UpdateFire(unsigned long Arg) 112 | { 113 | unsigned char *Buffer; 114 | 115 | /* Left and right offset to center the fire 116 | horizontally on the screen */ 117 | if(Offset > 0) 118 | Offset-=16; 119 | 120 | /* Set the screen pointer */ 121 | Buffer = GetWinSurface(Window); 122 | 123 | /* Update the buffer */ 124 | Buffer = DoTripleLines(Buffer, Offset); 125 | Buffer = DoDoubleLines(Buffer, Offset); 126 | Buffer = DoSingleLines(Buffer, Offset); 127 | DoRandomLines(Buffer, Offset); 128 | 129 | #define VERT_SHIFT 15 130 | 131 | /* Copy all but the bottom scan lines to the screen */ 132 | WaitForVerticalBlank(0); 133 | 134 | FirePreBlt(Window); 135 | StencilCopyToScreen(Window); 136 | FirePostBlt(Window); 137 | } 138 | 139 | void FirePreBlt(WINDOW Window) 140 | { 141 | SetWinPosY(Window, GetWinPosY(Window)+20); 142 | SetWinHeight(Window, GetWinHeight(Window)-20); 143 | } 144 | 145 | 146 | void FirePostBlt(WINDOW Window) 147 | { 148 | SetWinPosY(Window, GetWinPosY(Window)-20); 149 | SetWinHeight(Window, GetWinHeight(Window)+20); 150 | } 151 | 152 | /* Probably want to make a palette and have it loaded at 153 | this time or something */ 154 | void InitFirePalette(void) 155 | { 156 | static RGBCOLOR FirePal[256] = 157 | { 158 | {0, 0, 0},{8/4, 0, 0},{16/4, 0, 0},{24/4, 0, 0}, 159 | {32/4, 0, 0}, {41/4, 0, 0},{49/4, 0, 0},{57/4, 0, 0}, 160 | {65/4, 0, 0}, {74/4, 0, 0}, {82/4, 0, 0}, {90/4, 0, 0}, 161 | {98/4, 0, 0},{106/4, 0, 0},{115/4, 0, 0},{123/4, 0, 0}, 162 | {131/4, 0, 0}, {139/4, 0, 0},{148/4, 0, 0},{156/4, 0, 0}, 163 | {164/4, 0, 0},{172/4, 0, 0},{180/4, 0, 0},{189/4, 0, 0}, 164 | {197/4, 0, 0}, {205/4, 0, 0},{213/4, 0, 0},{222/4, 0, 0}, 165 | {230/4, 0, 0},{238/4, 0, 0}, {246/4, 0, 0},{255/4, 0, 0}, 166 | {254/4, 9/4, 0},{254/4, 19/4, 0},{254/4, 29/4, 0},{254/4, 39/4, 0}, 167 | {253/4, 49/4, 0},{253/4, 59/4, 0},{253/4, 69/4, 0},{253/4, 79/4, 0}, 168 | {252/4, 88/4, 0},{252/4, 98/4, 0},{252/4, 108/4, 0}, 169 | {252/4, 118/4, 0},{251/4, 128/4, 0},{251/4, 138/4, 0}, 170 | {251/4, 148/4, 0},{251/4, 158/4, 0},{250/4, 167/4, 0}, 171 | {250/4, 177/4, 0},{250/4, 187/4, 0}, {250/4, 197/4, 0}, 172 | {249/4, 207/4, 0},{249/4, 217/4, 0},{249/4, 227/4, 0}, 173 | {249/4, 237/4, 0},{249/4, 247/4, 0},{249/4, 247/4, 0}, 174 | {249/4, 237/4, 0},{249/4, 247/4, 0},{250/4, 247/4, 0}, 175 | {252/4, 237/4, 0},{254/4, 247/4, 0},{255/4, 247/4, 0}}; 176 | int i; 177 | 178 | for(i=0;i<16;i++) 179 | { 180 | SetCurPalColorWithGamma(i, &FirePal[i*2]); 181 | } 182 | for(i=32;i<64;i++) 183 | { 184 | SetCurPalColorWithGamma(i-16, &FirePal[i]); 185 | } 186 | } 187 | 188 | /* Screen update functions. They are diveded into triple lines (top), 189 | double lines (middle), single lines (bottom) and random (last few 190 | rows). */ 191 | unsigned char* DoTripleLines(unsigned char *Screen, int Offset) 192 | { 193 | int i,j; 194 | 195 | for(i=0;i>2; 205 | 206 | /* Decriment the value so that it will fade */ 207 | if(Screen[j] < 31 && Screen[j] > 0) 208 | Screen[j]--; 209 | } 210 | /* Copy the row */ 211 | memcpy(Screen+Pitch, Screen, GetWinWidth(Window)); 212 | memcpy(Screen+PitchTwo, Screen, GetWinWidth(Window)); 213 | 214 | 215 | /* Incriment by the pitch of the surface 216 | (even though its in memory anyways) */ 217 | Screen += PitchThree; 218 | } 219 | 220 | return Screen; 221 | } 222 | 223 | unsigned char* DoDoubleLines(unsigned char *Screen, int Offset) 224 | { 225 | int i,j; 226 | 227 | for(i=0;i>2; 237 | } 238 | memcpy(Screen+Pitch, Screen, GetWinWidth(Window)); 239 | 240 | Screen += PitchTwo; 241 | } 242 | 243 | return Screen; 244 | } 245 | 246 | unsigned char* DoSingleLines(unsigned char *Screen, int Offset) 247 | { 248 | int i, j; 249 | 250 | /* Single line algo */ 251 | for(i=0;i>1); 261 | } 262 | 263 | /* Incriment by the pitch of the surface 264 | (even though its in memory anyways) */ 265 | Screen += Pitch; 266 | } 267 | 268 | return Screen; 269 | } 270 | 271 | void DoRandomLines(unsigned char *Screen, int Offset) 272 | { 273 | int j; 274 | int Temp; 275 | 276 | /* Calculate the offset into the table */ 277 | Temp = Random()%RANDLT_SIZE; 278 | 279 | /* Fill the two last scan lines */ 280 | for(j=0;j 26 | #include 27 | #include 28 | 29 | /* Local includes */ 30 | #include "window.h" 31 | #include "screen.h" 32 | #include "random.h" 33 | 34 | void InitGoo(WINDOW Win) 35 | { 36 | long i; 37 | float fTemp, fAngle; 38 | unsigned char cTemp; 39 | RGBCOLOR Color; 40 | unsigned char *Surface = GetWinSurface(Win); 41 | int Width = GetWinWidth(Win); 42 | int Height = GetWinHeight(Win); 43 | 44 | /* Cover the surface of the window with random pixels */ 45 | for(i=0;i 26 | #include 27 | #include 28 | 29 | /* Local includes */ 30 | #include "window.h" 31 | #include "line.h" 32 | 33 | static unsigned char Color; 34 | static COOR CurX, CurY; 35 | 36 | /* Initialization */ 37 | void InitLine(void) 38 | { 39 | CurX = CurY = 0; 40 | Color = 0; 41 | } 42 | 43 | 44 | /* Line color set/get */ 45 | void SetLineColor(unsigned char Index) 46 | { 47 | Color = Index; 48 | } 49 | 50 | void GetLineColor(unsigned char *Index) 51 | { 52 | *Index = Color; 53 | } 54 | 55 | /* Draw a line */ 56 | void Line(WINDOW Win, COOR x1, COOR y1, COOR x2, COOR y2) 57 | { 58 | long dx, dy; 59 | unsigned char *Dest; 60 | short i; 61 | long XInc, YInc; 62 | 63 | assert(x1 < GetWinWidth(Win)); 64 | assert(x2 < GetWinWidth(Win)); 65 | assert(y1 < GetWinHeight(Win)); 66 | assert(y2 < GetWinHeight(Win)); 67 | 68 | /* Calculate deltas */ 69 | dx = x2 - x1; 70 | dy = y2 - y1; 71 | 72 | /* Init the incriment values */ 73 | XInc = 1; 74 | YInc = GetWinPitch(Win); 75 | 76 | /* Make sure dx and dy are > 0 and set the inc values 77 | properly */ 78 | if(dx < 0) 79 | { 80 | dx *= -1; 81 | XInc *= -1; 82 | } 83 | if(dy < 0) 84 | { 85 | dy *= -1; 86 | YInc *= -1; 87 | } 88 | 89 | /* Calculate the position in the buffer to start */ 90 | Dest = GetWinSurface(Win) + x1 + (y1*GetWinPitch(Win)); 91 | 92 | /* Special cases */ 93 | if(dy == dx) 94 | /* Diagonal line */ 95 | { 96 | for(i=0; i<=dx; i++) 97 | { 98 | *Dest = Color; 99 | Dest += XInc + YInc; 100 | } 101 | } 102 | else if(dy == 0) 103 | /* Horizontal line */ 104 | { 105 | for(i=0;i<=dx;i++) 106 | { 107 | *Dest = Color; 108 | Dest += XInc; 109 | } 110 | } 111 | else if(dx == 0) 112 | /* Vertical line */ 113 | { 114 | for(i=0;i<=dy;i++) 115 | { 116 | *Dest = Color; 117 | Dest += YInc; 118 | } 119 | } 120 | else 121 | { 122 | /* Non-special case line */ 123 | unsigned short Error; 124 | long Length; 125 | 126 | Error = 0; 127 | if(dx > dy) 128 | { 129 | Length = dx+1; 130 | while(Length--) 131 | { 132 | *Dest = Color; 133 | Dest += XInc; 134 | 135 | Error += dy; 136 | if(Error > dx) 137 | { 138 | Error -= dx; 139 | Dest += YInc; 140 | } 141 | } 142 | } 143 | else 144 | { 145 | Length = dy+1; 146 | while(Length--) 147 | { 148 | *Dest = Color; 149 | Dest += YInc; 150 | 151 | Error += dx; 152 | if(Error > dy) 153 | { 154 | Error -= dy; 155 | Dest += XInc; 156 | } 157 | } 158 | } 159 | } 160 | } 161 | 162 | /* Cursor (logo) based line drawing */ 163 | void MoveTo(COOR x, COOR y) 164 | { 165 | CurX = x; 166 | CurY = y; 167 | } 168 | 169 | /* Draw a line from the current cursor position to a new one */ 170 | void LineTo(WINDOW Win, COOR x, COOR y) 171 | { 172 | Line(Win, CurX, CurY, x, y); 173 | MoveTo(x, y); 174 | } 175 | 176 | 177 | void GetCursor(COOR *x, COOR *y) 178 | { 179 | *x = CurX; 180 | *y = CurY; 181 | } 182 | 183 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /to-translate/xp32/line.h: -------------------------------------------------------------------------------- 1 | /* 2 | * line.h 3 | 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 | * 18 | * Burt Samograd 19 | * Copywrite 1997 20 | */ 21 | 22 | #ifndef _LINE_H 23 | #define _LINE_H 24 | 25 | #include "window.h" 26 | 27 | typedef unsigned long COOR; 28 | 29 | /* Initialization */ 30 | void InitLine(void); 31 | 32 | /* Line colors */ 33 | void SetLineColor(unsigned char Index); 34 | void GetLineColor(unsigned char *Index); 35 | 36 | /* Draw a line */ 37 | void Line(WINDOW Win, COOR x1, COOR y1, COOR x2, COOR y2); 38 | 39 | /* Cursor (logo) based line drawing */ 40 | void GetCursor(COOR *x, COOR *y); 41 | void MoveTo(COOR x, COOR y); 42 | void LineTo(WINDOW Win, COOR x, COOR y); 43 | 44 | #endif 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /to-translate/xp32/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * list.h 3 | * 4 | * Header file for list routines. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1995 22 | */ 23 | 24 | #ifndef _LIST_H 25 | #define _LIST_H 26 | 27 | /* Helpful when dealing with C++ compilers */ 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | /* Some handy typedefs */ 33 | typedef void * PVOID; 34 | 35 | /* Keep data structures hidden */ 36 | typedef struct tagLIST* HLIST; 37 | typedef struct tagELEMENT* HELEMENT; 38 | 39 | /* Function pointer typedefs */ 40 | typedef void(*ElementFreeFunc)(HELEMENT); 41 | typedef long (*ElementCompFunc)(HELEMENT, PVOID); 42 | 43 | /* Size Limits for implimentation */ 44 | #define MAX_LIST_HEADS 16 45 | #define MAX_LIST_ELEMENTS 128 46 | 47 | /* List Function declerations */ 48 | 49 | /* Must be called before any routines are called */ 50 | long ListInit(void); 51 | 52 | HLIST ListCreate(void); 53 | 54 | long ListCount(HLIST hList); 55 | 56 | PVOID ListFirst(HLIST hList); 57 | PVOID ListLast(HLIST hList); 58 | PVOID ListNext(HLIST hList); 59 | PVOID ListPrev(HLIST hList); 60 | PVOID ListCurr(HLIST hList); 61 | 62 | long ListAdd(HLIST hList, PVOID Item); 63 | long ListInsert(HLIST hList, PVOID Item); 64 | long ListAppend(HLIST hList, PVOID Item); 65 | long ListPrepend(HLIST hList, PVOID Item); 66 | 67 | PVOID ListRemove(HLIST hList); 68 | void ListConcat(HLIST hListOne, HLIST hListTwo); 69 | void ListFree(HLIST hList, ElementFreeFunc pfFree); 70 | PVOID ListTrim(HLIST hList); 71 | PVOID ListSearch(HLIST hList, ElementCompFunc pfComp, PVOID CompData); 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif /* _LIST_H */ 78 | 79 | /* 80 | * Burt Samograd 81 | * Student Number : 12216933 82 | * User ID : q4j1 83 | */ 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /to-translate/xp32/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Main routine for demo. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | /* C Includes */ 25 | #include 26 | #include 27 | #include 28 | 29 | #ifdef DOS 30 | #include 31 | #include 32 | #include 33 | #endif /* DOS */ 34 | 35 | /* Local includes */ 36 | #include "screen.h" 37 | #include "line.h" 38 | #include "font.h" 39 | #include "window.h" 40 | #include "fbanim.h" 41 | #include "list.h" 42 | #include "random.h" 43 | #include "seq.h" 44 | 45 | int main(void) 46 | { 47 | /* Initialize the subsystems */ 48 | ListInit(); 49 | InitFrameBasedAnimSystem(); 50 | 51 | SetMode320x200(); 52 | InitWindowSystem(Screen, ScreenWidth, ScreenHeight); 53 | 54 | InitLine(); 55 | InitFont(); 56 | InitRandom(1024, 0); 57 | 58 | /* Clear the main window */ 59 | ClearWindow(GetMainWindow()); 60 | 61 | /* Start the first seqence of the demo */ 62 | RunOne(); 63 | 64 | RunFrameBasedAnimSystem(); 65 | SetModeText(); 66 | 67 | printf("Demo XP32 - 1997\n" 68 | "Programming, Art and Design - Burt Samograd\n" 69 | "Story Boarding - Ryan Slemko\n\n"); 70 | return 0; 71 | } 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /to-translate/xp32/matrix.c: -------------------------------------------------------------------------------- 1 | /* 2 | * matrix.c 3 | * 4 | * Matrix library data structure and routines. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | /* C Includes */ 25 | #include 26 | 27 | /* Local includes */ 28 | #include "vector.h" 29 | #include "matrix.h" 30 | 31 | /* Create a unit matrix */ 32 | void UnitMatrix(MATRIX *m) 33 | { 34 | memset(m, 0, sizeof(MATRIX)); 35 | m->D.a[0] = m->D.a[5] = m->D.a[10] = m->D.a[15] = 1.0; 36 | return; 37 | } 38 | 39 | /* Scale the values of a matrix by s */ 40 | void ScaleMatrix(MATRIX *m, SCALAR s) 41 | { 42 | ScaleVector(m->D.v+0, s); 43 | ScaleVector(m->D.v+1, s); 44 | ScaleVector(m->D.v+2, s); 45 | ScaleVector(m->D.v+3, s); 46 | return; 47 | } 48 | 49 | /* Multiply two matrices, result = a*b */ 50 | void MultMatrix(MATRIX *result, MATRIX *a, MATRIX *b) 51 | { 52 | #if 0 53 | int i, temp1, temp2; 54 | 55 | 56 | for(i=0;i<4;i++) 57 | { 58 | temp1 = i*4; 59 | result->D.a[i] = a->D.a[temp1] * b->D.a[i]; 60 | 61 | temp2 = i+4; 62 | result->D.a[temp2] = a->D.a[temp1+1] * b->D.a[temp2]; 63 | 64 | temp2 = i+8; 65 | result->D.a[temp2] = a->D.a[temp1+2] * b->D.a[temp2]; 66 | 67 | temp2 = i+12; 68 | result->D.a[temp2] = a->D.a[temp1+3] * b->D.a[temp2]; 69 | } 70 | #endif 71 | int i; 72 | 73 | for(i=0;i<4;i++) 74 | { 75 | result->D.v[i].x = 76 | a->D.v[i].x * b->D.v[0].x + 77 | a->D.v[i].y * b->D.v[1].x + 78 | a->D.v[i].z * b->D.v[2].x + 79 | a->D.v[i].w * b->D.v[3].x; 80 | 81 | result->D.v[i].y = 82 | a->D.v[i].x * b->D.v[0].y + 83 | a->D.v[i].y * b->D.v[1].y + 84 | a->D.v[i].z * b->D.v[2].y + 85 | a->D.v[i].w * b->D.v[3].y; 86 | 87 | result->D.v[i].z = 88 | a->D.v[i].x * b->D.v[0].z + 89 | a->D.v[i].y * b->D.v[1].z + 90 | a->D.v[i].z * b->D.v[2].z + 91 | a->D.v[i].w * b->D.v[3].z; 92 | 93 | result->D.v[i].w = 94 | a->D.v[i].x * b->D.v[0].w + 95 | a->D.v[i].y * b->D.v[1].w + 96 | a->D.v[i].z * b->D.v[2].w + 97 | a->D.v[i].w * b->D.v[3].w; 98 | } 99 | 100 | 101 | } 102 | 103 | void InvMatrix(MATRIX *m) 104 | { 105 | return; 106 | } 107 | 108 | /* Multiply a vector by a matrix */ 109 | void MatrixMultVec(VECTOR *result, MATRIX *m, VECTOR *v) 110 | { 111 | result->x = 112 | m->D.v[0].x * v->x + 113 | m->D.v[0].y * v->y + 114 | m->D.v[0].z * v->z + 115 | m->D.v[0].w * v->w; 116 | 117 | result->y = 118 | m->D.v[1].x * v->x + 119 | m->D.v[1].y * v->y + 120 | m->D.v[1].z * v->z + 121 | m->D.v[1].w * v->w; 122 | 123 | result->z = 124 | m->D.v[2].x * v->x + 125 | m->D.v[2].y * v->y + 126 | m->D.v[2].z * v->z + 127 | m->D.v[2].w * v->w; 128 | } 129 | 130 | /* Build a translation matrix from the given vector */ 131 | void TransMatrix(MATRIX *m, VECTOR *v) 132 | { 133 | m->D.v[0].w = v->x; 134 | m->D.v[1].w = v->y; 135 | m->D.v[2].w = v->z; 136 | } 137 | 138 | /* Build a Xrotation matrix */ 139 | void XRotMatrix(MATRIX *m, ANGLE a) 140 | { 141 | SCALAR CosA, SinA; 142 | 143 | CosA = COS(a); 144 | SinA = SIN(a); 145 | 146 | m->D.v[1].y = CosA; 147 | m->D.v[1].z = -SinA; 148 | m->D.v[2].y = SinA; 149 | m->D.v[2].z = CosA; 150 | } 151 | 152 | /* Build a Y rotation matrix */ 153 | void YRotMatrix(MATRIX *m, ANGLE a) 154 | { 155 | SCALAR CosA, SinA; 156 | 157 | CosA = COS(a); 158 | SinA = SIN(a); 159 | 160 | m->D.v[0].x = CosA; 161 | m->D.v[0].z = SinA; 162 | m->D.v[2].x = -SinA; 163 | m->D.v[2].z = CosA; 164 | } 165 | 166 | /* Build a Z rotation matrix */ 167 | void ZRotMatrix(MATRIX *m, ANGLE a) 168 | { 169 | SCALAR CosA, SinA; 170 | 171 | CosA = COS(a); 172 | SinA = SIN(a); 173 | 174 | m->D.v[0].x = CosA; 175 | m->D.v[0].y = -SinA; 176 | m->D.v[1].x = SinA; 177 | m->D.v[1].y = CosA; 178 | } 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /to-translate/xp32/matrix.h: -------------------------------------------------------------------------------- 1 | /* 2 | * matrix.h 3 | * 4 | * Matrix library data structure and routines. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | #include "scalar.h" 25 | #include "vector.h" 26 | #include "angle.h" 27 | 28 | #ifndef _MATRIX_H 29 | #define _MATRIX_H 30 | 31 | /* 4x4 matrix structure */ 32 | typedef struct tagMATRIX 33 | { 34 | union 35 | { 36 | SCALAR a[16]; 37 | VECTOR v[4]; /* Row vectors form matrix */ 38 | }D; 39 | } MATRIX; 40 | 41 | /* Matrix operations */ 42 | void UnitMatrix(MATRIX *m); 43 | void ScaleMatrix(MATRIX *m, SCALAR s); 44 | void MultMatrix(MATRIX *result, MATRIX *a, MATRIX *b); 45 | void InvMatrix(MATRIX *m); 46 | void MatrixMultVec(VECTOR *result, MATRIX *m, VECTOR *v); 47 | 48 | /* Matrix building functions */ 49 | void TransMatrix(MATRIX *m, VECTOR *v); 50 | void XRotMatrix(MATRIX *m, ANGLE a); 51 | void YRotMatrix(MATRIX *m, ANGLE a); 52 | void ZRotMatrix(MATRIX *m, ANGLE a); 53 | 54 | #define MATRIXMULTVEC(result, mat, vec) \ 55 | result.x = \ 56 | mat.D.v[0].x * vec.x + \ 57 | mat.D.v[0].y * vec.y +\ 58 | mat.D.v[0].z * vec.z +\ 59 | mat.D.v[0].w * vec.w;\ 60 | \ 61 | result.y = \ 62 | mat.D.v[1].x * vec.x +\ 63 | mat.D.v[1].y * vec.y +\ 64 | mat.D.v[1].z * vec.z +\ 65 | mat.D.v[1].w * vec.w;\ 66 | \ 67 | result.z = \ 68 | mat.D.v[2].x * vec.x +\ 69 | mat.D.v[2].y * vec.y +\ 70 | mat.D.v[2].z * vec.z +\ 71 | mat.D.v[2].w * vec.w;\ 72 | 73 | 74 | #endif 75 | 76 | 77 | -------------------------------------------------------------------------------- /to-translate/xp32/one.c: -------------------------------------------------------------------------------- 1 | /* 2 | * one.c 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation; either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 | * 18 | * Burt Samograd 19 | * Copyright 1997 20 | */ 21 | 22 | /* C Includes */ 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | /* Local includes */ 29 | #include "seq.h" 30 | #include "goo.h" 31 | #include "font.h" 32 | #include "window.h" 33 | #include "fbanim.h" 34 | #include "screen.h" 35 | 36 | /* Module global variables */ 37 | static int SequenceFinished; 38 | static WINDOW OffWin; 39 | 40 | /* Local functions */ 41 | static void InitOne(WINDOW Win); 42 | static void CreateStencil(WINDOW Win); 43 | static void UpdateOne(unsigned long Arg); 44 | static void UpdateStencil(WINDOW Win); 45 | static void RunNextSequence(void); 46 | 47 | /* Call to run this sequence */ 48 | void RunOne(void) 49 | { 50 | WINDOW MainWindow = GetMainWindow(); 51 | 52 | /* Create an offscreen window */ 53 | OffWin = NewWindow(GetWinWidth(MainWindow), GetWinHeight(MainWindow), 54 | 0, 0, NULL, NULL, WINDOW_OFFSCREEN | WINDOW_STENCIL); 55 | 56 | /* Initialize the sequence */ 57 | InitOne(OffWin); 58 | 59 | /* Start the sequence processes */ 60 | DynamicInsertProc(-1, 1, MED, UpdateOne, (unsigned long)OffWin); 61 | InsertProc(GetFrameCounter()+70, -1, 1, HIGH, FadePalette, 0); 62 | InsertProc(GetFrameCounter()+140, -1, 2, HIGH, 63 | UpdateStencil, (unsigned long)OffWin); 64 | } 65 | 66 | /* Initialize the first sequence */ 67 | void InitOne(WINDOW Win) 68 | { 69 | InitGoo(Win); 70 | CreateStencil(Win); 71 | } 72 | 73 | /* Create the stencil for the window */ 74 | void CreateStencil(WINDOW Win) 75 | { 76 | unsigned char *Stencil; 77 | unsigned char *Source; 78 | int i, j; 79 | char StringOne[] = 80 | "D E M O"; 81 | char StringTwo[] = 82 | "X P 3 2"; 83 | WINDOW TempWin; 84 | 85 | /* Create a temp window for printing fonts into */ 86 | TempWin = NewWindow(GetWinWidth(Win)/2, GetWinHeight(Win)/2, 0, 0, 87 | NULL, NULL, WINDOW_OFFSCREEN); 88 | SetWinFontColor(TempWin, 255); 89 | 90 | /* Print the string into the window */ 91 | SetWinCursorX(TempWin, GetWinWidth(TempWin)/2 - 7*GetFontWidth()); 92 | SetWinCursorY(TempWin, GetWinHeight(TempWin)/2 - GetFontHeight() - GetFontHeight()/2); 93 | PrintString(TempWin, StringOne); 94 | SetWinCursorX(TempWin, GetWinWidth(TempWin)/2 - 7*GetFontWidth()); 95 | SetWinCursorY(TempWin, GetWinHeight(TempWin)/2 + GetFontHeight()/2); 96 | PrintString(TempWin, StringTwo); 97 | 98 | /* Stretch copy the temp window surface into the Stencil */ 99 | Stencil = GetWinStencil(Win); 100 | Source = GetWinSurface(TempWin); 101 | for(i=0;i 127) 132 | GetWinStencil(Win)[i] = 255; 133 | else 134 | GetWinStencil(Win)[i] = 0; 135 | } 136 | 137 | FreeWindow(&TempWin); 138 | } 139 | 140 | /* Render the screen shot for the first sequence */ 141 | void UpdateOne(unsigned long Arg) 142 | { 143 | WINDOW Win = (WINDOW)Arg; 144 | 145 | /* Update the goo screen */ 146 | UpdateGoo(Win); 147 | 148 | /* Copy the offscreen window to screen */ 149 | WaitForVerticalBlank(0); 150 | StencilCopyToScreen(Win); 151 | 152 | /* If the sequence is finished, go to the next */ 153 | if(SequenceFinished && FadePaletteFinished()) 154 | RunNextSequence(); 155 | } 156 | 157 | /* Update the main window's stencil */ 158 | void UpdateStencil(WINDOW Win) 159 | { 160 | static int Line = 0; 161 | static int Width = 0; 162 | unsigned char* Stencil; 163 | 164 | #define FRAME_DELTA 10 165 | 166 | Stencil = GetWinStencil(Win)+(GetWinWidth(Win)*GetWinHeight(Win)/2) + FRAME_DELTA; 167 | 168 | memset(Stencil - GetWinWidth(Win)*Line, 255, Width); 169 | memset(Stencil + GetWinWidth(Win)*Line, 255, Width); 170 | Line += 1; 171 | 172 | if(Width < GetWinWidth(Win) - FRAME_DELTA*2) 173 | { 174 | Width += 4; 175 | Line = 0; 176 | } 177 | else if(!(Line < (GetWinHeight(Win)/2)-FRAME_DELTA)) 178 | { 179 | KillCurProc(); 180 | FadePaletteFinished(); 181 | InsertProc(GetFrameCounter()+70, -1, 1, HIGH, FadeToBlack, 0); 182 | SequenceFinished = 1; 183 | } 184 | } 185 | 186 | /* Start the next sequnce and clean up the stuff for this sequence */ 187 | static void RunNextSequence(void) 188 | { 189 | /* Stop the update process */ 190 | KillAllProcs(); 191 | 192 | /* De-allocate the offscreen window */ 193 | FreeWindow(&OffWin); 194 | 195 | /* Start the next sequence */ 196 | RunTwo(); 197 | } 198 | 199 | 200 | -------------------------------------------------------------------------------- /to-translate/xp32/random.c: -------------------------------------------------------------------------------- 1 | /* 2 | * random.c 3 | * 4 | * Fast random numbers. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | /* C includes */ 25 | #include 26 | 27 | /* Module global varaibles */ 28 | static int *Table; 29 | static int Cur; 30 | static int Size; 31 | 32 | /* Initialize the random number table */ 33 | void InitRandom(int Num, int Seed) 34 | { 35 | int i; 36 | 37 | /* Allocate the random number table */ 38 | Table = (int*)malloc(sizeof(int)*Num); 39 | Size = Num; 40 | 41 | /* See the random number generator */ 42 | srand(Seed); 43 | 44 | /* Fill in the table with random numbers */ 45 | for(i=0;i= Size) 75 | Cur = Arg%Size; 76 | else 77 | Cur = Arg; 78 | 79 | return Cur; 80 | } 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /to-translate/xp32/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | * random.h 3 | * 4 | * Fast random numbers. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | #ifndef _RANDOM_H 25 | #define _RANDOM_H 26 | 27 | /* Module interface functions */ 28 | void InitRandom(int Num, int Seed); 29 | void CleanupRandom(void); 30 | int Random(void); 31 | int SeedRandom(unsigned long Arg); 32 | 33 | #endif 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /to-translate/xp32/scalar.h: -------------------------------------------------------------------------------- 1 | /* 2 | * scalar.h 3 | * 4 | * Scalar type defintion header. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | #ifndef _SCALAR_H 25 | #define _SCALAR_H 26 | 27 | /* Define floats as sclars for now */ 28 | typedef double SCALAR; 29 | 30 | #endif 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /to-translate/xp32/screen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * screen.c 3 | * 4 | * ChangeLog: 5 | * 6 | * - removed DOS dependent code behind DOS #ifdef 7 | * - preparing for new graphics terminal driver. 8 | * 9 | * Screen (vga) utility functions 10 | * 11 | * This program is free software; you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation; either version 2 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 | * 25 | * Copyright Burt Samograd 26 | * Sept 1997 27 | */ 28 | 29 | /* C Includes */ 30 | #include 31 | #include 32 | #include 33 | 34 | /* DJGPP Includes */ 35 | #ifdef DOS 36 | #include 37 | #include 38 | #include 39 | #endif /* DOS */ 40 | 41 | /* Local includes */ 42 | #include "screen.h" 43 | #include "fbanim.h" 44 | 45 | /* Screen global variables */ 46 | unsigned char *Screen; 47 | long ScreenWidth; 48 | long ScreenHeight; 49 | long ScreenPitch; 50 | long BufferSize; 51 | RGBCOLOR PhysicalPalette[256], CurPalette[256]; 52 | 53 | /* Module global variables */ 54 | static unsigned char *ScreenPointer; 55 | static unsigned char GammaTable[] = { 56 | 0, 10, 14, 17, 19, 21, 23, 24, 26, 27, 28, 29, 31, 32, 33, 34, 57 | 35, 36, 37, 37, 38, 39, 40, 41, 41, 42, 43, 44, 44, 45, 46, 46, 58 | 47, 48, 48, 49, 49, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 59 | 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63}; 60 | 61 | /* VGA Register constants */ 62 | #define VGA_STATUS_REGISTER 0x3da 63 | #define VGA_PAL_READ 0x3c7 64 | #define VGA_PAL_WRITE 0x3c8 65 | #define VGA_PAL_DATA 0x3c9 66 | 67 | void SetMode320x200(void) 68 | { 69 | int i; 70 | RGBCOLOR Color; 71 | 72 | ScreenWidth = 320; 73 | ScreenHeight = 200; 74 | ScreenPitch = 320; 75 | 76 | #if DOS 77 | __dpmi_regs regs; 78 | 79 | /* Set the video mode to 13h through BIOS call */ 80 | memset(®s, 0, sizeof(regs)); 81 | regs.x.ax = 0x13; 82 | __dpmi_int(0x10, ®s); 83 | 84 | /* Set up the screen pointer */ 85 | __djgpp_nearptr_enable(); 86 | 87 | /* Initialize some variables */ 88 | Screen = (unsigned char *)0xa0000 + __djgpp_conventional_base; 89 | #else /* DOS */ 90 | Screen = (unsigned char*)calloc(ScreenWidth, ScreenHeight); 91 | #endif /* DOS */ 92 | ScreenPointer = Screen; 93 | 94 | Color.r = 0; 95 | Color.g = 0; 96 | Color.b = 0; 97 | for(i=0;i<256;i++) 98 | { 99 | SetPhysicalPalColor(i, CurPalette); 100 | } 101 | BufferSize = ScreenWidth*ScreenHeight; 102 | } 103 | 104 | void SetModeText(void) 105 | { 106 | #if DOS 107 | __dpmi_regs regs; 108 | 109 | /* Set the video mode to 13h through BIOS call */ 110 | memset(®s, 0, sizeof(regs)); 111 | regs.x.ax = 0x03; 112 | __dpmi_int(0x10, ®s); 113 | 114 | /* Diable the screen pointer */ 115 | __djgpp_nearptr_disable(); 116 | #else /* DOS */ 117 | fprintf(stderr, "(set-mode 'text)\n"); 118 | #endif 119 | } 120 | 121 | /* Wait for the end of the vertical retrace interval */ 122 | void WaitForVerticalBlank(unsigned long Arg) 123 | { 124 | #if DOS 125 | unsigned char Reg; 126 | 127 | /* Fall through only during the retrace interval */ 128 | /* See if we are in the display */ 129 | do 130 | { 131 | Reg = inp(VGA_STATUS_REGISTER); 132 | } 133 | while(!(Reg & 0x08)); 134 | 135 | /* See if we are in the retrace */ 136 | do 137 | { 138 | Reg = inp(VGA_STATUS_REGISTER); 139 | } 140 | while(Reg & 0x08); 141 | #else /* DOS */ 142 | fprintf(stderr, "(wait-for-vertical-blank)\n"); 143 | #endif /* DOS */ 144 | } 145 | 146 | void SetCurPalColorWithGamma(unsigned char Index, RGBCOLOR *Color) 147 | { 148 | /* save the gamma corrected palette color in the local palette */ 149 | CurPalette[Index].r = GammaTable[Color->r]; 150 | CurPalette[Index].g = GammaTable[Color->g]; 151 | CurPalette[Index].b = GammaTable[Color->b]; 152 | #if DOS 153 | #else /* DOS */ 154 | fprintf(stderr, "(set-current-pallette-color-with-gamma %d %d %d %d)\n", 155 | Index, 156 | GammaTable[Color->r], 157 | GammaTable[Color->g], 158 | GammaTable[Color->b]); 159 | #endif /* DOS */ 160 | 161 | } 162 | 163 | /* Set a raw palette color value without using gamma correction */ 164 | void SetCurPalColor(unsigned char Index, RGBCOLOR *Color) 165 | { 166 | /* Save the palette color in the local palette */ 167 | CurPalette[Index] = *Color; 168 | #if DOS 169 | #else /* DOS */ 170 | fprintf(stderr, "(set-current-pallette-color %d %d %d %d)\n", 171 | Index, Color->r, Color->g, Color->b); 172 | #endif /* DOS */ 173 | } 174 | 175 | void SetPhysicalPalColor(unsigned char Index, RGBCOLOR *Color) 176 | { 177 | #ifdef DOS 178 | /* Output color index to write port */ 179 | outp(VGA_PAL_WRITE, Index); 180 | outp(VGA_PAL_DATA, Color->r); 181 | outp(VGA_PAL_DATA, Color->g); 182 | outp(VGA_PAL_DATA, Color->b); 183 | 184 | /* Save the cur palette in the physical palette */ 185 | #else /* DOS */ 186 | fprintf(stderr, "(set-physical-palette-color %d %d %d %d)\n", 187 | Index, Color->r, Color->g, Color->b); 188 | #endif /* DOS */ 189 | PhysicalPalette[Index] = *Color; 190 | } 191 | 192 | /* Get the color from the current palette, which may be different from 193 | the physical palette if the palette hasn't been updated */ 194 | void GetCurPalColor(RGBCOLOR *Color, unsigned char Index) 195 | { 196 | *Color = CurPalette[Index]; 197 | } 198 | 199 | /* Get the actual color from the hardware palette */ 200 | void GetPhysicalPalColor(RGBCOLOR *Color, unsigned char Index) 201 | { 202 | #ifdef DOS 203 | /* Output color index to read port */ 204 | outp(VGA_PAL_READ, Index); 205 | 206 | /* Read RGB components into struct */ 207 | Color->r = inp(VGA_PAL_DATA); 208 | Color->g = inp(VGA_PAL_DATA); 209 | Color->b = inp(VGA_PAL_DATA); 210 | #else /* DOS */ 211 | fprintf(stderr, "(get-physical-palette-color %d)\n", Index); 212 | // FIXME: how to read output of graphics terminal easily when in a pipe 213 | // fscanf(1, "(%d %d %d)", &Color->r, &Color->g, &Color->b); 214 | #endif 215 | 216 | PhysicalPalette[Index] = *Color; 217 | } 218 | 219 | /* Update the physical palette */ 220 | void UpdatePhysicalPalette(void) 221 | { 222 | int i; 223 | 224 | /* WaitForVerticalBlank(0); */ 225 | for(i=0;i<256;i++) 226 | { 227 | SetPhysicalPalColor(i, CurPalette+i); 228 | } 229 | } 230 | 231 | static int FadeFinished; 232 | void FadePalette(unsigned long Arg) 233 | { 234 | int NumFinished = 0; 235 | int i; 236 | int Flag; 237 | 238 | FadeFinished = 0; 239 | 240 | for(i=0;i<256;i++) 241 | { 242 | Flag = 0; 243 | if(PhysicalPalette[i].r < CurPalette[i].r) 244 | { 245 | Flag = 1; 246 | PhysicalPalette[i].r++; 247 | } 248 | else if (PhysicalPalette[i].r > CurPalette[i].r) 249 | { 250 | Flag = 1; 251 | PhysicalPalette[i].r--; 252 | } 253 | else 254 | { 255 | NumFinished++; 256 | } 257 | 258 | if(PhysicalPalette[i].g < CurPalette[i].g) 259 | { 260 | Flag = 1; 261 | PhysicalPalette[i].g++; 262 | } 263 | else if (PhysicalPalette[i].g > CurPalette[i].g) 264 | { 265 | Flag = 1; 266 | PhysicalPalette[i].g--; 267 | } 268 | else 269 | { 270 | NumFinished++; 271 | } 272 | 273 | if(PhysicalPalette[i].b < CurPalette[i].b) 274 | { 275 | Flag = 1; 276 | PhysicalPalette[i].b++; 277 | } 278 | else if (PhysicalPalette[i].b > CurPalette[i].b) 279 | { 280 | Flag = 1; 281 | PhysicalPalette[i].b--; 282 | } 283 | else 284 | { 285 | NumFinished++; 286 | } 287 | 288 | if(Flag) 289 | SetPhysicalPalColor(i, PhysicalPalette+i); 290 | } 291 | 292 | if(NumFinished == 256*3) 293 | { 294 | FadeFinished = 1; 295 | KillCurProc(); 296 | } 297 | } 298 | 299 | /* Return non-zero if the current palette fade is finished. 300 | Reset the fade flag */ 301 | int FadePaletteFinished(void) 302 | { 303 | int Temp; 304 | 305 | Temp = FadeFinished; 306 | FadeFinished = 0; 307 | return Temp; 308 | } 309 | 310 | /* Fade the current palette to black */ 311 | void FadeToBlack(unsigned long Arg) 312 | { 313 | int NumFinished = 0; 314 | int i; 315 | int Flag; 316 | 317 | FadeFinished = 0; 318 | 319 | for(i=0;i<256;i++) 320 | { 321 | Flag = 0; 322 | 323 | if(PhysicalPalette[i].r != 0) 324 | { 325 | PhysicalPalette[i].r--; 326 | CurPalette[i].r = PhysicalPalette[i].r; 327 | Flag = 1; 328 | } 329 | else 330 | NumFinished++; 331 | 332 | if(PhysicalPalette[i].g != 0) 333 | { 334 | PhysicalPalette[i].g--; 335 | CurPalette[i].g = PhysicalPalette[i].g; 336 | Flag = 1; 337 | } 338 | else 339 | NumFinished++; 340 | 341 | if(PhysicalPalette[i].b != 0) 342 | { 343 | PhysicalPalette[i].b--; 344 | CurPalette[i].b = PhysicalPalette[i].b; 345 | Flag = 1; 346 | } 347 | else 348 | NumFinished++; 349 | 350 | if(Flag) 351 | SetPhysicalPalColor(i, PhysicalPalette+i); 352 | } 353 | 354 | if(NumFinished == 256*3) 355 | { 356 | FadeFinished = 1; 357 | KillCurProc(); 358 | } 359 | } 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | -------------------------------------------------------------------------------- /to-translate/xp32/screen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * screen.h 3 | * 4 | * Screen (vga) utility functions 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Copyright Burt Samograd 21 | * Sept 1997 22 | */ 23 | 24 | #ifndef _SCREEN_H 25 | #define _SCREEN_H 26 | 27 | /* Color structure */ 28 | typedef struct tagRGBCOLOR 29 | { 30 | unsigned char r,g,b; 31 | } RGBCOLOR; 32 | 33 | /* Screen global variables */ 34 | extern unsigned char *Screen; 35 | extern unsigned char *Offscreen; 36 | extern long ScreenWidth; 37 | extern long ScreenHeight; 38 | extern long ScreenPitch; 39 | extern long BufferSize; 40 | 41 | /* Utility functions */ 42 | void SetModeText(void); 43 | void SetMode320x200(void); 44 | 45 | void WaitForVerticalBlank(unsigned long Arg); 46 | 47 | /* Palette setting getting functions */ 48 | void SetCurPalColor(unsigned char Index, RGBCOLOR *Color); 49 | void SetCurPalColorWithGamma(unsigned char Index, RGBCOLOR *Color); 50 | void SetPhysicalPalColor(unsigned char Index, RGBCOLOR *Color); 51 | void GetCurPalColor(RGBCOLOR *Color, unsigned char Index); 52 | void GetPhysicalPalColor(RGBCOLOR *Color, unsigned char Index); 53 | 54 | /* The above functions only set the internal palette. The following functions 55 | can be used to set the physical (screen) palette */ 56 | void UpdatePhysicalPalette(void); 57 | void FadePalette(unsigned long NumFrames); 58 | void FadeToBlack(unsigned long NumFrames); 59 | int FadePaletteFinished(void); 60 | 61 | #endif /* _SCREEN_H */ 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /to-translate/xp32/seq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * seq.h 3 | * 4 | * Sequence processes sequence of demo. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | #ifndef _SEQ_H 25 | #define _SEQ_H 26 | 27 | void RunOne(void); 28 | void RunTwo(void); 29 | void RunThree(void); 30 | 31 | #endif 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /to-translate/xp32/smoke.c: -------------------------------------------------------------------------------- 1 | /* 2 | * smoke.c 3 | * 4 | * Simulation of smoke in a turbulent airflow 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | /* C includes */ 25 | #include 26 | #include 27 | 28 | /* Local includes */ 29 | #include "screen.h" 30 | #include "window.h" 31 | #include "random.h" 32 | 33 | /* Module globals */ 34 | static int SmokePosX; 35 | static int SmokePosY; 36 | 37 | void InitSmoke(unsigned long Arg) 38 | { 39 | WINDOW Win = (WINDOW)Arg; 40 | RGBCOLOR Color; 41 | int i; 42 | 43 | /* Set the palette colors */ 44 | for(i=0;i<128;i++) 45 | { 46 | Color.r = i; 47 | Color.g = i; 48 | Color.b = i; 49 | 50 | SetCurPalColor(i++, &Color); 51 | SetCurPalColor(i, &Color); 52 | } 53 | memset(&Color, 0, sizeof(Color)); 54 | 55 | Color.r = 63; 56 | Color.g = 63; 57 | Color.b = 0; 58 | for(i=0;i<63;i++) 59 | { 60 | SetCurPalColor(i+64, &Color); 61 | Color.g--; 62 | } 63 | 64 | SetWinBackgroundColor(Win, 0); 65 | ClearWindow(Win); 66 | 67 | SmokePosX = GetWinWidth(Win)/2; 68 | SmokePosY = GetWinHeight(Win)/2; 69 | } 70 | 71 | void UpdateSmoke(unsigned long Arg) 72 | { 73 | WINDOW Win = (WINDOW)Arg; 74 | int i, j; 75 | unsigned char *SourceBuffer, *DestBuffer; 76 | char Val; 77 | 78 | SourceBuffer = GetWinSurface(Win) + GetWinPitch(Win); 79 | DestBuffer = GetWinSurface(Win); 80 | 81 | for(i=GetWinHeight(Win);i>2;i--) 82 | { 83 | Val = Random(); 84 | Val /= 96; 85 | 86 | SourceBuffer += GetWinPitch(Win); 87 | DestBuffer += GetWinPitch(Win); 88 | 89 | for(j=Val;j>2; 110 | *(GetWinSurface(Win) + Offset-1) = Random()>>2; 111 | *(GetWinSurface(Win) + Offset + 1) = Random()>>2; 112 | *(GetWinSurface(Win) + Offset+GetWinPitch(Win)) = Random()>>2; 113 | } 114 | 115 | /* Update the smoke source */ 116 | /* Return -1 when the match hits the bottom of the screen. */ 117 | int MoveSmoke(WINDOW Win, int dx, int dy) 118 | { 119 | SmokePosX += dx; 120 | SmokePosY += dy; 121 | 122 | if(SmokePosX > GetWinWidth(Win)-2) 123 | SmokePosX = GetWinWidth(Win)-2; 124 | 125 | if(SmokePosX < 0) 126 | SmokePosX = 0; 127 | 128 | if(SmokePosY > GetWinHeight(Win)-2) 129 | { 130 | SmokePosY = GetWinHeight(Win)-2; 131 | return -1; 132 | } 133 | 134 | if(SmokePosY < 2) 135 | SmokePosY = 0; 136 | 137 | return 0; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /to-translate/xp32/smoke.h: -------------------------------------------------------------------------------- 1 | /* 2 | * smoke.h 3 | * 4 | * Smoke routine header file. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | #include "window.h" 25 | 26 | #ifndef _SMOKE_H 27 | #define _SMOKE_H 28 | 29 | /* Module interface functions */ 30 | int InitSmoke(unsigned long Arg); 31 | void UpdateSmoke(unsigned long Arg); 32 | int MoveSmoke(WINDOW Win, int dx, int dy); 33 | void SeedSmoke(unsigned long Arg); 34 | 35 | #endif 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /to-translate/xp32/terrain.c: -------------------------------------------------------------------------------- 1 | /* 2 | * terrain.c 3 | * 4 | * Terrain generation engine using simulated fault 5 | * generation. 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 | * 21 | * Burt Samograd 22 | * Copywrite 1997 23 | */ 24 | 25 | /* C includes */ 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | /* Local includes */ 32 | #include "terrain.h" 33 | #include "random.h" 34 | 35 | typedef struct 36 | { 37 | int x, y; 38 | } POINT; 39 | 40 | /* Local function definitions */ 41 | #if 0 42 | void ExtendLineToRect(int Width, int Height, POINT* p1, POINT* p2); 43 | void FaultData(TERRAINDATA *TData, POINT *p1, POINT *p2); 44 | #endif 45 | 46 | /* Allocate new terrain data map */ 47 | TERRAINDATA *NewTerrainData(int Width, int Height, int Scale) 48 | { 49 | TERRAINDATA *TData; 50 | 51 | /* Allocate the new terrain DATA structure */ 52 | TData = (TERRAINDATA*)malloc(sizeof(*TData)); 53 | 54 | /* Set the width and height of the terrain data */ 55 | TData->Width = Width; 56 | TData->Height = Height; 57 | TData->Scale = Scale; 58 | 59 | /* Calculate the buffer size */ 60 | TData->Size = TData->Width*TData->Height; 61 | 62 | /* Allocate the data buffer */ 63 | TData->Data = (unsigned char *)malloc(TData->Size); 64 | assert(TData->Data); 65 | 66 | return TData; 67 | } 68 | 69 | int FreeTerrainData(TERRAINDATA **TData) 70 | { 71 | /* Free the terrain data buffer */ 72 | if((*TData)->Data == NULL) 73 | free((*TData)->Data); 74 | 75 | /* Free the terrain parameter block */ 76 | free(*TData); 77 | 78 | /* Null the pointer */ 79 | *TData = NULL; 80 | 81 | return 0; 82 | } 83 | 84 | /* Build a complete set of terrain data based on the parameters given */ 85 | int BuildTerrain(TERRAINDATA *TData) 86 | { 87 | /* Perform all of the terrain building iterations */ 88 | while(BuildTerrainIteration(TData) == 0); 89 | 90 | return 0; 91 | } 92 | 93 | 94 | /* Build a section of the terrain by generating fractal information */ 95 | int BuildTerrainIteration(TERRAINDATA *TData) 96 | { 97 | int i, j; 98 | unsigned char *Temp; 99 | 100 | memset(TData->Data, 0, TData->Size); 101 | Temp = TData->Data+(1+TData->Width); 102 | for(i=1;iHeight-1;i++) 103 | { 104 | for(j=1;jWidth-1;j++) 105 | { 106 | *Temp = random()%128; 107 | Temp++; 108 | } 109 | Temp += 2; 110 | } 111 | 112 | /* Move the buffer pointer to the second scan line of the 113 | window's surface */ 114 | Temp = TData->Data+(1+TData->Width*1); 115 | 116 | /* Filter the window buffer */ 117 | for(i=1;iHeight-1;i++) 118 | { 119 | for(j=1;jWidth-1;j++) 120 | { 121 | *Temp = (*(Temp-1)+ 122 | *(Temp-TData->Width)+ 123 | *(Temp+1)+ 124 | *(Temp+TData->Width) + 125 | *(Temp-2)+ 126 | *(Temp-(TData->Width*2))+ 127 | *(Temp+2)+ 128 | *(Temp+(TData->Width*2)))/8/* + (char)Random()%16;*/; 129 | Temp++; 130 | } 131 | Temp += 2; 132 | } 133 | 134 | /* Move the buffer pointer to the second scan line of the 135 | window's surface */ 136 | Temp = TData->Data+(1+TData->Width*1); 137 | 138 | /* Filter the window buffer */ 139 | for(i=1;iHeight-1;i++) 140 | { 141 | for(j=1;jWidth-1;j++) 142 | { 143 | *Temp = (*(Temp-1)+ 144 | *(Temp-TData->Width)+ 145 | *(Temp+1)+ 146 | *(Temp+TData->Width))/4; 147 | Temp++; 148 | } 149 | Temp += 2; 150 | } 151 | 152 | return -1; 153 | } 154 | 155 | #if 0 156 | /* Perform a single terrain building iteration based on the current 157 | parameters, and then update the parameters for the next iteration. 158 | If this was the last iteration, return non-zero. */ 159 | int BuildTerrainIteration(TERRAINDATA *TData) 160 | { 161 | POINT p1, p2; 162 | 163 | /* Calculate the random points withing the square */ 164 | p1.x = Random()%TData->Width; 165 | p1.y = Random()%TData->Height; 166 | p2.x = Random()%TData->Width; 167 | p2.y = Random()%TData->Height; 168 | 169 | /* Extend the point to the edge of the bouding rect */ 170 | ExtendLineToRect(TData->Width, TData->Height, &p1, &p2); 171 | 172 | /* Update the data using the given fault line */ 173 | FaultData(TData, &p1, &p2); 174 | 175 | return 0; 176 | } 177 | 178 | /* Extend a line defined by points p1 and p2 that are contained 179 | within a rectangle of width and height, to the edges of the 180 | rectangle */ 181 | void ExtendLineToRect(int Width, int Height, POINT* p1, POINT* p2) 182 | { 183 | 184 | } 185 | 186 | /* Using the two points that lie on the outer edge of the terain 187 | data rectangle, generate a fault in the data */ 188 | void FaultData(TERRAINDATA *TData, POINT *p1, POINT *p2) 189 | { 190 | 191 | } 192 | #endif 193 | 194 | 195 | -------------------------------------------------------------------------------- /to-translate/xp32/terrain.h: -------------------------------------------------------------------------------- 1 | /* 2 | * terrain.h 3 | * 4 | * Terrain generation engine using simulated fault generation. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | #ifndef _TERRAIN_H 25 | #define _TERRAIN_H 26 | 27 | /* Terrain data structure */ 28 | typedef struct tagTERRAINDATA 29 | { 30 | int Width, Height; 31 | int Size; 32 | unsigned char *Data; 33 | int Seed; 34 | int Scale; 35 | } TERRAINDATA; 36 | 37 | /* Module function definitions */ 38 | TERRAINDATA *NewTerrainData(int Width, int Height, int Scale); 39 | int FreeTerrainData(TERRAINDATA **TData); 40 | int BuildTerrain(TERRAINDATA *TData); 41 | int BuildTerrainIteration(TERRAINDATA *TData); 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /to-translate/xp32/three.c: -------------------------------------------------------------------------------- 1 | /* 2 | * three.c 3 | * 4 | * Third sequence of the demo. Smoke and fire. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | /* C includes */ 25 | #include "stdlib.h" 26 | #include "string.h" 27 | 28 | /* Local includes */ 29 | #include "fbanim.h" 30 | #include "screen.h" 31 | #include "window.h" 32 | #include "smoke.h" 33 | #include "font.h" 34 | #include "fire.h" 35 | #include "goo.h" 36 | 37 | /* Module global variables */ 38 | static WINDOW OffWin; 39 | static unsigned char* BufferStencil; 40 | 41 | /* Local function declerations */ 42 | static void InitThree(void); 43 | static void CleanupThree(void); 44 | static void UpdateThree(unsigned long Arg); 45 | static void StartFire(unsigned long Arg); 46 | static void FinishSequence(unsigned long Arg); 47 | static void MoveMatch(unsigned long Arg); 48 | static void CreateStencil(WINDOW Win); 49 | /* static void UpdateStencil(unsigned long Arg); Defined but not used */ 50 | static void AfterFire(unsigned long Arg); 51 | static void WipeOnFinish(unsigned long Arg); 52 | 53 | /* Run the third seqence */ 54 | void RunThree(void) 55 | { 56 | WINDOW MainWindow = GetMainWindow(); 57 | 58 | OffWin = NewWindow(GetWinWidth(MainWindow), GetWinHeight(MainWindow), 0, 0, 59 | NULL, NULL, WINDOW_OFFSCREEN | WINDOW_STENCIL); 60 | InitThree(); 61 | 62 | UpdatePhysicalPalette(); 63 | memset(GetWinStencil(OffWin), 0xff, GetWinWidth(OffWin)*GetWinHeight(OffWin)); 64 | 65 | InsertProc(GetFrameCounter()+70*5, -1, 2, HIGH, MoveMatch, 0); 66 | InsertProc(GetFrameCounter()+70*5, -1, 2, HIGH, FadeToBlack, 0); 67 | 68 | DynamicInsertProc(-1, 1, LOW, UpdateThree, 0); 69 | } 70 | 71 | /* Iniitailze the third sequence */ 72 | void InitThree(void) 73 | { 74 | InitSmoke((unsigned long)OffWin); 75 | CreateStencil(OffWin); 76 | } 77 | 78 | void CleanupThree(void) 79 | { 80 | free(BufferStencil); 81 | } 82 | 83 | void UpdateThree(unsigned long Arg) 84 | { 85 | UpdateSmoke((unsigned long)OffWin); 86 | SeedSmoke((unsigned long)OffWin); 87 | WaitForVerticalBlank(0); 88 | StencilCopyToScreen(OffWin); 89 | } 90 | 91 | void StartFire(unsigned long Arg) 92 | { 93 | KillAllProcs(); 94 | ClearWindow(OffWin); 95 | InitFire((unsigned long)OffWin); 96 | 97 | DynamicInsertProc(-1, 1, HIGH, FadePalette, 0); 98 | DynamicInsertProc(-1, 1, HIGH, UpdateFire, (unsigned long)OffWin); 99 | InsertProc(GetFrameCounter()+5*70, -1, 1, HIGH, AfterFire, 0); 100 | 101 | KillCurProc(); 102 | } 103 | 104 | void MoveMatch(unsigned long Arg) 105 | { 106 | static float dy; 107 | 108 | dy += 0.1; 109 | 110 | if(MoveSmoke(OffWin, 0, (int)(dy+0.5))) 111 | DynamicInsertProc(-1, 1, HIGH, StartFire, 0); 112 | } 113 | 114 | /* Defined but not used. 115 | static void UpdateStencil(unsigned long Arg) 116 | { 117 | } 118 | */ 119 | 120 | /* Create the stencil for the window */ 121 | void CreateStencil(WINDOW Win) 122 | { 123 | unsigned char *Stencil; 124 | unsigned char *Source; 125 | int i, j; 126 | WINDOW TempWin; 127 | 128 | /* Create a temp window for printing fonts into */ 129 | TempWin = NewWindow(GetWinWidth(Win)/8, GetWinHeight(Win)/8, 0, 0, 130 | NULL, NULL, WINDOW_OFFSCREEN); 131 | SetWinFontColor(TempWin, 255); 132 | ClearWindow(TempWin); 133 | 134 | /* Allocate the buffer stencil thatp the window will use later */ 135 | BufferStencil = (unsigned char *)malloc(GetWinWidth(Win)*GetWinHeight(Win)); 136 | 137 | /* Print the string into the window */ 138 | SetWinCursorX(TempWin, 5); 139 | SetWinCursorY(TempWin, GetFontHeight()-2); 140 | PrintChar(TempWin, 'f'); 141 | 142 | SetWinCursorX(TempWin, GetWinWidth(TempWin)/2 - GetFontWidth()/2); 143 | PrintChar(TempWin, 'i'); 144 | 145 | SetWinCursorX(TempWin, GetWinWidth(TempWin) - GetFontWidth() - 4); 146 | PrintChar(TempWin, 'n'); 147 | 148 | /* Stretch copy the temp window surface into the Stencil */ 149 | Stencil = BufferStencil; 150 | Source = GetWinSurface(TempWin); 151 | for(i=0;i 98) 203 | BufferStencil[i] = 255; 204 | else 205 | BufferStencil[i] = 0; 206 | } 207 | FreeWindow(&TempWin); 208 | } 209 | 210 | static void AfterFire(unsigned long Arg) 211 | { 212 | InsertProc(GetFrameCounter()+3*35, -1, 1, HIGH, WipeOnFinish, 0); 213 | KillCurProc(); 214 | } 215 | 216 | static void WipeOnFinish(unsigned long Arg) 217 | { 218 | int i; 219 | static int Count; 220 | int Offset; 221 | 222 | Offset = Count; 223 | 224 | for(i=0;i 26 | #include 27 | 28 | /* Local includes */ 29 | #include "vector.h" 30 | 31 | 32 | void ZeroVector(VECTOR *v) 33 | { 34 | memset(v, 0, sizeof(*v)); 35 | v->w = 1.0; 36 | } 37 | 38 | void SetVector(VECTOR *v, SCALAR x, SCALAR y, SCALAR z) 39 | { 40 | v->x = x; 41 | v->y = y; 42 | v->z = z; 43 | v->w = 1.0; 44 | } 45 | 46 | void ScaleVector(VECTOR *v, SCALAR s) 47 | { 48 | v->x *= s; 49 | v->y *= s; 50 | v->z *= s; 51 | } 52 | 53 | /* Add vector b to vector a, leaving b unchanged */ 54 | void AddVector(VECTOR *a, VECTOR *b) 55 | { 56 | a->x += b->x; 57 | a->y += b->y; 58 | a->z += b->z; 59 | } 60 | 61 | SCALAR AbsVector(VECTOR *v) 62 | { 63 | double Temp; 64 | 65 | Temp = v->x*v->x; 66 | Temp += v->y*v->y; 67 | Temp += v->z*v->z; 68 | 69 | return sqrt(Temp); 70 | } 71 | 72 | /* Perform a normal a.b dor product */ 73 | SCALAR DotVector(VECTOR *a, VECTOR *b) 74 | { 75 | double Temp; 76 | 77 | Temp = a->x*b->x; 78 | Temp += a->y*b->y; 79 | Temp += a->z*b->z; 80 | Temp += a->w*b->w; 81 | 82 | return Temp; 83 | } 84 | 85 | /* Perform a normal axb cross product */ 86 | void CrossVector(VECTOR *result, VECTOR *a, VECTOR *b) 87 | { 88 | result->x = a->y*b->z - a->z*b->y; 89 | result->y = a->z*b->x - a->x*b->z; 90 | result->z = a->x*b->y - a->y*b->x; 91 | } 92 | 93 | /* Normalize a vector */ 94 | void NormVector(VECTOR *v) 95 | { 96 | SCALAR Temp; 97 | 98 | Temp = AbsVector(v); 99 | Temp = 1/Temp; 100 | v->x *= Temp; 101 | v->y *= Temp; 102 | v->z *= Temp; 103 | } 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /to-translate/xp32/vector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * vector.h 3 | * 4 | * Vector library routines. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copyright 1997 22 | */ 23 | 24 | #include "scalar.h" 25 | 26 | #ifndef _VECTOR_H 27 | #define _VECTOR_H 28 | 29 | /* Vector type */ 30 | typedef struct tagVECTOR 31 | { 32 | SCALAR x, y, z, w; 33 | } VECTOR; 34 | 35 | /* Vector operatons */ 36 | void ZeroVector(VECTOR *v); 37 | void SetVector(VECTOR *v, SCALAR x, SCALAR y, SCALAR z); 38 | void ScaleVector(VECTOR *v, SCALAR s); 39 | void AddVector(VECTOR *a, VECTOR *b); 40 | SCALAR AbsVector(VECTOR *v); 41 | SCALAR DotVector(VECTOR *a, VECTOR *b); 42 | void CrossVector(VECTOR *result, VECTOR *a, VECTOR *b); 43 | void NormVector(VECTOR *v); 44 | 45 | #define NegVector(v) ScaleVector(v, -1.0) 46 | 47 | #endif 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /to-translate/xp32/window.h: -------------------------------------------------------------------------------- 1 | /* 2 | * window.h 3 | * 4 | * Window routines for overlapping windows. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 | * 20 | * Burt Samograd 21 | * Copywrite 1997 22 | */ 23 | 24 | #ifndef _WINDOW_H 25 | #define _WINDOW_H 26 | 27 | /* Module typedef */ 28 | typedef struct tagWINDOW 29 | { 30 | int PosX, PosY; 31 | int Width, Height, Pitch; 32 | unsigned char* Surface; 33 | unsigned char* Stencil; 34 | struct tagWINDOW *Parent, *Child, *Next; 35 | void *Data; 36 | long Flags; 37 | 38 | /* Font cursor information */ 39 | int CursorX, CursorY; 40 | int StartCol, StartRow; 41 | long FontFlags; 42 | 43 | unsigned char BackgroundColor; 44 | unsigned char FontColor; 45 | unsigned char FrameColor; 46 | } *WINDOW; 47 | 48 | /* Window creation flags */ 49 | #define WINDOW_OFFSCREEN 1<<0 50 | #define WINDOW_ONSCREEN 1<<1 51 | #define WINDOW_STENCIL 1<<2 52 | #define WINDOW_ON_PARENT 1<<3 53 | 54 | /* Initialize the windowing system to use a rendering buffer */ 55 | int InitWindowSystem(unsigned char *Buffer, int Width, int Height); 56 | 57 | /* Create a window structure */ 58 | WINDOW NewWindow(int Width, int Height, int PosX, int PosY, 59 | WINDOW Parent, void *Data, int Flags); 60 | 61 | /* Free a window and all of its children */ 62 | void FreeWindow(WINDOW *Window); 63 | 64 | /* Utility functions */ 65 | void ClearWindow(WINDOW Win); 66 | void MoveWindow(WINDOW Win, int x, int y); 67 | int SizeWindow(WINDOW Win, int w, int h); 68 | 69 | void ScrollWindowUp(WINDOW Win, int Lines); 70 | void CopyToScreen(WINDOW Win); 71 | void StencilCopyToScreen(WINDOW Win); 72 | 73 | /* Get a pointer to the main window structure */ 74 | WINDOW GetMainWindow(void); 75 | 76 | /* Window structure set/get functions */ 77 | int GetWinPosX(WINDOW Win); 78 | int GetWinPosY(WINDOW Win); 79 | int GetWinWidth(WINDOW Win); 80 | int GetWinHeight(WINDOW Win); 81 | int GetWinPitch(WINDOW Win); 82 | unsigned char* GetWinSurface(WINDOW Win); 83 | unsigned char* GetWinStencil(WINDOW Win); 84 | WINDOW GetWinParent(WINDOW Win); 85 | WINDOW GetWinChild(WINDOW Win); 86 | WINDOW GetWinNext(WINDOW Win); 87 | void *GetWinData(WINDOW Win); 88 | long GetWinFlags(WINDOW Win); 89 | int GetWinCursorX(WINDOW Win); 90 | int GetWinCursorY(WINDOW Win); 91 | int GetWinStartCol(WINDOW Win); 92 | int GetWinStartRow(WINDOW Win); 93 | long GetWinFontFlags(WINDOW Win); 94 | unsigned char GetWinBackgroundColor(WINDOW Win); 95 | unsigned char GetWinFontColor(WINDOW Win); 96 | unsigned char GetWinFrameColor(WINDOW Win); 97 | 98 | void SetWinPosX(WINDOW Win, int Arg); 99 | void SetWinPosY(WINDOW Win, int Arg); 100 | void SetWinWidth(WINDOW Win, int Arg); 101 | void SetWinHeight(WINDOW Win, int Arg); 102 | void SetWinPitch(WINDOW Win, int Arg); 103 | void SetWinSurface(WINDOW Win, unsigned char* Arg); 104 | void SetWinStencil(WINDOW Win, unsigned char* Arg); 105 | void SetWinParent(WINDOW Win, WINDOW Arg); 106 | void SetWinChild(WINDOW Win, WINDOW Arg); 107 | void SetWinNext(WINDOW Win, WINDOW Arg); 108 | void SetWinData(WINDOW Win, void *Arg); 109 | void SetWinFlags(WINDOW Win, long Arg); 110 | void SetWinCursorX(WINDOW Win, int Arg); 111 | void SetWinCursorY(WINDOW Win, int Arg); 112 | void SetWinStartCol(WINDOW Win, int Arg); 113 | void SetWinStartRow(WINDOW Win, int Arg); 114 | void SetWinFontFlags(WINDOW Win, long Arg); 115 | void SetWinBackgroundColor(WINDOW Win, unsigned char Arg); 116 | void SetWinFontColor(WINDOW Win, unsigned char Arg); 117 | void SetWinFrameColor(WINDOW Win, unsigned char Arg); 118 | 119 | #endif 120 | 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /typed-cl.lisp: -------------------------------------------------------------------------------- 1 | (deftype any () t) 2 | 3 | (defun make-keyword (symbol) (read-from-string (concatenate 'string ":" (symbol-name symbol)))) 4 | 5 | ;(defun is-type (s) (nth-value 1 (subtypep t s))) 6 | (defun is-type (s) 7 | (declare (ignore s)) 8 | t) 9 | 10 | (defun generate-arg-names-types-and-declerations (fname args &key (caller-name "unknown")) 11 | "given a function name and argument list, generate the argument names, 12 | argument types and argument declerations and return them as 3 values" 13 | (let* (in-optional 14 | in-key 15 | in-body 16 | (arg-names (mapcar (lambda (arg) ; generate list of argument names 17 | (if (consp arg) 18 | (let ((type (first arg)) 19 | (name (second arg)) 20 | (default (third arg))) 21 | (if (or in-optional in-key) 22 | (if (typep (eval default) type) 23 | (list name default) 24 | (warn "~A of ~A: default value for ~A is not of the correct type: ~A" 25 | caller-name fname name type)) 26 | name)) 27 | (progn 28 | (case arg 29 | (&optional (setf in-optional t)) 30 | (&key (setf in-key t)) 31 | ((&body &rest) (setf in-body t)) 32 | (otherwise (error "~A of ~A: bad variable decleration in arglist: ~A" 33 | caller-name fname arg))) 34 | arg))) 35 | args)) 36 | (reset-in-vars (setf in-optional nil 37 | in-key nil 38 | in-body nil)) 39 | (arg-types (mapcar 40 | (lambda (arg) ; generate list of argument types 41 | (if in-body 42 | (car arg) 43 | (if (consp arg) 44 | (let ((type (first arg)) 45 | (name (second arg))) 46 | (if (is-type type) 47 | (if in-key 48 | (list (make-keyword name) type) 49 | type) 50 | (error "~A of ~A: unknown type name in variable decleration: ~A" 51 | caller-name fname type))) 52 | (case arg 53 | (&optional arg) 54 | (&key (progn (setf in-key t) arg)) 55 | ((&body &rest) (progn (setf in-body t) arg)) 56 | (otherwise 57 | (error "~A of ~A: bad variable decleration in arglist: ~A" 58 | caller-name fname arg)))))) 59 | args)) 60 | (arg-declerations (remove nil ; generate list of argument declerations 61 | (mapcar 62 | (lambda (name type) 63 | (case name 64 | ((&optional &key &body &rest) nil) 65 | (otherwise 66 | (if (consp name) 67 | (copy-list `(declare (type ,type ,(car name)))) 68 | (copy-list `(declare (type ,type ,name))))))) 69 | arg-names arg-types)))) 70 | (declare (ignore reset-in-vars)) 71 | (values arg-names arg-types arg-declerations))) 72 | 73 | ;(declaim (ftype (function (list &optional list) fixnum) fun)) 74 | (defmacro def (rettype fname args &body body) 75 | "declare a function with a typed return value and arg list, like so: 76 | 77 | (def float fun ((float x) &optional (float y 1) &key (float z 2) &rest args) (apply * (+ x y z) args)) 78 | 79 | *note: the above is actually invalid, but only gives an example of how to use all the parameters 80 | 81 | values after &optional are of the form: (type name default) 82 | values after &key are of the form: (type name default) 83 | 84 | all &optional and &key variables must have default values. 85 | " 86 | (let ((has-docstring (typep (car body) 'string))) 87 | (unless (is-type rettype) 88 | (error "def: return type is not a type: ~A" rettype)) 89 | (multiple-value-bind (arg-names arg-types arg-declerations) 90 | (generate-arg-names-types-and-declerations fname args :caller-name 'def) 91 | `(progn 92 | (declaim (ftype (function ,arg-types ,rettype) ,fname)) 93 | (defun ,fname ,arg-names 94 | ,@(if has-docstring (list (car body)) nil) 95 | ,@arg-declerations 96 | ,@(if has-docstring (cdr body) body)))))) 97 | 98 | ; Number Types 99 | ; System Class: float, integer, number, rational, real 100 | ; Type: bignum, bit, fixnum, short-float, single-float, double-float, long-float, signed-byte, unsigned-byte 101 | 102 | (deftype list-of (type) 103 | "defines a type (list-of type) that can be used in type declerations" 104 | (labels ((elements-are-of-type (seq type) 105 | (every (lambda (x) (typep x type)) seq))) 106 | (let ((predicate (gensym "list-of-type"))) 107 | (setf (symbol-function predicate) 108 | #'(lambda (seq) (elements-are-of-type seq type)) ) 109 | `(and list (satisfies ,predicate)) ))) 110 | 111 | (defmacro ldef (decls &body body) 112 | "Typed local function definitions like def, but using labels" 113 | (let (labels-def) 114 | (dolist (decl decls) 115 | (destructuring-bind (rettype fname args &body decl-body) decl 116 | (let ((has-docstring (typep (car decl-body) 'string))) 117 | (multiple-value-bind (arg-names arg-types arg-declerations) 118 | (generate-arg-names-types-and-declerations fname args :caller-name 'ldef) 119 | (push `(,fname ,arg-names 120 | ,@(if has-docstring (car decl-body) nil) 121 | (declare (ftype (function ,arg-types ,rettype) ,fname)) 122 | ,@arg-declerations 123 | ,@(if has-docstring (cdr decl-body) decl-body)) 124 | labels-def))))) 125 | `(labels ,(nreverse labels-def) ,@body))) 126 | 127 | (defmacro define-vars-macro (name let-form) 128 | "expand to vars/vars* macro definition" 129 | `(defmacro ,name (bindings &body body) 130 | (let ((var-list (mapcar (lambda (var) 131 | (if (and (consp var) 132 | (= (length var) 3)) 133 | (let ((type (first var)) 134 | (name (second var)) 135 | (value (third var))) 136 | (if (is-type type) 137 | (progn 138 | ;(unless (typep value type) ; makes too much noise when using programmatically set values 139 | ; (warn "~A: variable: ~A value: ~A is not of correct type: ~A" ',name name value type)) 140 | (list name value)) 141 | (error "~A: not a type name: ~A" ',let-form type))) 142 | (error (format nil "~A: invalid variable specification: ~A" ',name var)))) 143 | bindings)) 144 | (declerations (let (decls) 145 | (dolist (var bindings) 146 | (push `(declare (,(first var) ,(second var))) decls)) 147 | decls)) 148 | (let-binding ',let-form)) 149 | `(,let-binding ,var-list 150 | ,@declerations 151 | ,@body)))) 152 | (define-vars-macro vars let) 153 | (define-vars-macro vars* let*) 154 | 155 | (defmacro for (init-args condition update-vars &body body) 156 | "C-like for looping macro, variables must be defined prior to use in this macro" 157 | (let ((processed-init-args 158 | (let (tmp-list) 159 | (if (consp (car init-args)) 160 | (dolist (init-arg init-args) 161 | (setf tmp-list (append tmp-list init-arg))) 162 | (setf tmp-list init-args)) 163 | tmp-list))) 164 | `(progn 165 | (setf ,@processed-init-args) 166 | (loop 167 | ,@body 168 | ,@(if (consp (car update-vars)) 169 | update-vars 170 | (list update-vars)) 171 | (unless ,condition 172 | (return)))))) 173 | 174 | --------------------------------------------------------------------------------