Commit 0b07b5f1 authored by Chris Müller's avatar Chris Müller
Browse files

starting a rundementary runtime environment in c

parent 97a60c8c
set(SOURCES
value.c)
add_library(cherry-runtime SHARED ${SOURCES})
target_link_libraries(cherry-runtime gc)
/*
* 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 <stdint.h>
#define TRUE 1
#define FALSE 0
typedef void* cy_pointer_t;
typedef void* cy_const_pointer_t;
typedef uint8_t cy_byte_t;
typedef uint16_t cy_word_t;
typedef uint32_t cy_double_t;
typedef uint64_t cy_quad_t;
typedef int64_t cy_fixnum_t;
typedef double cy_float_t;
typedef char cy_boolean_t;
typedef uint32_t cy_unicode_t;
enum org_cherry_value_type {
CY_BOOLEAN,
CY_FIXNUM,
CY_FLOAT,
CY_STRING,
CY_CHAR,
CY_PAIR
};
struct org_cherry_meta {
enum org_cherry_value_type type;
};
struct org_cherry_value {
struct org_cherry_meta meta;
union {
cy_boolean_t boolean_value;
cy_fixnum_t fixnum_value;
cy_float_t float_value;
cy_byte_t* string_value;
cy_unicode_t char_value;
};
};
struct org_cherry_pair {
struct org_cherry_meta meta;
struct org_cherry_value* head;
struct org_cherry_value* tail;
};
struct org_cherry_value* __org_cherry_make_fixnum(cy_fixnum_t value);
struct org_cherry_value* __org_cherry_make_float(cy_float_t float_value);
struct org_cherry_value* __org_cherry_make_string(cy_byte_t* string_value);
struct org_cherry_value* __org_cherry_make_char(cy_unicode_t char_value);
struct org_cherry_pair* __org_cherry_list_cons(struct org_cherry_value* head, struct org_cherry_value* tail);
struct org_cherry_value* __org_cherry_list_head(struct org_cherry_pair* pair);
struct org_cherry_value* __org_cherry_list_tail(struct org_cherry_pair* pair);
struct org_cherry_value* __org_cherry_list_length(struct org_cherry_pair* pair);
struct org_cherry_value* __org_cherry_primitive_add(struct org_cherry_pair* pair);
/*
* 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 "runtime.h"
#include <gc.h>
// ----------------------------------------------------------------------------
// cherry object constructors
// ----------------------------------------------------------------------------
struct org_cherry_value*
__org_cherry_make_fixnum(cy_fixnum_t value)
{
struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
cy_value->meta.type = CY_FIXNUM;
cy_value->fixnum_value = value;
return cy_value;
}
struct org_cherry_value*
__org_cherry_make_float(cy_float_t value)
{
struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
cy_value->meta.type = CY_FLOAT;
cy_value->float_value = value;
return cy_value;
}
struct org_cherry_value*
__org_cherry_make_string(cy_byte_t* value)
{
struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
cy_value->meta.type = CY_STRING;
cy_value->string_value = value;
return cy_value;
}
struct org_cherry_value*
__org_cherry_make_char(cy_unicode_t value)
{
struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
cy_value->meta.type = CY_CHAR;
cy_value->char_value = value;
return cy_value;
}
// ----------------------------------------------------------------------------
// cherry list operations
// ----------------------------------------------------------------------------
struct org_cherry_pair*
__org_cherry_list_cons(struct org_cherry_value* head, struct org_cherry_value* tail)
{
struct org_cherry_pair* pair = GC_MALLOC(sizeof(struct org_cherry_pair));
pair->meta.type = CY_PAIR;
pair->head = head;
pair->tail = tail;
return pair;
}
struct org_cherry_value*
__org_cherry_list_head(struct org_cherry_pair* pair)
{
return pair->head;
}
struct org_cherry_value* __org_cherry_list_tail(struct org_cherry_pair* pair)
{
return pair->tail;
}
struct org_cherry_value* __org_cherry_list_length(struct org_cherry_pair* pair)
{
cy_fixnum_t length = 0;
while(pair->meta.type == CY_PAIR) {
length++;
pair = (struct org_cherry_pair*) pair->tail;
}
return __org_cherry_make_fixnum(length);
}
struct org_cherry_value*
__org_cherry_primitive_add(struct org_cherry_pair* pair)
{
cy_fixnum_t result = 0;
while(pair->meta.type == CY_PAIR) {
result += pair->head->fixnum_value;
pair = (struct org_cherry_pair*) pair->tail;
}
return __org_cherry_make_fixnum(result);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment