Skip to content

Commit

Permalink
New
Browse files Browse the repository at this point in the history
Add builtins -- all and any along with test cases.
  • Loading branch information
bnmnetp committed Feb 6, 2013
1 parent 711619d commit 6c609de
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ doc/static/skulpt.js

*.pyc
.DS_Store

/.d8_history
7 changes: 5 additions & 2 deletions dist/skulpt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,36 @@ Sk.builtin.jsmillis = function jsmillis()
return now.valueOf();
};

Sk.builtin.all = function all()
{
if (arguments.length == 0) {
throw new Sk.builtin.TypeError("all() takes exactly one argument (0 given)")
}
arguments = Sk.misceval.arrayFromArguments(arguments);
var lowest = arguments[0];
for (var i = 0; i < arguments.length; ++i)
{
if (! Sk.misceval.isTrue(arguments[i]))
return false;
}
return true;
};

Sk.builtin.any = function any()
{
if (arguments.length == 0) {
throw new Sk.builtin.TypeError("any() takes exactly one argument (0 given)")
}
arguments = Sk.misceval.arrayFromArguments(arguments);
var lowest = arguments[0];
for (var i = 0; i < arguments.length; ++i)
{
if (Sk.misceval.isTrue(arguments[i]))
return true;
}
return false;
};

/*
Sk.builtinFiles = {};
Sk.builtin.read = function read(x) {
Expand Down
2 changes: 2 additions & 0 deletions src/builtindict.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Sk.builtins = {
'input': Sk.builtin.input,
'raw_input': Sk.builtin.raw_input,
/*'read': Sk.builtin.read,*/
'all': Sk.builtin.all,
'any': Sk.builtin.any,
'jseval': Sk.builtin.jseval,
'jsmillis': Sk.builtin.jsmillis,
'long_div_mode': Sk.builtin.lng.longDivideMode
Expand Down
10 changes: 10 additions & 0 deletions test/run/t341.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
print all([1,2,3])
print all([1,2,0])
print all("abc")
print all([False, False, False])
print all([True, True, True])
print all(range(10))
print all(range(1,11))
print all(('a','b','c'))
print all(('a','','c'))

9 changes: 9 additions & 0 deletions test/run/t341.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
True
False
True
False
True
False
True
True
False
54 changes: 54 additions & 0 deletions test/run/t341.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: False
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: 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: all
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: range
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: [
]
107 changes: 107 additions & 0 deletions test/run/t341.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
Module(body=[Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[List(elts=[Num(n=1),
Num(n=2),
Num(n=3)],
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[List(elts=[Num(n=1),
Num(n=2),
Num(n=0)],
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[Str(s='abc')],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[List(elts=[Name(id='False',
ctx=Load()),
Name(id='False',
ctx=Load()),
Name(id='False',
ctx=Load())],
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[List(elts=[Name(id='True',
ctx=Load()),
Name(id='True',
ctx=Load()),
Name(id='True',
ctx=Load())],
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[Call(func=Name(id='range',
ctx=Load()),
args=[Num(n=10)],
keywords=[],
starargs=None,
kwargs=None)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[Call(func=Name(id='range',
ctx=Load()),
args=[Num(n=1),
Num(n=11)],
keywords=[],
starargs=None,
kwargs=None)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[Tuple(elts=[Str(s='a'),
Str(s='b'),
Str(s='c')],
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='all',
ctx=Load()),
args=[Tuple(elts=[Str(s='a'),
Str(s=''),
Str(s='c')],
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)],
nl=True)])
9 changes: 9 additions & 0 deletions test/run/t342.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
print any([1,2,3])
print any([1,2,0])
print any("abc")
print any([False, False, False])
print any([True, True, True])
print any(range(10))
print any(range(1,11))
print any(('a','b','c'))
print any(('a','','c'))
9 changes: 9 additions & 0 deletions test/run/t342.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
True
True
True
False
True
True
True
True
True
54 changes: 54 additions & 0 deletions test/run/t342.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: False
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: 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: any
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: range
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: [
]
Loading

0 comments on commit 6c609de

Please sign in to comment.