3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of Mes.
8 ;;; Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
23 ;;; libc.mes provides a minimal portable C library for mescc.
31 (mes-use-module (nyacc lang c99 parser))
32 (mes-use-module (mes libc-i386))))
35 (let* ((argc-argv (i386:_start))
36 (ast (with-input-from-string
37 (string-append "int _start () {int i;asm(\"" argc-argv "\");i=main ();exit (i);}")
42 (let* ((ast (with-input-from-string
45 strlen (char const* s)
57 (let* ((ast (with-input-from-string
64 int r = read (g_stdin, &c, 1);
65 //int r = read (0, &c, 1);
79 if (ungetc_char == -1)
81 int r = read (g_stdin, &c, 1);
88 //i = ungetc_buf[ungetc_char--];
89 i = ungetc_buf[ungetc_char];
91 ungetc_char = ungetc_char - 1;
109 (let* ((ast (with-input-from-string
112 assert_fail (char* s)
114 eputs (\"assert fail: \");
128 (let* ((ast (with-input-from-string
130 #define assert(x) ((x) ? (void)0 : assert_fail (#x))
132 ungetc (int c, int fd)
135 //assert (ungetc_char < 2);
136 assert (ungetc_char == -1 || ungetc_char < 2);
138 //ungetc_buf[++ungetc_char] = c;
140 ungetc_buf[ungetc_char] = c;
149 (let* ((ast (with-input-from-string
154 write (1, (char*)&c, 1);
163 (let* ((ast (with-input-from-string
168 write (fd, (char*)&c, 1);
177 (let* ((ast (with-input-from-string
180 eputs (char const* s)
192 (let* ((ast (with-input-from-string
195 fputs (char const* s, int fd)
207 (let* ((ast (with-input-from-string
222 (let* ((ast (with-input-from-string
225 strcmp (char const* a, char const* b)
227 while (*a && *b && *a == *b)
239 (let* ((ast (with-input-from-string
246 //static char itoa_buf[10];
260 *p-- = '0' + (x % 10);
275 (let* ((ast (with-input-from-string
280 //return (c>='0') && (c<='9');
281 if (c>='0' && c<='9') return 1;
290 (let* ((ast (with-input-from-string
292 //void *g_malloc_base = 0;
293 char *g_malloc_base = 0;
301 if (!g_malloc_base) g_malloc_base = p;
312 (let* ((ast (with-input-from-string
316 //realloc (void *p, int size)
317 realloc (int *p, int size)
319 brk (g_malloc_base + size);
320 return g_malloc_base;