Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Chris Müller
cherry
Commits
57f7466b
Commit
57f7466b
authored
Jul 11, 2013
by
Chris Müller
Browse files
fix last crystal artifacts and add if statements
parent
949912ab
Changes
9
Show whitespace changes
Inline
Side-by-side
CMakeLists.txt
View file @
57f7466b
...
...
@@ -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
)
include/cherry/read.h
View file @
57f7466b
...
...
@@ -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
);
include/cherry/runtime.h
View file @
57f7466b
...
...
@@ -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)
...
...
source/cherry.c
View file @
57f7466b
...
...
@@ -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
);
...
...
source/eval.c
View file @
57f7466b
...
...
@@ -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
)));
...
...
source/read.c
View file @
57f7466b
...
...
@@ -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
;
...
...
source/value.c
View file @
57f7466b
...
...
@@ -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"
);
}
// ----------------------------------------------------------------------------
...
...
test/lex.c
View file @
57f7466b
...
...
@@ -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
);
...
...
test/runtime.c
View file @
57f7466b
...
...
@@ -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
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment