Skip to content

Commit

Permalink
Merge branch 'master' of github.com:csev/skulpt into csev-master
Browse files Browse the repository at this point in the history
  • Loading branch information
bnmnetp committed Jan 27, 2013
2 parents 1094d8c + 9aa25b7 commit 034cdd5
Show file tree
Hide file tree
Showing 11 changed files with 543 additions and 10 deletions.
57 changes: 57 additions & 0 deletions src/lib/re/__init__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var $builtinmodule = function(name)
{
var mod = {};

var matchobj = function($gbl, $loc) {
$loc.__init__ = new Sk.builtin.func(function(self,thematch) {
self.thematch = thematch;
});
}

mod.MatchObject = Sk.misceval.buildClass(mod, matchobj, 'MatchObject', []);

// Internal function to return a Python list of strings
// From a JS regular expression string
mod._findre = function(res, string) {
var re = eval(res);
var matches = string.v.match(re);
retval = new Sk.builtin.list();
if ( matches == null ) return retval;
for (var i = 0; i < matches.length; ++i) {
var sitem = new Sk.builtin.str(matches[i]);
retval.v.push(sitem);
}
return retval;
}

mod.findall = new Sk.builtin.func(function(pattern, string, flags) {
var res = "/"+pattern.v.replace("/","\\/")+"/g";
var re = eval(res);
var matches = string.v.match(re);
retval = new Sk.builtin.list();
if ( matches == null ) return retval;
for (var i = 0; i < matches.length; ++i) {
var sitem = new Sk.builtin.str(matches[i]);
retval.v.push(sitem);
}
return retval;
});

mod.search = new Sk.builtin.func(function(pattern, string, flags) {
var res = "/"+pattern.v.replace("/","\\/")+"/";
lst = mod._findre(res,string);
if ( lst.v.length < 1 ) return null;
var mob = Sk.misceval.callsim(mod.MatchObject, lst);
return mob;
});

mod.match = new Sk.builtin.func(function(pattern, string, flags) {
var res = "/^"+pattern.v.replace("/","\\/")+"/";
lst = mod._findre(res,string);
if ( lst.v.length < 1 ) return null;
var mob = Sk.misceval.callsim(mod.MatchObject, lst);
return mob;
});

return mod;
}
7 changes: 5 additions & 2 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,12 @@ Sk.builtin.list.prototype['reverse'] = new Sk.builtin.func(function(self)
Sk.builtin.list.prototype['sort'] = new Sk.builtin.func(function(self, cmp, key, reverse)
{
goog.asserts.assert(!key, "todo;");
goog.asserts.assert(!reverse, "todo;");
Sk.mergeSort(self.v, cmp);
Sk.mergeSort(self.v, cmp, key, reverse);
return null;
});

// Make sure that key/value variations of lst.sort() work
// See issue 45 on github as to possible alternate approaches to this and
// why this was chosen - csev
Sk.builtin.list.prototype['sort'].func_code['co_varnames']=['__self__','cmp', 'key', 'reverse'];
goog.exportSymbol("Sk.builtin.list", Sk.builtin.list);
20 changes: 12 additions & 8 deletions src/mergesort.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ Sk.mergeSort = function(arr, cmp, key, reverse) // Replaced by quicksort
Sk.quickSort = function(arr, cmp, key, reverse)
{
goog.asserts.assert(!key, "todo;");
goog.asserts.assert(!reverse, "todo;");

if (!cmp)
{
cmp = Sk.mergeSort.stdCmp;
}

var partition = function(arr, begin, end, pivot)
var partition = function(arr, begin, end, pivot, reverse)
{
var tmp;
var piv=arr[pivot];
Expand All @@ -37,7 +36,12 @@ Sk.quickSort = function(arr, cmp, key, reverse)
var store=begin;
var ix;
for(ix=begin; ix<end-1; ++ix) {
if(Sk.misceval.callsim(cmp, arr[ix], piv) < 0) { // arr[ix]<=piv) {
if ( reverse ) {
var cmpresult = Sk.misceval.callsim(cmp, piv, arr[ix]);
} else {
var cmpresult = Sk.misceval.callsim(cmp, arr[ix], piv);
}
if( cmpresult < 0 ) {
// swap store, ix
tmp=arr[store];
arr[store]=arr[ix];
Expand All @@ -54,19 +58,19 @@ Sk.quickSort = function(arr, cmp, key, reverse)
return store;
}

var qsort = function(arr, begin, end)
var qsort = function(arr, begin, end, reverse)
{
if(end-1>begin) {
var pivot=begin+Math.floor(Math.random()*(end-begin));

pivot=partition(arr, begin, end, pivot);
pivot=partition(arr, begin, end, pivot, reverse);

qsort(arr, begin, pivot);
qsort(arr, pivot+1, end);
qsort(arr, begin, pivot, reverse);
qsort(arr, pivot+1, end, reverse);
}
}

qsort(arr, 0, arr.length);
qsort(arr, 0, arr.length, reverse);
return null;
};

Expand Down
8 changes: 8 additions & 0 deletions test/run/t338.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
l = [9,8,1,2,3,45,5]
l.sort()
print l
print "---------------- DEFAULT"
l.sort(reverse=True)
print l
print "---------------- REVERSE"

4 changes: 4 additions & 0 deletions test/run/t338.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[1, 2, 3, 5, 8, 9, 45]
---------------- DEFAULT
[45, 9, 8, 5, 3, 2, 1]
---------------- REVERSE
30 changes: 30 additions & 0 deletions test/run/t338.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: True
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: l
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
42 changes: 42 additions & 0 deletions test/run/t338.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Module(body=[Assign(targets=[Name(id='l',
ctx=Store())],
value=List(elts=[Num(n=9),
Num(n=8),
Num(n=1),
Num(n=2),
Num(n=3),
Num(n=45),
Num(n=5)],
ctx=Load())),
Expr(value=Call(func=Attribute(value=Name(id='l',
ctx=Load()),
attr='sort',
ctx=Load()),
args=[],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='l',
ctx=Load())],
nl=True),
Print(dest=None,
values=[Str(s='---------------- DEFAULT')],
nl=True),
Expr(value=Call(func=Attribute(value=Name(id='l',
ctx=Load()),
attr='sort',
ctx=Load()),
args=[],
keywords=[keyword(arg='reverse',
value=Name(id='True',
ctx=Load()))],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='l',
ctx=Load())],
nl=True),
Print(dest=None,
values=[Str(s='---------------- REVERSE')],
nl=True)])
51 changes: 51 additions & 0 deletions test/run/t339.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import re

val = re.findall("From","dlkjdsljkdlkdsjlk")
print val
if len(val) == 0 : print "Correct 1"
else : print "InCorrect 1"

val = re.findall("From","dlkjd From kdsjlk")
print val
if len(val) == 1 : print "Correct 2"
else : print "InCorrect 2"

val = re.findall("From","From dlkjd From kdsjlk")
print val
if len(val) == 2 : print "Correct 3"
else : print "InCorrect 3"

val = re.findall("[0-9]+/[0-9]+","1/2 1/3 3/4 1/8 fred 10/0")
print val
if len(val) == 5 : print "Correct 4"
else : print "InCorrect 4"

# Won't work because JS match does not deal with ()
# print re.findall("From .*@(\\S*)","From csev@umich.edu Sat 09:25:14")

# These return either None or a trivial MatchObject with no methods

val = re.search("From","dlkjdsljkdlkdsjlk")
if val is None: print "Correct 5"
else : print "InCorrect 5",val

val = re.search("From","dlkjd From kdsjlk")
if val is not None: print "Correct 6"
else : print "InCorrect 6",val

val = re.search("From","From dlkjd From kdsjlk")
if val is not None: print "Correct 7"
else : print "InCorrect 7",val

val = re.match("From","dlkjdsljkdlkdsjlk")
if val is None: print "Correct 8"
else : print "InCorrect 8",val

val = re.match("From","dlkjd From kdsjlk")
if val is None: print "Correct 9"
else : print "InCorrect 9",val

val = re.match("From","From dlkjd From kdsjlk")
if val is not None: print "Correct 10"
else : print "InCorrect 10",val

14 changes: 14 additions & 0 deletions test/run/t339.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[]
Correct 1
['From']
Correct 2
['From', 'From']
Correct 3
['1/2', '1/3', '3/4', '1/8', '10/0']
Correct 4
Correct 5
Correct 6
Correct 7
Correct 8
Correct 9
Correct 10
54 changes: 54 additions & 0 deletions test/run/t339.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: None
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: len
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: re
is_referenced: True
is_imported: True
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: val
is_referenced: True
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
Loading

0 comments on commit 034cdd5

Please sign in to comment.