compiler

Unnamed Compiled Systems Language Project
git clone http://frotz.net/git/compiler.git
Log | Files | Refs

life.src (1729B)


      1 
      2 var state i32 = 0xd3f56332;
      3 
      4 func xorshift32() i32 {
      5         state = state ^ (state << 13);
      6         state = state ^ ((state >> 17) & 0x7FFF);
      7         state = state ^ (state << 5);
      8         return state;
      9 }
     10 
     11 var grid [25][80]byte;
     12 var next [25][80]byte;
     13 
     14 func randomize() {
     15 	var x i32;
     16 	var y i32 = 1;
     17 	while (y < 24) {
     18 		x = 1;
     19 		while (x < 79) {
     20 			if ((xorshift32() & 15) < 5) {
     21 				grid[y][x] = 1;
     22 			}
     23 			x++;
     24 		}
     25 		y++;
     26 	}
     27 }
     28 
     29 func update () {
     30 	var x i32;
     31 	var y i32 = 1;
     32 	while (y < 24) {
     33 		x = 1;
     34 		while (x < 79) {
     35 			grid[y][x] = next[y][x];
     36 			x++;
     37 		}
     38 		y++;
     39 	}
     40 }
     41 
     42 // 1. Any live cell with two or three neighbors survives.
     43 // 2. Any dead cell with three live neighbors becomes a live cell.
     44 // 3. All other live cells die in the next generation. Similarly, all other dead cells stay dead.
     45 
     46 func compute() {
     47 	var x i32;
     48 	var y i32 = 1;
     49 	while (y < 24) {
     50 		x = 1;
     51 		while (x < 79) {
     52 			var n i32 =
     53 				grid[y-1][x-1] + grid[y-1][x] + grid[y-1][x+1] +
     54 				grid[y][x-1]                  +   grid[y][x+1] +
     55 				grid[y+1][x-1] + grid[y+1][x] + grid[y+1][x+1];
     56 			if (grid[y][x] == 0) {
     57 				if (n == 3) {
     58 					next[y][x] = 1;
     59 				} else {
     60 					next[y][x] = 0;
     61 				}
     62 			} else {
     63 				if (n < 2) {
     64 					next[y][x] = 0;
     65 				} else if (n > 3) {
     66 					next[y][x] = 0;
     67 				} else {
     68 					next[y][x] = 1;
     69 				}
     70 			}
     71 			x++;
     72 		}
     73 		y++;
     74 	}
     75 }
     76 func show() {
     77 	var x i32;
     78 	var y i32 = 1;
     79 	_putc_(0x1b);
     80 	_putc_(0x5b);
     81 	_putc_(0x48);
     82 	while (y < 24) {
     83 		x = 1;
     84 		while (x < 79) {
     85 			if (grid[y][x] != 0) {
     86 				_putc_(0x78);
     87 			} else {
     88 				_putc_(0x20);
     89 			}
     90 			x++;
     91 		}
     92 		_putc_(0x0a);
     93 		y++;
     94 	}
     95 	while (y < 10000000) { y++; }
     96 }
     97 
     98 func start() i32 {
     99         randomize();
    100 	while (true) {
    101         	show();
    102 		compute();
    103 		update();
    104 	}
    105 	return 0;
    106 }