Browse Source

Put unicode functions in unicode.c, and tidy their names.

pull/12/head
Damien George 11 years ago
parent
commit
8cc96a35e5
  1. 14
      py/lexer.c
  2. 23
      py/misc.h
  3. 2
      py/parse.c
  4. 2
      py/repl.c
  5. 10
      py/runtime.c
  6. 19
      py/unicode.c
  7. 2
      stm/Makefile
  8. 2
      unix-cpy/Makefile
  9. 2
      unix/Makefile

14
py/lexer.c

@ -54,9 +54,9 @@ void mp_token_show(const mp_token_t *tok) {
const char *j = i + tok->len; const char *j = i + tok->len;
printf(" "); printf(" ");
while (i < j) { while (i < j) {
unichar c = g_utf8_get_char(i); unichar c = utf8_get_char(i);
i = g_utf8_next_char(i); i = utf8_next_char(i);
if (g_unichar_isprint(c)) { if (unichar_isprint(c)) {
printf("%c", c); printf("%c", c);
} else { } else {
printf("?"); printf("?");
@ -116,19 +116,19 @@ static bool is_char_and(mp_lexer_t *lex, char c1, char c2) {
} }
static bool is_whitespace(mp_lexer_t *lex) { static bool is_whitespace(mp_lexer_t *lex) {
return g_unichar_isspace(lex->chr0); return unichar_isspace(lex->chr0);
} }
static bool is_letter(mp_lexer_t *lex) { static bool is_letter(mp_lexer_t *lex) {
return g_unichar_isalpha(lex->chr0); return unichar_isalpha(lex->chr0);
} }
static bool is_digit(mp_lexer_t *lex) { static bool is_digit(mp_lexer_t *lex) {
return g_unichar_isdigit(lex->chr0); return unichar_isdigit(lex->chr0);
} }
static bool is_following_digit(mp_lexer_t *lex) { static bool is_following_digit(mp_lexer_t *lex) {
return g_unichar_isdigit(lex->chr1); return unichar_isdigit(lex->chr1);
} }
// TODO UNICODE include unicode characters in definition of identifiers // TODO UNICODE include unicode characters in definition of identifiers

23
py/misc.h

@ -37,24 +37,13 @@ int m_get_total_bytes_allocated(void);
typedef int unichar; // TODO typedef int unichar; // TODO
unichar g_utf8_get_char(const char *s); unichar utf8_get_char(const char *s);
char *g_utf8_next_char(const char *s); char *utf8_next_char(const char *s);
bool g_unichar_isspace(unichar c); bool unichar_isspace(unichar c);
bool g_unichar_isalpha(unichar c); bool unichar_isalpha(unichar c);
bool g_unichar_isprint(unichar c); bool unichar_isprint(unichar c);
bool g_unichar_isdigit(unichar c); bool unichar_isdigit(unichar c);
//char *g_strdup(const char *s);
/** blob ********************************************************/
/*
unsigned short decode_le16(byte *buf);
unsigned int decode_le32(byte *buf);
void encode_le16(byte *buf, unsigned short i);
void encode_le32(byte *buf, unsigned int i);
*/
/** string ******************************************************/ /** string ******************************************************/

2
py/parse.c

@ -212,7 +212,7 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
} }
} }
for (; i < len; i++) { for (; i < len; i++) {
if (g_unichar_isdigit(str[i]) && str[i] - '0' < base) { if (unichar_isdigit(str[i]) && str[i] - '0' < base) {
int_val = base * int_val + str[i] - '0'; int_val = base * int_val + str[i] - '0';
} else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') { } else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') {
int_val = base * int_val + str[i] - 'a' + 10; int_val = base * int_val + str[i] - 'a' + 10;

2
py/repl.c

@ -8,7 +8,7 @@ bool str_startswith_word(const char *str, const char *head) {
return false; return false;
} }
} }
return head[i] == '\0' && (str[i] == '\0' || !g_unichar_isalpha(str[i])); return head[i] == '\0' && (str[i] == '\0' || !unichar_isalpha(str[i]));
} }
bool mp_repl_is_compound_stmt(const char *line) { bool mp_repl_is_compound_stmt(const char *line) {

10
py/runtime.c

@ -226,7 +226,7 @@ void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args)
unique_codes[unique_code_id].is_generator = false; unique_codes[unique_code_id].is_generator = false;
unique_codes[unique_code_id].u_native.fun = fun; unique_codes[unique_code_id].u_native.fun = fun;
printf("native code: %d bytes\n", len); //printf("native code: %d bytes\n", len);
#ifdef DEBUG_PRINT #ifdef DEBUG_PRINT
DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args); DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
@ -421,8 +421,7 @@ mp_obj_t rt_load_build_class(void) {
DEBUG_OP_printf("load_build_class\n"); DEBUG_OP_printf("load_build_class\n");
mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, rt_q___build_class__, false); mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, rt_q___build_class__, false);
if (elem == NULL) { if (elem == NULL) {
printf("name doesn't exist: __build_class__\n"); nlr_jump(mp_obj_new_exception_msg(rt_q_NameError, "name '__build_class__' is not defined"));
assert(0);
} }
return elem->value; return elem->value;
} }
@ -525,7 +524,7 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
break; break;
} }
default: printf("%d\n", op); assert(0); default: assert(0);
} }
if (fit_small_int(lhs_val)) { if (fit_small_int(lhs_val)) {
return MP_OBJ_NEW_SMALL_INT(lhs_val); return MP_OBJ_NEW_SMALL_INT(lhs_val);
@ -831,8 +830,7 @@ void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
} else if (MP_OBJ_IS_TYPE(base, &instance_type)) { } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
mp_obj_instance_store_attr(base, attr, value); mp_obj_instance_store_attr(base, attr, value);
} else { } else {
printf("?AttributeError: '%s' object has no attribute '%s'\n", mp_obj_get_type_str(base), qstr_str(attr)); nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
assert(0);
} }
} }

19
py/misc.c → py/unicode.c

@ -1,5 +1,4 @@
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "misc.h" #include "misc.h"
@ -39,27 +38,27 @@ static const uint8_t attr[] = {
AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0 AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0
}; };
unichar g_utf8_get_char(const char *s) { unichar utf8_get_char(const char *s) {
return *s; return *s;
} }
char *g_utf8_next_char(const char *s) { char *utf8_next_char(const char *s) {
return (char*)(s + 1); return (char*)(s + 1);
} }
bool g_unichar_isspace(unichar c) { bool unichar_isspace(unichar c) {
return c < 128 && (attr[c] & FL_SPACE) != 0; return c < 128 && (attr[c] & FL_SPACE) != 0;
} }
bool g_unichar_isalpha(unichar c) { bool unichar_isalpha(unichar c) {
return c < 128 && (attr[c] & FL_ALPHA) != 0; return c < 128 && (attr[c] & FL_ALPHA) != 0;
} }
bool g_unichar_isprint(unichar c) { bool unichar_isprint(unichar c) {
return c < 128 && (attr[c] & FL_PRINT) != 0; return c < 128 && (attr[c] & FL_PRINT) != 0;
} }
bool g_unichar_isdigit(unichar c) { bool unichar_isdigit(unichar c) {
return c < 128 && (attr[c] & FL_DIGIT) != 0; return c < 128 && (attr[c] & FL_DIGIT) != 0;
} }
@ -76,9 +75,3 @@ bool char_is_lower(unichar c) {
return c < 128 && (attr[c] & FL_LOWER) != 0; return c < 128 && (attr[c] & FL_LOWER) != 0;
} }
*/ */
/*
char *g_strdup(const char *s) {
return strdup(s);
}
*/

2
stm/Makefile

@ -45,7 +45,7 @@ PY_O = \
malloc.o \ malloc.o \
qstr.o \ qstr.o \
vstr.o \ vstr.o \
misc.o \ unicode.o \
lexer.o \ lexer.o \
parse.o \ parse.o \
scope.o \ scope.o \

2
unix-cpy/Makefile

@ -14,7 +14,7 @@ PY_O = \
malloc.o \ malloc.o \
qstr.o \ qstr.o \
vstr.o \ vstr.o \
misc.o \ unicode.o \
lexer.o \ lexer.o \
lexerunix.o \ lexerunix.o \
parse.o \ parse.o \

2
unix/Makefile

@ -15,7 +15,7 @@ PY_O = \
malloc.o \ malloc.o \
qstr.o \ qstr.o \
vstr.o \ vstr.o \
misc.o \ unicode.o \
lexer.o \ lexer.o \
lexerunix.o \ lexerunix.o \
parse.o \ parse.o \

Loading…
Cancel
Save