From 707c3a31cd6ab01751731167de24c5a805001378 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Mon, 17 Apr 2017 23:29:54 +0200 Subject: [PATCH] mescc: Add atoi. * libc/mlibc.c (atoi): New function. * module/mes/libc.mes (atoi): New function. (libc): Add it. --- libc/mlibc.c | 19 +++++++++++++++++++ module/mes/libc.mes | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/libc/mlibc.c b/libc/mlibc.c index 8688d7fd..98bcd9dd 100644 --- a/libc/mlibc.c +++ b/libc/mlibc.c @@ -312,6 +312,25 @@ isdigit (int c) { return (c>='0') && (c<='9'); } + +int +atoi (char const *s) +{ + int i = 0; + int sign = 1; + if (*s && *s == '-') + { + sign = -1; + s++; + } + while (isdigit (*s)) + { + i *= 10; + i += (*s - '0'); + s++; + } + return i * sign; +} #endif char itoa_buf[10]; diff --git a/module/mes/libc.mes b/module/mes/libc.mes index 993699fa..31607f8e 100644 --- a/module/mes/libc.mes +++ b/module/mes/libc.mes @@ -285,6 +285,32 @@ isdigit (char c) parse-c99))) ast)) +(define atoi + (let* ((ast (with-input-from-string + " +int +atoi (char const *s) +{ + int i = 0; + int sign = 1; + if (*s && *s == '-') + { + sign = -1; + s++; + } + while (isdigit (*s)) + { + i *= 10; + i += (*s - '0'); + s++; + } + return i * sign; +} +" +;;paredit:" + parse-c99))) + ast)) + (define malloc (let* ((ast (with-input-from-string " @@ -374,6 +400,7 @@ getenv (char const* s) strcmp itoa isdigit + atoi malloc realloc strncmp