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
67 int r = read (g_stdin, &c, 1);
68 //int r = read (0, &c, 1);
79 if (ungetc_char == -1)
81 int r = read (g_stdin, &c, 1);
99 (let* ((ast (with-input-from-string
102 assert_fail (char* s)
104 eputs (\"assert fail: \");
118 (let* ((ast (with-input-from-string
120 #define assert(x) ((x) ? (void)0 : assert_fail (#x))
122 ungetc (int c, int fd)
124 assert (ungetc_char == -1);
134 (let* ((ast (with-input-from-string
139 //write (STDOUT, s, strlen (s));
140 //int i = write (STDOUT, s, strlen (s));
141 write (1, (char*)&c, 1);
150 (let* ((ast (with-input-from-string
153 eputs (char const* s)
155 //write (STDERR, s, strlen (s));
156 //write (2, s, strlen (s));
167 (let* ((ast (with-input-from-string
170 fputs (char const* s, int fd)
182 (let* ((ast (with-input-from-string
187 //write (STDOUT, s, strlen (s));
188 //int i = write (STDOUT, s, strlen (s));
199 (let* ((ast (with-input-from-string
202 strcmp (char const* a, char const* b)
204 while (*a && *b && *a == *b)
216 (let* ((ast (with-input-from-string
223 //static char itoa_buf[10];
237 *p-- = '0' + (x % 10);
252 (let* ((ast (with-input-from-string
257 //return (c>='0') && (c<='9');
258 if (c>='0' && c<='9') return 1;
267 (let* ((ast (with-input-from-string
269 //void *g_malloc_base = 0;
270 char *g_malloc_base = 0;
278 if (!g_malloc_base) g_malloc_base = p;
289 (let* ((ast (with-input-from-string
293 //realloc (void *p, int size)
294 realloc (int *p, int size)
296 brk (g_malloc_base + size);
297 return g_malloc_base;