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;
102 (let* ((ast (with-input-from-string
105 assert_fail (char* s)
107 eputs (\"assert fail: \");
121 (let* ((ast (with-input-from-string
123 #define assert(x) ((x) ? (void)0 : assert_fail (#x))
125 ungetc (int c, int fd)
128 //assert (ungetc_char < 2);
129 assert (ungetc_char == -1 || ungetc_char < 2);
131 //ungetc_buf[++ungetc_char] = c;
133 ungetc_buf[ungetc_char] = c;
142 (let* ((ast (with-input-from-string
147 //write (STDOUT, s, strlen (s));
148 //int i = write (STDOUT, s, strlen (s));
149 write (1, (char*)&c, 1);
158 (let* ((ast (with-input-from-string
161 eputs (char const* s)
163 //write (STDERR, s, strlen (s));
164 //write (2, s, strlen (s));
175 (let* ((ast (with-input-from-string
178 fputs (char const* s, int fd)
190 (let* ((ast (with-input-from-string
195 //write (STDOUT, s, strlen (s));
196 //int i = write (STDOUT, s, strlen (s));
207 (let* ((ast (with-input-from-string
210 strcmp (char const* a, char const* b)
212 while (*a && *b && *a == *b)
224 (let* ((ast (with-input-from-string
231 //static char itoa_buf[10];
245 *p-- = '0' + (x % 10);
260 (let* ((ast (with-input-from-string
265 //return (c>='0') && (c<='9');
266 if (c>='0' && c<='9') return 1;
275 (let* ((ast (with-input-from-string
277 //void *g_malloc_base = 0;
278 char *g_malloc_base = 0;
286 if (!g_malloc_base) g_malloc_base = p;
297 (let* ((ast (with-input-from-string
301 //realloc (void *p, int size)
302 realloc (int *p, int size)
304 brk (g_malloc_base + size);
305 return g_malloc_base;