Merge branch 'master' into builtins

This commit is contained in:
Windel Bouwman
2018-10-16 21:51:42 +02:00
committed by GitHub
3 changed files with 55 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
# RustPython
A Python Interpreter written in Rust :snake: :scream: :metal:.
A Python-3 (CPython >= 3.5.0) Interpreter written in Rust :snake: :scream: :metal:.
[![Build Status](https://travis-ci.org/RustPython/RustPython.svg?branch=master)](https://travis-ci.org/RustPython/RustPython)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
@@ -26,16 +26,16 @@ Or use the interactive shell:
# Goals
- Full python environment entirely in Rust (not CPython bindings)
- Full Python-3 environment entirely in Rust (not CPython bindings)
- A clean implementation without compatibility hacks
# Code organization
- `parser`: python lexing, parsing and ast
- `vm`: python virtual machine
- `src`: using the other subcrates to bring rustpython to life.
- `docs`: documentation (work in progress)
- `py_code_object`: CPython bytecode to rustpython bytecode convertor (work in progress)
- `parser`: python lexing, parsing and ast
- `vm`: python virtual machine
- `src`: using the other subcrates to bring rustpython to life.
- `docs`: documentation (work in progress)
- `py_code_object`: CPython bytecode to rustpython bytecode convertor (work in progress)
- `tests`: integration test snippets
# Contributing

View File

@@ -0,0 +1,23 @@
assert ord("a") == 97
assert ord("é") == 233
assert ord("🤡") == 129313
try:
ord()
except TypeError:
pass
else:
assert False, "TypeError not raised when ord() is called with no argument"
try:
ord("")
except TypeError:
pass
else:
assert False, "TypeError not raised when ord() is called with an empty string"
try:
ord("ab")
except TypeError:
pass
else:
assert False, "TypeError not raised when ord() is called with more than one character"

View File

@@ -350,7 +350,27 @@ fn builtin_next(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// builtin_object
// builtin_oct
// builtin_open
// builtin_ord
fn builtin_ord(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(string, Some(vm.ctx.str_type()))]);
let string = objstr::get_value(string);
let string_len = string.chars().count();
if string_len > 1 {
return Err(vm.new_type_error(
format!(
"ord() expected a character, but string of length {} found",
string_len
)
.to_string(),
));
}
match string.chars().next() {
Some(character) => Ok(vm.context().new_int(character as i32)),
None => Err(vm.new_type_error(
"ord() could not guess the integer representing this character".to_string(),
)),
}
}
fn builtin_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
@@ -481,8 +501,12 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
dict.insert(String::from("list"), ctx.list_type());
dict.insert(String::from("locals"), ctx.new_rustfunc(builtin_locals));
dict.insert(String::from("map"), ctx.new_rustfunc(builtin_map));
dict.insert(String::from("max"), ctx.new_rustfunc(builtin_max));
dict.insert(String::from("min"), ctx.new_rustfunc(builtin_min));
dict.insert(String::from("ord"), ctx.new_rustfunc(builtin_ord));
dict.insert(String::from("next"), ctx.new_rustfunc(builtin_next));
dict.insert(String::from("pow"), ctx.new_rustfunc(builtin_pow));
dict.insert(String::from("print"), ctx.new_rustfunc(builtin_print));