Skip to content

Commit

Permalink
Add hex, oct, and bin builtin functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnmnetp committed Mar 12, 2013
1 parent 5c945a5 commit 397608d
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 6 deletions.
9 changes: 5 additions & 4 deletions dist/skulpt.js

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

31 changes: 29 additions & 2 deletions src/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Sk.builtin.len = function(item)
{
if (item.sq$length)
return item.sq$length();

if (item.mp$length)
return item.mp$length();

Expand Down Expand Up @@ -56,7 +56,7 @@ Sk.builtin.sum = function sum(iter,start)
if (iter instanceof Sk.builtin.list) {
iter = iter.v;
} else {
throw "TypeError: an iterable is required";
throw "TypeError: an iterable is required";
}
if (start === undefined ) {
start = 0;
Expand Down Expand Up @@ -109,6 +109,33 @@ Sk.builtin.chr = function chr(x)
return new Sk.builtin.str(String.fromCharCode(x));
};

Sk.builtin.hex = function hex(x)
{
if (typeof x !== "number")
{
throw "TypeError: an integer is required";
}
return new Sk.builtin.str('0x'+x.toString(16));
};

Sk.builtin.oct = function oct(x)
{
if (typeof x !== "number")
{
throw "TypeError: an integer is required";
}
return new Sk.builtin.str('0'+x.toString(8));
};

Sk.builtin.bin = function bin(x)
{
if (typeof x !== "number")
{
throw "TypeError: an integer is required";
}
return new Sk.builtin.str('0b'+x.toString(2));
};

Sk.builtin.dir = function dir(x)
{
var names = [];
Expand Down
3 changes: 3 additions & 0 deletions src/builtindict.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Sk.builtins = {
'round': Sk.builtin.round,
'ord': Sk.builtin.ord,
'chr': Sk.builtin.chr,
'hex': Sk.builtin.hex,
'oct': Sk.builtin.oct,
'bin': Sk.builtin.bin,
'dir': Sk.builtin.dir,
'repr': Sk.builtin.repr,
'open': Sk.builtin.open,
Expand Down
8 changes: 8 additions & 0 deletions test/run/t344.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
for ch in "Hello world!":
d = ord(ch)
h = hex(d)
o = oct(d)
b = bin(d)

print ch, d, h, o, b

12 changes: 12 additions & 0 deletions test/run/t344.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
H 72 0x48 0110 0b1001000
e 101 0x65 0145 0b1100101
l 108 0x6c 0154 0b1101100
l 108 0x6c 0154 0b1101100
o 111 0x6f 0157 0b1101111
32 0x20 040 0b100000
w 119 0x77 0167 0b1110111
o 111 0x6f 0157 0b1101111
r 114 0x72 0162 0b1110010
l 108 0x6c 0154 0b1101100
d 100 0x64 0144 0b1100100
! 33 0x21 041 0b100001
114 changes: 114 additions & 0 deletions test/run/t344.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: b
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: [
]
name: bin
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: ch
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: [
]
name: d
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: [
]
name: h
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: [
]
name: hex
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: o
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: [
]
name: oct
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: ord
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: [
]
52 changes: 52 additions & 0 deletions test/run/t344.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Module(body=[For(target=Name(id='ch',
ctx=Store()),
iter=Str(s='Hello world!'),
body=[Assign(targets=[Name(id='d',
ctx=Store())],
value=Call(func=Name(id='ord',
ctx=Load()),
args=[Name(id='ch',
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)),
Assign(targets=[Name(id='h',
ctx=Store())],
value=Call(func=Name(id='hex',
ctx=Load()),
args=[Name(id='d',
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)),
Assign(targets=[Name(id='o',
ctx=Store())],
value=Call(func=Name(id='oct',
ctx=Load()),
args=[Name(id='d',
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)),
Assign(targets=[Name(id='b',
ctx=Store())],
value=Call(func=Name(id='bin',
ctx=Load()),
args=[Name(id='d',
ctx=Load())],
keywords=[],
starargs=None,
kwargs=None)),
Print(dest=None,
values=[Name(id='ch',
ctx=Load()),
Name(id='d',
ctx=Load()),
Name(id='h',
ctx=Load()),
Name(id='o',
ctx=Load()),
Name(id='b',
ctx=Load())],
nl=True)],
orelse=[])])

0 comments on commit 397608d

Please sign in to comment.