Skip to content

Commit

Permalink
Start to implement interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
ab25cq committed Jan 24, 2017
1 parent 2217b40 commit b245b57
Show file tree
Hide file tree
Showing 19 changed files with 2,591 additions and 881 deletions.
4 changes: 4 additions & 0 deletions Fundamental.clc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class String
}

def chars(index:int):char {
if(index < 0) {
index += self.len;
}

if(index>= 0 && index < self.len) {
return self.buffer[index];
}
Expand Down
341 changes: 341 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ LIB_OBJS=@LIB_OBJS@
##########################################################
# main
##########################################################
all: lib cclover2 clover2
all: lib cclover2 clover2 iclover2
if test $(OS) = DARWIN; then ctags src/*.c > /dev/null 2>&1; else ctags -R; fi

cclover2: config.h $(COMPILER_OBJS)
clang -o cclover2 $(COMPILER_OBJS) $(CFLAGS) -lclover2 $(LIBS)
cclover2: config.h src/compiler.o $(COMPILER_OBJS)
clang -o cclover2 src/compiler.o $(COMPILER_OBJS) $(CFLAGS) -lclover2 $(LIBS)

clover2: config.h $(OBJS)
clang -o clover2 $(OBJS) $(CFLAGS) -lclover2 $(LIBS)
clover2: config.h src/main.o $(OBJS)
clang -o clover2 src/main.o $(OBJS) $(CFLAGS) -lclover2 $(LIBS)

iclover2: config.h src/interpreter.o $(OBJS) $(COMPILER_OBJS)
clang -o iclover2 src/interpreter.o $(OBJS) $(COMPILER_OBJS) $(CFLAGS) -lclover2 $(LIBS)

lib: $(LIBSONAME)
# rm -f install
Expand Down Expand Up @@ -106,6 +109,7 @@ install:
$(INSTALL) -m 644 SortableArray.clcl ~/.clover2
$(INSTALL) -m 644 IHashKey.clcl ~/.clover2
$(INSTALL) -m 644 IEqualable.clcl ~/.clover2
$(INSTALL) -m 644 ISortable.clcl ~/.clover2
$(INSTALL) -m 644 HashItem.clcl ~/.clover2
$(INSTALL) -m 644 Hash.clcl ~/.clover2
$(INSTALL) -m 644 ListItem.clcl ~/.clover2
Expand All @@ -123,7 +127,10 @@ install:
$(INSTALL) -m 644 Tuple9.clcl ~/.clover2
$(INSTALL) -m 644 Tuple10.clcl ~/.clover2
$(INSTALL) -m 644 File.clcl ~/.clover2
$(INSTALL) -m 644 Path.clcl ~/.clover2
$(INSTALL) -m 644 Directory.clcl ~/.clover2
$(INSTALL) -m 644 termios.clcl ~/.clover2
$(INSTALL) -m 644 Job.clcl ~/.clover2
$(INSTALL) -m 644 Command.clcl ~/.clover2
$(INSTALL) -m 644 tm.clcl ~/.clover2
$(INSTALL) -m 644 stat.clcl ~/.clover2
Expand Down
4 changes: 3 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ FEATURES
1. primitive class and none primitive class with boxing and unboxing
2. Lambda and closure, regex are first-class object
3. Generics
4. No inheritance, only interfaces and classes
4. No inheritance, only interfaces and classes, modules
5. open class

See clover2 wiki on github (Japanese)

LICENSE is GPL. see LICENSE file

4 changes: 4 additions & 0 deletions String.clc
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,10 @@ class Buffer
}

def chars(index:size_t):byte throws Exception {
if(index < 0) {
index += self.len;
}

if(index >= 0 && index < self.len) {
return((self.buffer + index)->byte);
}
Expand Down
74 changes: 60 additions & 14 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -3794,8 +3794,54 @@ INSTALL="/usr/bin/install -c"
#AC_CHECK_HEADER(pthread.h, [AC_DEFINE(HAVE_PTHREAD_H, 1)], [ exit ])
#AC_HAVE_LIBRARY(pthread, [ LIBS="$LIBS -lpthread"; ], [ exit ])
#AC_CHECK_HEADER(readline/readline.h, [AC_DEFINE(HAVE_READLINE_H, 1)], [ exit ])
#AC_HAVE_LIBRARY(readline, [ LIBS="$LIBS -lreadline"; ], [ exit ])
ac_fn_c_check_header_mongrel "$LINENO" "readline/readline.h" "ac_cv_header_readline_readline_h" "$ac_includes_default"
if test "x$ac_cv_header_readline_readline_h" = xyes; then :
$as_echo "#define HAVE_READLINE_H 1" >>confdefs.h
else
exit
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lreadline" >&5
$as_echo_n "checking for main in -lreadline... " >&6; }
if ${ac_cv_lib_readline_main+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lreadline $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
int
main ()
{
return main ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_readline_main=yes
else
ac_cv_lib_readline_main=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_readline_main" >&5
$as_echo "$ac_cv_lib_readline_main" >&6; }
if test "x$ac_cv_lib_readline_main" = xyes; then :
LIBS="$LIBS -lreadline";
else
exit
fi
ac_cv_lib_readline=ac_cv_lib_readline_main
if test "$OS" != FREEBSD
then
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -ldl" >&5
Expand Down Expand Up @@ -3897,13 +3943,13 @@ if test "x$ac_cv_header_pcre_h" = xyes; then :
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lm" >&5
$as_echo_n "checking for main in -lm... " >&6; }
if ${ac_cv_lib_m_main+:} false; then :
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lpcre" >&5
$as_echo_n "checking for main in -lpcre... " >&6; }
if ${ac_cv_lib_pcre_main+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lm $LIBS"
LIBS="-lpcre $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
Expand All @@ -3917,20 +3963,20 @@ return main ();
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_m_main=yes
ac_cv_lib_pcre_main=yes
else
ac_cv_lib_m_main=no
ac_cv_lib_pcre_main=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_m_main" >&5
$as_echo "$ac_cv_lib_m_main" >&6; }
if test "x$ac_cv_lib_m_main" = xyes; then :
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_pcre_main" >&5
$as_echo "$ac_cv_lib_pcre_main" >&6; }
if test "x$ac_cv_lib_pcre_main" = xyes; then :
LIBS="$LIBS -lpcre";
fi
ac_cv_lib_m=ac_cv_lib_m_main
ac_cv_lib_pcre=ac_cv_lib_pcre_main
Expand All @@ -3944,8 +3990,8 @@ CFLAGS="$CFLAGS -Qunused-arguments"
OBJS="src/main.o src/vm.o src/object.o src/array.o src/string.o src/block.o src/regex.o src/integer.o src/byte.o src/short.o src/long.o src/float.o src/pointer.o src/char.o src/bool.o src/hash.o src/list.o src/tuple.o src/carray.o src/type.o src/stack.o src/heap.o src/exception.o src/native_method.o src/class_system.o src/class_clover.o src/clover_to_clang.o"
COMPILER_OBJS="src/compiler.o src/parser.o src/node_type.o src/node.o src/cast.o src/vtable.o src/script.o src/node_block.o src/node_block_type.o src/class_compiler.o src/klass_compile_time.o src/method_compiler.o src/module.o"
OBJS="src/vm.o src/object.o src/array.o src/string.o src/block.o src/regex.o src/integer.o src/byte.o src/short.o src/long.o src/float.o src/pointer.o src/char.o src/bool.o src/hash.o src/list.o src/tuple.o src/carray.o src/type.o src/stack.o src/heap.o src/exception.o src/native_method.o src/class_system.o src/class_clover.o src/clover_to_clang.o"
COMPILER_OBJS="src/parser.o src/node_type.o src/node.o src/cast.o src/vtable.o src/script.o src/node_block.o src/node_block_type.o src/class_compiler.o src/klass_compile_time.o src/method_compiler.o src/module.o"
LIB_OBJS="src/buffer.o src/debug.o src/xfunc.o src/klass.o src/constant.o src/code.o src/alignment.o src/utf.o"
Expand Down
10 changes: 6 additions & 4 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ INSTALL="/usr/bin/install -c"

#AC_CHECK_HEADER(pthread.h, [AC_DEFINE(HAVE_PTHREAD_H, 1)], [ exit ])
#AC_HAVE_LIBRARY(pthread, [ LIBS="$LIBS -lpthread"; ], [ exit ])
#AC_CHECK_HEADER(readline/readline.h, [AC_DEFINE(HAVE_READLINE_H, 1)], [ exit ])
#AC_HAVE_LIBRARY(readline, [ LIBS="$LIBS -lreadline"; ], [ exit ])

AC_CHECK_HEADER(readline/readline.h, [AC_DEFINE(HAVE_READLINE_H, 1)], [ exit ])
AC_HAVE_LIBRARY(readline, [ LIBS="$LIBS -lreadline"; ], [ exit ])

if test "$OS" != FREEBSD
then
AC_HAVE_LIBRARY(dl, [ LIBS="$LIBS -ldl"; ], [])
Expand Down Expand Up @@ -204,8 +206,8 @@ CFLAGS="$CFLAGS -Qunused-arguments"

AC_SUBST(CFLAGS)

OBJS="src/main.o src/vm.o src/object.o src/array.o src/string.o src/block.o src/regex.o src/integer.o src/byte.o src/short.o src/long.o src/float.o src/pointer.o src/char.o src/bool.o src/hash.o src/list.o src/tuple.o src/carray.o src/type.o src/stack.o src/heap.o src/exception.o src/native_method.o src/class_system.o src/class_clover.o src/clover_to_clang.o"
COMPILER_OBJS="src/compiler.o src/parser.o src/node_type.o src/node.o src/cast.o src/vtable.o src/script.o src/node_block.o src/node_block_type.o src/class_compiler.o src/klass_compile_time.o src/method_compiler.o src/module.o"
OBJS="src/vm.o src/object.o src/array.o src/string.o src/block.o src/regex.o src/integer.o src/byte.o src/short.o src/long.o src/float.o src/pointer.o src/char.o src/bool.o src/hash.o src/list.o src/tuple.o src/carray.o src/type.o src/stack.o src/heap.o src/exception.o src/native_method.o src/class_system.o src/class_clover.o src/clover_to_clang.o"
COMPILER_OBJS="src/parser.o src/node_type.o src/node.o src/cast.o src/vtable.o src/script.o src/node_block.o src/node_block_type.o src/class_compiler.o src/klass_compile_time.o src/method_compiler.o src/module.o"
LIB_OBJS="src/buffer.o src/debug.o src/xfunc.o src/klass.o src/constant.o src/code.o src/alignment.o src/utf.o"

AC_SUBST(OBJS)
Expand Down
2 changes: 1 addition & 1 deletion debug.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export LD_LIBRARY_PATH=.; gdb ./cclover2
export LD_LIBRARY_PATH=.; gdb ./iclover2

Binary file added iclover2
Binary file not shown.
1 change: 1 addition & 0 deletions iclover2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export LD_LIBRARY_PATH=.; ./iclover2
6 changes: 2 additions & 4 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,8 @@ unsigned int sNodeTree_create_path_value(MANAGED char* value, int len);

/// script.c ///
BOOL compile_script(char* fname, char* source);
BOOL read_source(char* fname, sBuf* source);
BOOL delete_comment(sBuf* source, sBuf* source2);

/// cast.c ///
void cast_right_type_to_byte(sNodeType** right_type, sCompileInfo* info);
Expand Down Expand Up @@ -1913,10 +1915,6 @@ BOOL initialize_equalable_carray_object(CLObject array_object, int num_elements,
CLObject create_sortable_carray_object_with_elements(int num_elements, CLObject* elements);
BOOL initialize_sortable_carray_object(CLObject array_object, int num_elements, CLObject* items, CLVALUE* stack, int var_num, CLVALUE** stack_ptr, sVMInfo* info, sCLClass* class_items);

/// compiler.c ///
BOOL read_source(char* fname, sBuf* source);
BOOL delete_comment(sBuf* source, sBuf* source2);

/// utf.c ///
int utf8_index_to_utf32_index(char* str, int utf8index);
int utf32_index_to_utf8_index(char* str, int utf32index);
Expand Down
108 changes: 0 additions & 108 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,114 +19,6 @@ static void compiler_final()
final_vtable();
}

BOOL read_source(char* fname, sBuf* source)
{
int f = open(fname, O_RDONLY);

if(f < 0) {
fprintf(stderr, "%s doesn't exist\n", fname);
return FALSE;
}

while(1) {
char buf[BUFSIZ];
int size = read(f, buf, BUFSIZ);

if(size < 0) {
fprintf(stderr, "unexpected error\n");
close(f);
return FALSE;
}

buf[size] = 0;
sBuf_append_str(source, buf);

if(size < BUFSIZ) {
break;
}
}

close(f);

return TRUE;
}

BOOL delete_comment(sBuf* source, sBuf* source2)
{
char* p;
BOOL in_string;

p = source->mBuf;

in_string = FALSE;

while(*p) {
if(*p == '"') {
in_string = !in_string;
sBuf_append_char(source2, *p);
p++;
}
else if(!in_string && *p =='#')
{
p++;

while(1) {
if(*p == 0) {
break;
}
else if(*p == '\n') {
//p++; // no delete line field for error message
break;
}
else {
p++;
}
}
}
else if(!in_string && *p == '/' && *(p+1) == '*') {
int nest;

p+=2;
nest = 0;
while(1) {
if(*p == '"') {
p++;
in_string = !in_string;
}
else if(*p == 0) {
fprintf(stderr, "there is not a comment end until source end\n");
return FALSE;
}
else if(!in_string && *p == '/' && *(p+1) == '*') {
p+=2;
nest++;
}
else if(!in_string && *p == '*' && *(p+1) == '/') {
p+=2;
if(nest == 0) {
break;
}

nest--;
}
else if(*p == '\n') {
sBuf_append_char(source2, *p); // no delete line field for error message
p++;
}
else {
p++;
}
}
}
else {
sBuf_append_char(source2, *p);
p++;
}
}

return TRUE;
}

static BOOL compiler(char* fname)
{
if(access(fname, F_OK) != 0) {
Expand Down
Loading

0 comments on commit b245b57

Please sign in to comment.