[pypy-commit] stmgc c7: add a new demo (sorting) and implement some more operators
Remi Meier
noreply at buildbot.pypy.org
Tue Jan 28 11:20:31 CET 2014
Author: Remi Meier
Branch: c7
Changeset: r680:bf658db49e2b
Date: 2014-01-28 11:19 +0100
http://bitbucket.org/pypy/stmgc/changeset/bf658db49e2b/
Log: add a new demo (sorting) and implement some more operators
diff --git a/duhton/demo/sort.duh b/duhton/demo/sort.duh
new file mode 100644
--- /dev/null
+++ b/duhton/demo/sort.duh
@@ -0,0 +1,162 @@
+
+
+
+
+(setq c (container (list 1 2 3 4)))
+
+
+(setq _rand (container (list 133542157 362436069 521288629 88675123)))
+(defun xor128 ()
+ (setq lst (get _rand))
+ (setq x (get lst 0))
+ (setq y (get lst 1))
+ (setq z (get lst 2))
+ (setq w (get lst 3))
+
+ (setq t (^ x (<< x 11)))
+ (setq x y)
+ (setq y z)
+ (setq z w)
+
+ (setq w (^ w (^ (>> w 19) (^ t (>> t 8)))))
+ (set lst 0 x)
+ (set lst 1 y)
+ (set lst 2 z)
+ (set lst 3 w)
+ w
+ )
+
+
+(defun random_list (n)
+ (if (> n 0)
+ (progn
+ (setq res (random_list (- n 1)))
+ (append res (% (xor128) 10))
+ res
+ )
+ (list )
+ )
+ )
+
+
+
+(defun merge_lists (as bs res)
+ ;; empties the two lists and merges the result to res
+ (setq len_as (len as))
+ (setq len_bs (len bs))
+ (if (< 0 len_as)
+ (if (< 0 len_bs)
+ (if (> (get as 0) (get bs 0))
+ (append res (pop bs 0))
+ (append res (pop as 0))
+ )
+ (append res (pop as 0))
+ )
+ (if (< 0 len_bs)
+ (append res (pop bs 0))
+ )
+ )
+ (if (|| (< 0 len_as) (< 0 len_bs))
+ (merge_lists as bs res)
+ )
+ )
+
+
+(defun append_to_correct_half (xs first second half_len)
+ (if (< 0 (len xs))
+ (progn
+ (setq elem (pop xs 0))
+ (if (< 0 half_len)
+ (append_to_correct_half xs
+ (append first elem)
+ second
+ (- half_len 1))
+
+ (append_to_correct_half xs
+ first
+ (append second elem)
+ (- half_len 1))
+ )
+ )
+ )
+ )
+
+(defun split_list (xs)
+ ;; empties xs and fills 2 new lists to be returned
+ (setq half_len (/ (len xs) 2))
+
+ (setq first (list))
+ (setq second (list))
+
+ (append_to_correct_half xs first second half_len)
+ (list first second)
+ )
+
+
+(defun merge_sort (xs)
+ (if (<= (len xs) 1) ; 1 elem
+ xs
+ (progn ; many elems
+ (setq lists (split_list xs))
+ (setq left (merge_sort (get lists 0)))
+ (setq right (merge_sort (get lists 1)))
+ ;; (print left right)
+ (setq merged (list))
+ (merge_lists left right merged)
+ ;; (print (quote >) merged)
+ merged
+ )
+ )
+ )
+
+
+(defun copy_list_helper (xs res idx)
+ (if (< idx (len xs))
+ (progn
+ (append res (get xs idx))
+ (copy_list_helper xs res (+ idx 1))
+ )
+ )
+ )
+(defun copy_list (xs)
+ (setq res (list))
+ (copy_list_helper xs res 0)
+ res
+ )
+(defun print_list (xs)
+ (print (quote len:) (len xs) (quote ->) xs)
+ )
+;; (defun rotate (tree)
+;; (if (pair? tree)
+;; (progn
+;; (setq left (car tree))
+;; (print left)
+;; (print (cdr tree))
+;; )
+;; (print 111111)
+;; )
+;; )
+
+;; (defun create-tree (n)
+;; (if (== n 0)
+;; (progn
+;; (set c (+ (get c) 1))
+;; (get c))
+;; (cons (create-tree (- n 1)) (create-tree (- n 1))))
+;; )
+
+
+
+(setq as (random_list 20))
+(setq bs (random_list 20))
+(print as)
+(print bs)
+(print (split_list as))
+
+(setq cs (random_list 200))
+(print_list cs)
+(print_list (merge_sort (copy_list cs)))
+
+
+
+
diff --git a/duhton/glob.c b/duhton/glob.c
--- a/duhton/glob.c
+++ b/duhton/glob.c
@@ -140,6 +140,96 @@
return Du_None;
}
+DuObject *du_xor(DuObject *cons, DuObject *locals)
+{
+ int result = 0;
+ /* _du_read1(cons); IMMUTABLE */
+ DuObject *expr = _DuCons_CAR(cons);
+ DuObject *next = _DuCons_NEXT(cons);
+
+ _du_save2(next, locals);
+ DuObject *obj = Du_Eval(expr, locals);
+ result = DuInt_AsInt(obj);
+ _du_restore2(next, locals);
+
+ cons = next;
+
+ while (cons != Du_None) {
+ /* _du_read1(cons); IMMUTABLE */
+ expr = _DuCons_CAR(cons);
+ next = _DuCons_NEXT(cons);
+
+ _du_save2(next, locals);
+ obj = Du_Eval(expr, locals);
+ result ^= DuInt_AsInt(obj);
+ _du_restore2(next, locals);
+
+ cons = next;
+ }
+
+ return DuInt_FromInt(result);
+}
+
+DuObject *du_lshift(DuObject *cons, DuObject *locals)
+{
+ int result = 0;
+ /* _du_read1(cons); IMMUTABLE */
+ DuObject *expr = _DuCons_CAR(cons);
+ DuObject *next = _DuCons_NEXT(cons);
+
+ _du_save2(next, locals);
+ DuObject *obj = Du_Eval(expr, locals);
+ result = DuInt_AsInt(obj);
+ _du_restore2(next, locals);
+
+ cons = next;
+
+ while (cons != Du_None) {
+ /* _du_read1(cons); IMMUTABLE */
+ expr = _DuCons_CAR(cons);
+ next = _DuCons_NEXT(cons);
+
+ _du_save2(next, locals);
+ obj = Du_Eval(expr, locals);
+ result <<= DuInt_AsInt(obj);
+ _du_restore2(next, locals);
+
+ cons = next;
+ }
+
+ return DuInt_FromInt(result);
+}
+
+DuObject *du_rshift(DuObject *cons, DuObject *locals)
+{
+ int result = 0;
+ /* _du_read1(cons); IMMUTABLE */
+ DuObject *expr = _DuCons_CAR(cons);
+ DuObject *next = _DuCons_NEXT(cons);
+
+ _du_save2(next, locals);
+ DuObject *obj = Du_Eval(expr, locals);
+ result = DuInt_AsInt(obj);
+ _du_restore2(next, locals);
+
+ cons = next;
+
+ while (cons != Du_None) {
+ /* _du_read1(cons); IMMUTABLE */
+ expr = _DuCons_CAR(cons);
+ next = _DuCons_NEXT(cons);
+
+ _du_save2(next, locals);
+ obj = Du_Eval(expr, locals);
+ result >>= DuInt_AsInt(obj);
+ _du_restore2(next, locals);
+
+ cons = next;
+ }
+
+ return DuInt_FromInt(result);
+}
+
DuObject *du_add(DuObject *cons, DuObject *locals)
{
int result = 0;
@@ -221,6 +311,32 @@
return DuInt_FromInt(result);
}
+DuObject *du_mod(DuObject *cons, DuObject *locals)
+{
+ int result = 0;
+ int first = 1;
+
+ while (cons != Du_None) {
+ /* _du_read1(cons); IMMUTABLE */
+ DuObject *expr = _DuCons_CAR(cons);
+ DuObject *next = _DuCons_NEXT(cons);
+
+ _du_save2(next, locals);
+ DuObject *obj = Du_Eval(expr, locals);
+ if (first) {
+ result = DuInt_AsInt(obj);
+ first = 0;
+ } else {
+ result %= DuInt_AsInt(obj);
+ }
+ _du_restore2(next, locals);
+
+ cons = next;
+ }
+ return DuInt_FromInt(result);
+}
+
+
static DuObject *_du_intcmp(DuObject *cons, DuObject *locals, int mode)
{
DuObject *obj_a, *obj_b;
@@ -236,6 +352,8 @@
case 3: r = a != b; break;
case 4: r = a > b; break;
case 5: r = a >= b; break;
+ case 6: r = a && b; break;
+ case 7: r = a || b; break;
}
return DuInt_FromInt(r);
}
@@ -252,6 +370,12 @@
{ return _du_intcmp(cons, locals, 4); }
DuObject *du_ge(DuObject *cons, DuObject *locals)
{ return _du_intcmp(cons, locals, 5); }
+DuObject *du_and(DuObject *cons, DuObject *locals)
+{ return _du_intcmp(cons, locals, 6); }
+DuObject *du_or(DuObject *cons, DuObject *locals)
+{ return _du_intcmp(cons, locals, 7); }
+
+
DuObject *du_type(DuObject *cons, DuObject *locals)
{
@@ -396,7 +520,7 @@
_du_getargs2("append", cons, locals, &lst, &newobj);
DuList_Append(lst, newobj);
- return newobj;
+ return lst;
}
DuObject *du_pop(DuObject *cons, DuObject *locals)
@@ -638,9 +762,15 @@
DuFrame_SetBuiltinMacro(Du_Globals, "setq", du_setq);
DuFrame_SetBuiltinMacro(Du_Globals, "print", du_print);
DuFrame_SetBuiltinMacro(Du_Globals, "+", du_add);
+ DuFrame_SetBuiltinMacro(Du_Globals, "^", du_xor);
+ DuFrame_SetBuiltinMacro(Du_Globals, "<<", du_lshift);
+ DuFrame_SetBuiltinMacro(Du_Globals, ">>", du_rshift);
+ DuFrame_SetBuiltinMacro(Du_Globals, "%", du_mod);
DuFrame_SetBuiltinMacro(Du_Globals, "-", du_sub);
DuFrame_SetBuiltinMacro(Du_Globals, "*", du_mul);
DuFrame_SetBuiltinMacro(Du_Globals, "/", du_div);
+ DuFrame_SetBuiltinMacro(Du_Globals, "||", du_or);
+ DuFrame_SetBuiltinMacro(Du_Globals, "&&", du_and);
DuFrame_SetBuiltinMacro(Du_Globals, "<", du_lt);
DuFrame_SetBuiltinMacro(Du_Globals, "<=", du_le);
DuFrame_SetBuiltinMacro(Du_Globals, "==", du_eq);
More information about the pypy-commit
mailing list