1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-30 15:13:03 +00:00

Remove lots of headers. Add parse c function.

This commit is contained in:
Calvin Rose
2017-06-24 14:27:29 -04:00
parent 70478a410b
commit c3d65cb91d
12 changed files with 304 additions and 679 deletions

View File

@@ -1,59 +0,0 @@
/*
* 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.
*/
#ifndef COMPILE_H_9VXF71HY
#define COMPILE_H_9VXF71HY
#include <gst/gst.h>
#include <setjmp.h>
typedef struct GstCompiler GstCompiler;
typedef struct GstScope GstScope;
/* Compilation state */
struct GstCompiler {
Gst *vm;
GstValue error;
jmp_buf onError;
GstScope *tail;
GstBuffer *buffer;
};
/* Initialize the Compiler */
void gst_compiler(GstCompiler *c, Gst *vm);
/* Add many globals */
void gst_compiler_globals(GstCompiler *c, GstValue env);
/* Register a global for the compilation environment. */
void gst_compiler_global(GstCompiler *c, const char *name, GstValue x);
/* Use a module */
void gst_compiler_usemodule(GstCompiler *c, const char *modulename);
/* Compile a function that evaluates the given form. */
GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form);
/* Load the library */
void gst_compile_load(Gst *vm);
#endif /* end of include guard: COMPILE_H_9VXF71HY */

View File

@@ -1,39 +0,0 @@
/*
* 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.
*/
#ifndef disasm_h_INCLUDED
#define disasm_h_INCLUDED
#include <gst/gst.h>
#include <stdio.h>
/* Print disassembly for a given funciton */
void gst_dasm(FILE * out, uint16_t * byteCode, uint32_t len);
/* Print the disassembly for a function definition */
void gst_dasm_funcdef(FILE * out, GstFuncDef * def);
/* Print the disassembly for a function */
void gst_dasm_function(FILE * out, GstFunction * f);
#endif // disasm_h_INCLUDED

View File

@@ -25,6 +25,7 @@
#include <stdint.h>
#include <stdarg.h>
#include <setjmp.h>
/* String utils */
#define gst_string_raw(s) ((uint32_t *)(s) - 2)
@@ -161,6 +162,10 @@ typedef struct GstFuncEnv GstFuncEnv;
typedef union GstValueUnion GstValueUnion;
typedef struct GstModuleItem GstModuleItem;
typedef struct GstUserType GstUserType;
typedef struct GstParser GstParser;
typedef struct GstParseState GstParseState;
typedef struct GstCompiler GstCompiler;
typedef struct GstScope GstScope;
/* C Api data types */
struct GstModuleItem {
@@ -312,6 +317,62 @@ struct Gst {
GstValue ret; /* Returned value from gst_start. */
};
/* The type of a ParseState */
typedef enum ParseType {
PTYPE_FORM,
PTYPE_STRING,
PTYPE_TOKEN
} ParseType;
/* Contain a parse state that goes on the parse stack */
struct GstParseState {
ParseType type;
union {
struct {
uint8_t endDelimiter;
GstArray *array;
} form;
struct {
GstBuffer *buffer;
uint32_t count;
uint32_t accum;
enum {
STRING_STATE_BASE,
STRING_STATE_ESCAPE,
STRING_STATE_ESCAPE_UNICODE,
STRING_STATE_ESCAPE_HEX
} state;
} string;
} buf;
};
/* Holds the parsing state */
struct GstParser {
Gst *vm;
const char *error;
GstParseState *data;
GstValue value;
uint32_t count;
uint32_t cap;
uint32_t index;
uint32_t quoteCount;
enum {
GST_PARSER_PENDING = 0,
GST_PARSER_FULL,
GST_PARSER_ERROR,
GST_PARSER_ROOT
} status;
};
/* Compilation state */
struct GstCompiler {
Gst *vm;
GstValue error;
jmp_buf onError;
GstScope *tail;
GstBuffer *buffer;
};
/* Bytecode */
enum GstOpCode {
GST_OP_FLS, /* Load false */
@@ -456,6 +517,28 @@ const char *gst_deserialize(
const char *gst_serialize(Gst *vm, GstBuffer *buffer, GstValue x);
/***/
/* Parsing */
/***/
void gst_parser(GstParser *p, Gst *vm);
int gst_parse_cstring(GstParser *p, const char *string);
int gst_parse_string(GstParser *p, const uint8_t *string);
void gst_parse_byte(GstParser *p, uint8_t byte);
int gst_parse_hasvalue(GstParser *p);
GstValue gst_parse_consume(GstParser *p);
/***/
/* Compilation */
/***/
void gst_compiler(GstCompiler *c, Gst *vm);
void gst_compiler_nilglobals(GstCompiler *c, GstValue env);
void gst_compiler_globals(GstCompiler *c, GstValue env);
void gst_compiler_global(GstCompiler *c, const char *name, GstValue x);
void gst_compiler_usemodule(GstCompiler *c, const char *modulename);
GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form);
/****/
/* GC */
/****/
@@ -488,6 +571,12 @@ GstValue gst_arg(Gst *vm, uint32_t index);
void gst_set_arg(Gst *vm, uint32_t index, GstValue x);
uint32_t gst_count_args(Gst *vm);
/***/
/* Stl */
/***/
void gst_stl_load(Gst *vm);
/****/
/* C Api */
/****/

View File

@@ -1,72 +0,0 @@
/*
* 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.
*/
#ifndef PARSE_H_ONYWMADW
#define PARSE_H_ONYWMADW
#include <gst/gst.h>
typedef struct GstParser GstParser;
typedef struct GstParseState GstParseState;
/* Holds the parsing state */
struct GstParser {
Gst *vm;
const char *error;
GstParseState *data;
GstValue value;
uint32_t count;
uint32_t cap;
uint32_t index;
uint32_t quoteCount;
enum {
GST_PARSER_PENDING = 0,
GST_PARSER_FULL,
GST_PARSER_ERROR,
GST_PARSER_ROOT
} status;
};
/* Initialize a parser */
void gst_parser(GstParser *p, Gst *vm);
/* 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, const uint8_t *string);
/* Parse a single byte */
void gst_parse_byte(GstParser *p, uint8_t byte);
/* Check if a parser has a value that needs to be handled. If
* so, the parser will not parse any more input until that value
* is consumed. */
int gst_parse_hasvalue(GstParser *p);
/* Gets a value from the parser */
GstValue gst_parse_consume(GstParser *p);
/* Load the parsing library */
void gst_parse_load(Gst *vm);
#endif /* end of include guard: PARSE_H_ONYWMADW */

View File

@@ -1,32 +0,0 @@
/*
* 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.
*/
#ifndef stl_h_INCLUDED
#define stl_h_INCLUDED
#include <gst/gst.h>
/* Load the standard library */
void gst_stl_load(Gst *vm);
#endif // stl_h_INCLUDED