Skip to content

Commit

Permalink
fix(yakshalisp): fixes to enable/disable print, rename set to setq
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Jul 30, 2023
1 parent c6424e9 commit 4517818
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 93 deletions.
2 changes: 1 addition & 1 deletion compiler/src/utilities/cpp_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//#define YAKSHA_DEBUG_COMPILE
//#endif
//// ---- yaksha high level debug enabled if we are in possible debug mode
//#define YAKSHA_DEBUG
// #define YAKSHA_DEBUG
#if !defined(YAKSHA_DEBUG) && \
(defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG)) && \
!defined(YAKSHA_FAST_TESTS)
Expand Down
9 changes: 1 addition & 8 deletions compiler/src/yaksha_lisp/prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ const std::string YAKSHA_LISP_PRELUDE = R"<><><><>(
(def YK_PRELUDE_INCLUDED 1)
# =========== #
(defun noop (&x) (ghost nil))
# Disable print and println
# You can enable it by calling (enable_print)
(= n____original_print print)
(= n____original_println println)
(defun disable_print () (= print noop) (= println noop))
(defun enable_print () (= print n____original_print) (= println n____original_println))
(disable_print)
# =========== #
# Base functionality
# =========== #
Expand Down Expand Up @@ -244,7 +237,7 @@ const std::string YAKSHA_LISP_PRELUDE = R"<><><><>(
(defun ykt_keyword_try () (yk_create_token YK_TOKEN_KEYWORD_TRY "try"))
(defun ykt_keyword_while () (yk_create_token YK_TOKEN_KEYWORD_WHILE "while"))
(system_enable_gc)
(system_lock_current_scope)
(system_lock_root_scope)
)<><><><>";
#endif
9 changes: 1 addition & 8 deletions compiler/src/yaksha_lisp/prelude_template.yaka
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ macros! {
(def YK_PRELUDE_INCLUDED 1)
# =========== #
(defun noop (&x) (ghost nil))
# Disable print and println
# You can enable it by calling (enable_print)
(= n____original_print print)
(= n____original_println println)
(defun disable_print () (= print noop) (= println noop))
(defun enable_print () (= print n____original_print) (= println n____original_println))
(disable_print)
# =========== #
# Base functionality
# =========== #
Expand Down Expand Up @@ -55,6 +48,6 @@ macros! {
(defun ykt_newline () (yk_create_token YK_TOKEN_NEW_LINE "\n"))
# $TOKEN_GENERATION$ #
(system_enable_gc)
(system_lock_current_scope)
(system_lock_root_scope)
# _ END _ #
}
32 changes: 25 additions & 7 deletions compiler/src/yaksha_lisp/yaksha_lisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ void yaksha_envmap::setup_builtins() {
yaksha_lisp_builtins::bitwise_right_shift_));
// set builtin will update the value of a symbol in the current scope or parent scope(s)
// as long as the symbol is defined
set("set", create_builtin(this, "set", yaksha_lisp_builtins::set_));
set("setq", create_builtin(this, "setq", yaksha_lisp_builtins::setq_));
set("index", create_builtin(this, "index", yaksha_lisp_builtins::index_));
set("map_get",
create_builtin(this, "map_get", yaksha_lisp_builtins::map_get_));
Expand Down Expand Up @@ -918,13 +918,23 @@ void yaksha_envmap::setup_builtins() {
create_builtin(this, "magic_dot", yaksha_lisp_builtins::magic_dot_));
// ---
set("system_enable_gc",
create_builtin(this, "system_enable_gc", yaksha_lisp_builtins::system_enable_gc_));
create_builtin(this, "system_enable_gc",
yaksha_lisp_builtins::system_enable_gc_));
set("system_disable_gc",
create_builtin(this, "system_disable_gc", yaksha_lisp_builtins::system_disable_gc_));
set("system_lock_current_scope",
create_builtin(this, "system_lock_current_scope", yaksha_lisp_builtins::system_lock_current_scope_));
set("system_unlock_current_scope",
create_builtin(this, "system_unlock_current_scope", yaksha_lisp_builtins::system_unlock_current_scope_));
create_builtin(this, "system_disable_gc",
yaksha_lisp_builtins::system_disable_gc_));
set("system_lock_root_scope",
create_builtin(this, "system_lock_root_scope",
yaksha_lisp_builtins::system_lock_root_scope_));
set("system_unlock_root_scope",
create_builtin(this, "system_unlock_root_scope",
yaksha_lisp_builtins::system_unlock_root_scope_));
set("system_enable_print",
create_builtin(this, "system_enable_print",
yaksha_lisp_builtins::system_enable_print_));
set("system_disable_print",
create_builtin(this, "system_disable_print",
yaksha_lisp_builtins::system_disable_print_));
builtins_created_ = true;
}
void yaksha_envmap::push_closure(yaksha_envmap *env) {
Expand Down Expand Up @@ -1045,6 +1055,8 @@ void yaksha_envmap::setup_prelude() {
}
void yaksha_envmap::lockdown() { this->locked_down_env_ = true; }
void yaksha_envmap::unlock() { this->locked_down_env_ = false; }
void yaksha_envmap::lockdown_root() { mm_->root_lock(); }
void yaksha_envmap::unlock_root() { mm_->root_unlock(); }
void yaksha_envmap::gc_enable() {
if (this->mm_ != nullptr) { this->mm_->enable_gc(); }
}
Expand Down Expand Up @@ -1536,6 +1548,12 @@ void yaksha_macros::gc_mark() {
}
void yaksha_macros::enable_gc() { this->enable_gc_ = true; }
void yaksha_macros::disable_gc() { this->enable_gc_ = false; }
void yaksha_macros::root_lock() {
if (builtins_root_ != nullptr) { builtins_root_->lockdown(); }
}
void yaksha_macros::root_unlock() {
if (builtins_root_ != nullptr) { builtins_root_->unlock(); }
}
void yaksha_lisp_value::gc_mark(yk_memory_manager *memory_manager) {
if (get_bit(&mark_, GC_POOL_MARK_BIT)) { return; }
memory_manager->mark(this);// this is used
Expand Down
6 changes: 6 additions & 0 deletions compiler/src/yaksha_lisp/yaksha_lisp.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ namespace yaksha {
virtual void gc_mark() = 0;
virtual void enable_gc() = 0;
virtual void disable_gc() = 0;
virtual void root_lock() = 0;
virtual void root_unlock() = 0;
};
// ┬┌┬┐┌─┐┌─┐┬─┐┌┬┐ ┬─┐┌─┐┌─┐┌─┐┬ ┬ ┬┌─┐┬─┐
// ││││├─┘│ │├┬┘ │ ├┬┘├┤ └─┐│ ││ └┐┌┘├┤ ├┬┘
Expand Down Expand Up @@ -269,6 +271,8 @@ namespace yaksha {
std::string identifier_{};
bool builtins_created_{false};
bool prelude_evaluate_success_{false};
void lockdown_root();
void unlock_root();

private:
std::vector<yaksha_envmap *> closures_{};
Expand Down Expand Up @@ -354,6 +358,8 @@ namespace yaksha {
void gc_mark() override;
void enable_gc() override;
void disable_gc() override;
void root_lock() override;
void root_unlock() override;

private:
bool enable_gc_{false};
Expand Down
41 changes: 29 additions & 12 deletions compiler/src/yaksha_lisp/yaksha_lisp_builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unordered_set>
using namespace yaksha;
// TODO make the code here nicer, too much copy pasta
static bool yaksha_macro_print_allowed = false;
static yaksha_lisp_value *
eq_operator(yaksha_envmap *env,
const std::vector<yaksha_lisp_value *> &e_args) {
Expand Down Expand Up @@ -346,9 +347,9 @@ yaksha_lisp_builtins::print_(const std::vector<yaksha_lisp_value *> &args,
auto e_args = eval_args(args, env);
auto r = yaksha_lisp_builtins::copy_val(env, e_args[0]);
if (r->type_ == yaksha_lisp_value_type::NUMBER) {
std::cout << r->num_;
if (yaksha_macro_print_allowed) { std::cout << r->num_; }
} else if (r->type_ == yaksha_lisp_value_type::STRING) {
std::cout << r->str_;
if (yaksha_macro_print_allowed) { std::cout << r->str_; }
} else {
throw parsing_error{"print/println takes only numbers or strings", "", 0,
0};
Expand All @@ -359,7 +360,7 @@ yaksha_lisp_value *
yaksha_lisp_builtins::println_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env) {
auto v = yaksha_lisp_builtins::print_(args, env);
std::cout << std::endl;
if (yaksha_macro_print_allowed) { std::cout << std::endl; }
return v;
}
yaksha_lisp_value *
Expand Down Expand Up @@ -467,15 +468,15 @@ yaksha_lisp_builtins::raise_error_(const std::vector<yaksha_lisp_value *> &args,
throw parsing_error{r->str_, "", 0, 0};
}
yaksha_lisp_value *
yaksha_lisp_builtins::set_(const std::vector<yaksha_lisp_value *> &args,
yaksha_lisp_builtins::setq_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env) {
if (args.size() != 2) {
throw parsing_error{"set takes 2 arguments", "", 0, 0};
throw parsing_error{"setq takes 2 arguments", "", 0, 0};
}
auto name = args[0];
if (!(name->type_ == yaksha_lisp_value_type::EXPR &&
name->expr_->token_->type_ == yaksha_lisp_token_type::SYMBOL)) {
throw parsing_error{"set takes a symbol as first argument", "", 0, 0};
throw parsing_error{"setq takes a symbol as first argument", "", 0, 0};
}
auto val = env->eval(args[1]);
env->set(name->expr_->token_->token_, val, false);
Expand Down Expand Up @@ -1564,20 +1565,20 @@ yaksha_lisp_builtins::magic_dot_(const std::vector<yaksha_lisp_value *> &args,
throw parsing_error{"magic_dot takes a module or map as first argument", "",
0, 0};
}
yaksha_lisp_value *yaksha_lisp_builtins::system_lock_current_scope_(
yaksha_lisp_value *yaksha_lisp_builtins::system_lock_root_scope_(
const std::vector<yaksha_lisp_value *> &args, yaksha_envmap *env) {
if (!args.empty()) {
throw parsing_error{"system_lock_current_scope takes no arguments", "", 0, 0};
throw parsing_error{"system_lock_root_scope takes no arguments", "", 0, 0};
}
env->lockdown();
env->lockdown_root();
return env->create_nil();
}
yaksha_lisp_value *yaksha_lisp_builtins::system_unlock_current_scope_(
yaksha_lisp_value *yaksha_lisp_builtins::system_unlock_root_scope_(
const std::vector<yaksha_lisp_value *> &args, yaksha_envmap *env) {
if (!args.empty()) {
throw parsing_error{"system_unlock_current_scope takes no arguments", "", 0, 0};
throw parsing_error{"system_unlock_root_scope takes no arguments", "", 0, 0};
}
env->unlock();
env->unlock_root();
return env->create_nil();
}
yaksha_lisp_value *yaksha_lisp_builtins::system_enable_gc_(
Expand All @@ -1596,3 +1597,19 @@ yaksha_lisp_value *yaksha_lisp_builtins::system_disable_gc_(
env->gc_disable();
return env->create_nil();
}
yaksha_lisp_value *yaksha_lisp_builtins::system_enable_print_(
const std::vector<yaksha_lisp_value *> &args, yaksha_envmap *env) {
if (!args.empty()) {
throw parsing_error{"system_enable_print takes no arguments", "", 0, 0};
}
yaksha_macro_print_allowed = true;
return env->create_nil();
}
yaksha_lisp_value *yaksha_lisp_builtins::system_disable_print_(
const std::vector<yaksha_lisp_value *> &args, yaksha_envmap *env) {
if (!args.empty()) {
throw parsing_error{"system_disable_print takes no arguments", "", 0, 0};
}
yaksha_macro_print_allowed = false;
return env->create_nil();
}
13 changes: 10 additions & 3 deletions compiler/src/yaksha_lisp/yaksha_lisp_builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ namespace yaksha {
yaksha_envmap *env);
// only if it is defined in the current scope or a parent scope(s)
// changes the value
static yaksha_lisp_value *set_(const std::vector<yaksha_lisp_value *> &args,
static yaksha_lisp_value *
setq_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
// sets the value in the current scope if it is defined there
// otherwise defines it in the current scope
Expand Down Expand Up @@ -223,17 +224,23 @@ namespace yaksha {
yaksha_envmap *env);
// --
static yaksha_lisp_value *
system_lock_current_scope_(const std::vector<yaksha_lisp_value *> &args,
system_lock_root_scope_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
static yaksha_lisp_value *
system_unlock_current_scope_(const std::vector<yaksha_lisp_value *> &args,
system_unlock_root_scope_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
static yaksha_lisp_value *
system_enable_gc_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
static yaksha_lisp_value *
system_disable_gc_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
static yaksha_lisp_value *
system_enable_print_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
static yaksha_lisp_value *
system_disable_print_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
// ----------------------------------------------------------------- //
static yaksha_lisp_value *copy_val(yaksha_envmap *env,
yaksha_lisp_value *item);
Expand Down
16 changes: 8 additions & 8 deletions compiler/tests/test_yaksha_lisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ TEST_CASE("yaksha_lisp: tail of a list") {
(if (== 2 (head (tail l))) (= success 1))
)");
}
TEST_CASE("yaksha_lisp: use set to change a value") {
TEST_CASE("yaksha_lisp: use setq to change a value") {
test_snippet_execute(R"(
(def a 1)
(set a 2)
(setq a 2)
(if (== 2 a) (= success 1))
)");
}
Expand Down Expand Up @@ -543,7 +543,7 @@ TEST_CASE("yaksha_lisp: test bitwise_right_shift") {
TEST_CASE("yaksha_lisp: test set") {
test_snippet_execute(R"(
(def a 2)
(set a 1)
(setq a 1)
(if (== a 1) (= success 1))
)");
}
Expand Down Expand Up @@ -592,7 +592,7 @@ TEST_CASE("yaksha_lisp: random string test") {
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 5);
std::vector<std::string> keywords = {
"if", "def", "define", "defun", "set", "print", "=", "!=", "==",
"if", "def", "define", "defun", "setq", "print", "=", "!=", "==",
"<", "<=", ">=", "+", "-", "*", "/", "%"};
std::uniform_int_distribution<> dis_keywords(
0, static_cast<int>(keywords.size() - 1));
Expand Down Expand Up @@ -620,7 +620,7 @@ TEST_CASE("yaksha_lisp: random s-expression test with keywords") {
for (int rounds = 0; rounds < 300; rounds++) {
std::string random_code = "(";
std::vector<std::string> keywords = {
"if", "def", "define", "defun", "set", "print", "=", "!=", "==",
"if", "def", "define", "defun", "setq", "print", "=", "!=", "==",
"<", "<=", ">=", "+", "-", "*", "/", "%"};
std::random_device rd;
std::mt19937 gen(rd());
Expand Down Expand Up @@ -716,7 +716,7 @@ TEST_CASE("yaksha_macro_support: macros!{}") {
// ----------
yaksha_macro_preprocess_happy_path(R"**(
macros! { # Comment here
(enable_print)
(system_enable_print)
(println "// Oi, macros can print stuff")
(= board_game_addict 1)
(= success (== board_game_addict 1)) # Ha!
Expand Down Expand Up @@ -762,9 +762,9 @@ TEST_CASE("yaksha_macro_support: list builtins") {
yaksha_macro_preprocess_happy_path(R"**(# In order to print a list of builtins in YakshaLisp
# just run this!
macros! {
(enable_print)
(system_enable_print)
(println "Builtins:")
(println (repr (map_keys (this))))
(println (repr (map_keys (parent))))
(= success 1)
}
Expand Down
Loading

0 comments on commit 4517818

Please sign in to comment.