[llvm-commits] CVS: llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/README screen1.h screen2.h screen3.h simulate.cpp simulate.h
Chris Lattner
lattner at cs.uiuc.edu
Mon Oct 4 13:01:30 PDT 2004
Changes in directory llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul:
README added (r1.1)
screen1.h added (r1.1)
screen2.h added (r1.1)
screen3.h added (r1.1)
simulate.cpp added (r1.1)
simulate.h added (r1.1)
---
Log message:
Initial checkin of all of the source
---
Diffs of the changes: (+341 -0)
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/README
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/README:1.1
*** /dev/null Mon Oct 4 15:01:25 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/README Mon Oct 4 15:01:15 2004
***************
*** 0 ****
--- 1,2 ----
+ Pages 396-403 with header files from Ch 7. "Using C++" by
+ Bruce Eckel, McGraw Hill, 1989.
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen1.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen1.h:1.1
*** /dev/null Mon Oct 4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen1.h Mon Oct 4 15:01:15 2004
***************
*** 0 ****
--- 1,16 ----
+ class cursor_controller {
+ public:
+ // simple cursor motion:
+ void up(int rows = 1);
+ void down(int rows = 1);
+ void right(int cols = 1);
+ void left(int cols = 1); //
+ void move(int row, int col); // absolute positioning
+ void home() {move(1,1);}
+ // erasing portions of the screen:
+ void clear_screen() ; // also send cursor home
+ void clear_eol();
+ void save();
+ void restore();
+ };
+
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen2.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen2.h:1.1
*** /dev/null Mon Oct 4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen2.h Mon Oct 4 15:01:15 2004
***************
*** 0 ****
--- 1,9 ----
+ class cursor_controller2: public cursor_controller {
+ public:
+ void normal();
+ void high_intensity();
+ void blink();
+ void reverse();
+ void invisible();
+ };
+
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen3.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen3.h:1.1
*** /dev/null Mon Oct 4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/screen3.h Mon Oct 4 15:01:15 2004
***************
*** 0 ****
--- 1,24 ----
+ static int object_count;
+
+ class screen_controller : public cursor_controller2 {
+ int rowmax, colmax;
+ public:
+ screen_controller(int rows=25, int cols=80);
+ void setrows(int rows);
+ void setcols(int cols);
+ int maxrow() { return rowmax;}
+ int maxcol() { return colmax;}
+ ~screen_controller();
+ void upper_left();
+ void lower_left();
+ void upper_right();
+ void lower_right();
+ void draw_vertical(int row, int col, int length, char lc);
+ void draw_horizontal(int row, int col, int length, char lc);
+ void center(int row, char *text);
+ void move(int row, int col);
+ void pause(int seconds=1);
+ void drawbox(int hor, int ver, int ul, int ur, int ll, int lr);
+ };
+
+ extern screen_controller screen;
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/simulate.cpp
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/simulate.cpp:1.1
*** /dev/null Mon Oct 4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/simulate.cpp Mon Oct 4 15:01:15 2004
***************
*** 0 ****
--- 1,272 ----
+ #include "screen1.h"
+
+ // begin screen1.cpp implementation
+
+ void printf(char *s, int i, int j) {}
+
+ void cursor_controller::up(int rows) {
+ printf("%dA", rows, 0);
+ }
+
+ void cursor_controller::down(int rows) {
+ printf("%dB", rows, 0);
+ }
+
+ void cursor_controller::right(int cols) {
+ printf("%dC", cols, 0);
+ }
+
+ void cursor_controller::left(int cols) {
+ printf("%dD", cols, 0);
+ }
+
+ void cursor_controller::move(int row, int col) {
+ printf("%d;%dH", row, col);
+ }
+
+ void cursor_controller::clear_screen() {
+ printf("2J", 0, 0);
+ }
+
+ void cursor_controller::clear_eol() {
+ printf("K", 0, 0);
+ }
+
+ void cursor_controller::save() {
+ printf("s", 0, 0);
+ }
+
+ void cursor_controller::restore() {
+ printf("u", 0, 0);
+ }
+ // end screen1.cpp
+
+ #include "screen2.h"
+
+ //begin screen2 implementation
+
+ void cursor_controller2::normal() {
+ printf("0", 0, 0);
+ }
+
+ void cursor_controller2::high_intensity() {
+ printf("1", 0, 0);
+ }
+
+ void cursor_controller2::blink() {
+ printf("5", 0, 0);
+ }
+
+ void cursor_controller2::reverse() {
+ printf("7", 0, 0);
+ }
+
+ void cursor_controller2::invisible() {
+ printf("8", 0, 0);
+ }
+
+ #include "screen3.h"
+
+ // begin screen3 implementation
+
+ screen_controller screen;
+
+ void error(char *message) {}
+ int strlen(char *str) {return 0;}
+
+ screen_controller::screen_controller(int rows, int cols) : rowmax(rows), colmax(cols) {
+ if (object_count)
+ error("only one instance allowed");
+ else
+ object_count = 1;
+ printf("turn off wrap", 0, 0);
+ }
+
+ void screen_controller::setrows(int rows) {
+ rowmax = rows;
+ }
+
+ void screen_controller::setcols(int cols) {
+ colmax = cols;
+ }
+
+ screen_controller::~screen_controller() {
+ normal();
+ printf("turn on line wrap", 0, 0);
+ }
+
+ void screen_controller::upper_left() {
+ move(1, 1);
+ }
+
+ void screen_controller::lower_left() {
+ move(rowmax, 1);
+ }
+
+ void screen_controller::upper_right() {
+ move(1, colmax);
+ }
+
+ void screen_controller::lower_right() {
+ move(rowmax, colmax);
+ }
+
+ void screen_controller::draw_vertical(int row, int col, int length, char l_char) {
+ if (row > rowmax || (row+length) > rowmax)
+ error("draw_vertical: row index out of bounds");
+ if (col > colmax)
+ error("draw_vertical: col index out of bounds");
+ for (int rrow = row; rrow <= row+length; rrow++) {
+ cursor_controller::move(rrow,col);
+ printf("%c", (int) l_char, 0);
+ }
+ }
+
+ void screen_controller::draw_horizontal(int row, int col, int length, char l_char) {
+ if (col > colmax || (col+length) > colmax)
+ error("draw_horizontal: row index out of bounds");
+ if (row > rowmax)
+ error("draw_horizontal: col index out of bounds");
+ for (int ccol = col; ccol <= col+length; ccol++) {
+ cursor_controller::move(row,ccol);
+ printf("%c", (int) l_char, 0);
+ }
+ }
+
+ void screen_controller::center(int row, char *text) {
+ move(row, (colmax-strlen(text))/2);
+ printf("%c", (int) *text, 0);
+ }
+
+
+ void screen_controller::move(int row, int col) {
+ if (row > rowmax)
+ error("move: row index out of bounds");
+ if (col > colmax)
+ error("move: col index out of bounds");
+ cursor_controller::move(row,col);
+ }
+
+ void screen_controller::pause(int seconds) {}
+
+ void screen_controller::drawbox(int hor, int ver, int ul, int ur, int ll, int lr) {
+ draw_vertical(0, 0, maxrow()-1, ver);
+ draw_vertical(0, maxcol(), maxrow()-1, ver);
+ draw_horizontal(0, 0, maxcol()-1, hor);
+ draw_horizontal(maxrow(), 0, maxcol()-1, hor);
+ upper_left(); printf("%d", ul, 0);
+ lower_left(); printf("%d", ll, 0);
+ upper_right(); printf("%d", ur, 0);
+ lower_right(); printf("%d", lr, 0);
+ }
+
+ // end screen3 implementation
+
+ #include "simulate.h"
+
+ simulation_unit *s_grid[xsize][ysize];
+
+ class init_grid {
+ public:
+ init_grid();
+ };
+
+ init_grid grid_initializer;
+
+ init_grid::init_grid() {
+ for (int i = 0; i < xsize; i++)
+ for (int j = 0; j < ysize; j++)
+ s_grid[i][j] = 0;
+ }
+
+ void simulation_unit::move(int x_steps, int y_steps) {
+ int x_new = x + x_steps;
+ int y_new = y + y_steps;
+ if (x_new < 0 || x_new >= xsize)
+ error("move: x coord out of bounds");
+ if (y_new < 0 || y_new >= ysize)
+ error("move: y coord out of bounds");
+ if (s_grid[x_new][y_new] == 0) {
+ s_grid[x][y] = 0;
+ erase();
+ s_grid[x=x_new][y=y_new] = this;
+ display();
+ }
+ }
+
+ // now the test of simulation
+ class pop_around : public simulation_unit {
+ public:
+ void display() {
+ screen.move(x,y);
+ printf("@", 0, 0);
+ }
+ void erase() {
+ screen.move(x,y);
+ printf("", 0, 0);
+ }
+ pop_around(int xi, int yi) : simulation_unit(xi,yi) {
+ display();
+ }
+ ~pop_around() {}
+ void cycle() {
+ int x_direction; int y_direction;
+ if (x == 0) x_direction = 0;
+ else if (x == xsize-1) x_direction = 1;
+ else x_direction = 2;
+ if (y == 0) y_direction = 0;
+ else if (y == ysize-1) y_direction = 1;
+ else y_direction = 2;
+ int x_jump = x_direction ? -1 : 1;
+ int y_jump = y_direction ? -1 : 1;
+ move(x_jump, y_jump);
+ }
+ };
+
+ class crawl_around : public simulation_unit {
+ public:
+ void display() {
+ screen.move(x,y);
+ printf("*", 0, 0);
+ }
+ void erase() {
+ screen.move(x,y);
+ printf("", 0, 0);
+ }
+ crawl_around(int xi, int yi) : simulation_unit(xi,yi) {
+ display();
+ }
+ ~crawl_around() {}
+ void cycle() {
+ int x_step = 0 ? -1 : 1;
+ if (x + x_step < 0) x_step = -x_step;
+ if (x + x_step >= xsize) x_step = -x_step;
+ int y_step = 0 ? -1 : 1;
+ if (y + y_step < 0) y_step = -y_step;
+ if (y + y_step >= ysize) y_step = -y_step;
+ move(x_step, y_step);
+ }
+ };
+
+ main() {
+ screen.clear_screen();
+ int pop_factor = 1;
+ int crawl_factor = 2;
+ for (int x = 0; x < xsize; x++)
+ for (int y = 0; y < ysize; y++) {
+ if (pop_factor)
+ if (!s_grid[x][y])
+ s_grid[x][y] = new pop_around(x,y);
+ if (crawl_factor)
+ if (!s_grid[x][y])
+ s_grid[x][y] = new crawl_around(x,y);
+ }
+ int xrand = 5;
+ int yrand = 6;
+ while (1) {
+ int x_location = xrand;
+ int y_location = yrand;
+ if (s_grid[x_location][y_location])
+ s_grid[x_location][y_location]->cycle();
+ }
+ }
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/simulate.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/simulate.h:1.1
*** /dev/null Mon Oct 4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/simul/simulate.h Mon Oct 4 15:01:15 2004
***************
*** 0 ****
--- 1,18 ----
+ #define xsize 25
+ #define ysize 80
+
+ class simulation_unit {
+ protected:
+ int x, y;
+ public:
+ virtual void display() {}
+ virtual void erase() {}
+ simulation_unit(int x_loc, int y_loc) : x(x_loc), y(y_loc) {
+ display();
+ }
+ virtual ~simulation_unit() {erase();}
+ virtual void cycle() {}
+ void move(int x_steps, int y_steps);
+ };
+
+ extern simulation_unit *s_grid[xsize][ysize];
More information about the llvm-commits
mailing list