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 if (ungetc_char == -1)
69 int r = read (g_stdin, &c, 1);
76 //i = ungetc_buf[ungetc_char--];
77 i = ungetc_buf[ungetc_char];
79 ungetc_char = ungetc_char - 1;
91 (let* ((ast (with-input-from-string
96 eputs (\"assert fail: \");
110 (let* ((ast (with-input-from-string
112 //#define assert(x) ((x) ? (void)0 : assert_fail (#x))
114 ungetc (int c, int fd)
117 //assert (ungetc_char < 2);
118 //assert (ungetc_char == -1 || ungetc_char < 2);
120 //ungetc_buf[++ungetc_char] = c;
122 ungetc_buf[ungetc_char] = c;
131 (let* ((ast (with-input-from-string
136 write (1, (char*)&c, 1);
145 (let* ((ast (with-input-from-string
150 write (fd, (char*)&c, 1);
159 (let* ((ast (with-input-from-string
162 eputs (char const* s)
174 (let* ((ast (with-input-from-string
177 fputs (char const* s, int fd)
189 (let* ((ast (with-input-from-string
204 (let* ((ast (with-input-from-string
207 strcmp (char const* a, char const* b)
209 while (*a && *b && *a == *b)
221 (let* ((ast (with-input-from-string
228 //static char itoa_buf[10];
242 *p-- = '0' + (x % 10);
257 (let* ((ast (with-input-from-string
262 //return (c>='0') && (c<='9');
263 if (c>='0' && c<='9') return 1;
272 (let* ((ast (with-input-from-string
274 //void *g_malloc_base = 0;
275 char *g_malloc_base = 0;
284 if (!g_malloc_base) g_malloc_base = p;
294 (let* ((ast (with-input-from-string
298 //realloc (void *p, int size)
299 realloc (int *p, int size)
301 brk (g_malloc_base + size);
302 return g_malloc_base;