Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

#include <assert.h>

#include "ucode/chunk.h"
#include "ucode/types.h"
#include "ucode/util.h"
#include "ucode/internal/chunk.h"

#define OFFSETINFO_BITS (sizeof(((uc_offsetinfo_t *)NULL)->entries[0]) * 8)
#define OFFSETINFO_BYTE_BITS 3
Expand Down
11 changes: 5 additions & 6 deletions compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
#include <assert.h>
#include <errno.h>

#include "ucode/compiler.h"
#include "ucode/chunk.h"
#include "ucode/vm.h" /* I_* */
#include "ucode/source.h"
#include "ucode/program.h"
#include "ucode/lib.h" /* uc_error_context_format() */
#include "ucode/internal/compiler.h"
#include "ucode/internal/chunk.h"
#include "ucode/internal/vm.h" /* ISA: I_*, uc_vm_insn_t */
#include "ucode/internal/program.h"
#include "ucode/internal/lib.h" /* uc_error_context_format() */

#ifndef NO_COMPILE

Expand Down
6 changes: 3 additions & 3 deletions examples/exception-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include <stdio.h>

#include <ucode/compiler.h>
#include <ucode/lib.h>
#include <ucode/vm.h>
#include "ucode/compiler.h"
#include "ucode/lib.h"
#include "ucode/vm.h"


#define MULTILINE_STRING(...) #__VA_ARGS__
Expand Down
6 changes: 3 additions & 3 deletions examples/execute-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include <stdio.h>

#include <ucode/compiler.h>
#include <ucode/lib.h>
#include <ucode/vm.h>
#include "ucode/compiler.h"
#include "ucode/lib.h"
#include "ucode/vm.h"


static uc_parse_config_t config = {
Expand Down
6 changes: 3 additions & 3 deletions examples/execute-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

#include <stdio.h>

#include <ucode/compiler.h>
#include <ucode/lib.h>
#include <ucode/vm.h>
#include "ucode/compiler.h"
#include "ucode/lib.h"
#include "ucode/vm.h"


#define MULTILINE_STRING(...) #__VA_ARGS__
Expand Down
6 changes: 3 additions & 3 deletions examples/native-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include <stdio.h>

#include <ucode/compiler.h>
#include <ucode/lib.h>
#include <ucode/vm.h>
#include "ucode/compiler.h"
#include "ucode/lib.h"
#include "ucode/vm.h"


#define MULTILINE_STRING(...) #__VA_ARGS__
Expand Down
6 changes: 3 additions & 3 deletions examples/state-reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include <stdio.h>

#include <ucode/compiler.h>
#include <ucode/lib.h>
#include <ucode/vm.h>
#include "ucode/compiler.h"
#include "ucode/lib.h"
#include "ucode/vm.h"


#define MULTILINE_STRING(...) #__VA_ARGS__
Expand Down
6 changes: 3 additions & 3 deletions examples/state-reuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include <stdio.h>

#include <ucode/compiler.h>
#include <ucode/lib.h>
#include <ucode/vm.h>
#include "ucode/compiler.h"
#include "ucode/lib.h"
#include "ucode/vm.h"


#define MULTILINE_STRING(...) #__VA_ARGS__
Expand Down
135 changes: 33 additions & 102 deletions include/ucode/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,123 +14,54 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/* Public compilation API: uc_compile() and the parse configuration. */

#ifndef UCODE_COMPILER_H
#define UCODE_COMPILER_H

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#include "source.h"
#include "lexer.h"
#include "types.h"
#include "util.h"

typedef enum {
P_NONE,

P_COMMA, /* , */

P_ASSIGN, /* = += -= *= /= %= <<= >>= &= ^= |= ||= &&= **= ??= */

P_TERNARY, /* ?: */

P_OR, /* || ?? */
P_AND, /* && */
P_BOR, /* | */
P_BXOR, /* ^ */
P_BAND, /* & */

P_EQUAL, /* === !== == != */
P_COMPARE, /* < <= > >= in */

P_SHIFT, /* << >> */

P_ADD, /* + - */
P_MUL, /* * / % */

P_EXP, /* ** */
#include <ucode/types.h>
#include <ucode/source.h>

P_UNARY, /* ! ~ +… -… ++… --… */

P_INC, /* …++ …-- */
uc_declare_vector(uc_search_path_t, char *);

P_CALL, /* ….…, …[…], …(…) */
struct uc_parse_config {
bool lstrip_blocks;
bool trim_blocks;
bool strict_declarations;
bool raw_mode;
uc_search_path_t module_search_path;
uc_search_path_t force_dynlink_list;
bool setup_signal_handlers;
bool compile_module;
};

P_PRIMARY /* (…) */
} uc_precedence_t;
extern uc_parse_config_t uc_default_parse_config;

typedef enum {
F_ASSIGNABLE = (1 << 0),
F_ALTBLOCKMODE = (1 << 1),
} uc_exprflag_t;
void uc_search_path_init(uc_search_path_t *search_path);

typedef struct uc_patchlist {
struct uc_patchlist *parent;
size_t depth, count, *entries;
uc_tokentype_t token;
} uc_patchlist_t;
static inline void
uc_search_path_add(uc_search_path_t *search_path, char *path) {
uc_vector_push(search_path, xstrdup(path));
}

typedef struct uc_exprstack {
struct uc_exprstack *parent;
uint32_t flags;
uc_tokentype_t token;
} uc_exprstack_t;
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]);

typedef struct {
uc_value_t *name;
ssize_t depth;
size_t from;
bool captured;
bool constant;
bool funcstub;
} uc_local_t;

typedef struct {
uc_value_t *name;
size_t index;
bool local;
bool constant;
} uc_upval_t;

uc_declare_vector(uc_locals_t, uc_local_t);
uc_declare_vector(uc_upvals_t, uc_upval_t);
uc_declare_vector(uc_jmplist_t, size_t);

typedef struct {
uc_parse_config_t *config;
uc_lexer_t lex;
uc_token_t prev, curr;
bool synchronizing;
uc_stringbuf_t *error;
} uc_parser_t;

typedef struct uc_compiler {
struct uc_compiler *parent;
uc_locals_t locals;
uc_upvals_t upvals;
uc_patchlist_t *patchlist;
uc_exprstack_t *exprstack;
uc_function_t *function;
uc_parser_t *parser;
uc_program_t *program;
size_t scope_depth, current_srcpos, last_insn, try_depth, tailcall_off;
} uc_compiler_t;

typedef struct {
void (*prefix)(uc_compiler_t *);
void (*infix)(uc_compiler_t *);
uc_precedence_t precedence;
} uc_parse_rule_t;
uc_vector_clear(search_path);
}

/*
* Compile the given source into a program. On success returns a new
* uc_program_t (caller must release with uc_program_put()); on failure
* returns NULL and, if errp is non-NULL, stores a heap-allocated error
* message (caller must free()).
*/
uc_program_t *uc_compile(uc_parse_config_t *config, uc_source_t *source, char **errp);

#define uc_compiler_exprstack_push(compiler, token, flags) \
uc_exprstack_t expr = { compiler->exprstack, flags, token }; \
compiler->exprstack = &expr

#define uc_compiler_exprstack_pop(compiler) \
if (compiler->exprstack) \
compiler->exprstack = compiler->exprstack->parent

#endif /* UCODE_COMPILER_H */
6 changes: 3 additions & 3 deletions include/ucode/chunk.h → include/ucode/internal/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#include <stdint.h>
#include <stddef.h>

#include "vallist.h"
#include "util.h"
#include "types.h"
#include "ucode/internal/vallist.h"
#include "ucode/internal/util.h"
#include "ucode/internal/types.h"

__hidden void uc_chunk_init(uc_chunk_t *chunk);
__hidden void uc_chunk_free(uc_chunk_t *chunk);
Expand Down
Loading
Loading