Skip to content
Open
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
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ ifdef DEBUG
CFLAGS += -g
endif

CORE_OBJECTS = src/lexer.o src/token.o src/ast.o src/parse_helpers.o src/errors.o
CORE_OBJECTS = src/compiler/ast.o src/compiler/lexer.o src/compiler/parse_helpers.o
CORE_OBJECTS += src/compiler/token.o

CORE_OBJECTS += $(patsubst %.c,%.o,$(wildcard src/ds/*.c))
CORE_OBJECTS += $(patsubst %.c,%.o,$(wildcard src/model/*.c))
CORE_OBJECTS += $(patsubst %.c,%.o,$(wildcard src/runtime/*.c))
Expand All @@ -22,21 +24,22 @@ TEST_OBJECTS += $(patsubst %.c,%.o,$(wildcard tests/unit/runtime/*.c))
test: bin/segment tests/units
./tests/all.sh

bin/segment: src/grammar.c ${CORE_OBJECTS} ${EXEC_OBJECTS}
bin/segment: src/compiler/grammar.c ${CORE_OBJECTS} ${EXEC_OBJECTS}
mkdir -p bin/
${CC} ${CORE_OBJECTS} ${EXEC_OBJECTS} -o bin/segment

src/lexer.c: src/lexer.rl src/grammar.c
ragel -C -G2 src/lexer.rl
src/compiler/lexer.c: src/compiler/lexer.rl src/compiler/grammar.c
ragel -C -G2 src/compiler/lexer.rl

src/grammar.c: src/grammar.y
cd src && lemon -s grammar.y
src/compiler/grammar.c: src/compiler/grammar.y
cd src/compiler && lemon -s grammar.y

tests/units: ${CORE_OBJECTS} ${TEST_OBJECTS}
${CC} ${CORE_OBJECTS} ${TEST_OBJECTS} -lcunit -o tests/suite

.PHONY: clean
clean:
rm -f src/*.o src/grammar.c src/grammar.h src/grammar.out src/lexer.c
rm -f src/debug/*.o src/ds/*.o src/model/*.o src/runtime/*.o
rm -f src/*.o
rm -f src/compiler/grammar.c src/compiler/grammar.h src/compiler/grammar.out src/compiler/lexer.c
rm -f src/debug/*.o src/ds/*.o src/model/*.o src/runtime/*.o src/compiler/*.o
rm -f tests/unit/*.o tests/unit/ds/*.o tests/unit/model/*.o tests/unit/runtime/*.o
2 changes: 1 addition & 1 deletion src/ast.c → src/compiler/ast.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <stdio.h>

#include "ast.h"
#include "compiler/ast.h"

/* Visitor */

Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions src/compiler/compiler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "compiler/compiler.h"

#include "runtime/bytecode.h"
#include "model/module.h"

typedef struct {
seg_object *entry_block;

uint32_t literal_count;
size_t literal_capacity;
seg_object *literal_pool;
} seg_module_builder;

seg_err seg_compile(seg_runtime *r, seg_block_node *ast, seg_object *module)
{
return SEG_NOTYET("seg_compile");
}
16 changes: 16 additions & 0 deletions src/compiler/compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef COMPILER_H
#define COMPILER_H

#include "model/object.h"
#include "model/errors.h"
#include "compiler/ast.h"
#include "runtime/runtime.h"

/*
* Compile a parsed and optimized abstract syntax tree into an existing module, populating it with
* a sequence of literals discovered from this source file. Most notably, at least one of these
* literals will be a Block object containing bytecode sequences that the interpreter can execute.
*/
seg_err seg_compile(seg_runtime *r, seg_block_node *ast, seg_object *module);

#endif /* end of include guard: COMPILER_H */
6 changes: 3 additions & 3 deletions src/grammar.y → src/compiler/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <stdlib.h>
#include <stdio.h>

#include "ast.h"
#include "token.h"
#include "parse_helpers.h"
#include "compiler/ast.h"
#include "compiler/token.h"
#include "compiler/parse_helpers.h"
#include "runtime/symboltable.h"

#define INTERN(out, name, length) seg_symboltable_intern(state->symboltable, name, length, out)
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.h → src/compiler/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/types.h>

#include "ast.h"
#include "compiler/ast.h"
#include "options.h"
#include "runtime/symboltable.h"

Expand Down
10 changes: 5 additions & 5 deletions src/lexer.rl → src/compiler/lexer.rl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#include <string.h>
#include <sys/types.h>

#include "lexer.h"
#include "ast.h"
#include "token.h"
#include "compiler/lexer.h"
#include "compiler/ast.h"
#include "compiler/token.h"
#include "options.h"
#include "parse_helpers.h"
#include "compiler/parse_helpers.h"
#include "runtime/runtime.h"

#include "grammar.h"
#include "compiler/grammar.h"
#include "grammar.c"

static void report(const char *name, const char *ts, const char *te, seg_options *opts) {
Expand Down
4 changes: 2 additions & 2 deletions src/parse_helpers.c → src/compiler/parse_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <string.h>
#include <stdio.h>

#include "parse_helpers.h"
#include "ast.h"
#include "compiler/parse_helpers.h"
#include "compiler/ast.h"

struct seg_parser_context {
seg_block_node *block;
Expand Down
4 changes: 2 additions & 2 deletions src/parse_helpers.h → src/compiler/parse_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <stddef.h>

#include "ast.h"
#include "token.h"
#include "compiler/ast.h"
#include "compiler/token.h"
#include "runtime/symboltable.h"

/* Methods that have special significance. */
Expand Down
2 changes: 1 addition & 1 deletion src/token.c → src/compiler/token.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string.h>
#include <errno.h>

#include "token.h"
#include "compiler/token.h"
#include "runtime/symboltable.h"

seg_token *seg_new_token(const char *start, const char *end)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/debug/ast_printer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef AST_PRINTER_H
#define AST_PRINTER_H

#include "../ast.h"
#include "compiler/ast.h"

/*
Print an ASCII-art representation of a parsed AST.
Expand Down
2 changes: 1 addition & 1 deletion src/ds/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdint.h>

#include "errors.h"
#include "model/errors.h"

/*
* Settings that control the growth behavior of a hashtable.
Expand Down
2 changes: 1 addition & 1 deletion src/ds/ptrtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h>
#include <stddef.h>

#include "errors.h"
#include "model/errors.h"
#include "ds/hashtable.h"

/*
Expand Down
2 changes: 1 addition & 1 deletion src/ds/stringtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stddef.h>
#include <stdint.h>

#include "errors.h"
#include "model/errors.h"
#include "ds/hashtable.h"

/*
Expand Down
2 changes: 1 addition & 1 deletion src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <sys/mman.h>

#include "options.h"
#include "lexer.h"
#include "compiler/lexer.h"
#include "debug/ast_printer.h"
#include "debug/symbol_printer.h"
#include "runtime/runtime.h"
Expand Down
55 changes: 55 additions & 0 deletions src/model/array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "model/array.h"

seg_err seg_empty_array(seg_runtime *r, uint64_t capacity, seg_object *out)
{
seg_err err;
seg_object length;
const seg_bootstrap_objects *boots = seg_runtime_bootstraps(r);

SEG_TRY(seg_slotted_with_length(r, boots->array_class, capacity + 1, out));

SEG_TRY(seg_integer(r, 0, &length));
SEG_TRY(seg_slot_atput(*out, 0, length));

return SEG_OK;
}

seg_err seg_carray(
seg_runtime *r,
uint64_t capacity,
uint64_t length,
seg_object *contents,
seg_object *out
) {
return SEG_NOTYET("seg_carray");
}

seg_err seg_varray(seg_runtime *r, uint64_t capacity, uint64_t length, seg_object *out, ...)
{
return SEG_NOTYET("seg_varray");
}

seg_err seg_array_capacity(seg_object array, uint64_t *capacity)
{
seg_err err;
uint64_t slotted_length;

SEG_TRY(seg_slotted_length(array, &slotted_length));
*capacity = slotted_length - 1;

return SEG_OK;
}

seg_err seg_array_length(seg_object array, uint64_t *length)
{
seg_err err;
seg_object o_length;
int64_t signed_length;

SEG_TRY(seg_slot_at(array, 0, &o_length));
SEG_TRY(seg_integer_value(o_length, &signed_length));

*length = (uint64_t) signed_length;

return SEG_OK;
}
42 changes: 42 additions & 0 deletions src/model/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef ARRAY_H
#define ARRAY_H

#include <stdint.h>
#include <stdarg.h>

#include "runtime/runtime.h"
#include "model/errors.h"
#include "model/object.h"

/*
* Construct an empty Array instance with zero length and a specified initial capacity.
*/
seg_err seg_empty_array(seg_runtime *r, uint64_t capacity, seg_object *out);

/*
* Construct an Array instance from a C array of seg_object references.
*/
seg_err seg_carray(
seg_runtime *r,
uint64_t capacity,
uint64_t length,
seg_object *contents,
seg_object *out
);

/*
* Construct an Array instance from a sequence of seg_object references using varargs.
*/
seg_err seg_varray(seg_runtime *r, uint64_t capacity, uint64_t length, seg_object *out, ...);

/*
* Access the current maximum capacity of an Array.
*/
seg_err seg_array_capacity(seg_object array, uint64_t *capacity);

/*
* Access the occupied length of an Array.
*/
seg_err seg_array_length(seg_object array, uint64_t *length);

#endif /* end of include guard: ARRAY_H */
11 changes: 11 additions & 0 deletions src/model/block.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "model/block.h"

seg_err seg_block(
seg_runtime *r,
seg_instruction *bytecode,
uint32_t bytecode_length,
seg_object parent_scope,
seg_object *out
) {
return SEG_NOTYET("seg_block");
}
28 changes: 28 additions & 0 deletions src/model/block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef BLOCK_H
#define BLOCK_H

#include "runtime/runtime.h"
#include "runtime/bytecode.h"
#include "model/errors.h"
#include "model/object.h"

typedef enum {
SEG_BLOCK_SLOT_NAME = 0,
SEG_BLOCK_SLOT_VERSION,
SEG_BLOCK_SLOT_LITERAL_POOL,
SEG_BLOCK_SLOT_ENTRY_BLOCK,
SEG_BLOCK_SLOTCOUNT
} seg_block_slots;

/*
* Construct a Block object containing a compiled bytecode sequence and an optional parent scope.
*/
seg_err seg_block(
seg_runtime *r,
seg_instruction *bytecode,
uint32_t bytecode_length,
seg_object parent_scope,
seg_object *out
);

#endif /* end of include guard: BLOCK_H */
2 changes: 1 addition & 1 deletion src/errors.c → src/model/errors.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "errors.h"
#include "model/errors.h"

struct __seg_err __seg_err_nomem = {
.code = SEG_CODE_NOMEM,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/model/klass.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdarg.h>

#include "object.h"
#include "errors.h"
#include "model/errors.h"

/* Slot indices used by Class objects. */
typedef enum {
Expand Down
21 changes: 21 additions & 0 deletions src/model/module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "module.h"

seg_err seg_module(seg_runtime *r, const char *name, const char *version, seg_object *out)
{
return SEG_NOTYET("seg_module");
}

seg_err seg_module_entry_block(seg_object module, seg_object *out)
{
return SEG_NOTYET("seg_module_entry_block");
}

seg_err seg_module_set_entry_block(seg_object module, seg_object block)
{
return SEG_NOTYET("seg_module_set_entry_block");
}

seg_err seg_module_append(seg_object module, seg_object *literals, uint32_t literal_count)
{
return SEG_NOTYET("seg_module_append");
}
Loading