Explorar el Código

Changed System.random() to Int.random()

Steven Hall hace 8 años
padre
commit
8a9fcdc6a3

+ 0 - 8
docs/system.html

@@ -139,14 +139,6 @@
 	$ echo $?
 				</code></pre>
 				
-				<h4 class="section-h4">Random Number</h4>
-				<p>System has a random number generator:</p>
-				<pre><code class="swift">
-	func main() {
-		System.random(1, 10) // Returns an integer between 1 and 10 inclusively
-	}
-				</code></pre>
-
          	</div>
          	<!-- END CONTENT -->
          	

+ 2 - 0
docs/types.html

@@ -89,6 +89,8 @@
 	var b = 0xFF;		// hexadecimal
 	var c = 0O7777;		// octal
 	var d = 0B0101;		// binary
+
+	var e = Int.random(1, 10) // Returns a random integer between 1 and 10 inclusive
 			</code></pre>
 			
 			<h4 class="section-h4">Float</h4>

+ 35 - 32
src/runtime/gravity_core.c

@@ -1261,6 +1261,38 @@ static bool int_loop (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uin
 	RETURN_VALUE(VALUE_FROM_INT(t2-t1), rindex);
 }
 
+static bool int_random (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
+	#pragma unused(args)
+	if (nargs != 3) RETURN_ERROR("Int.random() expects 2 integer arguments");
+
+	if (!VALUE_ISA_INT(GET_VALUE(1)) || !VALUE_ISA_INT(GET_VALUE(2))) RETURN_ERROR("Int.random() arguments must be integers");
+
+	gravity_int_t num1 = VALUE_AS_INT(GET_VALUE(1));
+	gravity_int_t num2 = VALUE_AS_INT(GET_VALUE(2));
+
+	// Only Seed once
+	static bool already_seeded = false;
+	if (!already_seeded) {
+		srand((unsigned)time(NULL));
+		already_seeded = true;
+	}
+
+	int r;
+	// if num1 is lower, consider it min, otherwise, num2 is min
+	if (num1 < num2) {
+		// returns a random integer between num1 and num2 inclusive
+		r = (int)((rand() % (num2 - num1 + 1)) + num1);
+	}
+	else if (num1 > num2) {
+		r = (int)((rand() % (num1 - num2 + 1)) + num2);
+	}
+	else {
+		r = (int)num1;
+	}
+	RETURN_VALUE(VALUE_FROM_INT(r), rindex);
+}
+
+
 // MARK: - Bool Class -
 
 static bool operator_bool_add (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
@@ -1795,37 +1827,6 @@ static bool system_nanotime (gravity_vm *vm, gravity_value_t *args, uint16_t nar
 	RETURN_VALUE(VALUE_FROM_INT(t), rindex);
 }
 
-static bool system_random (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
-	#pragma unused(args)
-	if (nargs != 3) RETURN_ERROR("System.random() expects 2 integer arguments");
-
-	if (!VALUE_ISA_INT(GET_VALUE(1)) || !VALUE_ISA_INT(GET_VALUE(2))) RETURN_ERROR("System.random() arguments must be integers");
-
-	gravity_int_t num1 = VALUE_AS_INT(GET_VALUE(1));
-	gravity_int_t num2 = VALUE_AS_INT(GET_VALUE(2));
-
-	// Only Seed once
-	static bool already_seeded = false;
-	if (!already_seeded) {
-		srand((unsigned)time(NULL));
-		already_seeded = true;
-	}
-
-	int r;
-	// if num1 is lower, consider it min, otherwise, num2 is min
-	if (num1 < num2) {
-		// returns a random integer between num1 and num2 inclusive
-		r = (int)((rand() % (num2 - num1 + 1)) + num1);
-	}
-	else if (num1 > num2) {
-		r = (int)((rand() % (num1 - num2 + 1)) + num2);
-	}
-	else {
-		r = (int)num1;
-	}
-	RETURN_VALUE(VALUE_FROM_INT(r), rindex);
-}
-
 static bool system_realprint (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex, bool cr) {
 	#pragma unused (rindex)
 	for (uint16_t i=1; i<nargs; ++i) {
@@ -2028,6 +2029,9 @@ static void gravity_core_init (void) {
 	gravity_class_bind(gravity_class_int, GRAVITY_OPERATOR_NEG_NAME, NEW_CLOSURE_VALUE(operator_int_neg));
 	gravity_class_bind(gravity_class_int, GRAVITY_OPERATOR_NOT_NAME, NEW_CLOSURE_VALUE(operator_int_not));
 	gravity_class_bind(gravity_class_int, GRAVITY_INTERNAL_LOOP_NAME, NEW_CLOSURE_VALUE(int_loop));
+	// Meta
+	gravity_class_t *int_meta = gravity_class_get_meta(gravity_class_int);
+	gravity_class_bind(int_meta, "random", NEW_CLOSURE_VALUE(int_random));
 	
 	// FLOAT CLASS
 	gravity_class_bind(gravity_class_float, GRAVITY_OPERATOR_ADD_NAME, NEW_CLOSURE_VALUE(operator_float_add));
@@ -2109,7 +2113,6 @@ static void gravity_core_init (void) {
 	gravity_class_bind(system_meta, GRAVITY_SYSTEM_NANOTIME_NAME, NEW_CLOSURE_VALUE(system_nanotime));
 	gravity_class_bind(system_meta, GRAVITY_SYSTEM_PRINT_NAME, NEW_CLOSURE_VALUE(system_print));
 	gravity_class_bind(system_meta, GRAVITY_SYSTEM_PUT_NAME, NEW_CLOSURE_VALUE(system_put));
-	gravity_class_bind(system_meta, "random", NEW_CLOSURE_VALUE(system_random));
 	gravity_class_bind(system_meta, "exit", NEW_CLOSURE_VALUE(system_exit));
 	
 	gravity_value_t value = VALUE_FROM_OBJECT(computed_property(NULL, NEW_FUNCTION(system_get), NEW_FUNCTION(system_set)));

+ 6 - 6
test/random_int.gravity

@@ -1,22 +1,22 @@
 #unittest {
-	name: "Test System.random()";
+	name: "Test Int.random()";
 	error: NONE;
 	result: true;
 };
 
 func main() {
-  var c1 = System.random(1, 1) == 1
+  var c1 = Int.random(1, 1) == 1
 
-  var r = System.random(1, 2)
+  var r = Int.random(1, 2)
   var c2 = r >= 1 and r <= 2
 
-  r = System.random(-2, -1)
+  r = Int.random(-2, -1)
   var c3 = r >= -2 and r <= -1
 
-  r = System.random(5, 0)
+  r = Int.random(5, 0)
   var c4 = r >= 0 and r <= 5
 
-  r = System.random(-1, 1)
+  r = Int.random(-1, 1)
   var c5 = r >= -1 and r <= 1
 
   return c1 and c2 and c3 and c4 and c5

+ 2 - 2
test/random_int_error_args_not_ints.gravity

@@ -1,8 +1,8 @@
 #unittest {
-	name: "Test System.random() args not ints";
+	name: "Test Int.random() args not ints";
 	error: RUNTIME;
 };
 
 func main() {
-	return System.random("test", 1)
+	return Int.random("test", 1)
 }

+ 2 - 2
test/random_int_error_no_args.gravity

@@ -1,8 +1,8 @@
 #unittest {
-	name: "Test System.random() no arguments";
+	name: "Test Int.random() no arguments";
 	error: RUNTIME;
 };
 
 func main() {
-	return System.random()
+	return Int.random()
 }

+ 12 - 12
test/random_int_multiple_attempts.gravity

@@ -1,5 +1,5 @@
 #unittest {
-	name: "System.random() test multiple calls of the same random() call";
+	name: "Int.random() test multiple calls of the same random() call";
 	error: NONE;
 	result: true;
 };
@@ -12,28 +12,28 @@ func main() {
 	var min = 1
 	var max = 3
 
-  var r = System.random(min, max)
+  var r = Int.random(min, max)
   var b = r >= min and r <= max
 
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
-  r = System.random(min, max)
+  r = Int.random(min, max)
   b = r >= min and r <= max and b
 
   return b;