summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/class.c81
-rw-r--r--src/etc.c1
-rw-r--r--src/gc.c2
-rw-r--r--src/init.c9
-rw-r--r--src/math.c688
-rw-r--r--src/object.c1
-rw-r--r--src/parse.y2
-rw-r--r--src/re.c42
-rw-r--r--src/struct.c781
9 files changed, 43 insertions, 1564 deletions
diff --git a/src/class.c b/src/class.c
index e70bd7e73..c2def3e8c 100644
--- a/src/class.c
+++ b/src/class.c
@@ -1777,9 +1777,9 @@ mrb_init_class(mrb_state *mrb)
/* name basic classes */
mrb_define_const(mrb, bob, "BasicObject", mrb_obj_value(bob));
mrb_define_const(mrb, obj, "BasicObject", mrb_obj_value(bob));
- mrb_define_const(mrb, obj, "Object", mrb_obj_value(obj));
- mrb_define_const(mrb, obj, "Module", mrb_obj_value(mod));
- mrb_define_const(mrb, obj, "Class", mrb_obj_value(cls));
+ mrb_define_const(mrb, obj, "Object", mrb_obj_value(obj));
+ mrb_define_const(mrb, obj, "Module", mrb_obj_value(mod));
+ mrb_define_const(mrb, obj, "Class", mrb_obj_value(cls));
/* name each classes */
mrb_name_class(mrb, bob, mrb_intern(mrb, "BasicObject"));
@@ -1789,43 +1789,44 @@ mrb_init_class(mrb_state *mrb)
mrb_undef_method(mrb, mod, "new");
MRB_SET_INSTANCE_TT(cls, MRB_TT_CLASS);
- mrb_define_method(mrb, bob, "initialize", mrb_bob_init, ARGS_NONE());
- mrb_define_method(mrb, bob, "!", mrb_bob_not, ARGS_NONE());
- mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, ARGS_ANY()); /* 15.3.1.3.30 */
- mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, ARGS_ANY());
- mrb_define_method(mrb, cls, "superclass", mrb_class_superclass, ARGS_NONE()); /* 15.2.3.3.4 */
- mrb_define_method(mrb, cls, "new", mrb_instance_new, ARGS_ANY()); /* 15.2.3.3.3 */
- mrb_define_method(mrb, cls, "inherited", mrb_bob_init, ARGS_REQ(1));
- mrb_define_method(mrb, mod, "class_variable_defined?", mrb_mod_cvar_defined, ARGS_REQ(1)); /* 15.2.2.4.16 */
- mrb_define_method(mrb, mod, "class_variable_get", mrb_mod_cvar_get, ARGS_REQ(1)); /* 15.2.2.4.17 */
- mrb_define_method(mrb, mod, "class_variable_set", mrb_mod_cvar_set, ARGS_REQ(2)); /* 15.2.2.4.18 */
- mrb_define_method(mrb, mod, "extend_object", mrb_mod_extend_object, ARGS_REQ(1)); /* 15.2.2.4.25 */
- mrb_define_method(mrb, mod, "extended", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.26 */
- mrb_define_method(mrb, mod, "include", mrb_mod_include, ARGS_ANY()); /* 15.2.2.4.27 */
- mrb_define_method(mrb, mod, "include?", mrb_mod_include_p, ARGS_REQ(1)); /* 15.2.2.4.28 */
- mrb_define_method(mrb, mod, "append_features", mrb_mod_append_features, ARGS_REQ(1)); /* 15.2.2.4.10 */
- mrb_define_method(mrb, mod, "class_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.15 */
- mrb_define_method(mrb, mod, "included", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.29 */
- mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, ARGS_NONE()); /* 15.2.2.4.30 */
- mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, ARGS_ANY()); /* 15.2.2.4.33 */
- mrb_define_method(mrb, mod, "method_defined?", mrb_mod_method_defined, ARGS_REQ(1)); /* 15.2.2.4.34 */
- mrb_define_method(mrb, mod, "module_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.35 */
- mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, ARGS_REQ(1)); /* 15.2.2.4.39 */
- mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, ARGS_ANY()); /* 15.2.2.4.41 */
-
- mrb_define_method(mrb, mod, "to_s", mrb_mod_to_s, ARGS_NONE());
- mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, ARGS_NONE());
- mrb_define_method(mrb, mod, "alias_method", mrb_mod_alias, ARGS_ANY()); /* 15.2.2.4.8 */
- mrb_define_method(mrb, mod, "ancestors", mrb_mod_ancestors, ARGS_NONE()); /* 15.2.2.4.9 */
- mrb_define_method(mrb, mod, "undef_method", mrb_mod_undef, ARGS_ANY()); /* 15.2.2.4.41 */
- mrb_define_method(mrb, mod, "const_defined?", mrb_mod_const_defined, ARGS_REQ(1)); /* 15.2.2.4.20 */
- mrb_define_method(mrb, mod, "const_get", mrb_mod_const_get, ARGS_REQ(1)); /* 15.2.2.4.21 */
- mrb_define_method(mrb, mod, "const_set", mrb_mod_const_set, ARGS_REQ(2)); /* 15.2.2.4.23 */
- mrb_define_method(mrb, mod, "remove_const", mrb_mod_remove_const, ARGS_REQ(1)); /* 15.2.2.4.40 */
- mrb_define_method(mrb, mod, "define_method", mod_define_method, ARGS_REQ(1));
- mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, ARGS_NONE()); /* 15.2.2.4.19 */
-
- mrb_define_method(mrb, mod, "===", mrb_mod_eqq, ARGS_REQ(1));
+ mrb_define_method(mrb, bob, "initialize", mrb_bob_init, ARGS_NONE());
+ mrb_define_method(mrb, bob, "!", mrb_bob_not, ARGS_NONE());
+ mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, ARGS_ANY()); /* 15.3.1.3.30 */
+
+ mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, ARGS_ANY());
+ mrb_define_method(mrb, cls, "superclass", mrb_class_superclass, ARGS_NONE()); /* 15.2.3.3.4 */
+ mrb_define_method(mrb, cls, "new", mrb_instance_new, ARGS_ANY()); /* 15.2.3.3.3 */
+ mrb_define_method(mrb, cls, "inherited", mrb_bob_init, ARGS_REQ(1));
+
+ mrb_define_method(mrb, mod, "class_variable_defined?", mrb_mod_cvar_defined, ARGS_REQ(1)); /* 15.2.2.4.16 */
+ mrb_define_method(mrb, mod, "class_variable_get", mrb_mod_cvar_get, ARGS_REQ(1)); /* 15.2.2.4.17 */
+ mrb_define_method(mrb, mod, "class_variable_set", mrb_mod_cvar_set, ARGS_REQ(2)); /* 15.2.2.4.18 */
+ mrb_define_method(mrb, mod, "extend_object", mrb_mod_extend_object, ARGS_REQ(1)); /* 15.2.2.4.25 */
+ mrb_define_method(mrb, mod, "extended", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.26 */
+ mrb_define_method(mrb, mod, "include", mrb_mod_include, ARGS_ANY()); /* 15.2.2.4.27 */
+ mrb_define_method(mrb, mod, "include?", mrb_mod_include_p, ARGS_REQ(1)); /* 15.2.2.4.28 */
+ mrb_define_method(mrb, mod, "append_features", mrb_mod_append_features, ARGS_REQ(1)); /* 15.2.2.4.10 */
+ mrb_define_method(mrb, mod, "class_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.15 */
+ mrb_define_method(mrb, mod, "included", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.29 */
+ mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, ARGS_NONE()); /* 15.2.2.4.30 */
+ mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, ARGS_ANY()); /* 15.2.2.4.33 */
+ mrb_define_method(mrb, mod, "method_defined?", mrb_mod_method_defined, ARGS_REQ(1)); /* 15.2.2.4.34 */
+ mrb_define_method(mrb, mod, "module_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.35 */
+ mrb_define_method(mrb, mod, "remove_class_variable", mrb_mod_remove_cvar, ARGS_REQ(1)); /* 15.2.2.4.39 */
+ mrb_define_method(mrb, mod, "remove_method", mrb_mod_remove_method, ARGS_ANY()); /* 15.2.2.4.41 */
+ mrb_define_method(mrb, mod, "to_s", mrb_mod_to_s, ARGS_NONE());
+ mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, ARGS_NONE());
+ mrb_define_method(mrb, mod, "alias_method", mrb_mod_alias, ARGS_ANY()); /* 15.2.2.4.8 */
+ mrb_define_method(mrb, mod, "ancestors", mrb_mod_ancestors, ARGS_NONE()); /* 15.2.2.4.9 */
+ mrb_define_method(mrb, mod, "undef_method", mrb_mod_undef, ARGS_ANY()); /* 15.2.2.4.41 */
+ mrb_define_method(mrb, mod, "const_defined?", mrb_mod_const_defined, ARGS_REQ(1)); /* 15.2.2.4.20 */
+ mrb_define_method(mrb, mod, "const_get", mrb_mod_const_get, ARGS_REQ(1)); /* 15.2.2.4.21 */
+ mrb_define_method(mrb, mod, "const_set", mrb_mod_const_set, ARGS_REQ(2)); /* 15.2.2.4.23 */
+ mrb_define_method(mrb, mod, "remove_const", mrb_mod_remove_const, ARGS_REQ(1)); /* 15.2.2.4.40 */
+ mrb_define_method(mrb, mod, "define_method", mod_define_method, ARGS_REQ(1));
+ mrb_define_method(mrb, mod, "class_variables", mrb_mod_class_variables, ARGS_NONE()); /* 15.2.2.4.19 */
+ mrb_define_method(mrb, mod, "===", mrb_mod_eqq, ARGS_REQ(1));
+
mrb_undef_method(mrb, cls, "append_features");
mrb_undef_method(mrb, cls, "extend_object");
}
diff --git a/src/etc.c b/src/etc.c
index 6a43ddd31..644465c80 100644
--- a/src/etc.c
+++ b/src/etc.c
@@ -170,7 +170,6 @@ mrb_obj_id(mrb_value obj)
case MRB_TT_ARRAY:
case MRB_TT_HASH:
case MRB_TT_RANGE:
- case MRB_TT_STRUCT:
case MRB_TT_EXCEPTION:
case MRB_TT_FILE:
case MRB_TT_DATA:
diff --git a/src/gc.c b/src/gc.c
index 7d9d80139..ae47de025 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -310,7 +310,7 @@ mrb_free_heap(mrb_state *mrb)
static void
gc_protect(mrb_state *mrb, struct RBasic *p)
{
- if (mrb->arena_idx > MRB_ARENA_SIZE) {
+ if (mrb->arena_idx >= MRB_ARENA_SIZE) {
/* arena overflow error */
mrb->arena_idx = MRB_ARENA_SIZE - 4; /* force room in arena */
mrb_raise(mrb, E_RUNTIME_ERROR, "arena overflow error");
diff --git a/src/init.c b/src/init.c
index a5727561d..0d1a24881 100644
--- a/src/init.c
+++ b/src/init.c
@@ -20,9 +20,7 @@ void mrb_init_array(mrb_state*);
void mrb_init_hash(mrb_state*);
void mrb_init_numeric(mrb_state*);
void mrb_init_range(mrb_state*);
-void mrb_init_struct(mrb_state*);
void mrb_init_gc(mrb_state*);
-void mrb_init_regexp(mrb_state*);
void mrb_init_print(mrb_state*);
void mrb_init_math(mrb_state*);
void mrb_init_mrblib(mrb_state*);
@@ -49,17 +47,10 @@ mrb_init_core(mrb_state *mrb)
mrb_init_hash(mrb); DONE;
mrb_init_numeric(mrb); DONE;
mrb_init_range(mrb); DONE;
-#ifdef ENABLE_STRUCT
- mrb_init_struct(mrb); DONE;
-#endif
mrb_init_gc(mrb); DONE;
- mrb_init_regexp(mrb); DONE;
#ifdef ENABLE_STDIO
mrb_init_print(mrb); DONE;
#endif
-#ifdef ENABLE_MATH
- mrb_init_math(mrb); DONE;
-#endif
mrb_init_mrblib(mrb); DONE;
#ifndef DISABLE_GEMS
mrb_init_mrbgems(mrb); DONE;
diff --git a/src/math.c b/src/math.c
deleted file mode 100644
index 782b75c97..000000000
--- a/src/math.c
+++ /dev/null
@@ -1,688 +0,0 @@
-/*
-** math.c - Math module
-**
-** See Copyright Notice in mruby.h
-*/
-
-#include "mruby.h"
-#include "mruby/array.h"
-
-#ifdef ENABLE_MATH
-#include <math.h>
-
-#define domain_error(msg) \
- mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg)
-
-/* math functions not provided under Microsoft Visual C++ */
-#ifdef _MSC_VER
-
-#define MATH_TOLERANCE 1E-12
-
-#define asinh(x) log(x + sqrt(pow(x,2.0) + 1))
-#define acosh(x) log(x + sqrt(pow(x,2.0) - 1))
-#define atanh(x) (log(1+x) - log(1-x))/2.0
-#define cbrt(x) pow(x,1.0/3.0)
-
-/* Declaration of complementary Error function */
-double
-erfc(double x);
-
-/*
-** Implementations of error functions
-** credits to http://www.digitalmars.com/archives/cplusplus/3634.html
-*/
-
-/* Implementation of Error function */
-double
-erf(double x)
-{
- static const double two_sqrtpi = 1.128379167095512574;
- double sum = x;
- double term = x;
- double xsqr = x*x;
- int j= 1;
- if (fabs(x) > 2.2) {
- return 1.0 - erfc(x);
- }
- do {
- term *= xsqr/j;
- sum -= term/(2*j+1);
- ++j;
- term *= xsqr/j;
- sum += term/(2*j+1);
- ++j;
- } while (fabs(term/sum) > MATH_TOLERANCE);
- return two_sqrtpi*sum;
-}
-
-/* Implementation of complementary Error function */
-double
-erfc(double x)
-{
- static const double one_sqrtpi= 0.564189583547756287;
- double a = 1;
- double b = x;
- double c = x;
- double d = x*x+0.5;
- double q1;
- double q2 = b/d;
- double n = 1.0;
- double t;
- if (fabs(x) < 2.2) {
- return 1.0 - erf(x);
- }
- if (x < 0.0) { /*signbit(x)*/
- return 2.0 - erfc(-x);
- }
- do {
- t = a*n+b*x;
- a = b;
- b = t;
- t = c*n+d*x;
- c = d;
- d = t;
- n += 0.5;
- q1 = q2;
- q2 = b/d;
- } while (fabs(q1-q2)/q2 > MATH_TOLERANCE);
- return one_sqrtpi*exp(-x*x)*q2;
-}
-
-#endif
-
-/*
- TRIGONOMETRIC FUNCTIONS
-*/
-
-/*
- * call-seq:
- * Math.sin(x) -> float
- *
- * Computes the sine of <i>x</i> (expressed in radians). Returns
- * -1..1.
- */
-static mrb_value
-math_sin(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = sin(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.cos(x) -> float
- *
- * Computes the cosine of <i>x</i> (expressed in radians). Returns
- * -1..1.
- */
-static mrb_value
-math_cos(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = cos(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.tan(x) -> float
- *
- * Returns the tangent of <i>x</i> (expressed in radians).
- */
-static mrb_value
-math_tan(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = tan(x);
-
- return mrb_float_value(x);
-}
-
-/*
- INVERSE TRIGONOMETRIC FUNCTIONS
-*/
-
-/*
- * call-seq:
- * Math.asin(x) -> float
- *
- * Computes the arc sine of <i>x</i>. Returns -{PI/2} .. {PI/2}.
- */
-static mrb_value
-math_asin(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = asin(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.acos(x) -> float
- *
- * Computes the arc cosine of <i>x</i>. Returns 0..PI.
- */
-static mrb_value
-math_acos(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = acos(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.atan(x) -> float
- *
- * Computes the arc tangent of <i>x</i>. Returns -{PI/2} .. {PI/2}.
- */
-static mrb_value
-math_atan(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = atan(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.atan2(y, x) -> float
- *
- * Computes the arc tangent given <i>y</i> and <i>x</i>. Returns
- * -PI..PI.
- *
- * Math.atan2(-0.0, -1.0) #=> -3.141592653589793
- * Math.atan2(-1.0, -1.0) #=> -2.356194490192345
- * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966
- * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483
- * Math.atan2(-0.0, 1.0) #=> -0.0
- * Math.atan2(0.0, 1.0) #=> 0.0
- * Math.atan2(1.0, 1.0) #=> 0.7853981633974483
- * Math.atan2(1.0, 0.0) #=> 1.5707963267948966
- * Math.atan2(1.0, -1.0) #=> 2.356194490192345
- * Math.atan2(0.0, -1.0) #=> 3.141592653589793
- *
- */
-static mrb_value
-math_atan2(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x, y;
-
- mrb_get_args(mrb, "ff", &x, &y);
- x = atan2(x, y);
-
- return mrb_float_value(x);
-}
-
-
-
-/*
- HYPERBOLIC TRIG FUNCTIONS
-*/
-/*
- * call-seq:
- * Math.sinh(x) -> float
- *
- * Computes the hyperbolic sine of <i>x</i> (expressed in
- * radians).
- */
-static mrb_value
-math_sinh(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = sinh(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.cosh(x) -> float
- *
- * Computes the hyperbolic cosine of <i>x</i> (expressed in radians).
- */
-static mrb_value
-math_cosh(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = cosh(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.tanh() -> float
- *
- * Computes the hyperbolic tangent of <i>x</i> (expressed in
- * radians).
- */
-static mrb_value
-math_tanh(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = tanh(x);
-
- return mrb_float_value(x);
-}
-
-
-/*
- INVERSE HYPERBOLIC TRIG FUNCTIONS
-*/
-
-/*
- * call-seq:
- * Math.asinh(x) -> float
- *
- * Computes the inverse hyperbolic sine of <i>x</i>.
- */
-static mrb_value
-math_asinh(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
-
- x = asinh(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.acosh(x) -> float
- *
- * Computes the inverse hyperbolic cosine of <i>x</i>.
- */
-static mrb_value
-math_acosh(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = acosh(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.atanh(x) -> float
- *
- * Computes the inverse hyperbolic tangent of <i>x</i>.
- */
-static mrb_value
-math_atanh(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = atanh(x);
-
- return mrb_float_value(x);
-}
-
-/*
- EXPONENTIALS AND LOGARITHMS
-*/
-#if defined __CYGWIN__
-# include <cygwin/version.h>
-# if CYGWIN_VERSION_DLL_MAJOR < 1005
-# define nan(x) nan()
-# endif
-# define log(x) ((x) < 0.0 ? nan("") : log(x))
-# define log10(x) ((x) < 0.0 ? nan("") : log10(x))
-#endif
-
-#ifndef log2
-#ifndef HAVE_LOG2
-double
-log2(double x)
-{
- return log10(x)/log10(2.0);
-}
-#else
-extern double log2(double);
-#endif
-#endif
-
-/*
- * call-seq:
- * Math.exp(x) -> float
- *
- * Returns e**x.
- *
- * Math.exp(0) #=> 1.0
- * Math.exp(1) #=> 2.718281828459045
- * Math.exp(1.5) #=> 4.4816890703380645
- *
- */
-static mrb_value
-math_exp(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = exp(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.log(numeric) -> float
- * Math.log(num,base) -> float
- *
- * Returns the natural logarithm of <i>numeric</i>.
- * If additional second argument is given, it will be the base
- * of logarithm.
- *
- * Math.log(1) #=> 0.0
- * Math.log(Math::E) #=> 1.0
- * Math.log(Math::E**3) #=> 3.0
- * Math.log(12,3) #=> 2.2618595071429146
- *
- */
-static mrb_value
-math_log(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x, base;
- int argc;
-
- argc = mrb_get_args(mrb, "f|f", &x, &base);
- x = log(x);
- if (argc == 2) {
- x /= log(base);
- }
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.log2(numeric) -> float
- *
- * Returns the base 2 logarithm of <i>numeric</i>.
- *
- * Math.log2(1) #=> 0.0
- * Math.log2(2) #=> 1.0
- * Math.log2(32768) #=> 15.0
- * Math.log2(65536) #=> 16.0
- *
- */
-static mrb_value
-math_log2(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = log2(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.log10(numeric) -> float
- *
- * Returns the base 10 logarithm of <i>numeric</i>.
- *
- * Math.log10(1) #=> 0.0
- * Math.log10(10) #=> 1.0
- * Math.log10(10**100) #=> 100.0
- *
- */
-static mrb_value
-math_log10(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = log10(x);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.sqrt(numeric) -> float
- *
- * Returns the square root of <i>numeric</i>.
- *
- */
-static mrb_value
-math_sqrt(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = sqrt(x);
-
- return mrb_float_value(x);
-}
-
-
-/*
- * call-seq:
- * Math.cbrt(numeric) -> float
- *
- * Returns the cube root of <i>numeric</i>.
- *
- * -9.upto(9) {|x|
- * p [x, Math.cbrt(x), Math.cbrt(x)**3]
- * }
- * #=>
- * [-9, -2.0800838230519, -9.0]
- * [-8, -2.0, -8.0]
- * [-7, -1.91293118277239, -7.0]
- * [-6, -1.81712059283214, -6.0]
- * [-5, -1.7099759466767, -5.0]
- * [-4, -1.5874010519682, -4.0]
- * [-3, -1.44224957030741, -3.0]
- * [-2, -1.25992104989487, -2.0]
- * [-1, -1.0, -1.0]
- * [0, 0.0, 0.0]
- * [1, 1.0, 1.0]
- * [2, 1.25992104989487, 2.0]
- * [3, 1.44224957030741, 3.0]
- * [4, 1.5874010519682, 4.0]
- * [5, 1.7099759466767, 5.0]
- * [6, 1.81712059283214, 6.0]
- * [7, 1.91293118277239, 7.0]
- * [8, 2.0, 8.0]
- * [9, 2.0800838230519, 9.0]
- *
- */
-static mrb_value
-math_cbrt(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = cbrt(x);
-
- return mrb_float_value(x);
-}
-
-
-/*
- * call-seq:
- * Math.frexp(numeric) -> [ fraction, exponent ]
- *
- * Returns a two-element array containing the normalized fraction (a
- * <code>Float</code>) and exponent (a <code>Fixnum</code>) of
- * <i>numeric</i>.
- *
- * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
- * fraction * 2**exponent #=> 1234.0
- */
-static mrb_value
-math_frexp(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
- int exp;
-
- mrb_get_args(mrb, "f", &x);
- x = frexp(x, &exp);
-
- return mrb_assoc_new(mrb, mrb_float_value(x), mrb_fixnum_value(exp));
-}
-
-/*
- * call-seq:
- * Math.ldexp(flt, int) -> float
- *
- * Returns the value of <i>flt</i>*(2**<i>int</i>).
- *
- * fraction, exponent = Math.frexp(1234)
- * Math.ldexp(fraction, exponent) #=> 1234.0
- */
-static mrb_value
-math_ldexp(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
- mrb_int i;
-
- mrb_get_args(mrb, "fi", &x, &i);
- x = ldexp(x, i);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.hypot(x, y) -> float
- *
- * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle
- * with sides <i>x</i> and <i>y</i>.
- *
- * Math.hypot(3, 4) #=> 5.0
- */
-static mrb_value
-math_hypot(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x, y;
-
- mrb_get_args(mrb, "ff", &x, &y);
- x = hypot(x, y);
-
- return mrb_float_value(x);
-}
-
-/*
- * call-seq:
- * Math.erf(x) -> float
- *
- * Calculates the error function of x.
- */
-static mrb_value
-math_erf(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = erf(x);
-
- return mrb_float_value(x);
-}
-
-
-/*
- * call-seq:
- * Math.erfc(x) -> float
- *
- * Calculates the complementary error function of x.
- */
-static mrb_value
-math_erfc(mrb_state *mrb, mrb_value obj)
-{
- mrb_float x;
-
- mrb_get_args(mrb, "f", &x);
- x = erfc(x);
-
- return mrb_float_value(x);
-}
-
-/* ------------------------------------------------------------------------*/
-void
-mrb_init_math(mrb_state *mrb)
-{
- struct RClass *mrb_math;
- mrb_math = mrb_define_module(mrb, "Math");
-
-#ifdef M_PI
- mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(M_PI));
-#else
- mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(atan(1.0)*4.0));
-#endif
-
-#ifdef M_E
- mrb_define_const(mrb, mrb_math, "E", mrb_float_value(M_E));
-#else
- mrb_define_const(mrb, mrb_math, "E", mrb_float_value(exp(1.0)));
-#endif
-
-#ifdef MRB_USE_FLOAT
- mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-5));
-#else
- mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-12));
-#endif
-
- mrb_define_module_function(mrb, mrb_math, "sin", math_sin, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "cos", math_cos, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "tan", math_tan, ARGS_REQ(1));
-
- mrb_define_module_function(mrb, mrb_math, "asin", math_asin, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "acos", math_acos, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "atan", math_atan, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, ARGS_REQ(2));
-
- mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, ARGS_REQ(1));
-
- mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, ARGS_REQ(1));
-
- mrb_define_module_function(mrb, mrb_math, "exp", math_exp, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "log", math_log, ARGS_REQ(1)|ARGS_OPT(1));
- mrb_define_module_function(mrb, mrb_math, "log2", math_log2, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "log10", math_log10, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, ARGS_REQ(1));
-
- mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, ARGS_REQ(2));
-
- mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, ARGS_REQ(2));
-
- mrb_define_module_function(mrb, mrb_math, "erf", math_erf, ARGS_REQ(1));
- mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, ARGS_REQ(1));
-}
-#endif /* ENABLE_MATH */
diff --git a/src/object.c b/src/object.c
index e087c35c0..6707fc6e4 100644
--- a/src/object.c
+++ b/src/object.c
@@ -379,7 +379,6 @@ static const struct types {
{MRB_TT_HASH, "Hash"},
{MRB_TT_STRING, "String"},
{MRB_TT_RANGE, "Range"},
- {MRB_TT_STRUCT, "Struct"},
// {MRB_TT_BIGNUM, "Bignum"},
{MRB_TT_FILE, "File"},
{MRB_TT_DATA, "Data"}, /* internal use: wrapped C pointers */
diff --git a/src/parse.y b/src/parse.y
index 365bc5f18..2c5690ea3 100644
--- a/src/parse.y
+++ b/src/parse.y
@@ -4440,7 +4440,7 @@ parser_yylex(parser_state *p)
p->lex_strterm = new_strterm(p, str_regexp, term, paren);
#endif
p->regexp = 1;
- p->sterm = '/';
+ p->sterm = term;
return tREGEXP_BEG;
case 's':
diff --git a/src/re.c b/src/re.c
deleted file mode 100644
index 744732e58..000000000
--- a/src/re.c
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
-** re.c - Regexp class
-**
-** See Copyright Notice in mruby.h
-*/
-
-#include "mruby.h"
-#include <string.h>
-#include "mruby/string.h"
-#include "re.h"
-#include "mruby/array.h"
-#include "mruby/class.h"
-#include "error.h"
-
-/*
- * Document-class: RegexpError
- *
- * Raised when given an invalid regexp expression.
- *
- * Regexp.new("?")
- *
- * <em>raises the exception:</em>
- *
- * RegexpError: target of repeat operator is not specified: /?/
- */
-
-/*
- * Document-class: Regexp
- *
- * A <code>Regexp</code> holds a regular expression, used to match a pattern
- * against strings. Regexps are created using the <code>/.../</code> and
- * <code>%r{...}</code> literals, and by the <code>Regexp::new</code>
- * constructor.
- *
- * :include: doc/re.rdoc
- */
-
-void
-mrb_init_regexp(mrb_state *mrb)
-{
- //mrb_define_class(mrb, REGEXP_CLASS, mrb->object_class);
-}
diff --git a/src/struct.c b/src/struct.c
deleted file mode 100644
index d7b63259e..000000000
--- a/src/struct.c
+++ /dev/null
@@ -1,781 +0,0 @@
-/*
-** struct.c - Struct class
-**
-** See Copyright Notice in mruby.h
-*/
-
-#include "mruby.h"
-#ifdef ENABLE_STRUCT
-#include <string.h>
-#include "error.h"
-#include "mruby/struct.h"
-#include "mruby/array.h"
-#include <stdarg.h>
-
-#include "mruby/string.h"
-#include "mruby/class.h"
-#include "mruby/variable.h"
-
-
-static struct RClass *
-struct_class(mrb_state *mrb)
-{
- return mrb_class_get(mrb, "Struct");
-}
-
-static inline mrb_value
-struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id)
-{
- struct RClass* kclass;
- struct RClass* sclass = struct_class(mrb);
-
- mrb_value ans;
- for (;;) {
- ans = mrb_iv_get(mrb, c, id);
- if (!mrb_nil_p(ans)) return ans;
- kclass = RCLASS_SUPER(c);
- if (kclass == 0 || kclass == sclass)
- return mrb_nil_value();
- c = mrb_obj_value(kclass);
- }
-}
-
-mrb_value
-mrb_struct_iv_get(mrb_state *mrb, mrb_value c, const char *name)
-{
- return struct_ivar_get(mrb, c, mrb_intern(mrb, name));
-}
-
-mrb_value
-mrb_struct_s_members(mrb_state *mrb, mrb_value klass)
-{
- mrb_value members = struct_ivar_get(mrb, klass, mrb_intern(mrb, "__members__"));
-
- if (mrb_nil_p(members)) {
- mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct");
- }
- if (!mrb_array_p(members)) {
- mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
- }
- return members;
-}
-
-mrb_value
-mrb_struct_members(mrb_state *mrb, mrb_value s)
-{
- mrb_value members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_obj_class(mrb, s)));
- if (mrb_type(s) == MRB_TT_STRUCT) {
- if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
- mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)",
- RARRAY_LEN(members), RSTRUCT_LEN(s));
- }
- }
- return members;
-}
-
-static mrb_value
-mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
-{
- mrb_value members, ary;
- mrb_value *p, *pend;
-
- members = mrb_struct_s_members(mrb, klass);
- ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members));
- p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members);
- while (p < pend) {
- mrb_ary_push(mrb, ary, *p);
- p++;
- }
-
- return ary;
-}
-
-static inline void
-struct_copy(mrb_value *dst, const mrb_value *src, size_t size)
-{
- size_t i;
-
- for (i = 0; i < size; i++) {
- dst[i] = src[i];
- }
-}
-
-/* 15.2.18.4.6 */
-/*
- * call-seq:
- * struct.members -> array
- *
- * Returns an array of strings representing the names of the instance
- * variables.
- *
- * Customer = Struct.new(:name, :address, :zip)
- * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
- * joe.members #=> [:name, :address, :zip]
- */
-
-static mrb_value
-mrb_struct_members_m(mrb_state *mrb, mrb_value obj)
-{
- return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj)));
-}
-
-mrb_value
-mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id)
-{
- mrb_value members, slot, *ptr, *ptr_members;
- long i, len;
-
- ptr = RSTRUCT_PTR(obj);
- members = mrb_struct_members(mrb, obj);
- ptr_members = RARRAY_PTR(members);
- slot = mrb_symbol_value(id);
- len = RARRAY_LEN(members);
- for (i=0; i<len; i++) {
- if (mrb_obj_equal(mrb, ptr_members[i], slot)) {
- return ptr[i];
- }
- }
- mrb_name_error(mrb, id, "%s is not struct member", mrb_sym2name(mrb, id));
- return mrb_nil_value(); /* not reached */
-}
-
-static mrb_value
-mrb_struct_ref(mrb_state *mrb, mrb_value obj)
-{
- return mrb_struct_getmember(mrb, obj, mrb->ci->mid);
-}
-
-static mrb_value mrb_struct_ref0(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[0];}
-static mrb_value mrb_struct_ref1(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[1];}
-static mrb_value mrb_struct_ref2(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[2];}
-static mrb_value mrb_struct_ref3(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[3];}
-static mrb_value mrb_struct_ref4(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[4];}
-static mrb_value mrb_struct_ref5(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[5];}
-static mrb_value mrb_struct_ref6(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[6];}
-static mrb_value mrb_struct_ref7(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[7];}
-static mrb_value mrb_struct_ref8(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[8];}
-static mrb_value mrb_struct_ref9(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[9];}
-
-#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
-#define N_REF_FUNC numberof(ref_func)
-
-static mrb_value (*const ref_func[])(mrb_state*, mrb_value) = {
- mrb_struct_ref0,
- mrb_struct_ref1,
- mrb_struct_ref2,
- mrb_struct_ref3,
- mrb_struct_ref4,
- mrb_struct_ref5,
- mrb_struct_ref6,
- mrb_struct_ref7,
- mrb_struct_ref8,
- mrb_struct_ref9,
-};
-
-mrb_sym
-mrb_id_attrset(mrb_state *mrb, mrb_sym id)
-{
- const char *name;
- char *buf;
- int len;
- mrb_sym mid;
-
- name = mrb_sym2name_len(mrb, id, &len);
- buf = (char *)mrb_malloc(mrb, len+2);
- memcpy(buf, name, len);
- buf[len] = '=';
- buf[len+1] = '\0';
-
- mid = mrb_intern2(mrb, buf, len+1);
- mrb_free(mrb, buf);
- return mid;
-}
-
-static mrb_value
-mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val)
-{
- const char *name;
- int i, len;
- mrb_sym mid;
- mrb_value members, slot, *ptr, *ptr_members;
-
- /* get base id */
- name = mrb_sym2name_len(mrb, mrb->ci->mid, &len);
- mid = mrb_intern2(mrb, name, len-1); /* omit last "=" */
-
- members = mrb_struct_members(mrb, obj);
- ptr_members = RARRAY_PTR(members);
- len = RARRAY_LEN(members);
- ptr = RSTRUCT_PTR(obj);
- for (i=0; i<len; i++) {
- slot = ptr_members[i];
- if (mrb_symbol(slot) == mid) {
- return ptr[i] = val;
- }
- }
-
- mrb_name_error(mrb, mid, "`%s' is not a struct member",
- mrb_sym2name(mrb, mid));
- return mrb_nil_value(); /* not reached */
-}
-
-static mrb_value
-mrb_struct_set_m(mrb_state *mrb, mrb_value obj)
-{
- mrb_value val;
-
- mrb_get_args(mrb, "o", &val);
- return mrb_struct_set(mrb, obj, val);
-}
-
-#define is_notop_id(id) (id)//((id)>tLAST_TOKEN)
-#define is_local_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
-int
-mrb_is_local_id(mrb_sym id)
-{
- return is_local_id(id);
-}
-
-#define is_const_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_CONST)
-int
-mrb_is_const_id(mrb_sym id)
-{
- return is_const_id(id);
-}
-
-static mrb_value
-make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass)
-{
- mrb_value nstr, *ptr_members;
- mrb_sym id;
- long i, len;
- struct RClass *c;
-
- if (mrb_nil_p(name)) {
- c = mrb_class_new(mrb, klass);
- }
- else {
- /* old style: should we warn? */
- name = mrb_str_to_str(mrb, name);
- id = mrb_to_id(mrb, name);
- if (!mrb_is_const_id(id)) {
- mrb_name_error(mrb, id, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name));
- }
- if (mrb_const_defined_at(mrb, klass, id)) {
- mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name));
- //?rb_mod_remove_const(klass, mrb_sym2name(mrb, id));
- }
- c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
- }
- MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT);
- nstr = mrb_obj_value(c);
- mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members);
-
- mrb_define_class_method(mrb, c, "new", mrb_instance_new, ARGS_ANY());
- mrb_define_class_method(mrb, c, "[]", mrb_instance_new, ARGS_ANY());
- mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, ARGS_NONE());
- //RSTRUCT(nstr)->basic.c->super = c->c;
- ptr_members = RARRAY_PTR(members);
- len = RARRAY_LEN(members);
- for (i=0; i< len; i++) {
- mrb_sym id = mrb_symbol(ptr_members[i]);
- if (mrb_is_local_id(id) || mrb_is_const_id(id)) {
- if (i < N_REF_FUNC) {
- mrb_define_method_id(mrb, c, id, ref_func[i], ARGS_NONE());
- }
- else {
- mrb_define_method_id(mrb, c, id, mrb_struct_ref, ARGS_NONE());
- }
- mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, ARGS_REQ(1));
- }
- }
-
- return nstr;
-}
-
-mrb_value
-mrb_struct_define(mrb_state *mrb, const char *name, ...)
-{
- va_list ar;
- mrb_value nm, ary;
- char *mem;
-
- if (!name) nm = mrb_nil_value();
- else nm = mrb_str_new2(mrb, name);
- ary = mrb_ary_new(mrb);
-
- va_start(ar, name);
- while ((mem = va_arg(ar, char*)) != 0) {
- mrb_sym slot = mrb_intern(mrb, mem);
- mrb_ary_push(mrb, ary, mrb_symbol_value(slot));
- }
- va_end(ar);
-
- return make_struct(mrb, nm, ary, struct_class(mrb));
-}
-
-/* 15.2.18.3.1 */
-/*
- * call-seq:
- * Struct.new( [aString] [, aSym]+> ) -> StructClass
- * StructClass.new(arg, ...) -> obj
- * StructClass[arg, ...] -> obj
- *
- * Creates a new class, named by <i>aString</i>, containing accessor
- * methods for the given symbols. If the name <i>aString</i> is
- * omitted, an anonymous structure class will be created. Otherwise,
- * the name of this struct will appear as a constant in class
- * <code>Struct</code>, so it must be unique for all
- * <code>Struct</code>s in the system and should start with a capital
- * letter. Assigning a structure class to a constant effectively gives
- * the class the name of the constant.
- *
- * <code>Struct::new</code> returns a new <code>Class</code> object,
- * which can then be used to create specific instances of the new
- * structure. The number of actual parameters must be
- * less than or equal to the number of attributes defined for this
- * class; unset parameters default to <code>nil</code>. Passing too many
- * parameters will raise an <code>ArgumentError</code>.
- *
- * The remaining methods listed in this section (class and instance)
- * are defined for this generated class.
- *
- * # Create a structure with a name in Struct
- * Struct.new("Customer", :name, :address) #=> Struct::Customer
- * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main">
- *
- * # Create a structure named by its constant
- * Customer = Struct.new(:name, :address) #=> Customer
- * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
- */
-static mrb_value
-mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
-{
- mrb_value name, rest;
- mrb_value *pargv;
- int argcnt;
- long i;
- mrb_value b, st;
- mrb_sym id;
- mrb_value *argv;
- int argc;
-
- name = mrb_nil_value();
- rest = mrb_nil_value();
- mrb_get_args(mrb, "*&", &argv, &argc, &b);
- if (argc == 0) { /* special case to avoid crash */
- rest = mrb_ary_new(mrb);
- }
- else {
- if (argc > 0) name = argv[0];
- if (argc > 1) rest = argv[1];
- if (mrb_array_p(rest)) {
- if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
- /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
- mrb_ary_unshift(mrb, rest, name);
- name = mrb_nil_value();
- }
- }
- else {
- pargv = &argv[1];
- argcnt = argc-1;
- if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
- /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
- name = mrb_nil_value();
- pargv = &argv[0];
- argcnt++;
- }
- rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
- }
- for (i=0; i<RARRAY_LEN(rest); i++) {
- id = mrb_to_id(mrb, RARRAY_PTR(rest)[i]);
- RARRAY_PTR(rest)[i] = mrb_symbol_value(id);
- }
- }
- st = make_struct(mrb, name, rest, struct_class(mrb));
- if (!mrb_nil_p(b)) {
- mrb_funcall(mrb, b, "call", 1, &st);
- }
-
- return st;
-}
-
-static int
-num_members(mrb_state *mrb, struct RClass *klass)
-{
- mrb_value members;
- members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern(mrb, "__members__"));
- if (!mrb_array_p(members)) {
- mrb_raise(mrb, E_TYPE_ERROR, "broken members");
- }
- return RARRAY_LEN(members);
-}
-
-/* 15.2.18.4.8 */
-/*
- */
-static mrb_value
-mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_value self)
-{
- struct RClass *klass = mrb_obj_class(mrb, self);
- int n;
- struct RStruct *st;
-
- n = num_members(mrb, klass);
- if (n < argc) {
- mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
- }
- st = RSTRUCT(self);
- st->ptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n);
- st->len = n;
- struct_copy(st->ptr, argv, argc);
-
- return self;
-}
-
-static mrb_value
-mrb_struct_initialize_m(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value self)
-{
- mrb_value *argv;
- int argc;
-
- mrb_get_args(mrb, "*", &argv, &argc);
- return mrb_struct_initialize_withArg(mrb, argc, argv, self);
-}
-
-mrb_value
-mrb_struct_initialize(mrb_state *mrb, mrb_value self, mrb_value values)
-{
- return mrb_struct_initialize_withArg(mrb, RARRAY_LEN(values), RARRAY_PTR(values), self);
-}
-
-static mrb_value
-inspect_struct(mrb_state *mrb, mrb_value s, int recur)
-{
- const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s));
- mrb_value members, str = mrb_str_new(mrb, "#<struct ", 9);
- mrb_value *ptr, *ptr_members;
- long i, len;
-
- if (cn) {
- mrb_str_append(mrb, str, mrb_str_new_cstr(mrb, cn));
- }
- if (recur) {
- return mrb_str_cat2(mrb, str, ":...>");
- }
-
- members = mrb_struct_members(mrb, s);
- ptr_members = RARRAY_PTR(members);
- ptr = RSTRUCT_PTR(s);
- len = RSTRUCT_LEN(s);
- for (i=0; i<len; i++) {
- mrb_value slot;
- mrb_sym id;
-
- if (i > 0) {
- mrb_str_cat2(mrb, str, ", ");
- }
- else if (cn) {
- mrb_str_cat2(mrb, str, " ");
- }
- slot = ptr_members[i];
- id = mrb_symbol(slot);
- if (mrb_is_local_id(id) || mrb_is_const_id(id)) {
- const char *name;
- int len;
-
- name = mrb_sym2name_len(mrb, id, &len);
- mrb_str_append(mrb, str, mrb_str_new(mrb, name, len));
- }
- else {
- mrb_str_append(mrb, str, mrb_inspect(mrb, slot));
- }
- mrb_str_cat2(mrb, str, "=");
- mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i]));
- }
- mrb_str_cat2(mrb, str, ">");
-
- return str;
-}
-
-/*
- * call-seq:
- * struct.to_s -> string
- * struct.inspect -> string
- *
- * Describe the contents of this struct in a string.
- */
-static mrb_value
-mrb_struct_inspect(mrb_state *mrb, mrb_value s)
-{
- return inspect_struct(mrb, s, 0);
-}
-
-/* 15.2.18.4.9 */
-/* :nodoc: */
-mrb_value
-mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
-{
- mrb_value s;
-
- mrb_get_args(mrb, "o", &s);
-
- if (mrb_obj_equal(mrb, copy, s)) return copy;
- if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) {
- mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
- }
- if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) {
- mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch");
- }
- struct_copy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), RSTRUCT_LEN(copy));
-
- return copy;
-}
-
-static mrb_value
-mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id)
-{
- mrb_value *ptr, members, *ptr_members;
- long i, len;
-
- ptr = RSTRUCT_PTR(s);
- members = mrb_struct_members(mrb, s);
- ptr_members = RARRAY_PTR(members);
- len = RARRAY_LEN(members);
- for (i=0; i<len; i++) {
- if (mrb_symbol(ptr_members[i]) == id) {
- return ptr[i];
- }
- }
- mrb_name_error(mrb, id, "no member '%s' in struct", mrb_sym2name(mrb, id));
- return mrb_nil_value(); /* not reached */
-}
-
-/* 15.2.18.4.2 */
-/*
- * call-seq:
- * struct[symbol] -> anObject
- * struct[fixnum] -> anObject
- *
- * Attribute Reference---Returns the value of the instance variable
- * named by <i>symbol</i>, or indexed (0..length-1) by
- * <i>fixnum</i>. Will raise <code>NameError</code> if the named
- * variable does not exist, or <code>IndexError</code> if the index is
- * out of range.
- *
- * Customer = Struct.new(:name, :address, :zip)
- * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
- *
- * joe["name"] #=> "Joe Smith"
- * joe[:name] #=> "Joe Smith"
- * joe[0] #=> "Joe Smith"
- */
-mrb_value
-mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx)
-{
- long i;
-
- if (mrb_string_p(idx) || mrb_symbol_p(idx)) {
- return mrb_struct_aref_id(mrb, s, mrb_to_id(mrb, idx));
- }
-
- i = mrb_fixnum(idx);
- if (i < 0) i = RSTRUCT_LEN(s) + i;
- if (i < 0)
- mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)",
- i, RSTRUCT_LEN(s));
- if (RSTRUCT_LEN(s) <= i)
- mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)",
- i, RSTRUCT_LEN(s));
- return RSTRUCT_PTR(s)[i];
-}
-
-mrb_value
-mrb_struct_aref(mrb_state *mrb, mrb_value s)
-{
- mrb_value idx;
-
- mrb_get_args(mrb, "o", &idx);
- return mrb_struct_aref_n(mrb, s, idx);
-}
-
-static mrb_value
-mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
-{
- mrb_value members, *ptr, *ptr_members;
- long i, len;
-
- members = mrb_struct_members(mrb, s);
- len = RARRAY_LEN(members);
- if (RSTRUCT_LEN(s) != len) {
- mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)",
- len, RSTRUCT_LEN(s));
- }
- ptr = RSTRUCT_PTR(s);
- ptr_members = RARRAY_PTR(members);
- for (i=0; i<len; i++) {
- if (mrb_symbol(ptr_members[i]) == id) {
- ptr[i] = val;
- return val;
- }
- }
- mrb_name_error(mrb, id, "no member '%s' in struct", mrb_sym2name(mrb, id));
- return val; /* not reach */
-}
-
-/* 15.2.18.4.3 */
-/*
- * call-seq:
- * struct[symbol] = obj -> obj
- * struct[fixnum] = obj -> obj
- *
- * Attribute Assignment---Assigns to the instance variable named by
- * <i>symbol</i> or <i>fixnum</i> the value <i>obj</i> and
- * returns it. Will raise a <code>NameError</code> if the named
- * variable does not exist, or an <code>IndexError</code> if the index
- * is out of range.
- *
- * Customer = Struct.new(:name, :address, :zip)
- * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
- *
- * joe["name"] = "Luke"
- * joe[:zip] = "90210"
- *
- * joe.name #=> "Luke"
- * joe.zip #=> "90210"
- */
-
-mrb_value
-mrb_struct_aset(mrb_state *mrb, mrb_value s)
-{
- long i;
- mrb_value idx;
- mrb_value val;
-
- mrb_get_args(mrb, "oo", &idx, &val);
-
- if (mrb_string_p(idx) || mrb_symbol_p(idx)) {
- return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val);
- }
-
- i = mrb_fixnum(idx);
- if (i < 0) i = RSTRUCT_LEN(s) + i;
- if (i < 0) {
- mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)",
- i, RSTRUCT_LEN(s));
- }
- if (RSTRUCT_LEN(s) <= i) {
- mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)",
- i, RSTRUCT_LEN(s));
- }
- return RSTRUCT_PTR(s)[i] = val;
-}
-
-/* 15.2.18.4.1 */
-/*
- * call-seq:
- * struct == other_struct -> true or false
- *
- * Equality---Returns <code>true</code> if <i>other_struct</i> is
- * equal to this one: they must be of the same class as generated by
- * <code>Struct::new</code>, and the values of all instance variables
- * must be equal (according to <code>Object#==</code>).
- *
- * Customer = Struct.new(:name, :address, :zip)
- * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
- * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
- * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
- * joe == joejr #=> true
- * joe == jane #=> false
- */
-
-static mrb_value
-mrb_struct_equal(mrb_state *mrb, mrb_value s)
-{
- mrb_value s2;
- mrb_value *ptr, *ptr2;
- long i, len;
-
- mrb_get_args(mrb, "o", &s2);
- if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value();
- if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value();
- if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value();
- if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
- mrb_bug("inconsistent struct"); /* should never happen */
- }
- ptr = RSTRUCT_PTR(s);
- ptr2 = RSTRUCT_PTR(s2);
- len = RSTRUCT_LEN(s);
- for (i=0; i<len; i++) {
- if (!mrb_equal(mrb, ptr[i], ptr2[i])) return mrb_false_value();
- }
- return mrb_true_value();
-}
-
-/* 15.2.18.4.12(x) */
-/*
- * code-seq:
- * struct.eql?(other) -> true or false
- *
- * Two structures are equal if they are the same object, or if all their
- * fields are equal (using <code>eql?</code>).
- */
-static mrb_value
-mrb_struct_eql(mrb_state *mrb, mrb_value s)
-{
- mrb_value s2;
- mrb_value *ptr, *ptr2;
- long i, len;
-
- mrb_get_args(mrb, "o", &s2);
- if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value();
- if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value();
- if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value();
- if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
- mrb_bug("inconsistent struct"); /* should never happen */
- }
-
- ptr = RSTRUCT_PTR(s);
- ptr2 = RSTRUCT_PTR(s2);
- len = RSTRUCT_LEN(s);
- for (i=0; i<len; i++) {
- if (!mrb_eql(mrb, ptr[i], ptr2[i])) return mrb_false_value();
- }
- return mrb_true_value();
-}
-
-/*
- * A <code>Struct</code> is a convenient way to bundle a number of
- * attributes together, using accessor methods, without having to write
- * an explicit class.
- *
- * The <code>Struct</code> class is a generator of specific classes,
- * each one of which is defined to hold a set of variables and their
- * accessors. In these examples, we'll call the generated class
- * ``<i>Customer</i>Class,'' and we'll show an example instance of that
- * class as ``<i>Customer</i>Inst.''
- *
- * In the descriptions that follow, the parameter <i>symbol</i> refers
- * to a symbol, which is either a quoted string or a
- * <code>Symbol</code> (such as <code>:name</code>).
- */
-void
-mrb_init_struct(mrb_state *mrb)
-{
- struct RClass *st;
- st = mrb_define_class(mrb, "Struct", mrb->object_class);
-
- mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, ARGS_ANY()); /* 15.2.18.3.1 */
-
- mrb_define_method(mrb, st, "==", mrb_struct_equal, ARGS_REQ(1)); /* 15.2.18.4.1 */
- mrb_define_method(mrb, st, "[]", mrb_struct_aref, ARGS_REQ(1)); /* 15.2.18.4.2 */
- mrb_define_method(mrb, st, "[]=", mrb_struct_aset, ARGS_REQ(2)); /* 15.2.18.4.3 */
- mrb_define_method(mrb, st, "members", mrb_struct_members_m, ARGS_NONE()); /* 15.2.18.4.6 */
- mrb_define_method(mrb, st, "initialize", mrb_struct_initialize_m,ARGS_ANY()); /* 15.2.18.4.8 */
- mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, ARGS_REQ(1)); /* 15.2.18.4.9 */
- mrb_define_method(mrb, st, "inspect", mrb_struct_inspect, ARGS_NONE()); /* 15.2.18.4.10(x) */
- mrb_define_alias(mrb, st, "to_s", "inspect"); /* 15.2.18.4.11(x) */
- mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */
-
-}
-#endif /* ENABLE_STRUCT */