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

Adding new XOR builtins function and testcase #59

Merged
merged 1 commit into from
Dec 13, 2023
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
18 changes: 18 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 22 additions & 12 deletions tests/builtins.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h3>even</h3><p>Pure Builtin(1): Checks if a number is even.</p><div class='grou
<pre class='prettyprint lang-rye'><code language='lang-rye'>even 2
; returns 1</code></pre>
</div>
<h3>mod</h3><p>Pure Builtin(2): Calculates module of two integers.</p><div class='group'>
<h3>mod</h3><p>Pure Builtin(2): Calculates module (remainder) of two integers.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>3 .mod 2
; returns 1</code></pre>
</div>
Expand All @@ -94,7 +94,7 @@ <h3>false</h3><p>Pure Builtin(0): Returns a falsy value.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>false
; returns 0</code></pre>
</div>
<h3>not</h3><p>Pure Builtin(1): Turns a truthy value to non-truthy and reverse.</p><div class='group'>
<h3>not</h3><p>Pure Builtin(1): Turns a truthy value to a non-truthy and reverse.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>not 0
; returns 1</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>not 1
Expand All @@ -116,6 +116,16 @@ <h3>or</h3><p>Pure Builtin(2): Bitwise OR operation between two values.</p><div
<pre class='prettyprint lang-rye'><code language='lang-rye'>0 .or 0
; returns 0</code></pre>
</div>
<h3>xor</h3><p>Pure Builtin(2): Bitwise XOR operation between two values.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>0 .xor 0
; returns 0</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>1 .xor 1
; returns 0</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>0 .xor 1
; returns 1</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>1 .xor 0
; returns 1</code></pre>
</div>
<h3>all</h3><p>Builtin(1): Takes a block, if all values or expressions are truthy it returns the last one, otherwise false.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>all { 1 1 1 }
; returns 1</code></pre>
Expand Down Expand Up @@ -405,27 +415,27 @@ <h3>unique</h3><p>Builtin(2): Accepts Rye value and Tagword with a Block or Stri
</div>
</div>
<h2>Type conversion and checking</h2><p>Functions that convert between Rye value types.</p><div class='section'>
<h3>to-word</h3><p>Pure Builtin(1): Takes a Rye value (like string) and returns a Word with that name.</p><div class='group'>
<h3>to-word</h3><p>Pure Builtin(1): Tries to change a Rye value to a word with same name.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>to-word "test"
; returns test</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>to-word 'test
; returns test</code></pre>
</div>
<h3>to-integer</h3><p>Builtin(1): Splits a line of string into values by separator by respecting quotes</p><div class='group'>
<h3>to-integer</h3><p>Builtin(1): Tries to change a Rye value (like string) to integer.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>to-integer "123"
; returns 123</code></pre>
</div>
<h3>to-string</h3><p>Pure Builtin(1): Takes a Rye value and returns a string representation.</p><div class='group'>
<h3>to-string</h3><p>Pure Builtin(1): Tries to turn a Rye value to string.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>to-string 123
; returns "123"</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>to-string 'word
; returns "word"</code></pre>
</div>
<h3>to-uri</h3><p>Pure Builtin(1): Takes a Rye value and return a URI if possible.</p><div class='group'>
<h3>to-uri</h3><p>Pure Builtin(1): Tries to change Rye value to an URI.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>to-uri "https://example.com"
; returns https://example.com</code></pre>
</div>
<h3>to-file</h3><p>Pure Builtin(1): Takes a Rye value and returns file if possible.</p><div class='group'>
<h3>to-file</h3><p>Pure Builtin(1): Tries to change Rye value to a file.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>to-file "file.txt"
; returns file://file.txt</code></pre>
</div>
Expand Down Expand Up @@ -465,7 +475,7 @@ <h3>is-string</h3><p>Pure Builtin(1): Returns true if value is a string.</p><div
<pre class='prettyprint lang-rye'><code language='lang-rye'>is-string test@gmail.com
; returns 0</code></pre>
</div>
<h3>type?</h3><p>Pure Builtin(1): Returns the type of Rye value (as a word).</p><div class='group'>
<h3>type?</h3><p>Pure Builtin(1): Returns the type of Rye value as a word.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>type? "ABC"
; returns string</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>type? 123
Expand All @@ -486,11 +496,11 @@ <h3>capture-stdout</h3><p>Builtin(1): Takes a block of code and does (runs) it.<
; returns "out
; "</code></pre>
</div>
<h3>dump</h3><p>Pure Builtin(1): Set docstring of the current context.</p><div class='group'>
<h3>dump</h3><p>Pure Builtin(1): Retunrs (dumps) content of a function.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>fn { x } { x + 1 } |dump
; returns { fn { x } { x ._+ 1 } }</code></pre>
</div>
<h3>doc\of?</h3><p>Pure Builtin(1): Get docstring of the first argument.</p><div class='group'>
<h3>doc\of?</h3><p>Pure Builtin(1): Get docstring of the passed context.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>fn { x "docstring" } { x + 1 } |doc\of?
; returns "docstring"</code></pre>
</div>
Expand Down Expand Up @@ -892,13 +902,13 @@ <h3>assert-equal</h3><p>Pure Builtin(2): Test if two values are equal. Fail if n
</div>
</div>
<h2>Functions that change values in-place</h2><p></p><div class='section'>
<h3>inc!</h3><p>Builtin(1): Increments integer value by 1 in place.</p><div class='group'>
<h3>inc!</h3><p>Builtin(1): Searches for a word and increments it's integer value in-place.</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>a: 100 , inc! 'a
; returns 101</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>a: 100 , inc! 'a , a
; returns 101</code></pre>
</div>
<h3>change!</h3><p>Builtin(2): Changes value in a word, if value changes returns true otherwise false</p><div class='group'>
<h3>change!</h3><p>Builtin(2): Searches for a word and changes it's value in-place. If value changes returns true otherwise false</p><div class='group'>
<pre class='prettyprint lang-rye'><code language='lang-rye'>a: 1 change! 2 'a
; returns 1</code></pre>
<pre class='prettyprint lang-rye'><code language='lang-rye'>a: 2 change! 2 'a
Expand Down
11 changes: 10 additions & 1 deletion tests/builtins.rye
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand Down
2 changes: 2 additions & 0 deletions tests/main.rye
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading