Commit 761c68fa authored by Chris Müller's avatar Chris Müller
Browse files

refactor project directory structure

parent 8539b546
......@@ -9,5 +9,10 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(CHERRY_VERSION 0.1)
include_directories(crystal/include)
include_directories(include)
add_subdirectory(crystal)
add_subdirectory(runtime)
add_subdirectory(source)
......@@ -18,8 +18,7 @@
#pragma once
#include "standard.h"
#include <crystal/standard.h>
#include <stdlib.h>
#include <stdint.h>
......@@ -27,22 +26,23 @@ struct CyArray;
struct CyFile {
const char* filename;
const byte* begin;
const byte_t* begin;
};
struct CyContext {
struct CyFile* file;
byte* src;
struct CyArray* buffer;
byte_t* src;
struct CryArray* buffer;
};
struct CyContext* cy_context_new(const byte* source, const char* filename);
struct CyContext* cy_context_repl_new(const byte* source);
struct CyContext* cy_context_new(const byte_t* source, const char* filename);
struct CyContext* cy_context_repl_new(const byte_t* source);
void cy_context_free(struct CyContext* context);
void cy_error(struct CyContext* context, const char* format, ...);
enum CyTOK {
TOK_EOF,
......@@ -64,9 +64,9 @@ enum CyTOK {
};
const byte* cy_tok_to_string(enum CyTOK token);
const byte_t* cy_tok_to_string(enum CyTOK token);
enum CyTOK cy_lex(struct CyContext* context);
byte* cy_token_string(struct CyContext* context);
byte_t* cy_token_string(struct CyContext* context);
size_t cy_token_length(struct CyContext* context);
add_subdirectory(runtime)
add_subdirectory(cherry)
set(CORE_SOURCES
parser.c)
set(INTERPRETER_SOURCES
cherry.c)
add_library(cherry-core ${CORE_SOURCES})
add_executable(cherry ${INTERPRETER_SOURCES})
target_link_libraries(cherry readline crystal cherry-core cherry-runtime)
set(CORE_SOURCES
array.c
red_black_tree.c
map.c
parser.c
unicode.c)
set(INTERPRETER_SOURCES
cherry.c)
add_library(cherry-core SHARED ${CORE_SOURCES})
add_executable(cherry ${INTERPRETER_SOURCES})
target_link_libraries(cherry-core gc)
target_link_libraries(cherry readline cherry-core cherry-runtime)
/*
* Cherry programming language
* Copyright (C) 2013 Christoph Mueller
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "array.h"
#include <assert.h>
#include <string.h>
struct CyArray
{
uint8_t* data;
size_t alloc;
size_t length;
};
struct CyArray*
cy_array_new(size_t init_capacity)
{
assert(init_capacity != 0);
struct CyArray* array = cy_malloc(struct CyArray);
array->alloc = init_capacity;
array->length = 0;
array->data = cy_calloc(uint8_t, array->alloc);
return array;
}
void
cy_array_free(struct CyArray* array)
{
assert(array != 0);
cy_free(array->data);
cy_free(array);
}
size_t
cy_array_size(struct CyArray* array)
{
assert(array != 0);
return array->length;
}
size_t
cy_array_capacity(struct CyArray* array)
{
assert(array != 0);
return array->alloc;
}
size_t
cy_array_ensure(struct CyArray* array, size_t min_capacity)
{
assert(array != 0);
if(array->alloc < min_capacity) {
size_t new_capacity = (array->alloc >> 1);
array->alloc = (new_capacity > min_capacity) ? new_capacity : min_capacity;
array->data = cy_realloc(uint8_t, array->data, array->alloc);
}
return array->alloc;
}
size_t
cy_array_trim(struct CyArray* array)
{
assert(array != 0);
array->data = cy_realloc(uint8_t, array->data, array->length);
array->alloc = array->length;
return array->alloc;
}
void
cy_array_append(struct CyArray* array, const_pointer data, size_t size)
{
assert(array != 0);
cy_array_ensure(array, array->length + size);
memcpy(array->data + array->length, data, size);
array->length += size;
}
int
cy_array_insert(struct CyArray* array, size_t index, const_pointer data, size_t size)
{
assert(array != 0);
if(index >= array->length)
return CHERRY_OUT_OF_INDEX_ERROR;
cy_array_ensure(array, array->length + size);
memmove(array->data + index + size, array->data + index, array->length - index);
memcpy(array->data + index, data, size);
array->length += size;
return CHERRY_OK;
}
int
cy_array_replace(struct CyArray* array, size_t index, const_pointer data, size_t size)
{
assert(array != 0);
if(index >= array->length && index + size > array->length)
return CHERRY_OUT_OF_INDEX_ERROR;
cy_array_ensure(array, array->length + size);
memcpy(array->data + index, data, size);
}
int
cy_array_remove(struct CyArray* array, size_t index, size_t size)
{
assert(array != 0);
if(index >= array->length)
return CHERRY_OUT_OF_INDEX_ERROR;
memmove(array->data + index, array->data + index + size, array->length - index - size);
array->length -= size;
return CHERRY_OK;
}
void
cy_array_clear(struct CyArray* array)
{
assert(array != 0);
array->length = 0;
}
pointer
cy_array_get(struct CyArray* array, size_t index)
{
assert(array != 0);
if(index < array->length)
return array->data + index;
return 0;
}
pointer
cy_array_clone(struct CyArray* array)
{
assert(array != 0);
uint8_t* ptr = 0;
if(array->length > 0) {
ptr = cy_calloc(uint8_t, array->length);
memcpy(ptr, array->data, array->length);
}
return ptr;
}
struct CyPtrArray
{
pointer* data;
size_t alloc;
size_t length;
};
struct CyPtrArray*
cy_ptr_array_new(size_t init_capacity)
{
assert(init_capacity != 0);
struct CyPtrArray* array = cy_malloc(struct CyPtrArray);
array->alloc = init_capacity;
array->length = 0;
array->data = cy_calloc(pointer, array->alloc);
return array;
}
void
cy_ptr_array_free(struct CyPtrArray* array)
{
assert(array != 0);
cy_free(array->data);
cy_free(array);
}
size_t
cy_ptr_array_size(struct CyPtrArray* array)
{
assert(array != 0);
return array->length;
}
size_t
cy_ptr_array_capacity(struct CyPtrArray* array)
{
assert(array != 0);
return array->alloc;
}
size_t
cy_ptr_array_ensure(struct CyPtrArray* array, size_t min_capacity)
{
assert(array != 0);
if(array->alloc < min_capacity) {
size_t new_capacity = (array->alloc >> 1);
array->alloc = (new_capacity > min_capacity) ? new_capacity : min_capacity;
array->data = cy_realloc(pointer, array->data, array->alloc);
}
return array->alloc;
}
size_t
cy_ptr_array_trim(struct CyPtrArray* array)
{
assert(array != 0);
array->data = cy_realloc(pointer, array->data, array->length);
array->alloc = array->length;
return array->alloc;
}
void
cy_ptr_array_append(struct CyPtrArray* array, pointer ptr)
{
assert(array != 0);
cy_ptr_array_ensure(array, array->length + 1);
*(array->data + array->length) = ptr;
array->length += 1;
}
int
cy_ptr_array_insert(struct CyPtrArray* array, size_t index, pointer ptr)
{
assert(array != 0);
if(index >= array->length)
return CHERRY_OUT_OF_INDEX_ERROR;
cy_ptr_array_ensure(array, array->length + 1);
memmove(array->data + index + 1, array->data + index, (array->length - index) * sizeof(pointer));
*(array->data + index) = ptr;
array->length += 1;
return CHERRY_OK;
}
pointer
cy_ptr_array_replace(struct CyPtrArray* array, size_t index, pointer ptr)
{
assert(array != 0);
if(index >= array->length)
return 0;
pointer data = *(array->data + index);
*(array->data + index) = ptr;
return data;
}
pointer
cy_ptr_array_remove(struct CyPtrArray* array, size_t index)
{
assert(array != 0);
if(index >= array->length)
return 0;
pointer data = *(array->data + index);
memmove(array->data + index, array->data + index + 1, (array->length - index - 1) * sizeof(pointer));
array->length -= 1;
return data;
}
pointer
cy_ptr_array_get(struct CyPtrArray* array, size_t index)
{
assert(array != 0);
if(index < array->length)
return *(array->data + index);
return 0;
}
void
cy_ptr_array_clear(struct CyPtrArray* array, cy_free_funptr fun)
{
assert(array != 0);
if(fun != 0) {
size_t index = 0;
for(index = 0; index < array->length; ++index)
fun(*(array->data + index));
}
array->length = 0;
}
/*
* Cherry programming language
* Copyright (C) 2013 Christoph Mueller
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "standard.h"
#define CHERRY_OUT_OF_INDEX_ERROR -1
struct CyArray;
struct CyPtrArray;
struct CyArray* cy_array_new(size_t init_capacity);
void cy_array_free(struct CyArray* array);
size_t cy_array_size(struct CyArray* array);
size_t cy_array_capacity(struct CyArray* array);
size_t cy_array_ensure(struct CyArray* array, size_t min_capacity);
size_t cy_array_trim(struct CyArray* array);
void cy_array_append(struct CyArray* array, const_pointer data, size_t size);
int cy_array_insert(struct CyArray* array, size_t index, const_pointer data, size_t size);
int cy_array_remove(struct CyArray* array, size_t index, size_t size);
int cy_array_replace(struct CyArray* array, size_t index, const_pointer data, size_t size);
void cy_array_clear(struct CyArray* array);
pointer cy_array_clone(struct CyArray* array);
pointer cy_array_get(struct CyArray* array, size_t index);
struct CyPtrArray* cy_ptr_array_new(size_t init_capacity);
void cy_ptr_array_free(struct CyPtrArray* array);
size_t cy_ptr_array_size(struct CyPtrArray* array);
size_t cy_ptr_array_capacity(struct CyPtrArray* array);
size_t cy_ptr_array_ensure(struct CyPtrArray* array, size_t min_capacity);
size_t cy_ptr_array_trim(struct CyPtrArray* array);
void cy_ptr_array_append(struct CyPtrArray* array, pointer ptr);
int cy_ptr_array_insert(struct CyPtrArray* array, size_t index, pointer ptr);
pointer cy_ptr_array_remove(struct CyPtrArray* array, size_t index);
pointer cy_ptr_array_replace(struct CyPtrArray* array, size_t index, pointer ptr);
void cy_ptr_array_clear(struct CyPtrArray* array, cy_free_funptr fun);
pointer cy_ptr_array_get(struct CyPtrArray* array, size_t index);
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
int main(int argc, char** argv)
{
printf("Welcome to Cherry 0.1\n");
while(1) {
char* line = readline("> ");
if(line && *line)
add_history(line);
free(line);
}
return 0;
}
/*
* Cherry programming language
* Copyright (C) 2013 Christoph Mueller
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "map.h"
#include "red_black_tree.h"
#include <assert.h>
struct CyMap*
cy_map_new(cy_ordering_funptr compare)
{
return cy_cast(struct CyMap*, cy_rbtree_new(compare));
}
void
cy_map_clear(struct CyMap* map, cy_free_funptr fun_key, cy_free_funptr fun_data)
{
assert(map != 0);
cy_rbtree_clear(cy_cast(struct CyRbTree*, map), fun_key, fun_data);
}
size_t
cy_map_size(struct CyMap* map)
{
assert(map != 0);
return cy_cast(struct CyRbTree*, map)->nodes;
}
pointer
cy_map_lookup(struct CyMap* map, const_pointer key)
{
assert(map != 0);
struct CyRbNode* result = cy_rbtree_lookup(cy_cast(struct CyRbTree*, map), key);
return (result != 0) ? cy_cast(pointer, result->data) : 0;
}
int
cy_map_insert(struct CyMap* map, const_pointer key, const_pointer value)
{
assert(map != 0);
struct CyRbNode* result = cy_rbtree_insert(cy_cast(struct CyRbTree*, map), key, value);
return result != 0;
}
struct CyMapPair
cy_map_remove(struct CyMap* map, const_pointer key)
{
assert(map != 0);
struct CyMapPair pair = { .key = 0, .data = 0 };
struct CyRbNode* result = cy_rbtree_remove(cy_cast(struct CyRbTree*, map), key);