Permalink
Browse files

Replace assert by error if the error could be caused by the user

  • Loading branch information...
1 parent d373eed commit 1f66f14d63c1bfafb6ef80ad7b998b81c2e7670a @crazyjul crazyjul committed Apr 24, 2012
Showing with 37 additions and 10 deletions.
  1. +7 −2 src/language/input.lua
  2. +18 −5 src/language/output.lua
  3. +12 −3 src/language/types.lua
@@ -2,8 +2,13 @@ input = { __semantic = {} }
function DefineInput( name, type, semantic )
- assert( input[ name ] == nil )
- assert( input.__semantic[ semantic ] == nil )
+ if input[ name ] ~= nil then
+ error( "An entry named '" .. name .."' already exists in the input structure", 2 )
+ end
+
+ if input.__semantic[ semantic ] ~= nil then
+ error( "An entry already have the semantic '" .. semantic .. "' in the input structure", 2 )
+ end
-- :TODO: Validate semantic and type value
@@ -4,9 +4,17 @@ Language = Language or {}
Language.OutputMetaType = {
__newindex = function( table, key, value )
- assert( rawget( table, key ) == nil )
- assert( table.__definition[ key ] ~= nil )
- assert( table.__definition[ key ].type == value.type )
+ if rawget( table, key ) ~= nil then
+ error( "Entry " .. key .. " of output structure has already been assigned", 2 )
+ end
+
+ if table.__definition[ key ] == nil then
+ error( "Unknown entry in output structure : " .. key, 2 )
+ end
+
+ if table.__definition[ key ].type ~= value.type then
+ error( "Invalid type for " .. ", expect " .. table.__definition[ key ].type .. " got " .. value.type, 2 )
+ end
rawset( table, key, value );
@@ -19,8 +27,13 @@ setmetatable( output, Language.OutputMetaType );
function DefineOutput( name, type, semantic )
- assert( output.__definition[ name ] == nil )
- assert( output.__semantic[ semantic ] == nil )
+ if output.__definition[ name ] ~= nil then
+ error( "An entry named '" .. name .."' already exists in the output structure", 2 )
+ end
+
+ if output.__semantic[ semantic ] ~= nil then
+ error( "An entry already have the semantic '" .. semantic .. "' in the output structure", 2 )
+ end
-- :TODO: Validate semantic and type value
@@ -14,14 +14,20 @@ end
Language.VectorMetatable = {
__add = function( a, b )
- assert( a.type == b.type );
+ if a.type ~= b.type then
+ error( "Can't add two vector of different size", 2 )
+ end
local result = { type = a.type, node="add", arguments={a,b} }
setmetatable( result, Language.VectorMetatable )
return result
end,
__mul = function( a, b )
- assert( type(a) == "number" or type(b) == "number" or a.type == b.type );
+
+ if not( type(a) == "number" or type(b) == "number" or a.type == b.type ) then
+ error( "You can only multiply a vector by another one of the same size or a number", 2 );
+ end
+
local result = { type = ( type(a) == "number" and b.type ) or a.type, node="mul", arguments={a,b} }
setmetatable( result, Language.VectorMetatable )
return result
@@ -56,7 +62,10 @@ Language.DefineVectorType = function( type, count )
_G[ name .. "_new" ] =
function( ... )
- assert( #{...} == count );
+ if #{...} ~= count then
+ error( "Wrong argument count, expect " .. count .. " got " .. #{...}, 2 )
+ end
+
local var = { type = name, node="variable", value={...} }
Language.AttachVectorMetatable( var )

0 comments on commit 1f66f14

Please sign in to comment.