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 @@
Pure Builtin(1): Checks if a number is even.
even 2
; returns 1
Pure Builtin(2): Calculates module of two integers.
Pure Builtin(2): Calculates module (remainder) of two integers.
3 .mod 2
; returns 1
Pure Builtin(0): Returns a falsy value.
false
; returns 0
Pure Builtin(1): Turns a truthy value to non-truthy and reverse.
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
Functions that convert between Rye value types.
Pure Builtin(1): Takes a Rye value (like string) and returns a Word with that name.
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
Builtin(1): Splits a line of string into values by separator by respecting quotes
Builtin(1): Tries to change a Rye value (like string) to integer.
to-integer "123"
; returns 123
Pure Builtin(1): Takes a Rye value and returns a string representation.
Pure Builtin(1): Tries to turn a Rye value to string.
to-string 123
; returns "123"
to-string 'word
; returns "word"
Pure Builtin(1): Takes a Rye value and return a URI if possible.
Pure Builtin(1): Tries to change Rye value to an URI.
to-uri "https://example.com"
; returns https://example.com
Pure Builtin(1): Takes a Rye value and returns file if possible.
Pure Builtin(1): Tries to change Rye value to a file.
to-file "file.txt"
; returns file://file.txt
Pure Builtin(1): Returns true if value is a string.
is-string test@gmail.com
; returns 0
Pure Builtin(1): Returns the type of Rye value (as a word).
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
; "
Pure Builtin(1): Set docstring of the current context.
Pure Builtin(1): Retunrs (dumps) content of a function.
fn { x } { x + 1 } |dump
; returns { fn { x } { x ._+ 1 } }
Pure Builtin(1): Get docstring of the first argument.
Pure Builtin(1): Get docstring of the passed context.
fn { x "docstring" } { x + 1 } |doc\of?
; returns "docstring"
Pure Builtin(2): Test if two values are equal. Fail if n
Builtin(1): Increments integer value by 1 in place.
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
Builtin(2): Changes value in a word, if value changes returns true otherwise false
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