From f780b7883988f283b58ea7276a65ed0b1e1d163f Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Wed, 13 Dec 2023 16:20:17 +0530 Subject: [PATCH] Adding xor builtins function --- evaldo/builtins.go | 18 ++++++++++++++++++ tests/builtins.html | 34 ++++++++++++++++++++++------------ tests/builtins.rye | 11 ++++++++++- tests/main.rye | 2 ++ 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 0e3985a7..70678d5d 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -740,6 +740,24 @@ var builtins = map[string]*env.Builtin{ } }, }, + "xor": { + Argsn: 2, + Doc: "Bitwise XOR operation between two values.", + Pure: true, + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch s1 := arg0.(type) { + case env.Integer: + switch s2 := arg1.(type) { + case env.Integer: + return *env.NewInteger(s1.Value ^ s2.Value) + default: + return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "xor") + } + default: + return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "xor") + } + }, + }, "require_": { // Argsn: 1, diff --git a/tests/builtins.html b/tests/builtins.html index 09defc9a..19f94674 100644 --- a/tests/builtins.html +++ b/tests/builtins.html @@ -80,7 +80,7 @@

even

Pure Builtin(1): Checks if a number is even.

even 2 ; returns 1
-

mod

Pure Builtin(2): Calculates module of two integers.

+

mod

Pure Builtin(2): Calculates module (remainder) of two integers.

3 .mod 2
 ; returns 1
@@ -94,7 +94,7 @@

false

Pure Builtin(0): Returns a falsy value.

false
 ; returns 0
-

not

Pure Builtin(1): Turns a truthy value to non-truthy and reverse.

+

not

Pure Builtin(1): Turns a truthy value to a non-truthy and reverse.

not 0
 ; returns 1
not 1
@@ -116,6 +116,16 @@ 

or

Pure Builtin(2): Bitwise OR operation between two values.

0 .or 0 ; returns 0
+

xor

Pure Builtin(2): Bitwise XOR operation between two values.

+
0 .xor 0
+; returns 0
+
1 .xor 1
+; returns 0
+
0 .xor 1
+; returns 1
+
1 .xor 0
+; returns 1
+

all

Builtin(1): Takes a block, if all values or expressions are truthy it returns the last one, otherwise false.

all { 1 1 1 }
 ; returns 1
@@ -405,27 +415,27 @@

unique

Builtin(2): Accepts Rye value and Tagword with a Block or Stri

Type conversion and checking

Functions that convert between Rye value types.

-

to-word

Pure Builtin(1): Takes a Rye value (like string) and returns a Word with that name.

+

to-word

Pure Builtin(1): Tries to change a Rye value to a word with same name.

to-word "test"
 ; returns test
to-word 'test
 ; returns test
-

to-integer

Builtin(1): Splits a line of string into values by separator by respecting quotes

+

to-integer

Builtin(1): Tries to change a Rye value (like string) to integer.

to-integer "123"
 ; returns 123
-

to-string

Pure Builtin(1): Takes a Rye value and returns a string representation.

+

to-string

Pure Builtin(1): Tries to turn a Rye value to string.

to-string 123
 ; returns "123"
to-string 'word
 ; returns "word"
-

to-uri

Pure Builtin(1): Takes a Rye value and return a URI if possible.

+

to-uri

Pure Builtin(1): Tries to change Rye value to an URI.

to-uri "https://example.com"
 ; returns https://example.com
-

to-file

Pure Builtin(1): Takes a Rye value and returns file if possible.

+

to-file

Pure Builtin(1): Tries to change Rye value to a file.

to-file "file.txt"
 ; returns file://file.txt
@@ -465,7 +475,7 @@

is-string

Pure Builtin(1): Returns true if value is a string.

is-string test@gmail.com ; returns 0
-

type?

Pure Builtin(1): Returns the type of Rye value (as a word).

+

type?

Pure Builtin(1): Returns the type of Rye value as a word.

type? "ABC"
 ; returns string
type? 123
@@ -486,11 +496,11 @@ 

capture-stdout

Builtin(1): Takes a block of code and does (runs) it.< ; returns "out ; "

-

dump

Pure Builtin(1): Set docstring of the current context.

+

dump

Pure Builtin(1): Retunrs (dumps) content of a function.

fn { x } { x + 1 } |dump
 ; returns { fn { x } { x ._+ 1 } }
-

doc\of?

Pure Builtin(1): Get docstring of the first argument.

+

doc\of?

Pure Builtin(1): Get docstring of the passed context.

fn { x "docstring" } { x + 1 } |doc\of?
 ; returns "docstring"
@@ -892,13 +902,13 @@

assert-equal

Pure Builtin(2): Test if two values are equal. Fail if n

Functions that change values in-place

-

inc!

Builtin(1): Increments integer value by 1 in place.

+

inc!

Builtin(1): Searches for a word and increments it's integer value in-place.

a: 100 , inc! 'a
 ; returns 101
a: 100 , inc! 'a , a
 ; returns 101
-

change!

Builtin(2): Changes value in a word, if value changes returns true otherwise false

+

change!

Builtin(2): Searches for a word and changes it's value in-place. If value changes returns true otherwise false

a: 1 change! 2 'a
 ; returns 1
a: 2 change! 2 'a
diff --git a/tests/builtins.rye b/tests/builtins.rye
index a5b1a137..fdefc0d2 100644
--- a/tests/builtins.rye
+++ b/tests/builtins.rye
@@ -181,7 +181,16 @@ section "Logic functions"
 		equal { 1 .or 1 } 1
 		equal { 0 .or 0 } 0
 	}
-	
+
+	group "xor"
+	mold\nowrap ?xor
+	{ { function } }
+	{
+		equal { 0 .xor 0 } 0
+		equal { 1 .xor 1 } 0
+		equal { 0 .xor 1 } 1
+		equal { 1 .xor 0 } 1
+	}	
 	group "all"
 	mold\nowrap ?all
 	{ { block } }
diff --git a/tests/main.rye b/tests/main.rye
index de144859..6a8175a5 100644
--- a/tests/main.rye
+++ b/tests/main.rye
@@ -1,4 +1,6 @@
 ; Testing and Documenting frameworks that do test and generate html reference docs
+; Run test cases: ../rye main.rye test
+; Generate documentation: ../rye main.rye doc > builtins.html
 
 rye .args\raw :arg
 root-ctx: current-ctx