mirror of
https://github.com/janet-lang/janet
synced 2024-11-14 20:54:48 +00:00
3efd400025
changing C API model. This commit is not i a working state.
83 lines
2.4 KiB
C
83 lines
2.4 KiB
C
/*
|
|
* Copyright (c) 2017 Calvin Rose
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to
|
|
* deal in the Software without restriction, including without limitation the
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
* IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <dst/dst.h>
|
|
#include "bootstrap.h"
|
|
|
|
|
|
/* Simple read line functionality */
|
|
static char *dst_getline() {
|
|
char *line = malloc(100);
|
|
char *linep = line;
|
|
size_t lenmax = 100;
|
|
size_t len = lenmax;
|
|
int c;
|
|
if (line == NULL)
|
|
return NULL;
|
|
for (;;) {
|
|
c = fgetc(stdin);
|
|
if (c == EOF)
|
|
break;
|
|
if (--len == 0) {
|
|
len = lenmax;
|
|
char *linen = realloc(linep, lenmax *= 2);
|
|
if (linen == NULL) {
|
|
free(linep);
|
|
return NULL;
|
|
}
|
|
line = linen + (line - linep);
|
|
linep = linen;
|
|
}
|
|
if ((*line++ = c) == '\n')
|
|
break;
|
|
}
|
|
*line = '\0';
|
|
return linep;
|
|
}
|
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
Dst *vm = dst_init();
|
|
for (;;) {
|
|
char *line = dst_getline();
|
|
if (line) {
|
|
uint32_t len = 0;
|
|
int status = dst_parsec(vm, 0, line);
|
|
if (status == PARSE_OK) {
|
|
dst_description(vm, 0, 0);
|
|
}
|
|
const uint8_t *b = dst_bytes(vm, 0, &len);
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
putc(b[i]);
|
|
}
|
|
putc('\n');
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
dst_deinit(vm);
|
|
|
|
return 0;
|
|
}
|