From 5f16d1fbb993b91b20f353b8cf79edfc13da54a4 Mon Sep 17 00:00:00 2001 From: FelixBrendel Date: Wed, 26 Sep 2018 11:32:55 +0200 Subject: [PATCH] Reorganize folders and added windows build with MSVC for debugging --- .gitignore | 3 ++- build.bat | 36 ++++++++++++++++++++++++---- build.sh | 7 ++++-- .dir-locals.el => src/.dir-locals.el | 0 assert.c => src/assert.c | 0 ast.c => src/ast.c | 0 built_ins.c => src/built_ins.c | 1 + env.c => src/env.c | 0 error.c => src/error.c | 2 +- eval.c => src/eval.c | 33 +++++++++++++++---------- helpers.c => src/helpers.c | 0 io.c => src/io.c | 3 ++- main.c => src/main.c | 2 +- parse.c => src/parse.c | 0 test.lsp => src/test.lsp | 0 testing.c => src/testing.c | 6 ++--- 16 files changed, 66 insertions(+), 27 deletions(-) rename .dir-locals.el => src/.dir-locals.el (100%) rename assert.c => src/assert.c (100%) rename ast.c => src/ast.c (100%) rename built_ins.c => src/built_ins.c (99%) rename env.c => src/env.c (100%) rename error.c => src/error.c (94%) rename eval.c => src/eval.c (77%) rename helpers.c => src/helpers.c (100%) rename io.c => src/io.c (98%) rename main.c => src/main.c (100%) rename parse.c => src/parse.c (100%) rename test.lsp => src/test.lsp (100%) rename testing.c => src/testing.c (97%) diff --git a/.gitignore b/.gitignore index 81fba18..af8047f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /*.exe /*.ilk /*.pdb -/.vs/ \ No newline at end of file +/.vs/ +/vs/ \ No newline at end of file diff --git a/build.bat b/build.bat index ac698e3..09e3643 100644 --- a/build.bat +++ b/build.bat @@ -1,7 +1,33 @@ @echo off -clang main.c -g -o lisp.exe --std=c99 || exit /b 1 +pushd %~dp0 -echo. -echo --- Output Start --- -lisp test.lsp -echo --- Output End --- +set exeName=lisp.exe +set binDir=bin + +mkdir quickbuild +pushd quickbuild + +cl^ + ../src/main.c^ + /Fe%exeName% /W3 /TC^ + /nologo /EHsc /Z7^ + /link /incremental /debug:fastlink + +if %errorlevel% == 0 ( + echo. + if not exist ..\%binDir% mkdir ..\%binDir% + move %exeName% ..\%binDir%\ > NUL + pushd ..\%binDir% + echo ---------- Output start ---------- + %exeName% + echo ---------- Output end ---------- + del %exeName% /S /Q > NUL + popd +) else ( + echo. + echo Fucki'n 'ell +) + +popd +rd quickbuild /S /Q +popd diff --git a/build.sh b/build.sh index 3cb9ac6..a7263d2 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,9 @@ -clang main.c -g -o lisp --std=c99 || exit 1 +mkdir quickbuild +clang src/main.c -g -o ./quickbuild/lisp --std=c99 || exit 1 echo "" echo "--- Output Start ---" -./lisp test.lsp +./quickbuild/lisp echo "--- Output End ---" + +rm -rf quickbuild diff --git a/.dir-locals.el b/src/.dir-locals.el similarity index 100% rename from .dir-locals.el rename to src/.dir-locals.el diff --git a/assert.c b/src/assert.c similarity index 100% rename from assert.c rename to src/assert.c diff --git a/ast.c b/src/ast.c similarity index 100% rename from ast.c rename to src/ast.c diff --git a/built_ins.c b/src/built_ins.c similarity index 99% rename from built_ins.c rename to src/built_ins.c index d7131af..408306e 100644 --- a/built_ins.c +++ b/src/built_ins.c @@ -50,3 +50,4 @@ Ast_Node* built_in_divide(Ast_Node* operands) { } return create_ast_node_number(quotient); } + diff --git a/env.c b/src/env.c similarity index 100% rename from env.c rename to src/env.c diff --git a/error.c b/src/error.c similarity index 94% rename from error.c rename to src/error.c index 078a754..c860e12 100644 --- a/error.c +++ b/src/error.c @@ -38,6 +38,6 @@ char* Error_Type_to_string(Error_Type type) { case Error_Type_Wrong_Number_Of_Arguments: return "Wrong number of arguments"; case Error_Type_Type_Missmatch: return "Type Missmatch"; case Error_Type_Not_Yet_Implemented: return "Not yet implemented"; - case Error_Type_Unknown_Error: return "Unknown Error"; + default: return "Unknown Error"; } } diff --git a/eval.c b/src/eval.c similarity index 77% rename from eval.c rename to src/eval.c index 84bd96a..d698bb0 100644 --- a/eval.c +++ b/src/eval.c @@ -8,7 +8,7 @@ Ast_Node* eval_expr(Ast_Node* node, Environment* env); int is_truthy (Ast_Node* expression, Environment* env); int list_length(Ast_Node* node) { - if (node->type != Ast_Node_Type_Nil) + if (node->type == Ast_Node_Type_Nil) return 0; if (node->type != Ast_Node_Type_Pair) { @@ -16,12 +16,16 @@ int list_length(Ast_Node* node) { return 0; } - int len = 1; - while (1) { - break; + int len = 0; + while (node->type == Ast_Node_Type_Pair) { + ++len; + node = node->value.pair->rest; + if (node->type == Ast_Node_Type_Nil) + return len; } - return len; + create_error(Error_Type_Ill_Formed_List, node); + return 0; } void eval_operands(Ast_Node* operands, Environment* env) { @@ -36,8 +40,8 @@ void eval_operands(Ast_Node* operands, Environment* env) { } Ast_Node* eval_expr(Ast_Node* node, Environment* env) { -#define report_error(_type) \ - create_error(_type, node); \ +#define report_error(_type) \ + create_error(_type, node); \ return NULL Ast_Node* ret = new(Ast_Node); @@ -73,8 +77,10 @@ Ast_Node* eval_expr(Ast_Node* node, Environment* env) { eval_operands(operands, env); return built_in_divide(operands); } else if (string_equal("if", operator_name)) { - if (list_length(operands) != 3) { - report_error(Error_Type_Wrong_Number_Of_Arguments); + int operands_length = list_length(operands); + if (operands_length != 2 && operands_length != 3) { + create_error(Error_Type_Wrong_Number_Of_Arguments, operands); + return NULL; } Ast_Node* condition = operands->value.pair->first; @@ -83,12 +89,13 @@ Ast_Node* eval_expr(Ast_Node* node, Environment* env) { if (is_truthy(condition, env)) return eval_expr(then_part->value.pair->first, env); - else + else if (operands_length == 3) return eval_expr(else_part->value.pair->first, env); - /* } else if (string_equal("not", operator_name)) { */ + else return create_ast_node_nil(); + /* } else if (string_equal("not", operator_name)) { */ - /* } else if (string_equal("and", operator_name)) { */ - /* } else if (string_equal("or", operator_name)) { */ + /* } else if (string_equal("and", operator_name)) { */ + /* } else if (string_equal("or", operator_name)) { */ } else { report_error(Error_Type_Not_Yet_Implemented); } diff --git a/helpers.c b/src/helpers.c similarity index 100% rename from helpers.c rename to src/helpers.c diff --git a/io.c b/src/io.c similarity index 98% rename from io.c rename to src/io.c index f3cfd1a..bd64486 100644 --- a/io.c +++ b/src/io.c @@ -24,7 +24,7 @@ void log_message(Log_Level type, char* message) { case Log_Level_Warning: prefix = "WARNING"; break; case Log_Level_Info: prefix = "INFO"; break; case Log_Level_Debug: prefix = "DEBUG"; break; - case Log_Level_None: return; + default: return; } printf("%s: %s\n",prefix, message); } @@ -47,6 +47,7 @@ char* Ast_Node_Type_to_string(Ast_Node_Type type) { case(Ast_Node_Type_Built_In_Function): return "built-in function"; case(Ast_Node_Type_Pair): return "pair"; } + return "unknown"; } void print(Ast_Node* node) { diff --git a/main.c b/src/main.c similarity index 100% rename from main.c rename to src/main.c index 159b07e..3343dd4 100644 --- a/main.c +++ b/src/main.c @@ -11,8 +11,8 @@ #include "./io.c" #include "./assert.c" #include "./parse.c" -#include "./built_ins.c" #include "./env.c" +#include "./built_ins.c" #include "./eval.c" #include "./testing.c" diff --git a/parse.c b/src/parse.c similarity index 100% rename from parse.c rename to src/parse.c diff --git a/test.lsp b/src/test.lsp similarity index 100% rename from test.lsp rename to src/test.lsp diff --git a/testing.c b/src/testing.c similarity index 97% rename from testing.c rename to src/testing.c index bb15624..96ff2e1 100644 --- a/testing.c +++ b/src/testing.c @@ -28,13 +28,13 @@ #define assert_equal_int(variable, value) \ if (variable != value) { \ - print_assert_equal_fail(variable, value, int, "%d"); \ + print_assert_equal_fail(variable, value, size_t, "%zd"); \ return fail; \ } #define assert_not_equal_int(variable, value) \ if (variable == value) { \ - print_assert_not_equal_fail(variable, value, int, "%d"); \ + print_assert_not_equal_fail(variable, value, size_t, "%zd"); \ return fail; \ } @@ -60,7 +60,7 @@ #define invoke_test(name) \ printf("" #name ":"); \ if (name() == pass) { \ - for(int i = strlen(#name); i < 70; ++i) \ + for(size_t i = strlen(#name); i < 70; ++i) \ printf((i%3==1)? "." : " "); \ printf("%spassed%s\n", console_green, console_normal); \ } \