Commit 57f7466b authored by Chris Müller's avatar Chris Müller
Browse files

fix last crystal artifacts and add if statements

parent 949912ab
......@@ -12,7 +12,6 @@ set(CHERRY_VERSION 0.1)
include_directories(crystal/include)
include_directories(include)
add_subdirectory(crystal)
add_subdirectory(source)
add_subdirectory(test)
......@@ -19,7 +19,7 @@
#pragma once
#include "cherry/runtime.h"
#include <crystal/standard.h>
#include "cherry/standard.h"
#include <stdlib.h>
#include <stdint.h>
......@@ -30,15 +30,15 @@ struct org_cherry_array;
struct org_cherry_context {
const char* filename;
const byte_t* begin;
const byte_t* src;
const cy_byte_t* begin;
const cy_byte_t* src;
struct org_cherry_array* buffer;
cy_flags_t flags;
};
struct org_cherry_context* org_cherry_context(const byte_t* source, const char* filename, cy_flags_t flags);
struct org_cherry_context* org_cherry_context_repl(const byte_t* source);
struct org_cherry_context* org_cherry_context(const cy_byte_t* source, const char* filename, cy_flags_t flags);
struct org_cherry_context* org_cherry_context_repl(const cy_byte_t* source);
void org_cherry_error(struct org_cherry_context* context, const char* format, ...);
......@@ -64,13 +64,13 @@ enum org_cherry_tok {
};
const byte_t* org_cherry_tok_to_string(enum org_cherry_tok token);
const cy_byte_t* org_cherry_tok_to_string(enum org_cherry_tok token);
enum org_cherry_tok org_cherry_lex(struct org_cherry_context* context);
const byte_t* org_cherry_pos(struct org_cherry_context* context);
void org_cherry_rewind(struct org_cherry_context* context, const byte_t* pos);
const cy_byte_t* org_cherry_pos(struct org_cherry_context* context);
void org_cherry_rewind(struct org_cherry_context* context, const cy_byte_t* pos);
const byte_t* org_cherry_token_string(struct org_cherry_context* context);
const cy_byte_t* org_cherry_token_string(struct org_cherry_context* context);
size_t org_cherry_token_length(struct org_cherry_context* context);
struct org_cherry_value* org_cherry_read(struct org_cherry_context* context);
......@@ -139,8 +139,6 @@ extern struct org_cherry_value* org_cherry_symbol_quote;
extern struct org_cherry_value* org_cherry_symbol_define;
extern struct org_cherry_value* org_cherry_symbol_lambda;
extern struct org_cherry_value* org_cherry_symbol_if;
extern struct org_cherry_value* org_cherry_symbol_cond;
extern struct org_cherry_value* org_cherry_symbol_else;
......@@ -148,6 +146,10 @@ extern struct org_cherry_value* org_cherry_symbol_else;
// Evaluation
// ----------------------------------------------------------------------------
#define IS_FALSE(obj) (IS_BOOLEAN(obj) && !obj->boolean_value)
#define IS_TRUE(obj) (!IS_FALSE(obj))
#define IS_SELF_EVALUATING(value) \
(IS_BOOLEAN(value) || IS_FIXNUM(value) || IS_CHAR(value) || IS_STRING(value) || IS_FLOAT(value))
......@@ -166,12 +168,6 @@ extern struct org_cherry_value* org_cherry_symbol_else;
#define IS_IF(value) \
IS_TAGGED(value, org_cherry_symbol_if)
#define IS_COND(value) \
IS_TAGGED(value, org_cherry_symbol_cond)
#define IS_ELSE(value) \
IS_TAGGED(value, org_cherry_symbol_else)
#define IS_LAMBDA(value) \
IS_TAGGED(value, org_cherry_symbol_lambda)
......
......@@ -12,7 +12,7 @@ int main(int argc, char** argv)
struct org_cherry_symbollist* env = org_cherry_symbollist();
while(1) {
byte_t* line = (byte_t*) readline("> ");
cy_byte_t* line = (cy_byte_t*) readline("> ");
struct org_cherry_context* context = org_cherry_context_repl(line);
struct org_cherry_value* exp = org_cherry_read(context);
......
......@@ -102,19 +102,38 @@ org_cherry_apply(struct org_cherry_value* operator, struct org_cherry_value* ope
}
static struct org_cherry_value*
org_cherry_eval_if(struct org_cherry_symbollist* env, struct org_cherry_value* value)
{
if(IS_NULL(value))
return org_cherry_false;
if(IS_TRUE(org_cherry_eval(env, HEAD(value))))
return !IS_NULL(TAIL(value)) ? org_cherry_eval(env, HEAD(TAIL(value))) : org_cherry_false;
else if(!IS_NULL(TAIL(TAIL(value))))
return org_cherry_eval(env, HEAD(TAIL(TAIL(value))));
else
return org_cherry_false;
}
struct org_cherry_value*
org_cherry_eval(struct org_cherry_symbollist* env, struct org_cherry_value* value)
{
if(IS_SELF_EVALUATING(value))
return value;
else if(IS_VARIABLE(value))
return org_cherry_env_lookup(env, value);
else if(IS_QUOTE(value))
else if(IS_VARIABLE(value)) {
if(org_cherry_env_lookup(env, value))
return org_cherry_true;
else
return org_cherry_false;
} else if(IS_QUOTE(value))
return TEXT_OF_QUOTATION(value);
else if(IS_DEFINE(value))
return org_cherry_eval_define(env, value);
else if(IS_LAMBDA(value))
return org_cherry_procedure(env, HEAD(TAIL(value)), TAIL(TAIL(value)));
else if(IS_IF(value))
return org_cherry_eval_if(env, TAIL(value));
else if(IS_APPLICATION(value))
return org_cherry_apply(org_cherry_eval(env, HEAD(value)), org_cherry_eval_values(env, TAIL(value)));
......
......@@ -27,13 +27,13 @@
#include <gc.h>
struct org_cherry_context*
org_cherry_context(const byte_t* source, const char* filename, cy_flags_t flags)
org_cherry_context(const cy_byte_t* source, const char* filename, cy_flags_t flags)
{
struct org_cherry_context* context = GC_MALLOC(sizeof(struct org_cherry_context));
context->filename = filename;
context->begin = source;
context->src = (byte_t*) source;
context->buffer = org_cherry_array_new(sizeof(byte_t));
context->src = (cy_byte_t*) source;
context->buffer = org_cherry_array_new(sizeof(cy_byte_t));
context->flags = flags;
return context;
......@@ -41,7 +41,7 @@ org_cherry_context(const byte_t* source, const char* filename, cy_flags_t flags)
struct org_cherry_context*
org_cherry_context_repl(const byte_t* source)
org_cherry_context_repl(const cy_byte_t* source)
{
return org_cherry_context(source, 0, CY_SUPRESS_COMMENTS);
}
......@@ -67,13 +67,13 @@ org_cherry_error(struct org_cherry_context* context, const char* format, ...)
struct Mapping {
byte_t* string;
cy_byte_t* string;
enum org_cherry_tok value;
};
const byte_t*
const cy_byte_t*
org_cherry_tok_to_string(enum org_cherry_tok token)
{
switch(token) {
......@@ -127,8 +127,8 @@ static enum org_cherry_tok
lex_float(struct org_cherry_context* context)
{
struct org_cherry_array* buffer = context->buffer;
const byte_t* p = context->src;
unicode_t ch = org_cherry_utf8_get(p);
const cy_byte_t* p = context->src;
cy_unicode_t ch = org_cherry_utf8_get(p);
enum FloatState state = FP_START;
......@@ -219,8 +219,8 @@ static enum org_cherry_tok
lex_number(struct org_cherry_context* context)
{
struct org_cherry_array* buffer = context->buffer;
const byte_t* p = context->src;
unicode_t ch = org_cherry_utf8_get(p);
const cy_byte_t* p = context->src;
cy_unicode_t ch = org_cherry_utf8_get(p);
enum org_cherry_tok token = TOK_DEC;
enum NumberState state = INT_START;
......@@ -329,8 +329,8 @@ lex_character(struct org_cherry_context* context)
assert(context != 0);
struct org_cherry_array* buffer = context->buffer;
byte_t* p = org_cherry_utf8_next(context->src);
unicode_t ch = org_cherry_utf8_get(p);
cy_byte_t* p = org_cherry_utf8_next(context->src);
cy_unicode_t ch = org_cherry_utf8_get(p);
enum CharState state = CHAR_EAT;
int unicount = 0;
......@@ -405,8 +405,8 @@ lex_string(struct org_cherry_context* context)
struct org_cherry_array* buffer = context->buffer;
enum StringState state = STR_EAT;
byte_t* p = org_cherry_utf8_next(context->src);
unicode_t ch = org_cherry_utf8_get(p);
cy_byte_t* p = org_cherry_utf8_next(context->src);
cy_unicode_t ch = org_cherry_utf8_get(p);
int unicount = 0;
while(ch != '\0') {
......@@ -493,8 +493,8 @@ static enum org_cherry_tok
lex_comment(struct org_cherry_context* context)
{
struct org_cherry_array* buffer = context->buffer;
const byte_t* p = context->src;
unicode_t ch = org_cherry_utf8_get(p);
const cy_byte_t* p = context->src;
cy_unicode_t ch = org_cherry_utf8_get(p);
while(ch != '\0' && ch != '\r' && ch != '\n') {
org_cherry_array_append(buffer, p, org_cherry_utf8_codepoints(p));
......@@ -512,7 +512,7 @@ lex_comment(struct org_cherry_context* context)
static int
is_symbol_character(unicode_t ch)
is_symbol_character(cy_unicode_t ch)
{
return org_cherry_unicode_isalnum(ch) ||
ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' ||
......@@ -524,8 +524,8 @@ static enum org_cherry_tok
lex_symbol(struct org_cherry_context* context)
{
struct org_cherry_array* buffer = context->buffer;
const byte_t* p = context->src;
unicode_t ch = org_cherry_utf8_get(p);
const cy_byte_t* p = context->src;
cy_unicode_t ch = org_cherry_utf8_get(p);
while(is_symbol_character(ch)) {
org_cherry_array_append(buffer, p, org_cherry_utf8_codepoints(p));
......@@ -538,7 +538,7 @@ lex_symbol(struct org_cherry_context* context)
org_cherry_array_append(buffer, "\0", 1);
byte_t* sym = org_cherry_array_get(buffer, 0);
cy_byte_t* sym = org_cherry_array_get(buffer, 0);
if (strcmp(sym, "true") == 0)
return TOK_TRUE;
......@@ -560,11 +560,11 @@ org_cherry_lex(struct org_cherry_context* context)
assert(context != 0);
while(TRUE) {
const byte_t* p = context->src;
const cy_byte_t* p = context->src;
org_cherry_array_clear(context->buffer);
unicode_t ch = org_cherry_utf8_get(p);
cy_unicode_t ch = org_cherry_utf8_get(p);
if(org_cherry_unicode_isspace(ch)) {
p = org_cherry_utf8_next(p);
......@@ -617,7 +617,7 @@ org_cherry_lex(struct org_cherry_context* context)
return TOK_EOF;
}
const byte_t*
const cy_byte_t*
org_cherry_pos(struct org_cherry_context* context)
{
assert(context != 0);
......@@ -625,7 +625,7 @@ org_cherry_pos(struct org_cherry_context* context)
}
void
org_cherry_rewind(struct org_cherry_context* context, const byte_t* pos)
org_cherry_rewind(struct org_cherry_context* context, const cy_byte_t* pos)
{
assert(context != 0);
assert(context->begin <= pos && pos <= context->src);
......@@ -634,12 +634,12 @@ org_cherry_rewind(struct org_cherry_context* context, const byte_t* pos)
}
const byte_t*
const cy_byte_t*
org_cherry_token_string(struct org_cherry_context* context)
{
assert(context->buffer != 0);
return (const byte_t*) org_cherry_array_get(context->buffer, 0);
return (const cy_byte_t*) org_cherry_array_get(context->buffer, 0);
}
......@@ -655,7 +655,7 @@ org_cherry_token_length(struct org_cherry_context* context)
static struct org_cherry_value*
org_cherry_read_pair(struct org_cherry_context* context)
{
const byte_t* pos;
const cy_byte_t* pos;
enum org_cherry_tok tok;
struct org_cherry_value* head;
struct org_cherry_value* tail;
......
......@@ -33,9 +33,7 @@ struct org_cherry_value* org_cherry_false;
struct org_cherry_value* org_cherry_symbol_define;
struct org_cherry_value* org_cherry_symbol_lambda;
struct org_cherry_value* org_cherry_symbol_cond;
struct org_cherry_value* org_cherry_symbol_if;
struct org_cherry_value* org_cherry_symbol_else;
struct org_cherry_value* org_cherry_symbol_quote;
// ----------------------------------------------------------------------------
......@@ -316,8 +314,6 @@ org_cherry_initialize(struct org_cherry_pair* arguments)
org_cherry_symbol_define = org_cherry_symbol("define");
org_cherry_symbol_lambda = org_cherry_symbol("lambda");
org_cherry_symbol_if = org_cherry_symbol("if");
org_cherry_symbol_cond = org_cherry_symbol("cond");
org_cherry_symbol_else = org_cherry_symbol("else");
}
// ----------------------------------------------------------------------------
......
......@@ -21,9 +21,9 @@
#include <stdio.h>
#include <string.h>
static void test_lex_fixnum(const_pointer data)
static void test_lex_fixnum(cy_const_pointer_t data)
{
byte_t* fixnum = "0 5 0xFF 0xFFFF 1000 0777 0b0001 0b00011111";
cy_byte_t* fixnum = "0 5 0xFF 0xFFFF 1000 0777 0b0001 0b00011111";
struct org_cherry_context* c = org_cherry_context_repl(fixnum);
assert(org_cherry_lex(c) == TOK_DEC);
......@@ -52,9 +52,9 @@ static void test_lex_fixnum(const_pointer data)
}
static void test_lex_float(const_pointer data)
static void test_lex_float(cy_const_pointer_t data)
{
byte_t* fixnum = "1.0 4e+1 5e-10 1.0e20";
cy_byte_t* fixnum = "1.0 4e+1 5e-10 1.0e20";
struct org_cherry_context* c = org_cherry_context_repl(fixnum);
assert(org_cherry_lex(c) == TOK_FLOAT);
......@@ -71,9 +71,9 @@ static void test_lex_float(const_pointer data)
}
static void test_lex_string(const_pointer data)
static void test_lex_string(cy_const_pointer_t data)
{
byte_t* str = " \"asdf\" \"test\\uFFFF\\UFFFFFFFF\\n\" ";
cy_byte_t* str = " \"asdf\" \"test\\uFFFF\\UFFFFFFFF\\n\" ";
struct org_cherry_context* c = org_cherry_context_repl(str);
assert(org_cherry_lex(c) == TOK_STRING);
......@@ -84,9 +84,9 @@ static void test_lex_string(const_pointer data)
}
static void test_lex_comment(const_pointer data)
static void test_lex_comment(cy_const_pointer_t data)
{
byte_t* comment = "; comment\n";
cy_byte_t* comment = "; comment\n";
struct org_cherry_context* c = org_cherry_context(comment, 0, CY_DEFAULT);
assert(org_cherry_lex(c) == TOK_COMMENT);
......@@ -94,9 +94,9 @@ static void test_lex_comment(const_pointer data)
}
static void test_lex_symbols(const_pointer data)
static void test_lex_symbols(cy_const_pointer_t data)
{
byte_t* str = " true false null? + - ";
cy_byte_t* str = " true false null? + - ";
struct org_cherry_context* c = org_cherry_context_repl(str);
assert(org_cherry_lex(c) == TOK_TRUE);
......@@ -113,9 +113,9 @@ static void test_lex_symbols(const_pointer data)
}
static void test_lex_characters(const_pointer data)
static void test_lex_characters(cy_const_pointer_t data)
{
byte_t* str = " \\a \\A \\uFFFF \\newline \\space ";
cy_byte_t* str = " \\a \\A \\uFFFF \\newline \\space ";
struct org_cherry_context* c = org_cherry_context_repl(str);
assert(org_cherry_lex(c) == TOK_CHAR);
......@@ -135,9 +135,9 @@ static void test_lex_characters(const_pointer data)
}
static void test_lex_core(const_pointer data)
static void test_lex_core(cy_const_pointer_t data)
{
byte_t* str = " [ ] ( ) . ' ";
cy_byte_t* str = " [ ] ( ) . ' ";
struct org_cherry_context* c = org_cherry_context_repl(str);
assert(org_cherry_lex(c) == TOK_SQUARELEFTBRACE);
......@@ -149,14 +149,14 @@ static void test_lex_core(const_pointer data)
assert(org_cherry_lex(c) == TOK_EOF);
}
static void test_lex_rewind(const_pointer data)
static void test_lex_rewind(cy_const_pointer_t data)
{
byte_t* str = " [ ] ( ) . ' ";
cy_byte_t* str = " [ ] ( ) . ' ";
struct org_cherry_context* c = org_cherry_context_repl(str);
assert(org_cherry_lex(c) == TOK_SQUARELEFTBRACE);
const byte_t* position = org_cherry_pos(c);
const cy_byte_t* position = org_cherry_pos(c);
assert(org_cherry_lex(c) == TOK_SQUARERIGHTBRACE);
assert(org_cherry_lex(c) == TOK_ROUNDLEFTBRACE);
......
......@@ -28,8 +28,6 @@ static void test_initialization(cy_const_pointer_t data)
assert(IS_SYMBOL(org_cherry_symbol_define));
assert(IS_SYMBOL(org_cherry_symbol_lambda));
assert(IS_SYMBOL(org_cherry_symbol_if));
assert(IS_SYMBOL(org_cherry_symbol_cond));
assert(IS_SYMBOL(org_cherry_symbol_else));
}
static void test_symbollist(cy_const_pointer_t data)
......
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