Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small changes #159

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ jobs:
- name: Install snowball
run: |
set -x
sudo make install
mkdir -p ~/.snowball
cp -r stdlib/ ~/.snowball/stdlib
ls ~/.snowball
Expand Down
37 changes: 33 additions & 4 deletions stdlib/Assert.sn
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* You can use this class to capture and handle assertion failures in your code.
*/
public class AssertionError extends Exception
{ }
{ /* ._. nothing to see here! 👋 */ }
/**
* @macro assert
* @brief Asserts a condition.
Expand All @@ -35,16 +35,45 @@ public class AssertionError extends Exception
* and is used to provide additional context when the assertion fails.
*
* @throws AssertionError if the provided condition is false. The error message will
* contain the specified message or a default message if none is provided.
* contain the specified message or a default message if none is provided.
*
* Example Usage:
* ```c
* #assert(x > 0, "x must be greater than 0");
* assert!(x > 0, "x must be greater than 0");
* ```
*/
@export
macro assert(condition: expr, message: const[str] = "Assertion failed!") {
// todo: Should this be inside a macro or a function?
if !(#condition) {
// We use the `pkg!` macro here to ensure that the `AssertionError` class is
// properly imported into the current scope.
throw pkg!(new AssertionError(#message));
}
}
}
/**
* @macro assert_eq
* @brief Asserts that two values are equal.
*
* The `assert_eq` macro is used to check whether two values are equal. If the values
* are not equal, an `AssertionError` exception is thrown, allowing you to capture
* and handle the assertion failure. This macro is a fundamental tool for validating
* assumptions and ensuring the correctness of your code.
*
* @param[in] left The left-hand side of the equality check.
* @param[in] right The right-hand side of the equality check.
* @param[in] message The message to display if the assertion fails. This message is optional
* and is used to provide additional context when the assertion fails.
*
* @throws AssertionError if the provided values are not equal. The error message will
* contain the specified message or a default message if none is provided.
* @note Both values must support the `==` operator.
*/
@export
macro assert_eq(left: expr, right: expr, message: const[str] = "Assertion failed!") {
if !(#left == #right) {
// We use the `pkg!` macro here to ensure that the `AssertionError` class is
// properly imported into the current scope.
throw pkg!(new AssertionError(#message));
}
}
45 changes: 31 additions & 14 deletions stdlib/CLib.sn
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,43 @@
* @file Common bindings for the C standard library
*/

/**
* @brief C equivalent of an unknown pointer type.
* @note This type is used to represent a pointer to an unknown type.
* It is used in situations where the type of the pointer is not known.
* For example, when calling a C function that returns a pointer to an unknown type.
*/
public type c_obj = *const void;
/**
* @brief C equivalent of an i32.
* @note This type is used to represent a 32-bit signed integer.
*/
public type c_int = i32;
/**
* @brief C equivalent of an i8.
* @note This type is used to represent an 8-bit signed integer.
*/
public type c_char = i8;
public type c_bool = bool;
/**
* @brief C equivalent of a float (32-bit).
* @note This type is used to represent a 32-bit floating point number.
*/
public type c_float = f32;
/**
* @brief C equivalent of a double (64-bit).
* @note This type is used to represent a 64-bit floating point number.
*/
public type c_double = f64;
/**
* @brief C equivalent of a long (64-bit).
* @note This type is used to represent a 64-bit signed integer.
*/
public type c_long = i64;
// TODO: add more types
/**
* @brief C equivalent of a long long (128-bit).
* @note This type is used to represent a 128-bit signed integer.
*/
public type c_long_long = i128;

/**
* @brief Allocates size bytes of uninitialized storage.
Expand Down Expand Up @@ -192,15 +221,3 @@ public external unsafe func strcpy(*const c_char, *const c_char) *const c_char;
public external unsafe func strcat(*const c_char, *const c_char) *const c_char;
} // namespace c_string

/**
* @brief a namespace that contains some utility math functions.
*/
namespace cmath {
/**
* @brief It converts a string into a double.
* @param str(&char) - string to be converted

* @return f64 - number
*/
public external unsafe func strtod(*const c_char, c_obj) f64;
}
77 changes: 39 additions & 38 deletions stdlib/Char.sn
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/
@inline
public func is_space(c: u8) bool {
return c == ' ' || c == '\t'
|| c == '\n' || c == '\r';
return c == ' ' || c == '\t'
|| c == '\n' || c == '\r';
}
/**
* Returns true if the given character is an
Expand All @@ -31,7 +31,7 @@ public func is_upper(c: u8) bool
*/
@inline
public func is_lower(c: u8) bool
{ return c >= 'a' && c <= 'z'; }
{ return c >= 'a' && c <= 'z'; }
/**
* Returns true if the given character is a
* digit.
Expand All @@ -42,7 +42,7 @@ public func is_lower(c: u8) bool
*/
@inline
public func is_digit(c: u8) bool
{ return c >= '0' && c <= '9'; }
{ return c >= '0' && c <= '9'; }
/**
* Returns true if the given character is a
* hexadecimal digit.
Expand All @@ -53,8 +53,8 @@ public func is_digit(c: u8) bool
*/
@inline
public func is_hex(c: u8) bool {
return is_digit(c) || (c >= 'a' && c <= 'f')
|| (c >= 'A' && c <= 'F');
return is_digit(c) || (c >= 'a' && c <= 'f')
|| (c >= 'A' && c <= 'F');
}
/**
* Returns true if the given character is an
Expand All @@ -66,7 +66,7 @@ public func is_hex(c: u8) bool {
*/
@inline
public func is_oct(c: u8) bool
{ return c >= '0' && c <= '7'; }
{ return c >= '0' && c <= '7'; }
/**
* Returns true if the given character is a
* letter.
Expand All @@ -77,7 +77,7 @@ public func is_oct(c: u8) bool
*/
@inline
public func is_alpha(c: u8) bool
{ return is_upper(c) || is_lower(c); }
{ return is_upper(c) || is_lower(c); }
/**
* Returns true if the given character is a
* letter or digit.
Expand All @@ -88,7 +88,7 @@ public func is_alpha(c: u8) bool
*/
@inline
public func is_alpha_num(c: u8) bool
{ return is_alpha(c) || is_digit(c); }
{ return is_alpha(c) || is_digit(c); }
/**
* Returns true if the given character is a
* control character.
Expand All @@ -99,7 +99,7 @@ public func is_alpha_num(c: u8) bool
*/
@inline
public func is_control(c: u8) bool
{ return c < 32 || c == 127; }
{ return c < 32 || c == 127; }
/**
* Returns true if the given character is a
* printable character.
Expand All @@ -110,7 +110,7 @@ public func is_control(c: u8) bool
*/
@inline
public func is_print(c: u8) bool
{ return !is_control(c); }
{ return !is_control(c); }
/**
* Returns true if the given character is a
* punctuation character.
Expand All @@ -121,17 +121,18 @@ public func is_print(c: u8) bool
*/
@inline
public func is_punc(c: u8) bool {
return c == '!' || c == '"' || c == '#'
|| c == '$' || c == '%' || c == '&'
|| c == '\'' || c == '(' || c == ')'
|| c == '*' || c == '+' || c == ','
|| c == '-' || c == '.' || c == '/'
|| c == ':' || c == ';' || c == '<'
|| c == '=' || c == '>' || c == '?'
|| c == '@' || c == '[' || c == '\\'
|| c == ']' || c == '^' || c == '_'
|| c == '`' || c == '{' || c == '|'
|| c == '}' || c == '~';
// TODO: faster implementation
return c == '!' || c == '"' || c == '#'
|| c == '$' || c == '%' || c == '&'
|| c == '\'' || c == '(' || c == ')'
|| c == '*' || c == '+' || c == ','
|| c == '-' || c == '.' || c == '/'
|| c == ':' || c == ';' || c == '<'
|| c == '=' || c == '>' || c == '?'
|| c == '@' || c == '[' || c == '\\'
|| c == ']' || c == '^' || c == '_'
|| c == '`' || c == '{' || c == '|'
|| c == '}' || c == '~';
}
/**
* Returns true if the given character is a
Expand All @@ -143,12 +144,12 @@ public func is_punc(c: u8) bool {
*/
@inline
public func is_sym(c: u8) bool {
return c == '+' || c == '-' || c == '*'
|| c == '/' || c == '\\' || c == '|'
|| c == '&' || c == '^' || c == '<'
|| c == '>' || c == '=' || c == '~'
|| c == '@' || c == '$' || c == '%'
|| c == '!' || c == '?';
return c == '+' || c == '-' || c == '*'
|| c == '/' || c == '\\' || c == '|'
|| c == '&' || c == '^' || c == '<'
|| c == '>' || c == '=' || c == '~'
|| c == '@' || c == '$' || c == '%'
|| c == '!' || c == '?';
}
/**
* Returns true if the given character is a
Expand All @@ -160,14 +161,14 @@ public func is_sym(c: u8) bool {
*/
@inline
public func is_mark(c: u8) bool {
return c == '`' || c == '\'' || c == '^'
|| c == '"' || c == '~' || c == '.'
|| c == '-' || c == '_' || c == '*'
|| c == '+' || c == '=' || c == '<'
|| c == '>' || c == '@' || c == ':'
|| c == '/' || c == '\\' || c == '|'
|| c == '!' || c == '?' || c == '#'
|| c == '$' || c == '%' || c == '&'
|| c == '(' || c == ')' || c == '['
|| c == ']' || c == '{' || c == '}';
return c == '`' || c == '\'' || c == '^'
|| c == '"' || c == '~' || c == '.'
|| c == '-' || c == '_' || c == '*'
|| c == '+' || c == '=' || c == '<'
|| c == '>' || c == '@' || c == ':'
|| c == '/' || c == '\\' || c == '|'
|| c == '!' || c == '?' || c == '#'
|| c == '$' || c == '%' || c == '&'
|| c == '(' || c == ')' || c == '['
|| c == ']' || c == '{' || c == '}';
}
Loading
Loading