Skip to content

Commit 75ac1be

Browse files
Fixed issues #19, #20, #21
*Made classes/structs private if applicable *Revamped grouping
1 parent fac5cf2 commit 75ac1be

File tree

7 files changed

+150
-100
lines changed

7 files changed

+150
-100
lines changed

src/lexer.cpp

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ token* lexer::read_token() {
143143
case 303295209:
144144
return last_tok = new token(TOKEN_END_GROUP);
145145
default: {
146-
if (lexer_state->constants.count(hash)) {
147-
delete[] id_buf;
148-
return last_tok = new value_token((*lexer_state->constants.find(hash)).second->clone());
149-
}
150146
return last_tok = new identifier_token(id_buf, hash);
151147
}
152148
}
@@ -309,9 +305,10 @@ std::list<token*> lexer::tokenize(bool interactive_mode) {
309305
if(tok != nullptr)
310306
tokens.push_back(tok);
311307
}
312-
if (!eos()) {
308+
if (!eos())
313309
match_tok(last_tok, TOKEN_CLOSE_BRACE);
314-
}
310+
if (interactive_mode)
311+
lexer_state->group_all_references();
315312
return tokens;
316313
}
317314

@@ -330,6 +327,7 @@ token* lexer::tokenize_statement(bool interactive_mode) {
330327
return nullptr;
331328
}
332329
case TOKEN_END_GROUP:{
330+
delete last_tok;
333331
lexer_state->pop_group();
334332

335333
read_token();
@@ -340,16 +338,29 @@ token* lexer::tokenize_statement(bool interactive_mode) {
340338
delete last_tok;
341339
match_tok(read_token(), TOKEN_IDENTIFIER);
342340
identifier_token* id = (identifier_token*)last_tok;
341+
lexer_state->declare_id(id, GROUP_TYPE_VAR);
343342
match_tok(read_token(), TOKEN_SET);
344343
delete last_tok;
345344
match_tok(read_token(), TOKEN_VALUE);
346345
value_token* value_tok = (value_token*)last_tok;
347-
lexer_state->constants.insert(std::pair<unsigned long, value*>(id->id_hash, value_tok->get_value()));
346+
lexer_state->constants[id->id_hash] = value_tok;
348347
delete id;
349-
delete value_tok;
350348
read_token();
351349
return nullptr;
352350
}
351+
case TOKEN_STATIC_KW: {
352+
delete last_tok;
353+
match_tok(read_token(), TOKEN_IDENTIFIER);
354+
identifier_token* id = (identifier_token*)last_tok;
355+
lexer_state->declare_id(id, GROUP_TYPE_VAR);
356+
match_tok(read_token(), TOKEN_SET);
357+
delete last_tok;
358+
read_token();
359+
token* val_tok = tokenize_expression();
360+
std::list<token*> modifiers;
361+
modifiers.push_back(id);
362+
return new set_token(new variable_access_token(modifiers), val_tok, true);
363+
}
353364
case TOKEN_BREAK: {
354365
token* tok = last_tok;
355366
read_token();
@@ -362,8 +373,10 @@ token* lexer::tokenize_statement(bool interactive_mode) {
362373
char* buf = new char[create_str->values.size() + 1];
363374
unsigned char size = 0;
364375
for (auto it = create_str->values.begin(); it != create_str->values.end(); ++it) {
365-
value_token* val = (value_token*)*it;
366-
buf[size++] = *val->get_value()->get_char();
376+
value_token* val_tok = (value_token*)*it;
377+
value* val = val_tok->get_value();
378+
buf[size++] = *val->get_char();
379+
delete val;
367380
}
368381
buf[size] = 0;
369382
delete create_str;
@@ -428,6 +441,7 @@ token* lexer::tokenize_statement(bool interactive_mode) {
428441
delete last_tok;
429442
match_tok(read_token(), TOKEN_IDENTIFIER);
430443
identifier_token* proto_id = (identifier_token*)last_tok;
444+
lexer_state->declare_id(proto_id, GROUP_TYPE_STRUCT);
431445
match_tok(read_token(), TOKEN_OPEN_BRACE);
432446
delete last_tok;
433447
std::list<identifier_token*> properties;
@@ -440,13 +454,13 @@ token* lexer::tokenize_statement(bool interactive_mode) {
440454
throw ERROR_UNEXPECTED_END;
441455
delete last_tok;
442456
read_token();
443-
lexer_state->declare_id(proto_id);
444457
return new structure_prototype(proto_id, properties);
445458
}
446459
case TOKEN_FUNC_KW: {
447460
delete last_tok;
448461
match_tok(read_token(), TOKEN_IDENTIFIER);
449462
identifier_token* proto_id = (identifier_token*)last_tok;
463+
lexer_state->declare_id(proto_id, GROUP_TYPE_FUNC);
450464
match_tok(read_token(), TOKEN_OPEN_PARAM);
451465
delete last_tok;
452466
std::list<identifier_token*> params;
@@ -458,26 +472,14 @@ token* lexer::tokenize_statement(bool interactive_mode) {
458472
}
459473
match_tok(last_tok, TOKEN_IDENTIFIER);
460474
params.push_back((identifier_token*)last_tok);
475+
lexer_state->declare_id((identifier_token*)last_tok, GROUP_TYPE_VAR);
461476
}
462477
if (eos())
463478
throw ERROR_UNEXPECTED_TOKEN;
464479
delete last_tok;
465480
read_token();
466-
lexer_state->declare_id(proto_id);
467481
return new function_prototype(proto_id, params, tokenize_body());
468482
}
469-
case TOKEN_STATIC_KW: {
470-
delete last_tok;
471-
match_tok(read_token(), TOKEN_IDENTIFIER);
472-
identifier_token* id = (identifier_token*)last_tok;
473-
match_tok(read_token(), TOKEN_SET);
474-
delete last_tok;
475-
read_token();
476-
token* val_tok = tokenize_expression();
477-
std::list<token*> modifiers;
478-
modifiers.push_back(id);
479-
return new set_token(new variable_access_token(modifiers), val_tok, true);
480-
}
481483
}
482484
throw ERROR_UNEXPECTED_TOKEN;
483485
}
@@ -556,11 +558,15 @@ token* lexer::tokenize_value() {
556558
match_tok(last_tok, TOKEN_CLOSE_PARAM);
557559
delete last_tok;
558560
read_token();
559-
this->lexer_state->reference_id(identifier);
561+
this->lexer_state->reference_id(identifier, GROUP_TYPE_FUNC);
560562
return new function_call_token(identifier, arguments);
561563
}
562564
else
563565
{
566+
if(last_tok->type == TOKEN_SET)
567+
this->lexer_state->declare_id(identifier, GROUP_TYPE_VAR);
568+
else
569+
this->lexer_state->reference_id(identifier, GROUP_TYPE_VAR);
564570
variable_access_token* var_access = tokenize_var_access(identifier);
565571
if (last_tok->type == OP_INCRIMENT) {
566572
delete last_tok;
@@ -578,12 +584,19 @@ token* lexer::tokenize_value() {
578584
token* to_set = tokenize_expression();
579585
return new set_token(var_access, to_set, false);
580586
}
587+
588+
if (var_access->modifiers.size() <= 1 && lexer_state->constants.count(identifier->id_hash)) {
589+
unsigned long hash = identifier->id_hash;
590+
delete var_access;
591+
return new value_token(lexer_state->constants[hash]->get_value());
592+
}
581593
return var_access;
582594
}
583595
}
584596
else if (last_tok->type == TOKEN_REF_KW) {
585597
delete last_tok;
586598
match_tok(read_token(), TOKEN_IDENTIFIER);
599+
lexer_state->reference_id((identifier_token*)last_tok, GROUP_TYPE_VAR);
587600
get_reference_token* get_ref_tok = new get_reference_token(tokenize_var_access());
588601
return get_ref_tok;
589602
}
@@ -622,7 +635,7 @@ token* lexer::tokenize_value() {
622635
delete last_tok;
623636
match_tok(read_token(), TOKEN_IDENTIFIER);
624637
create_struct_token* new_struct = new create_struct_token((identifier_token*)last_tok);
625-
this->lexer_state->reference_id(new_struct->identifier);
638+
this->lexer_state->reference_id(new_struct->identifier, GROUP_TYPE_STRUCT);
626639
read_token();
627640
return new_struct;
628641
}
@@ -656,29 +669,4 @@ token* lexer::tokenize_expression(unsigned char min) {
656669
lhs = new binary_operator_token(lhs, rhs, op);
657670
}
658671
return lhs;
659-
}
660-
661-
void group::proc_id(identifier_token* id) {
662-
std::list<char> chars;
663-
664-
for (int i = 0; i < strlen(id->get_identifier()); i++)
665-
chars.push_back(id->get_identifier()[i]);
666-
667-
group* current = this;
668-
while (current != nullptr)
669-
{
670-
chars.push_back('@');
671-
for (int i = 0; i < strlen(identifier->get_identifier()); i++)
672-
chars.push_back(identifier->get_identifier()[i]);
673-
current = current->parent;
674-
}
675-
676-
char* new_buf = new char[chars.size() + 1];
677-
unsigned long in = 0;
678-
for (auto i = chars.begin(); i != chars.end(); ++i) {
679-
new_buf[in++] = *i;
680-
}
681-
new_buf[in] = 0;
682-
683-
id->set_c_str(new_buf);
684672
}

src/lexer.h

Lines changed: 89 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,107 @@
1010
#include "builtins.h"
1111
#include "tokens.h"
1212

13-
struct group {
14-
private:
15-
std::set<unsigned long> declerations;
16-
std::list<identifier_token*> to_process;
17-
identifier_token* identifier;
13+
#define GROUP_TYPE_STRUCT 0
14+
#define GROUP_TYPE_FUNC 1
15+
#define GROUP_TYPE_VAR 2
1816

19-
void proc_id(identifier_token* id);
17+
struct lexer_state {
18+
private:
19+
struct identifier_system
20+
{
21+
std::set<unsigned long> declerations;
22+
std::list<identifier_token*> references;
23+
};
24+
25+
struct group {
26+
private:
27+
identifier_system id_systems[3];
28+
identifier_token* identifier;
29+
30+
inline void proc_id(identifier_token* id) {
31+
std::list<char> chars;
32+
33+
for (int i = 0; i < strlen(id->get_identifier()); i++)
34+
chars.push_back(id->get_identifier()[i]);
35+
36+
group* current = this;
37+
while (current != nullptr)
38+
{
39+
chars.push_back('@');
40+
for (int i = 0; i < strlen(current->identifier->get_identifier()); i++)
41+
chars.push_back(current->identifier->get_identifier()[i]);
42+
current = current->parent;
43+
}
44+
45+
char* new_buf = new char[chars.size() + 1];
46+
unsigned long in = 0;
47+
for (auto i = chars.begin(); i != chars.end(); ++i) {
48+
new_buf[in++] = *i;
49+
}
50+
new_buf[in] = 0;
51+
52+
id->set_c_str(new_buf);
53+
}
54+
public:
55+
group* parent;
2056

21-
public:
22-
group* parent;
57+
group(struct identifier_token* identifier, group* parent = nullptr) {
58+
this->identifier = identifier;
59+
this->parent = parent;
60+
}
2361

24-
group(struct identifier_token* identifier, group* parent = nullptr) {
25-
this->identifier = identifier;
26-
this->parent = parent;
27-
}
62+
inline void proc_decleration(identifier_token* id, unsigned char type) {
63+
id_systems[type].declerations.insert(id->id_hash);
64+
proc_id(id);
65+
}
2866

29-
inline void proc_decleration(identifier_token* id) {
30-
declerations.insert(id->id_hash);
31-
proc_id(id);
32-
}
67+
inline void proc_reference(identifier_token* id, unsigned char type) {
68+
id_systems[type].references.push_back(id);
69+
}
3370

34-
inline void proc_reference(identifier_token* id) {
35-
to_process.push_back(id);
36-
}
71+
inline void group_references(bool remall = false) {
72+
for (unsigned char type = 0; type < 3; type++)
73+
{
74+
std::list<std::list<identifier_token*>::iterator> to_remove;
75+
for (auto i = id_systems[type].references.begin(); i != id_systems[type].references.end(); ++i) {
76+
if (id_systems[type].declerations.count((*i)->id_hash)) {
77+
proc_id(*i);
78+
if (!remall)
79+
to_remove.push_back(i);
80+
}
81+
}
82+
if (remall)
83+
id_systems[type].references.clear();
84+
else
85+
for (auto i = to_remove.begin(); i != to_remove.end(); ++i) {
86+
id_systems[type].references.erase(*i);
87+
}
88+
}
89+
}
3790

38-
~group() {
39-
for (auto i = to_process.begin(); i != to_process.end(); ++i) {
40-
if (declerations.count((*i)->id_hash))
41-
proc_id(*i);
91+
~group() {
92+
group_references(true);
93+
delete identifier;
4294
}
43-
}
44-
};
95+
};
4596

46-
struct lexer_state {
47-
private:
4897
group* top_group = nullptr;
4998
public:
50-
std::map<unsigned long, value*> constants;
99+
std::map<unsigned long, value_token*> constants;
51100

52101
~lexer_state() {
53102
for (auto it = this->constants.begin(); it != this->constants.end(); ++it)
54103
delete (*it).second;
55104
}
56105

57-
inline void declare_id(identifier_token* id) {
106+
inline void declare_id(identifier_token* id, unsigned char type) {
58107
if (top_group != nullptr)
59-
top_group->proc_decleration(id);
108+
top_group->proc_decleration(id, type);
60109
}
61110

62-
inline void reference_id(identifier_token* id) {
111+
inline void reference_id(identifier_token* id, unsigned char type) {
63112
if (top_group != nullptr)
64-
top_group->proc_reference(id);
113+
top_group->proc_reference(id, type);
65114
}
66115

67116
inline group* current_group() {
@@ -77,6 +126,14 @@ struct lexer_state {
77126
top_group = top_group->parent;
78127
delete to_delete;
79128
}
129+
130+
inline void group_all_references() {
131+
group* current = top_group;
132+
while (current != nullptr) {
133+
current->group_references();
134+
current = current->parent;
135+
}
136+
}
80137
};
81138

82139
class lexer {

src/objects.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int structure::hash() {
7676
return hash;
7777
}
7878

79-
collection::collection(unsigned long size, garbage_collector* gc) : collection(size, new reference_apartment(new value(VALUE_TYPE_COLLECTION, this))) {
79+
collection::collection(unsigned long size, garbage_collector* gc) : collection(size, gc->new_apartment(new value(VALUE_TYPE_COLLECTION, this))) {
8080
for (unsigned long i = 0; i < size; i++)
8181
{
8282
this->inner_collection[i] = gc->new_apartment(new value(VALUE_TYPE_NULL, nullptr));

src/runtime.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <fstream>
22
#include "collection.h"
33
#include "operators.h"
4+
#include "garbage.h"
45
#include "runtime.h"
56
#include "hash.h"
67

@@ -102,7 +103,9 @@ long double interpreter::run(const char* source, bool interactive_mode) {
102103
ret_val = nullptr;
103104
err = true;
104105
}
105-
106+
107+
garbage_collector.sweep(false);
108+
106109
for (auto it = to_execute.begin(); it != to_execute.end(); ++it) {
107110
if ((*it)->type != TOKEN_FUNC_PROTO && (*it)->type != TOKEN_STRUCT_PROTO)
108111
destroy_top_lvl_tok(*it);

src/runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class interpreter {
8989
void import_func(const char* identifier, built_in_function function);
9090

9191
inline void new_constant(const char* identifier, value* val) {
92-
this->lexer_state.constants[insecure_hash(identifier)] = val;
92+
this->lexer_state.constants[insecure_hash(identifier)] = new value_token(val);
9393
}
9494

9595
private:

0 commit comments

Comments
 (0)