1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-28 17:30:31 +00:00

Begin stl

This commit is contained in:
Calvin Rose 2017-03-08 17:34:25 -05:00
parent 18aaf9480b
commit ca0f8939ef
5 changed files with 98 additions and 4 deletions

1
gst.h
View File

@ -9,4 +9,3 @@
#include "value.h"
#endif // gst_h_INCLUDED

11
parse.c
View File

@ -437,6 +437,17 @@ int gst_parse_cstring(GstParser *p, const char *string) {
return bytesRead;
}
/* Parse a gst string */
int gst_parse_string(GstParser *p, uint8_t *string) {
uint32_t i;
p->status = GST_PARSER_PENDING;
for (i = 0; i < gst_string_length(string); ++i) {
if (p->status != GST_PARSER_PENDING) break;
dispatch_char(p, string[i]);
}
return i;
}
/* Parser initialization (memory allocation) */
void gst_parser(GstParser *p, Gst *vm) {
p->vm = vm;

View File

@ -30,7 +30,10 @@ struct GstParser {
/* Initialize a parser */
void gst_parser(GstParser *p, Gst *vm);
/* Parse a c style string. Returns true if successful */
/* Parse a c style string. Returns number of bytes read */
int gst_parse_cstring(GstParser *p, const char *string);
/* Parse a gst string. Returns number of bytes read */
int gst_parse_string(GstParser *p, uint8_t *string);
#endif /* end of include guard: PARSE_H_ONYWMADW */

75
stl.c Normal file
View File

@ -0,0 +1,75 @@
/* This implemets a standard library in gst. Some of this
* will eventually be ported over to gst if possible */
#include "gst.h"
/****/
/* Misc */
/****/
/* Print values for inspection */
int print(Gst *vm) {
uint32_t j, count;
count = gst_count_args(vm);
for (j = 0; j < count; ++j) {
string_put(stdout, gst_to_string(vm, gst_arg(vm, j)));
fputc('\n', stdout);
}
return GST_RETURN_OK;
}
/****/
/* Parsing */
/****/
/* Parse a source string into an AST */
int gst_stl_parse(Gst *vm) {
uint8_t *source = gst_to_string(vm, gst_arg(vm, 0));
GstParser p;
/* init state */
gst_parser(&p, vm);
/* Get and parse input until we have a full form */
gst_parse_string(&p, source);
if (p.status == GST_PARSER_PENDING) {
gst_c_throwc(vm, "incomplete source");
} else if (p.status == GST_PARSER_ERROR) {
gst_c_throwc(vm, p.error);
} else {
gst_c_return(vm, p.value);
}
}
/****/
/* Compiling */
/****/
/* Compile an ast */
int gst_stl_compile(Gst *vm) {
GstValue ast = gst_arg(vm, 0);
GstValue env = gst_arg(vm, 1);
GstValue ret;
GstCompiler c;
/* init state */
gst_compiler(&c, vm);
/* Check for environment variables */
if (env.type == GST_OBJECT) {
/* Iterate through environment, adding globals */
} else if (env.type != GST_NIL) {
gst_c_throwc(vm, "invalid type for environment");
}
/* Prepare return value */
ret.type = GST_FUNCTION;
ret.data.function = gst_compiler_compile(&c, ast);
/* Check for errors */
if (c.error != NULL) {
gst_c_return(vm, ret);
} else {
gst_c_throwc(vm, c.error);
}
}
/****/
/* IO */
/****/
/* TODO - add userdata to allow for manipulation of FILE pointers. */

10
util.h
View File

@ -49,10 +49,16 @@
/* C function helpers */
/* Return in a c function */
#define gst_c_return(vm, x) (do { (vm)->ret = (x); return GST_RETURN_OK; } while (0))
#define gst_c_return(vm, x) do { (vm)->ret = (x); return GST_RETURN_OK; } while (0)
/* Throw error from a c function */
#define gst_c_throw(vm, e) (do { (vm)->ret = (e); return GST_RETURN_ERROR; } while (0))
#define gst_c_throw(vm, e) do { (vm)->ret = (e); return GST_RETURN_ERROR; } while (0)
/* Throw c string error from a c function */
#define gst_c_throwc(vm, e) gst_c_throw((vm), gst_load_cstring((vm), (e)))
/* Assert from a c function */
#define gst_c_assert(vm, cond, e) do {if (cond) gst_c_throw((vm), (e)); } while (0)
/* What to do when out of memory */
#ifndef GST_OUT_OF_MEMORY