Browse Source

Rename 'Term' to 'Glyph'

master
cancel 6 years ago
parent
commit
008b0fe90a
  1. 4
      base.h
  2. 42
      field.c
  3. 12
      field.h
  4. 60
      sim.c

4
base.h

@ -11,7 +11,7 @@
#define ORCA_Y_MAX UINT16_MAX #define ORCA_Y_MAX UINT16_MAX
#define ORCA_X_MAX UINT16_MAX #define ORCA_X_MAX UINT16_MAX
typedef char Term; typedef char Glyph;
typedef uint16_t U16; typedef uint16_t U16;
typedef int16_t I16; typedef int16_t I16;
typedef uint32_t U32; typedef uint32_t U32;
@ -22,7 +22,7 @@ typedef size_t Usz;
typedef ssize_t Isz; typedef ssize_t Isz;
typedef struct { typedef struct {
Term* buffer; Glyph* buffer;
U16 height; U16 height;
U16 width; U16 width;
} Field; } Field;

42
field.c

@ -7,10 +7,10 @@ void field_init(Field* f) {
f->width = 0; f->width = 0;
} }
void field_init_fill(Field* f, Usz height, Usz width, Term fill_char) { void field_init_fill(Field* f, Usz height, Usz width, Glyph fill_char) {
assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX); assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
Usz num_cells = height * width; Usz num_cells = height * width;
f->buffer = malloc(num_cells * sizeof(Term)); f->buffer = malloc(num_cells * sizeof(Glyph));
memset(f->buffer, fill_char, num_cells); memset(f->buffer, fill_char, num_cells);
f->height = (U16)height; f->height = (U16)height;
f->width = (U16)width; f->width = (U16)width;
@ -19,7 +19,7 @@ void field_init_fill(Field* f, Usz height, Usz width, Term fill_char) {
void field_resize_raw(Field* f, Usz height, Usz width) { void field_resize_raw(Field* f, Usz height, Usz width) {
assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX); assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
Usz cells = height * width; Usz cells = height * width;
f->buffer = realloc(f->buffer, cells * sizeof(Term)); f->buffer = realloc(f->buffer, cells * sizeof(Glyph));
f->height = (U16)height; f->height = (U16)height;
f->width = (U16)width; f->width = (U16)width;
} }
@ -51,9 +51,9 @@ void field_copy_subrect(Field* src, Field* dest, Usz src_y, Usz src_x,
row_copy = row_copy_0; row_copy = row_copy_0;
if (row_copy_1 < row_copy) if (row_copy_1 < row_copy)
row_copy = row_copy_1; row_copy = row_copy_1;
Usz copy_bytes = row_copy * sizeof(Term); Usz copy_bytes = row_copy * sizeof(Glyph);
Term* src_p = src->buffer + src_y * src_width + src_x; Glyph* src_p = src->buffer + src_y * src_width + src_x;
Term* dest_p = dest->buffer + dest_y * dest_width + dest_x; Glyph* dest_p = dest->buffer + dest_y * dest_width + dest_x;
Usz src_stride; Usz src_stride;
Usz dest_stride; Usz dest_stride;
if (src_y >= dest_y) { if (src_y >= dest_y) {
@ -77,7 +77,7 @@ void field_copy_subrect(Field* src, Field* dest, Usz src_y, Usz src_x,
} }
void field_fill_subrect(Field* f, Usz y, Usz x, Usz height, Usz width, void field_fill_subrect(Field* f, Usz y, Usz x, Usz height, Usz width,
Term fill_char) { Glyph fill_char) {
Usz f_height = f->height; Usz f_height = f->height;
Usz f_width = f->width; Usz f_width = f->width;
if (y >= f_height || x >= f_width) if (y >= f_height || x >= f_width)
@ -92,8 +92,8 @@ void field_fill_subrect(Field* f, Usz y, Usz x, Usz height, Usz width,
Usz columns = width; Usz columns = width;
if (columns_0 < columns) if (columns_0 < columns)
columns = columns_0; columns = columns_0;
Usz fill_bytes = columns * sizeof(Term); Usz fill_bytes = columns * sizeof(Glyph);
Term* p = f->buffer + y * f_width + x; Glyph* p = f->buffer + y * f_width + x;
Usz iy = 0; Usz iy = 0;
for (;;) { for (;;) {
memset(p, fill_char, fill_bytes); memset(p, fill_char, fill_bytes);
@ -104,7 +104,7 @@ void field_fill_subrect(Field* f, Usz y, Usz x, Usz height, Usz width,
} }
} }
Term field_peek(Field* f, Usz y, Usz x) { Glyph field_peek(Field* f, Usz y, Usz x) {
Usz f_height = f->height; Usz f_height = f->height;
Usz f_width = f->width; Usz f_width = f->width;
assert(y < f_height && x < f_width); assert(y < f_height && x < f_width);
@ -113,7 +113,7 @@ Term field_peek(Field* f, Usz y, Usz x) {
return f->buffer[y * f_width + x]; return f->buffer[y * f_width + x];
} }
Term field_peek_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x) { Glyph field_peek_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x) {
Isz f_height = f->height; Isz f_height = f->height;
Isz f_width = f->width; Isz f_width = f->width;
Isz y0 = (Isz)y + (Isz)offs_y; Isz y0 = (Isz)y + (Isz)offs_y;
@ -123,41 +123,41 @@ Term field_peek_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x) {
return f->buffer[y0 * f_width + x0]; return f->buffer[y0 * f_width + x0];
} }
void field_poke(Field* f, Usz y, Usz x, Term term) { void field_poke(Field* f, Usz y, Usz x, Glyph glyph) {
Usz f_height = f->height; Usz f_height = f->height;
Usz f_width = f->width; Usz f_width = f->width;
assert(y < f_height && x < f_width); assert(y < f_height && x < f_width);
if (y >= f_height || x >= f_width) if (y >= f_height || x >= f_width)
return; return;
f->buffer[y * f_width + x] = term; f->buffer[y * f_width + x] = glyph;
} }
void field_poke_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x, void field_poke_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x,
Term term) { Glyph glyph) {
Isz f_height = f->height; Isz f_height = f->height;
Isz f_width = f->width; Isz f_width = f->width;
Isz y0 = (Isz)y + (Isz)offs_y; Isz y0 = (Isz)y + (Isz)offs_y;
Isz x0 = (Isz)x + (Isz)offs_x; Isz x0 = (Isz)x + (Isz)offs_x;
if (y0 >= f_height || x0 >= f_width || y0 < 0 || x0 < 0) if (y0 >= f_height || x0 >= f_width || y0 < 0 || x0 < 0)
return; return;
f->buffer[y0 * f_width + x0] = term; f->buffer[y0 * f_width + x0] = glyph;
} }
static inline bool term_char_is_valid(char c) { return c >= '#' && c <= '~'; } static inline bool glyph_char_is_valid(char c) { return c >= '#' && c <= '~'; }
void field_fput(Field* f, FILE* stream) { void field_fput(Field* f, FILE* stream) {
enum { Column_buffer_count = 4096 }; enum { Column_buffer_count = 4096 };
char out_buffer[Column_buffer_count]; char out_buffer[Column_buffer_count];
Usz f_height = f->height; Usz f_height = f->height;
Usz f_width = f->width; Usz f_width = f->width;
Term* f_buffer = f->buffer; Glyph* f_buffer = f->buffer;
if (f_width > Column_buffer_count - 2) if (f_width > Column_buffer_count - 2)
return; return;
for (Usz iy = 0; iy < f_height; ++iy) { for (Usz iy = 0; iy < f_height; ++iy) {
Term* row_p = f_buffer + f_width * iy; Glyph* row_p = f_buffer + f_width * iy;
for (Usz ix = 0; ix < f_width; ++ix) { for (Usz ix = 0; ix < f_width; ++ix) {
char c = row_p[ix]; char c = row_p[ix];
out_buffer[ix] = term_char_is_valid(c) ? c : '!'; out_buffer[ix] = glyph_char_is_valid(c) ? c : '!';
} }
out_buffer[f_width] = '\n'; out_buffer[f_width] = '\n';
out_buffer[f_width + 1] = '\0'; out_buffer[f_width + 1] = '\0';
@ -208,10 +208,10 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
return Field_load_error_not_a_rectangle; return Field_load_error_not_a_rectangle;
} }
field_resize_raw(field, rows + 1, first_row_columns); field_resize_raw(field, rows + 1, first_row_columns);
Term* rowbuff = field->buffer + first_row_columns * rows; Glyph* rowbuff = field->buffer + first_row_columns * rows;
for (Usz i = 0; i < len; ++i) { for (Usz i = 0; i < len; ++i) {
char c = buf[i]; char c = buf[i];
rowbuff[i] = term_char_is_valid(c) ? c : '.'; rowbuff[i] = glyph_char_is_valid(c) ? c : '.';
} }
++rows; ++rows;
} }

12
field.h

@ -2,18 +2,18 @@
#include "base.h" #include "base.h"
void field_init(Field* f); void field_init(Field* f);
void field_init_fill(Field* f, Usz height, Usz width, Term fill_char); void field_init_fill(Field* f, Usz height, Usz width, Glyph fill_char);
void field_resize_raw(Field* f, Usz height, Usz width); void field_resize_raw(Field* f, Usz height, Usz width);
void field_deinit(Field* f); void field_deinit(Field* f);
void field_copy_subrect(Field* src, Field* dest, Usz src_y, Usz src_x, void field_copy_subrect(Field* src, Field* dest, Usz src_y, Usz src_x,
Usz dest_y, Usz dest_x, Usz height, Usz width); Usz dest_y, Usz dest_x, Usz height, Usz width);
void field_fill_subrect(Field* f, Usz y, Usz x, Usz height, Usz width, void field_fill_subrect(Field* f, Usz y, Usz x, Usz height, Usz width,
Term fill_char); Glyph fill_char);
Term field_peek(Field* f, Usz y, Usz x); Glyph field_peek(Field* f, Usz y, Usz x);
Term field_peek_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x); Glyph field_peek_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x);
void field_poke(Field* f, Usz y, Usz x, Term term); void field_poke(Field* f, Usz y, Usz x, Glyph glyph);
void field_poke_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x, void field_poke_relative(Field* f, Usz y, Usz x, Isz offs_y, Isz offs_x,
Term term); Glyph glyph);
void field_fput(Field* f, FILE* stream); void field_fput(Field* f, FILE* stream);

60
sim.c

@ -1,64 +1,64 @@
#include "field.h" #include "field.h"
#include "sim.h" #include "sim.h"
static Term const indexed_terms[] = { static Glyph const indexed_glyphs[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '.', '*', ':', ';', '#', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '.', '*', ':', ';', '#',
}; };
enum { Terms_array_num = sizeof indexed_terms }; enum { Glyphs_array_num = sizeof indexed_glyphs };
static inline Usz index_of_term(Term c) { static inline Usz index_of_glyph(Glyph c) {
for (Usz i = 0; i < Terms_array_num; ++i) { for (Usz i = 0; i < Glyphs_array_num; ++i) {
if (indexed_terms[i] == c) if (indexed_glyphs[i] == c)
return i; return i;
} }
return SIZE_MAX; return SIZE_MAX;
} }
static inline Term term_lowered(Term c) { static inline Glyph glyph_lowered(Glyph c) {
return (c >= 'A' && c <= 'Z') ? (char)(c - ('a' - 'A')) : c; return (c >= 'A' && c <= 'Z') ? (char)(c - ('a' - 'A')) : c;
} }
// Always returns 0 through (sizeof indexed_terms) - 1, and works on // Always returns 0 through (sizeof indexed_glyphs) - 1, and works on
// capitalized terms as well. The index of the lower-cased term is returned if // capitalized glyphs as well. The index of the lower-cased glyph is returned
// the term is capitalized. // if the glyph is capitalized.
static inline Usz semantic_index_of_term(Term c) { static inline Usz semantic_index_of_glyph(Glyph c) {
Term c0 = term_lowered(c); Glyph c0 = glyph_lowered(c);
for (Usz i = 0; i < Terms_array_num; ++i) { for (Usz i = 0; i < Glyphs_array_num; ++i) {
if (indexed_terms[i] == c0) if (indexed_glyphs[i] == c0)
return i; return i;
} }
return 0; return 0;
} }
static inline Term terms_sum(Term a, Term b) { static inline Glyph glyphs_sum(Glyph a, Glyph b) {
Usz ia = semantic_index_of_term(a); Usz ia = semantic_index_of_glyph(a);
Usz ib = semantic_index_of_term(b); Usz ib = semantic_index_of_glyph(b);
return indexed_terms[(ia + ib) % Terms_array_num]; return indexed_glyphs[(ia + ib) % Glyphs_array_num];
} }
static inline Term terms_mod(Term a, Term b) { static inline Glyph glyphs_mod(Glyph a, Glyph b) {
Usz ia = semantic_index_of_term(a); Usz ia = semantic_index_of_glyph(a);
Usz ib = semantic_index_of_term(b); Usz ib = semantic_index_of_glyph(b);
return indexed_terms[ib == 0 ? 0 : (ia % ib)]; return indexed_glyphs[ib == 0 ? 0 : (ia % ib)];
} }
static inline void act_a(Field* f, Usz y, Usz x) { static inline void act_a(Field* f, Usz y, Usz x) {
Term inp0 = field_peek_relative(f, y, x, 0, 1); Glyph inp0 = field_peek_relative(f, y, x, 0, 1);
Term inp1 = field_peek_relative(f, y, x, 0, 2); Glyph inp1 = field_peek_relative(f, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') { if (inp0 != '.' && inp1 != '.') {
Term t = terms_sum(inp0, inp1); Glyph t = glyphs_sum(inp0, inp1);
field_poke_relative(f, y, x, 1, 0, t); field_poke_relative(f, y, x, 1, 0, t);
} }
} }
static inline void act_m(Field* f, Usz y, Usz x) { static inline void act_m(Field* f, Usz y, Usz x) {
Term inp0 = field_peek_relative(f, y, x, 0, 1); Glyph inp0 = field_peek_relative(f, y, x, 0, 1);
Term inp1 = field_peek_relative(f, y, x, 0, 2); Glyph inp1 = field_peek_relative(f, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') { if (inp0 != '.' && inp1 != '.') {
Term t = terms_mod(inp0, inp1); Glyph t = glyphs_mod(inp0, inp1);
field_poke_relative(f, y, x, 1, 0, t); field_poke_relative(f, y, x, 1, 0, t);
} }
} }
@ -66,11 +66,11 @@ static inline void act_m(Field* f, Usz y, Usz x) {
void orca_run(Field* f) { void orca_run(Field* f) {
Usz ny = f->height; Usz ny = f->height;
Usz nx = f->width; Usz nx = f->width;
Term* f_buffer = f->buffer; Glyph* f_buffer = f->buffer;
for (Usz iy = 0; iy < ny; ++iy) { for (Usz iy = 0; iy < ny; ++iy) {
Term* row = f_buffer + iy * nx; Glyph* row = f_buffer + iy * nx;
for (Usz ix = 0; ix < nx; ++ix) { for (Usz ix = 0; ix < nx; ++ix) {
Term c = row[ix]; Glyph c = row[ix];
switch (c) { switch (c) {
case 'a': case 'a':
act_a(f, iy, ix); act_a(f, iy, ix);

Loading…
Cancel
Save