Skip to content

Commit a8ca26a

Browse files
Group Fixes
1 parent 6237df4 commit a8ca26a

5 files changed

Lines changed: 36 additions & 15 deletions

File tree

src/Source.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ int main(int argc, char** argv) {
5050
interpreter.new_constant("E", new value(VALUE_TYPE_NUMERICAL, new long double(2.71828182)));
5151

5252
interpreter.import_func("quit", quit_repl);
53-
interpreter.import_func("sin", sine);
54-
interpreter.import_func("cos", cosine);
55-
interpreter.import_func("tan", tangent);
56-
interpreter.import_func("asin", inverse_sine);
57-
interpreter.import_func("acos", inverse_cosine);
58-
interpreter.import_func("atan", inverse_tan);
53+
interpreter.import_func("sin@math", sine);
54+
interpreter.import_func("cos@math", cosine);
55+
interpreter.import_func("tan@math", tangent);
56+
interpreter.import_func("asin@math", inverse_sine);
57+
interpreter.import_func("acos@math", inverse_cosine);
58+
interpreter.import_func("atan@math", inverse_tan);
5959

6060
if (argc > 1) {
6161
std::ifstream infile(argv[1], std::ifstream::binary);

src/lexer.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ inline function_call_token* print_encapsulate(token* token) {
5555
return new function_call_token(new identifier_token("print"), args);
5656
}
5757

58-
lexer::lexer(const char* source, unsigned long source_length, std::map<unsigned long, value*>* constants) {
58+
lexer::lexer(const char* source, unsigned long source_length, std::map<unsigned long, value*>* constants, std::set<unsigned long>* group_excluded_ids) {
5959
this->source = source;
6060
this->source_length = source_length;
6161
this->position = 0;
6262
this->last_char = 0;
6363
this->last_tok = nullptr;
6464

6565
this->constants = constants;
66+
this->group_excluded_ids = group_excluded_ids;
6667

6768
read_char();
6869
read_token();
@@ -324,7 +325,6 @@ token* lexer::tokenize_statement(bool interactive_mode) {
324325
identifier_token* id = (identifier_token*)last_tok;
325326
id->no_delete();
326327
group_stack.push_back((char*)id->get_identifier());
327-
group_defined_ids.push_back(std::set<unsigned long>());
328328
delete id;
329329
read_token();
330330
return nullptr;
@@ -335,7 +335,11 @@ token* lexer::tokenize_statement(bool interactive_mode) {
335335
}
336336
delete[] group_stack.back();
337337
group_stack.pop_back();
338-
group_defined_ids.pop_back();
338+
for (auto i = to_exclude.begin(); i != to_exclude.end(); ++i)
339+
{
340+
group_excluded_ids->insert(*i);
341+
}
342+
to_exclude.clear();
339343
read_token();
340344
return nullptr;
341345
case TOKEN_CONST_KW: {
@@ -535,11 +539,18 @@ variable_access_token* lexer::tokenize_var_access(identifier_token* identifier)
535539
}
536540

537541
identifier_token* lexer::apply_groups(identifier_token* id, bool creating) {
538-
if (group_stack.empty() || (!creating && !group_defined_ids.back().count(id->id_hash)))
542+
if (group_stack.empty()) {
543+
if (creating) {
544+
group_excluded_ids->insert(id->id_hash);
545+
}
546+
return id;
547+
}
548+
else if(group_excluded_ids->count(id->id_hash)) {
539549
return id;
550+
}
551+
540552
char* base_id = (char*)id->get_identifier();
541553
id->no_delete();
542-
group_defined_ids.back().insert(id->id_hash);
543554
delete id;
544555

545556
std::list<char> chars;
@@ -559,7 +570,12 @@ identifier_token* lexer::apply_groups(identifier_token* id, bool creating) {
559570
id_buf[i++] = (*it);
560571
id_buf[i] = 0;
561572

562-
return new identifier_token(id_buf, insecure_hash(id_buf));
573+
identifier_token* new_id = new identifier_token(id_buf, insecure_hash(id_buf));
574+
575+
if (creating)
576+
to_exclude.push_back(new_id->id_hash);
577+
578+
return new_id;
563579
}
564580

565581
token* lexer::tokenize_value() {

src/lexer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#include <set>
88
#include <map>
99

10+
#include "builtins.h"
1011
#include "tokens.h"
1112

1213
class lexer {
1314
public:
14-
lexer(const char* source, unsigned long source_length, std::map<unsigned long, value*>* constants);
15+
lexer(const char* source, unsigned long source_length, std::map<unsigned long, value*>* constants, std::set<unsigned long>* group_excluded_ids);
1516
~lexer();
1617

1718
inline bool eos() {
@@ -32,7 +33,8 @@ class lexer {
3233

3334
std::map<unsigned long, value*>* constants;
3435
std::list<char*> group_stack;
35-
std::list<std::set<unsigned long>> group_defined_ids;
36+
std::set<unsigned long>* group_excluded_ids;
37+
std::list<unsigned long> to_exclude;
3638

3739
//reads the next availible character.
3840
char read_char();

src/runtime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ long double interpreter::run(const char* source, bool interactive_mode) {
6464
lexer* lexer = nullptr;
6565
std::list<token*> to_execute;
6666
try {
67-
lexer = new class lexer(source, std::strlen(source), &constants);
67+
lexer = new class lexer(source, std::strlen(source), &constants, &group_excluded_ids);
6868
to_execute = lexer->tokenize(interactive_mode);
6969
delete lexer;
7070
}
@@ -145,6 +145,7 @@ void interpreter::import_func(const char* identifier, built_in_function function
145145
if (built_in_functions.count(id_hash))
146146
throw ERROR_FUNCTION_PROTO_ALREADY_DEFINED;
147147
built_in_functions[id_hash] = function;
148+
group_excluded_ids.insert(id_hash);
148149
}
149150

150151
void interpreter::set_ref(variable_access_token* access, reference_apartment* reference) {

src/runtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class interpreter {
108108
std::set<unsigned long> included_files;
109109
std::map<unsigned long, value*> constants;
110110

111+
std::set<unsigned long> group_excluded_ids;
112+
111113
bool break_mode;
112114

113115
void set_ref(variable_access_token* access, reference_apartment* reference);

0 commit comments

Comments
 (0)