Skip to content

Commit

Permalink
Merge pull request #96 from jow-/module-import-export-support
Browse files Browse the repository at this point in the history
Module import export support
  • Loading branch information
jow- authored Jul 30, 2022
2 parents 1219d7e + 156d584 commit e55965a
Show file tree
Hide file tree
Showing 71 changed files with 2,462 additions and 975 deletions.
598 changes: 577 additions & 21 deletions compiler.c

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions include/ucode/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
#include "util.h"
#include "types.h"

void uc_chunk_init(uc_chunk_t *chunk);
void uc_chunk_free(uc_chunk_t *chunk);
size_t uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t line);
__hidden void uc_chunk_init(uc_chunk_t *chunk);
__hidden void uc_chunk_free(uc_chunk_t *chunk);
__hidden size_t uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t line);

void uc_chunk_pop(uc_chunk_t *chunk);
__hidden void uc_chunk_pop(uc_chunk_t *chunk);

size_t uc_chunk_debug_get_srcpos(uc_chunk_t *chunk, size_t off);
void uc_chunk_debug_add_variable(uc_chunk_t *chunk, size_t from, size_t to, size_t slot, bool upval, uc_value_t *name);
uc_value_t *uc_chunk_debug_get_variable(uc_chunk_t *chunk, size_t off, size_t slot, bool upval);
__hidden size_t uc_chunk_debug_get_srcpos(uc_chunk_t *chunk, size_t off);
__hidden void uc_chunk_debug_add_variable(uc_chunk_t *chunk, size_t from, size_t to, size_t slot, bool upval, uc_value_t *name);
__hidden uc_value_t *uc_chunk_debug_get_variable(uc_chunk_t *chunk, size_t off, size_t slot, bool upval);

#endif /* UCODE_CHUNK_H */
40 changes: 18 additions & 22 deletions include/ucode/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,22 @@ typedef enum {
TK_NULLISH,
TK_PLACEH,
TK_TEMPLATE,
TK_IMPORT,
TK_EXPORT,
TK_FROM,
TK_AS,

TK_EOF,
TK_ERROR
} uc_tokentype_t;

typedef enum {
UC_LEX_IDENTIFY_BLOCK,
UC_LEX_BLOCK_COMMENT_START,
UC_LEX_BLOCK_EXPRESSION_START,
UC_LEX_BLOCK_EXPRESSION_EMIT_TAG,
UC_LEX_BLOCK_STATEMENT_START,
UC_LEX_BLOCK_COMMENT,
UC_LEX_IDENTIFY_TOKEN,
UC_LEX_PARSE_TOKEN,
UC_LEX_PLACEHOLDER,
UC_LEX_PLACEHOLDER_START,
UC_LEX_PLACEHOLDER_END,
UC_LEX_EOF
} uc_lex_state_t;

Expand All @@ -145,19 +146,9 @@ typedef struct {
uc_lex_state_t state;
uc_parse_config_t *config;
uc_source_t *source;
uint8_t eof:1;
uint8_t is_escape:1;
uint8_t is_placeholder:1;
uint8_t no_regexp:1;
uint8_t no_keyword:1;
size_t buflen;
char *buf, *bufstart, *bufend;
size_t lookbehindlen;
char *lookbehind;
const void *tok;
uc_token_t curr;
char esc[5];
uint8_t esclen;
int lead_surrogate;
size_t lastoff;
enum {
Expand All @@ -176,19 +167,24 @@ typedef struct {
size_t count;
size_t *entries;
} templates;
struct {
size_t count;
char *entries;
} buffer;
unsigned char *rbuf;
size_t rlen, rpos;
} uc_lexer_t;


void uc_lexer_init(uc_lexer_t *lex, uc_parse_config_t *config, uc_source_t *source);
void uc_lexer_free(uc_lexer_t *lex);
__hidden void uc_lexer_init(uc_lexer_t *lex, uc_parse_config_t *config, uc_source_t *source);
__hidden void uc_lexer_free(uc_lexer_t *lex);

uc_token_t *uc_lexer_next_token(uc_lexer_t *lex);
__hidden uc_token_t *uc_lexer_next_token(uc_lexer_t *lex);

bool uc_lexer_is_keyword(uc_value_t *label);
__hidden bool uc_lexer_is_keyword(uc_value_t *label);

bool utf8enc(char **out, int *rem, int code);
__hidden bool utf8enc(char **out, int *rem, int code);

const char *
uc_tokenname(unsigned type);
__hidden const char *uc_tokenname(unsigned type);

#endif /* UCODE_LEXER_H */
4 changes: 2 additions & 2 deletions include/ucode/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ extern const uc_function_list_t uc_stdlib_functions[];
void uc_stdlib_load(uc_value_t *scope);
uc_cfn_ptr_t uc_stdlib_function(const char *name);

bool uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact);
bool uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off);
__hidden bool uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact);
__hidden bool uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off);


/* vm helper */
Expand Down
20 changes: 12 additions & 8 deletions include/ucode/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "types.h"


uc_program_t *uc_program_new(uc_source_t *);
uc_program_t *uc_program_new(void);

static inline uc_program_t *
uc_program_get(uc_program_t *prog) {
Expand All @@ -46,15 +46,19 @@ uc_program_put(uc_program_t *prog) {
fn = fn##_tmp, \
fn##_tmp = (uc_function_t *)fn##_tmp->progref.prev)

uc_function_t *uc_program_function_new(uc_program_t *, const char *, size_t);
size_t uc_program_function_id(uc_program_t *, uc_function_t *);
uc_function_t *uc_program_function_load(uc_program_t *, size_t);
size_t uc_program_function_srcpos(uc_function_t *, size_t);
void uc_program_function_free(uc_function_t *);
#define uc_program_function_last(prog) (uc_function_t *)prog->functions.next

__hidden uc_function_t *uc_program_function_new(uc_program_t *, const char *, uc_source_t *, size_t);
__hidden size_t uc_program_function_id(uc_program_t *, uc_function_t *);
__hidden uc_function_t *uc_program_function_load(uc_program_t *, size_t);
__hidden uc_source_t *uc_program_function_source(uc_function_t *);
__hidden size_t uc_program_function_srcpos(uc_function_t *, size_t);
__hidden void uc_program_function_free(uc_function_t *);

uc_value_t *uc_program_get_constant(uc_program_t *, size_t);
ssize_t uc_program_add_constant(uc_program_t *, uc_value_t *);
__hidden ssize_t uc_program_export_lookup(uc_program_t *, uc_source_t *, uc_value_t *);

__hidden uc_value_t *uc_program_get_constant(uc_program_t *, size_t);
__hidden ssize_t uc_program_add_constant(uc_program_t *, uc_value_t *);

void uc_program_write(uc_program_t *, FILE *, bool);
uc_program_t *uc_program_load(uc_source_t *, char **);
Expand Down
13 changes: 8 additions & 5 deletions include/ucode/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef enum {
uc_source_t *uc_source_new_file(const char *path);
uc_source_t *uc_source_new_buffer(const char *name, char *buf, size_t len);

size_t uc_source_get_line(uc_source_t *source, size_t *offset);
__hidden size_t uc_source_get_line(uc_source_t *source, size_t *offset);

static inline uc_source_t *
uc_source_get(uc_source_t *source) {
Expand All @@ -47,11 +47,14 @@ uc_source_put(uc_source_t *source) {
ucv_put(source ? &source->header : NULL);
}

uc_source_type_t uc_source_type_test(uc_source_t *source);
__hidden uc_source_type_t uc_source_type_test(uc_source_t *source);

void uc_source_line_next(uc_source_t *source);
void uc_source_line_update(uc_source_t *source, size_t off);
__hidden void uc_source_line_next(uc_source_t *source);
__hidden void uc_source_line_update(uc_source_t *source, size_t off);

void uc_source_runpath_set(uc_source_t *source, const char *runpath);
__hidden void uc_source_runpath_set(uc_source_t *source, const char *runpath);

__hidden bool uc_source_export_add(uc_source_t *source, uc_value_t *name);
__hidden ssize_t uc_source_export_lookup(uc_source_t *source, uc_value_t *name);

#endif /* UCODE_SOURCE_H */
65 changes: 56 additions & 9 deletions include/ucode/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef enum uc_type {
typedef struct uc_value {
uint32_t type:4;
uint32_t mark:1;
uint32_t u64:1;
uint32_t u64_or_constant:1;
uint32_t refcount:26;
} uc_value_t;

Expand All @@ -65,13 +65,15 @@ typedef struct {
/* Source buffer defintions */

uc_declare_vector(uc_lineinfo_t, uint8_t);
uc_declare_vector(uc_exports_t, uc_value_t *);

typedef struct {
uc_value_t header;
char *filename, *runpath, *buffer;
FILE *fp;
size_t off;
uc_lineinfo_t lineinfo;
uc_exports_t exports;
} uc_source_t;


Expand Down Expand Up @@ -113,6 +115,7 @@ typedef struct uc_function {
bool arrow, vararg, strict;
size_t nargs;
size_t nupvals;
size_t srcidx;
size_t srcpos;
uc_chunk_t chunk;
struct uc_program *program;
Expand Down Expand Up @@ -202,23 +205,45 @@ uc_declare_vector(uc_resource_types_t, uc_resource_type_t *);

/* Program structure definitions */

uc_declare_vector(uc_sources_t, uc_source_t *);

typedef struct uc_program {
uc_value_t header;
uc_value_list_t constants;
uc_weakref_t functions;
uc_source_t *source;
uc_sources_t sources;
} uc_program_t;


/* Parser definitions */

uc_declare_vector(uc_search_path_t, char *);

typedef struct {
bool lstrip_blocks;
bool trim_blocks;
bool strict_declarations;
bool raw_mode;
uc_search_path_t module_search_path;
} uc_parse_config_t;

extern uc_parse_config_t uc_default_parse_config;

void uc_search_path_init(uc_search_path_t *search_path);

static inline void
uc_search_path_add(uc_search_path_t *search_path, char *path) {
uc_vector_push(search_path, xstrdup(path));
}

static inline void
uc_search_path_free(uc_search_path_t *search_path) {
while (search_path->count > 0)
free(search_path->entries[--search_path->count]);

uc_vector_clear(search_path);
}


/* VM definitions */

Expand Down Expand Up @@ -249,6 +274,7 @@ typedef struct {

uc_declare_vector(uc_callframes_t, uc_callframe_t);
uc_declare_vector(uc_stack_t, uc_value_t *);
uc_declare_vector(uc_modexports_t, uc_upvalref_t *);

typedef struct printbuf uc_stringbuf_t;

Expand All @@ -265,6 +291,7 @@ struct uc_vm {
uc_source_t *sources;
uc_weakref_t values;
uc_resource_types_t restypes;
uc_modexports_t exports;
union {
uint32_t u32;
int32_t s32;
Expand All @@ -283,13 +310,12 @@ struct uc_vm {

/* Value API */

void ucv_free(uc_value_t *, bool);
void ucv_put(uc_value_t *);

void ucv_unref(uc_weakref_t *);
void ucv_ref(uc_weakref_t *, uc_weakref_t *);
__hidden void ucv_free(uc_value_t *, bool);
__hidden void ucv_unref(uc_weakref_t *);
__hidden void ucv_ref(uc_weakref_t *, uc_weakref_t *);

uc_value_t *ucv_get(uc_value_t *uv);
void ucv_put(uc_value_t *);

uc_type_t ucv_type(uc_value_t *);
const char *ucv_typename(uc_value_t *);
Expand Down Expand Up @@ -448,7 +474,28 @@ ucv_is_arrowfn(uc_value_t *uv)
static inline bool
ucv_is_u64(uc_value_t *uv)
{
return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64 == true);
return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true &&
uv->type == UC_INTEGER);
}

static inline bool
ucv_is_constant(uc_value_t *uv)
{
return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true &&
(uv->type == UC_ARRAY || uv->type == UC_OBJECT));
}

static inline bool
ucv_set_constant(uc_value_t *uv, bool constant)
{
if (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant != constant &&
(uv->type == UC_ARRAY || uv->type == UC_OBJECT)) {
uv->u64_or_constant = constant;

return true;
}

return false;
}

static inline bool
Expand Down Expand Up @@ -499,6 +546,6 @@ ucv_clear_mark(uc_value_t *uv)

void ucv_gc(uc_vm_t *);

void ucv_freeall(uc_vm_t *);
__hidden void ucv_freeall(uc_vm_t *);

#endif /* UCODE_TYPES_H */
5 changes: 5 additions & 0 deletions include/ucode/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include <json-c/json.h>


#ifndef __hidden
#define __hidden __attribute__((visibility("hidden")))
#endif


/* alignment & array size */

#ifndef ALIGN
Expand Down
14 changes: 7 additions & 7 deletions include/ucode/vallist.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ typedef enum {
TAG_LSTR = 5
} uc_value_type_t;

uc_value_t *uc_number_parse(const char *buf, char **end);
uc_value_t *uc_number_parse_octal(const char *buf, char **end);
__hidden uc_value_t *uc_number_parse(const char *buf, char **end);
__hidden uc_value_t *uc_number_parse_octal(const char *buf, char **end);

bool uc_double_pack(double d, char *buf, bool little_endian);
double uc_double_unpack(const char *buf, bool little_endian);

void uc_vallist_init(uc_value_list_t *list);
void uc_vallist_free(uc_value_list_t *list);
__hidden void uc_vallist_init(uc_value_list_t *list);
__hidden void uc_vallist_free(uc_value_list_t *list);

ssize_t uc_vallist_add(uc_value_list_t *list, uc_value_t *value);
uc_value_type_t uc_vallist_type(uc_value_list_t *list, size_t idx);
uc_value_t *uc_vallist_get(uc_value_list_t *list, size_t idx);
__hidden ssize_t uc_vallist_add(uc_value_list_t *list, uc_value_t *value);
__hidden uc_value_type_t uc_vallist_type(uc_value_list_t *list, size_t idx);
__hidden uc_value_t *uc_vallist_get(uc_value_list_t *list, size_t idx);

#endif /* UCODE_VALUE_H */
4 changes: 3 additions & 1 deletion include/ucode/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ __insn(QMCALL) \
__insn(PRINT) \
__insn(NEXTK) \
__insn(NEXTKV) \
__insn(DELETE)
__insn(DELETE) \
__insn(IMPORT) \
__insn(EXPORT)


#undef __insn
Expand Down
Loading

0 comments on commit e55965a

Please sign in to comment.