Skip to content

Commit

Permalink
fixed issue #22
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealMichaelWang authored May 22, 2021
1 parent 13663e4 commit 5c3d4ce
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,16 @@ namespace fastcode {

return gc->new_apartment(new value(VALUE_TYPE_NUMERICAL, new long double(1)));
}

runtime::reference_apartment* system_call(std::list<value*> args, runtime::garbage_collector* gc) {
match_arg_len(args, 1);
match_arg_type(args.front(), VALUE_TYPE_COLLECTION);

char* command = to_c_str(args.front());
system(command);
delete[] command;

return gc->new_apartment(new value(VALUE_TYPE_NULL, nullptr));
}
}
}
2 changes: 2 additions & 0 deletions src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace fastcode {

runtime::reference_apartment* file_read_text(std::list<value*> args, runtime::garbage_collector* gc);
runtime::reference_apartment* file_write_text(std::list<value*> args, runtime::garbage_collector* gc);

runtime::reference_apartment* system_call(std::list<value*> args, runtime::garbage_collector* gc);
}
void handle_syntax_err(int syntax_error, unsigned int pos, const char* source);
void handle_runtime_err(int runtime_error, parsing::token* err_tok);
Expand Down
2 changes: 1 addition & 1 deletion src/references.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace fastcode {
}

void reference_apartment::remove_reference(reference_apartment* parent) {
if (this->references == 0)
if (this->references - parent->references < 0)
throw ERROR_CANNOT_DEREFERENCE;
this->references -= parent->references;
unsigned int children_count = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace fastcode {
import_func("len", builtins::get_length);
import_func("range", builtins::get_range);
import_func("handle", builtins::get_handle);
import_func("system", builtins::system_call);
import_func("read@file", builtins::file_read_text);
import_func("write@file", builtins::file_write_text);
import_func("count@linq", builtins::count_instances);
Expand Down Expand Up @@ -332,7 +333,7 @@ namespace fastcode {
call_stack.top()->manager->declare_var(set_tok->destination->get_identifier(), eval->get_value());
}
}
}
}
}
else {
if (eval->type == VALUE_EVAL_TYPE_REF) {
Expand Down
4 changes: 3 additions & 1 deletion src/variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ namespace fastcode {
if (parent == nullptr)
hash_buckets[id_hash % VARIABLE_HASH_BUCKET_SIZE] = new variable_bucket(id_hash, reference);
else
parent = new variable_bucket(id_hash, reference);
parent->next_bucket = new variable_bucket(id_hash, reference);
size++;
return reference;
}

Expand All @@ -71,6 +72,7 @@ namespace fastcode {
parent->next_bucket = bucket->next_bucket;
bucket->apartment->remove_reference();
delete bucket;
size--;
}

bool variable_manager::has_var(unsigned long id_hash)
Expand Down

0 comments on commit 5c3d4ce

Please sign in to comment.