summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dimension/parse.c9
-rw-r--r--dimension/realize.c75
-rw-r--r--dimension/tokenize.c21
-rw-r--r--libdimension/dimension/error.h10
4 files changed, 47 insertions, 68 deletions
diff --git a/dimension/parse.c b/dimension/parse.c
index 6951974..6ca6326 100644
--- a/dimension/parse.c
+++ b/dimension/parse.c
@@ -406,8 +406,7 @@ dmnsn_eval_unary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH,
- "Attempt to evaluate wrong unary operator.");
+ dmnsn_assert(false, "Attempt to evaluate wrong unary operator.");
}
ret = dmnsn_copy_astnode(astnode);
@@ -691,8 +690,7 @@ dmnsn_eval_binary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH,
- "Attempt to evaluate wrong binary operator.");
+ dmnsn_assert(false, "Attempt to evaluate wrong binary operator.");
}
ret.type = DMNSN_AST_INTEGER;
@@ -779,8 +777,7 @@ dmnsn_eval_binary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH,
- "Attempt to evaluate wrong binary operator.");
+ dmnsn_assert(false, "Attempt to evaluate wrong binary operator.");
}
ret.type = DMNSN_AST_FLOAT;
diff --git a/dimension/realize.c b/dimension/realize.c
index 21275ba..31fb021 100644
--- a/dimension/realize.c
+++ b/dimension/realize.c
@@ -36,7 +36,7 @@ dmnsn_realize_float(dmnsn_astnode astnode)
return *(long *)astnode.ptr;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid float.");
+ dmnsn_assert(false, "Invalid float.");
return 0; /* Silence compiler warning */
}
}
@@ -44,9 +44,7 @@ dmnsn_realize_float(dmnsn_astnode astnode)
static dmnsn_vector
dmnsn_realize_vector(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_VECTOR) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a vector.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_VECTOR, "Expected a vector.");
dmnsn_astnode xnode, ynode, znode;
dmnsn_array_get(astnode.children, 0, &xnode);
@@ -63,9 +61,7 @@ dmnsn_realize_vector(dmnsn_astnode astnode)
static dmnsn_color
dmnsn_realize_color(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_COLOR) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a color.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_COLOR, "Expected a color.");
dmnsn_astnode rnode, gnode, bnode, fnode, tnode;
dmnsn_array_get(astnode.children, 0, &rnode);
@@ -91,9 +87,7 @@ dmnsn_realize_color(dmnsn_astnode astnode)
static dmnsn_matrix
dmnsn_realize_rotation(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_ROTATION) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a rotation.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_ROTATION, "Expected a rotation.");
const double deg2rad = atan(1.0)/45.0;
@@ -123,9 +117,7 @@ dmnsn_realize_rotation(dmnsn_astnode astnode)
static dmnsn_matrix
dmnsn_realize_scale(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_SCALE) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a scale.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_SCALE, "Expected a scale.");
dmnsn_astnode scale_node;
dmnsn_array_get(astnode.children, 0, &scale_node);
@@ -137,9 +129,8 @@ dmnsn_realize_scale(dmnsn_astnode astnode)
static dmnsn_matrix
dmnsn_realize_translation(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_TRANSLATION) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a translation.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_TRANSLATION,
+ "Expected a translation.");
dmnsn_astnode trans_node;
dmnsn_array_get(astnode.children, 0, &trans_node);
@@ -151,6 +142,8 @@ dmnsn_realize_translation(dmnsn_astnode astnode)
static dmnsn_camera *
dmnsn_realize_camera(dmnsn_astnode astnode)
{
+ dmnsn_assert(astnode.type == DMNSN_AST_CAMERA, "Expected a camera.");
+
const double deg2rad = atan(1.0)/45.0;
dmnsn_astnode_type camera_type = DMNSN_AST_PERSPECTIVE;
@@ -270,7 +263,7 @@ dmnsn_realize_camera(dmnsn_astnode astnode)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid camera item.");
+ dmnsn_assert(false, "Invalid camera item.");
break;
}
}
@@ -337,7 +330,7 @@ dmnsn_realize_camera(dmnsn_astnode astnode)
}
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Unsupported camera type.");
+ dmnsn_assert(false, "Unsupported camera type.");
}
return camera;
@@ -346,9 +339,7 @@ dmnsn_realize_camera(dmnsn_astnode astnode)
static dmnsn_pigment *
dmnsn_realize_pigment(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_PIGMENT) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a pigment.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_PIGMENT, "Expected a pigment.");
dmnsn_pigment *pigment = NULL;
@@ -369,7 +360,7 @@ dmnsn_realize_pigment(dmnsn_astnode astnode)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid pigment color.");
+ dmnsn_assert(false, "Invalid pigment color.");
}
return pigment;
@@ -378,9 +369,7 @@ dmnsn_realize_pigment(dmnsn_astnode astnode)
static dmnsn_finish *
dmnsn_realize_reflection(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_REFLECTION) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a reflection.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_REFLECTION, "Expected a reflection.");
dmnsn_astnode min_node, max_node;
dmnsn_array_get(astnode.children, 0, &min_node);
@@ -406,7 +395,7 @@ dmnsn_realize_reflection(dmnsn_astnode astnode)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid reflection item.");
+ dmnsn_assert(false, "Invalid reflection item.");
}
}
@@ -421,9 +410,7 @@ dmnsn_realize_reflection(dmnsn_astnode astnode)
static dmnsn_finish *
dmnsn_realize_finish(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_FINISH) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a finish.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_FINISH, "Expected a finish.");
dmnsn_finish *finish = dmnsn_new_finish();
@@ -471,7 +458,7 @@ dmnsn_realize_finish(dmnsn_astnode astnode)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid finish item.");
+ dmnsn_assert(false, "Invalid finish item.");
}
}
@@ -510,9 +497,7 @@ dmnsn_realize_finish(dmnsn_astnode astnode)
static dmnsn_texture *
dmnsn_realize_texture(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_TEXTURE) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a texture.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_TEXTURE, "Expected a texture.");
dmnsn_texture *texture = dmnsn_new_texture();
if (!texture) {
@@ -536,7 +521,7 @@ dmnsn_realize_texture(dmnsn_astnode astnode)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid texture item.");
+ dmnsn_assert(false, "Invalid texture item.");
}
}
@@ -546,9 +531,8 @@ dmnsn_realize_texture(dmnsn_astnode astnode)
static void
dmnsn_realize_object_modifiers(dmnsn_astnode astnode, dmnsn_object *object)
{
- if (astnode.type != DMNSN_AST_OBJECT_MODIFIERS) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected object modifiers.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_OBJECT_MODIFIERS,
+ "Expected object modifiers.");
unsigned int i;
for (i = 0; i < dmnsn_array_size(astnode.children); ++i) {
@@ -581,7 +565,7 @@ dmnsn_realize_object_modifiers(dmnsn_astnode astnode, dmnsn_object *object)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid object modifier.");
+ dmnsn_assert(false, "Invalid object modifier.");
}
}
}
@@ -589,9 +573,7 @@ dmnsn_realize_object_modifiers(dmnsn_astnode astnode, dmnsn_object *object)
static dmnsn_object *
dmnsn_realize_box(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_BOX) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a box.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_BOX, "Expected a box.");
dmnsn_astnode corner1, corner2;
dmnsn_array_get(astnode.children, 0, &corner1);
@@ -627,9 +609,8 @@ dmnsn_realize_box(dmnsn_astnode astnode)
static dmnsn_light *
dmnsn_realize_light_source(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_LIGHT_SOURCE) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a light source.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_LIGHT_SOURCE,
+ "Expected a light source.");
dmnsn_astnode point, color_node;
dmnsn_array_get(astnode.children, 0, &point);
@@ -649,9 +630,7 @@ dmnsn_realize_light_source(dmnsn_astnode astnode)
static dmnsn_object *
dmnsn_realize_sphere(dmnsn_astnode astnode)
{
- if (astnode.type != DMNSN_AST_SPHERE) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a sphere.");
- }
+ dmnsn_assert(astnode.type == DMNSN_AST_SPHERE, "Expected a sphere.");
dmnsn_astnode center, radius;
dmnsn_array_get(astnode.children, 0, &center);
@@ -744,7 +723,7 @@ dmnsn_realize_astree(const dmnsn_astree *astree)
break;
default:
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Unrecognised syntax element.");
+ dmnsn_assert(false, "Unrecognised syntax element.");
}
}
diff --git a/dimension/tokenize.c b/dimension/tokenize.c
index 95aaa9b..8d85b2d 100644
--- a/dimension/tokenize.c
+++ b/dimension/tokenize.c
@@ -180,15 +180,10 @@ dmnsn_include_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_token_buffer *tbuffer = dmnsn_new_token_buffer(token, prev, filename);
dmnsn_astnode *inode = dmnsn_find_symbol(symtable, "__include__");
- if (!inode) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "__include__ unset.");
- }
+ dmnsn_assert(inode, "__include__ unset.");
+ dmnsn_assert(inode->type == DMNSN_AST_STRING, "__include__ has wrong type.");
const char *include = inode->ptr;
- if (inode->type != DMNSN_AST_STRING) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "__include__ has wrong type.");
- }
-
char *filename_copy = strdup(filename);
char *localdir = dirname(filename_copy);
char *local_include = malloc(strlen(localdir) + strlen(include) + 2);
@@ -228,9 +223,9 @@ dmnsn_include_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_declare_symbol(symtable, "__includes__", dmnsn_new_ast_array());
includes = dmnsn_find_symbol(symtable, "__includes__");
}
- if (includes->type != DMNSN_AST_ARRAY) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "__includes__ has wrong type.");
- }
+ dmnsn_assert(includes, "__includes__ unset.");
+ dmnsn_assert(includes->type == DMNSN_AST_ARRAY,
+ "__includes__ has wrong type.");
dmnsn_astnode fnode = dmnsn_new_ast_string(local_include);
free(local_include);
@@ -413,9 +408,7 @@ dmnsn_if_buffer(int token, dmnsn_token_buffer *prev,
dmnsn_token_buffer *tbuffer = dmnsn_new_token_buffer(token, prev, filename);
dmnsn_astnode *cnode = dmnsn_find_symbol(symtable, "__cond__");
- if (!cnode) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "__cond__ unset.");
- }
+ dmnsn_assert(cnode, "__cond__ unset.");
bool cond = false;
if (cnode->type == DMNSN_AST_INTEGER) {
@@ -423,7 +416,7 @@ dmnsn_if_buffer(int token, dmnsn_token_buffer *prev,
} else if (cnode->type == DMNSN_AST_FLOAT) {
cond = (*(double *)cnode->ptr) ? true : false;
} else {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "__cond__ has wrong type.");
+ dmnsn_assert(false, "__cond__ has wrong type.");
}
dmnsn_undef_symbol(symtable, "__cond__");
diff --git a/libdimension/dimension/error.h b/libdimension/dimension/error.h
index 3db61e1..37b8d66 100644
--- a/libdimension/dimension/error.h
+++ b/libdimension/dimension/error.h
@@ -48,6 +48,16 @@ typedef enum {
DMNSN_FUNC, __FILE__, __LINE__, \
str)
+/* Make an assertion */
+#ifdef NDEBUG
+ #define dmnsn_assert(expr, str) ((void)0)
+#else
+ #define dmnsn_assert(expr, str) \
+ if (!(expr)) { \
+ dmnsn_error(DMNSN_SEVERITY_HIGH, (str)); \
+ }
+#endif
+
/* Called by dmnsn_error() - don't call directly */
void dmnsn_report_error(dmnsn_severity severity,
const char *func, const char *file, unsigned int line,