1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of Mes.
7 * Mes is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
12 * Mes is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
24 void *malloc (size_t i);
25 int open (char const *s, int mode);
26 int read (int fd, void* buf, size_t n);
27 void write (int fd, char const* s, int n);
29 #define INT_MIN -2147483648
30 #define INT_MAX 2147483647
39 : // no outputs "=" (r)
47 getenv (char const* p)
53 read (int fd, void* buf, size_t n)
56 //syscall (SYS_write, fd, s, n));
67 : "" (fd), "" (buf), "" (n)
68 : "eax", "ebx", "ecx", "edx"
74 open (char const *s, int mode)
77 //syscall (SYS_open, mode));
91 int puts (char const*);
92 char const* itoa (int);
95 write (int fd, char const* s, int n)
98 //syscall (SYS_write, fd, s, n));
104 "mov $0x4, %%eax\n\t"
106 : // no outputs "=" (r)
107 : "" (fd), "" (s), "" (n)
108 : "eax", "ebx", "ecx", "edx"
119 "mov $0x2d,%%eax\n\t"
133 //write (STDOUT, s, strlen (s));
134 //int i = write (STDOUT, s, strlen (s));
135 write (1, (char*)&c, 1);
139 void *g_malloc_base = 0;
145 if (!g_malloc_base) g_malloc_base = p;
151 realloc (void *p, size_t size)
154 brk (g_malloc_base + size);
155 return g_malloc_base;
162 //munmap ((void*)p, *n);
171 strlen (char const* s)
179 strcmp (char const* a, char const* b)
181 while (*a && *b && *a == *b) {a++;b++;}
186 eputs (char const* s)
188 //int i = write (STDERR, s, strlen (s));
195 fputs (char const* s, int fd)
197 //int i = write (fd, s, strlen (s));
206 //int i = write (STDOUT, s, strlen (s));
213 assert_fail (char* s)
215 eputs ("assert fail: ");
221 #define assert(x) ((x) ? (void)0 : assert_fail (#x))
224 int ungetc_char = -1;
232 if (ungetc_char == -1)
234 int r = read (g_stdin, &c, 1);
235 if (r < 1) return -1;
239 i = ungetc_buf[ungetc_char--];
246 ungetc (int c, int fd)
248 assert (ungetc_char < 2);
249 ungetc_buf[++ungetc_char] = c;
258 //static char itoa_buf[10];
272 *p-- = '0' + (x % 10);
285 return (c>='0') && (c<='9');