• R/O
  • SSH

Joypy: Commit

Main interpreter and library.


Commit MetaInfo

Revisionf2d71c5ad1399ebb56725df0a1517767682628a3 (tree)
Time2019-07-21 11:26:11
AuthorSimon Forman <sforman@hush...>
CommiterSimon Forman

Log Message

Add mod, gcd, and hypot; term_expansion for math ops.

Change Summary

Incremental Difference

diff -r c0d894761421 -r f2d71c5ad139 thun/defs.txt
--- a/thun/defs.txt Sat Jul 20 17:36:58 2019 -0700
+++ b/thun/defs.txt Sat Jul 20 19:26:11 2019 -0700
@@ -9,7 +9,7 @@
99 b == [i] dip i
1010 binary == unary popd
1111 ccons == cons cons
12-cleave == fork [popd] dip
12+cleave == fork popdd
1313 codireco == cons dip rest cons
1414 dinfrirst == dip infrst
1515 disenstacken == ? [uncons ?] loop pop
@@ -22,7 +22,9 @@
2222 flatten == [] swap [concat] step
2323 fork == [i] app2
2424 fourth == rest third
25+gcd == true [tuck mod dup 0 >] loop pop
2526 grba == [stack popd] dip
27+hypot == [sqr] ii + sqrt
2628 ifte == [nullary] dipd swap branch
2729 ii == [dip] dupdip i
2830 infra == swons swaack [i] dip swaack
diff -r c0d894761421 -r f2d71c5ad139 thun/thun.pl
--- a/thun/thun.pl Sat Jul 20 17:36:58 2019 -0700
+++ b/thun/thun.pl Sat Jul 20 19:26:11 2019 -0700
@@ -44,6 +44,14 @@
4444 term_expansion(comparison_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
4545 F =.. [Y, B, A], catch((F -> C=true ; C=false), _, C=F))).
4646
47+% Likewise for math operators, try to evaluate, otherwise use the symbolic form.
48+
49+term_expansion(math_operator(X), (func(X, [A, B|S], [C|S]) :-
50+ F =.. [X, B, A], catch(C is F, _, C=F))).
51+
52+term_expansion(math_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
53+ F =.. [Y, B, A], catch(C is F, _, C=F))).
54+
4755
4856 /*
4957 An entry point.
@@ -128,10 +136,11 @@
128136 func(pop, [_|S], S ).
129137
130138 % Symbolic math. Compute the answer, or derivative, or whatever, later.
131-func(+, [A, B|S], [B + A|S]).
132-func(-, [A, B|S], [B - A|S]).
133-func(*, [A, B|S], [B * A|S]).
134-func(/, [A, B|S], [B / A|S]).
139+math_operator(+).
140+math_operator(-).
141+math_operator(*).
142+math_operator(/).
143+math_operator(mod).
135144
136145 % Attempt to calculate the value of a symbolic math expression.
137146 func(calc, [A|S], [B|S]) :- B is A.
Show on old repository browser