Skip to content

Commit 6de8c1f

Browse files
Replaced unnecessary array-based data structures
Replaced all ```std::vector```s with ```std::list```. It should be noted that vectors are NOT linked list, contrary to popular belief.
1 parent f89831c commit 6de8c1f

20 files changed

Lines changed: 135 additions & 128 deletions

src/Source.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
bool stop = false;
1010

11-
reference_apartment* quit_repl(std::vector<value*> args, garbage_collector* gc) {
11+
reference_apartment* quit_repl(std::list<value*> args, garbage_collector* gc) {
1212
stop = true;
1313
return gc->new_apartment(new value(VALUE_TYPE_NULL, nullptr));
1414
}
@@ -76,7 +76,7 @@ int main(int argc, char** argv) {
7676
return (int)exit_code;
7777
}
7878
else {
79-
std::cout << "FastCode [Version 2.0, written and designed by Michael Wang]" << std::endl << "Type \"quit()\" to exit the REPL." << std::endl;
79+
std::cout << "FastCode [Version 2.0, written and designed by Michael Wang]" << std::endl << "Type \"quit()\" to exit the REPL, type \"help()\" for help. " << std::endl << "For more information or documentation, go to https://github.com/TheRealMichaelWang/fastcode/wiki." << std::endl;
8080

8181
while (!stop)
8282
{

src/builtins.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#ifndef BUILTINS_H
22
#define BUILTINS_H
33

4-
#include <vector>
4+
#include <list>
55
#include "errors.h"
66
#include "value.h"
77
#include "references.h"
88
#include "garbage.h"
99

10-
typedef reference_apartment* (*built_in_function)(std::vector<value*> arguments, garbage_collector* gc);
10+
typedef reference_apartment* (*built_in_function)(std::list<value*> arguments, garbage_collector* gc);
1111

12-
inline void match_arg_len(std::vector<value*> arguments, unsigned int expected_size) {
12+
inline void match_arg_len(std::list<value*> arguments, unsigned int expected_size) {
1313
if (arguments.size() != expected_size)
1414
throw ERROR_UNEXPECTED_ARGUMENT_SIZE;
1515
}

src/console.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ void print_value(value* val) {
6161
}
6262
}
6363

64-
reference_apartment* print(std::vector<value*> arguments, garbage_collector* gc) {
64+
reference_apartment* print(std::list<value*> arguments, garbage_collector* gc) {
6565
for (auto it = arguments.begin(); it != arguments.end(); ++it) {
6666
print_value(*it);
6767
}
6868
return gc->new_apartment(new value(VALUE_TYPE_NULL, nullptr));
6969
}
7070

71-
reference_apartment* print_line(std::vector<value*> arguments, garbage_collector* gc) {
71+
reference_apartment* print_line(std::list<value*> arguments, garbage_collector* gc) {
7272
reference_apartment* appt = print(arguments, gc);
7373
std::cout << std::endl;
7474
return appt;
7575
}
7676

77-
reference_apartment* get_input(std::vector<value*> arguments, garbage_collector* gc) {
77+
reference_apartment* get_input(std::list<value*> arguments, garbage_collector* gc) {
7878
char* input = new char[250];
7979
std::cin.getline(input, 250);
8080
unsigned int size = strlen(input);

src/console.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#ifndef CONSOLE_H
44
#define CONSOLE_H
55

6-
#include <vector>
6+
#include <list>
77
#include "tokens.h"
88
#include "references.h"
99
#include "value.h"
1010

11-
reference_apartment* print(std::vector<value*> arguments, garbage_collector* gc);
12-
reference_apartment* print_line(std::vector<value*> arguments, garbage_collector* gc);
13-
reference_apartment* get_input(std::vector<value*> arguments, garbage_collector* gc);
11+
reference_apartment* print(std::list<value*> arguments, garbage_collector* gc);
12+
reference_apartment* print_line(std::list<value*> arguments, garbage_collector* gc);
13+
reference_apartment* get_input(std::list<value*> arguments, garbage_collector* gc);
1414

1515
void handle_syntax_err(int syntax_error, unsigned int pos, const char* source);
1616
void handle_runtime_err(int runtime_error, token* err_tok);

src/garbage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ garbage_collector::garbage_collector() {
99
garbage_collector::~garbage_collector() {
1010
while (size > 0)
1111
{
12-
sweep();
12+
sweep(true);
1313
}
1414
}
1515

@@ -26,7 +26,7 @@ reference_apartment* garbage_collector::new_apartment(value* initial_value) {
2626
}
2727
}
2828

29-
unsigned int garbage_collector::sweep() {
29+
unsigned int garbage_collector::sweep(bool pop_frame) {
3030
unsigned int destroyed_values = 0;
3131
reference_apartment* current = sweep_frames.empty() ? head : sweep_frames.top()->next_apartment;
3232
reference_apartment* previous = sweep_frames.empty() ? nullptr : sweep_frames.top();
@@ -49,7 +49,7 @@ unsigned int garbage_collector::sweep() {
4949
}
5050
}
5151
tail = previous;
52-
if (!sweep_frames.empty())
52+
if (!sweep_frames.empty() && pop_frame)
5353
sweep_frames.pop();
5454
return destroyed_values;
5555
}

src/garbage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class garbage_collector {
2121
reference_apartment* new_apartment(value* initial_value);
2222

2323
//de-allocates unused variable appartments within the current garbage collection frame
24-
unsigned int sweep();
24+
unsigned int sweep(bool pop_frame);
2525
private:
2626
unsigned int size;
2727
reference_apartment* head;

src/lexer.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#define TOKEN_QUICK_BODY 14 + MAX_TOKEN_LIMIT
2828

29-
#define TOKEN_DEFINE_KW 15 + MAX_TOKEN_LIMIT
29+
#define TOKEN_CONST_KW 15 + MAX_TOKEN_LIMIT
3030

3131
#define END 16 + MAX_TOKEN_LIMIT
3232

@@ -47,7 +47,7 @@ inline function_call_token* print_encapsulate(token* token) {
4747
return call_tok;
4848
}
4949
}
50-
std::vector<class token*> args;
50+
std::list<class token*> args;
5151
args.push_back(token);
5252
return new function_call_token(new identifier_token(new char[0], 275790354), args);
5353
}
@@ -83,11 +83,11 @@ token* lexer::read_token() {
8383
read_char();
8484
}
8585
if (isalpha(last_char) || last_char == '_' || last_char == '@') {
86-
std::vector<char> id_chars;
86+
std::list<char> id_chars;
8787
do {
8888
id_chars.push_back(last_char);
8989
}
90-
while (isalpha(read_char()) || last_char == '_');
90+
while (isalpha(read_char()) || last_char == '_' || last_char == '@');
9191
char* id_buf = new char[id_chars.size() + 1];
9292
int index = 0;
9393
for (auto it = id_chars.begin(); it != id_chars.end(); ++it)
@@ -130,7 +130,7 @@ token* lexer::read_token() {
130130
case 4135260141:
131131
return last_tok = new token(TOKEN_STATIC_KW);
132132
case 275975372:
133-
return last_tok = new token(TOKEN_DEFINE_KW);
133+
return last_tok = new token(TOKEN_CONST_KW);
134134
case 1413452809:
135135
return last_tok = new token(TOKEN_INCLUDE);
136136
case 264645514: //break
@@ -145,7 +145,7 @@ token* lexer::read_token() {
145145
}
146146
}
147147
else if (isdigit(last_char)) {
148-
std::vector<char> num_chars;
148+
std::list<char> num_chars;
149149
do {
150150
num_chars.push_back(last_char);
151151
}
@@ -160,7 +160,7 @@ token* lexer::read_token() {
160160
return last_tok = to_ret;
161161
}
162162
else if (last_char == '\"') {
163-
std::vector<token*> chars;
163+
std::list<token*> chars;
164164
read_char();
165165
while (!eos() && last_char != '\"')
166166
{
@@ -294,8 +294,8 @@ char lexer::read_data_char() {
294294
return ret_char;
295295
}
296296

297-
std::vector<token*> lexer::tokenize(bool interactive_mode) {
298-
std::vector<token*> tokens;
297+
std::list<token*> lexer::tokenize(bool interactive_mode) {
298+
std::list<token*> tokens;
299299
while (!eos() && last_tok->type != TOKEN_CLOSE_BRACE)
300300
{
301301
token* tok = tokenize_statement(interactive_mode);
@@ -311,10 +311,12 @@ std::vector<token*> lexer::tokenize(bool interactive_mode) {
311311
token* lexer::tokenize_statement(bool interactive_mode) {
312312
switch (last_tok->type)
313313
{
314-
case TOKEN_DEFINE_KW: {
314+
case TOKEN_CONST_KW: {
315315
delete last_tok;
316316
match_tok(read_token(), TOKEN_IDENTIFIER);
317317
identifier_token* id = (identifier_token*)last_tok;
318+
match_tok(read_token(), TOKEN_SET);
319+
delete last_tok;
318320
match_tok(read_token(), TOKEN_VALUE);
319321
value_token* value_tok = (value_token*)last_tok;
320322
constants->insert(std::pair<unsigned long, value*>(id->id_hash, value_tok->get_value()));
@@ -402,7 +404,7 @@ token* lexer::tokenize_statement(bool interactive_mode) {
402404
identifier_token* proto_id = (identifier_token*)last_tok;
403405
match_tok(read_token(), TOKEN_OPEN_BRACE);
404406
delete last_tok;
405-
std::vector<identifier_token*> properties;
407+
std::list<identifier_token*> properties;
406408
while (!eos() && read_token()->type != TOKEN_CLOSE_BRACE)
407409
{
408410
match_tok(last_tok, TOKEN_IDENTIFIER);
@@ -420,7 +422,7 @@ token* lexer::tokenize_statement(bool interactive_mode) {
420422
identifier_token* proto_id = (identifier_token*)last_tok;
421423
match_tok(read_token(), TOKEN_OPEN_PARAM);
422424
delete last_tok;
423-
std::vector<identifier_token*> params;
425+
std::list<identifier_token*> params;
424426
while (!eos() && read_token()->type != TOKEN_CLOSE_PARAM)
425427
{
426428
if (last_tok->type == TOKEN_COMMA) {
@@ -444,27 +446,27 @@ token* lexer::tokenize_statement(bool interactive_mode) {
444446
delete last_tok;
445447
read_token();
446448
token* val_tok = tokenize_expression();
447-
std::vector<token*> modifiers;
449+
std::list<token*> modifiers;
448450
modifiers.push_back(id);
449451
return new set_token(new variable_access_token(modifiers), val_tok, true);
450452
}
451453
}
452454
throw ERROR_UNEXPECTED_TOKEN;
453455
}
454456

455-
std::vector<token*> lexer::tokenize_body() {
457+
std::list<token*> lexer::tokenize_body() {
456458
if (last_tok->type == TOKEN_OPEN_BRACE) {
457459
delete last_tok;
458460
read_token();
459-
std::vector<token*> body = tokenize(false);
461+
std::list<token*> body = tokenize(false);
460462
delete last_tok;
461463
read_token();
462464
return body;
463465
}
464466
else if (last_tok->type == TOKEN_QUICK_BODY) {
465467
delete last_tok;
466468
read_token();
467-
std::vector<token*> body;
469+
std::list<token*> body;
468470
body.push_back(tokenize_statement(false));
469471
return body;
470472
}
@@ -481,7 +483,7 @@ variable_access_token* lexer::tokenize_var_access(){
481483
}
482484

483485
variable_access_token* lexer::tokenize_var_access(identifier_token* identifier) {
484-
std::vector<token*> toks;
486+
std::list<token*> toks;
485487
toks.push_back(identifier);
486488
while (true)
487489
{
@@ -512,7 +514,7 @@ token* lexer::tokenize_value() {
512514
//tokenize function call
513515
if (last_tok->type == TOKEN_OPEN_PARAM) {
514516
delete last_tok;
515-
std::vector<token*> arguments;
517+
std::list<token*> arguments;
516518
while (true)
517519
{
518520
read_token();
@@ -568,7 +570,7 @@ token* lexer::tokenize_value() {
568570
}
569571
else if (last_tok->type == TOKEN_OPEN_BRACKET) {
570572
delete last_tok;
571-
std::vector<token*> values;
573+
std::list<token*> values;
572574
while (true)
573575
{
574576
read_token();

src/lexer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#ifndef LEXER_H
44
#define LEXER_H
55

6-
#include <vector>
6+
#include <list>
7+
#include <stack>
78
#include <map>
89

910
#include "tokens.h"
@@ -21,7 +22,7 @@ class lexer {
2122
return this->position;
2223
}
2324

24-
std::vector<token*> tokenize(bool interactive_mode);
25+
std::list<token*> tokenize(bool interactive_mode);
2526
private:
2627
const char* source;
2728
unsigned long position;
@@ -47,7 +48,7 @@ class lexer {
4748
//reads the next top-level token
4849
token* read_token();
4950
token* tokenize_statement(bool interactive_mode);
50-
std::vector<token*> tokenize_body();
51+
std::list<token*> tokenize_body();
5152
variable_access_token* tokenize_var_access();
5253
variable_access_token* tokenize_var_access(identifier_token* identifier);
5354
token* tokenize_expression(unsigned char min = 0);

src/linq.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
#include "collection.h"
33
#include "linq.h"
44

5-
reference_apartment* allocate_array(std::vector<value*> arguments, garbage_collector* gc) {
5+
reference_apartment* allocate_array(std::list<value*> arguments, garbage_collector* gc) {
66
match_arg_len(arguments, 1);
7-
match_arg_type(arguments[0], VALUE_TYPE_NUMERICAL);
7+
match_arg_type(arguments.front(), VALUE_TYPE_NUMERICAL);
88

9-
if (*arguments[0]->get_numerical() < 0) {
9+
if (*arguments.front()->get_numerical() < 0) {
1010
throw ERROR_INVALID_VALUE_TYPE;
1111
}
1212

13-
unsigned long req_size = (unsigned long)*arguments[0]->get_numerical();
13+
unsigned long req_size = (unsigned long)*arguments.front()->get_numerical();
1414

1515
collection* allocated_array = new collection(req_size, gc);
1616
return allocated_array->get_parent();
1717
}
1818

19-
reference_apartment* get_length(std::vector<value*> arguments, garbage_collector* gc) {
19+
reference_apartment* get_length(std::list<value*> arguments, garbage_collector* gc) {
2020
match_arg_len(arguments, 1);
21-
match_arg_type(arguments[0], VALUE_TYPE_COLLECTION);
21+
match_arg_type(arguments.front(), VALUE_TYPE_COLLECTION);
2222

23-
collection* collection = (class collection*)arguments[0]->ptr;
23+
collection* collection = (class collection*)arguments.front()->ptr;
2424
return gc->new_apartment(new value(VALUE_TYPE_NUMERICAL, new long double((long double)collection->size)));
2525
}

src/linq.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#ifndef LINQ_H
44
#define LINQ_H
55

6-
#include <vector>
6+
#include <list>
77
#include "value.h"
88
#include "references.h"
99
#include "garbage.h"
1010

11-
reference_apartment* allocate_array(std::vector<value*> arguments, garbage_collector* gc);
12-
reference_apartment* get_length(std::vector<value*> arguments, garbage_collector* gc);
11+
reference_apartment* allocate_array(std::list<value*> arguments, garbage_collector* gc);
12+
reference_apartment* get_length(std::list<value*> arguments, garbage_collector* gc);
1313

1414
#endif // !LINQ_H

0 commit comments

Comments
 (0)