Skip to content

Commit 8eb6aae

Browse files
Compartmentalized Static Lexer Properties
Added a wrapper struct to store static properties (constants, groups, etc...).
1 parent e850d81 commit 8eb6aae

7 files changed

Lines changed: 47 additions & 41 deletions

File tree

src/Source.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ int main(int argc, char** argv) {
4646
const char* working_dir = argv[0];
4747
interpreter interpreter;
4848

49-
interpreter.new_constant("pi", new value(VALUE_TYPE_NUMERICAL, new long double(3.1415926)));
50-
interpreter.new_constant("E", new value(VALUE_TYPE_NUMERICAL, new long double(2.71828182)));
49+
interpreter.new_constant("pi@math", new value(VALUE_TYPE_NUMERICAL, new long double(3.1415926)));
50+
interpreter.new_constant("e@math", new value(VALUE_TYPE_NUMERICAL, new long double(2.71828182)));
5151

5252
interpreter.import_func("quit", quit_repl);
53+
54+
interpreter.import_func("abs", numabs);
5355
interpreter.import_func("sin@math", sine);
5456
interpreter.import_func("cos@math", cosine);
5557
interpreter.import_func("tan@math", tangent);

src/lexer.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ 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, std::set<unsigned long>* group_excluded_ids) {
58+
lexer::lexer(const char* source, unsigned long source_length, struct lexer_state* lexer_state) {
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

65-
this->constants = constants;
66-
this->group_excluded_ids = group_excluded_ids;
65+
this->lexer_state = lexer_state;
6766

6867
read_char();
6968
read_token();
@@ -144,9 +143,9 @@ token* lexer::read_token() {
144143
case 303295209:
145144
return last_tok = new token(TOKEN_END_GROUP);
146145
default: {
147-
if (constants->count(hash)) {
146+
if (lexer_state->constants.count(hash)) {
148147
delete[] id_buf;
149-
return last_tok = new value_token((*constants->find(hash)).second->clone());
148+
return last_tok = new value_token((*lexer_state->constants.find(hash)).second->clone());
150149
}
151150
return last_tok = new identifier_token(id_buf, hash);
152151
}
@@ -324,22 +323,20 @@ token* lexer::tokenize_statement(bool interactive_mode) {
324323
match_tok(read_token(), TOKEN_IDENTIFIER);
325324
identifier_token* id = (identifier_token*)last_tok;
326325
id->no_delete();
327-
group_stack.push_back((char*)id->get_identifier());
326+
lexer_state->group_stack.push_back((char*)id->get_identifier());
328327
delete id;
329328
read_token();
330329
return nullptr;
331330
}
332331
case TOKEN_END_GROUP:
333-
if (group_stack.empty()) {
332+
if (lexer_state->group_stack.empty()) {
334333
throw ERROR_UNEXPECTED_TOKEN;
335334
}
336-
delete[] group_stack.back();
337-
group_stack.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();
335+
delete[] lexer_state->group_stack.back();
336+
lexer_state->group_stack.pop_back();
337+
for (auto i = lexer_state->to_exclude.begin(); i != lexer_state->to_exclude.end(); ++i)
338+
lexer_state->namespace_register.insert(*i);
339+
lexer_state->to_exclude.clear();
343340
read_token();
344341
return nullptr;
345342
case TOKEN_CONST_KW: {
@@ -350,7 +347,7 @@ token* lexer::tokenize_statement(bool interactive_mode) {
350347
delete last_tok;
351348
match_tok(read_token(), TOKEN_VALUE);
352349
value_token* value_tok = (value_token*)last_tok;
353-
constants->insert(std::pair<unsigned long, value*>(id->id_hash, value_tok->get_value()));
350+
lexer_state->constants.insert(std::pair<unsigned long, value*>(id->id_hash, value_tok->get_value()));
354351
delete id;
355352
delete value_tok;
356353
read_token();
@@ -539,13 +536,13 @@ variable_access_token* lexer::tokenize_var_access(identifier_token* identifier)
539536
}
540537

541538
identifier_token* lexer::apply_groups(identifier_token* id, bool creating) {
542-
if (group_stack.empty()) {
539+
if (lexer_state->group_stack.empty()) {
543540
if (creating) {
544-
group_excluded_ids->insert(id->id_hash);
541+
lexer_state->namespace_register.insert(id->id_hash);
545542
}
546543
return id;
547544
}
548-
else if(group_excluded_ids->count(id->id_hash)) {
545+
else if(lexer_state->namespace_register.count(id->id_hash)) {
549546
return id;
550547
}
551548

@@ -558,7 +555,7 @@ identifier_token* lexer::apply_groups(identifier_token* id, bool creating) {
558555
for (size_t i = 0; i < strlen(base_id); i++)
559556
chars.push_back(base_id[i]);
560557

561-
for (auto group_kw = group_stack.rbegin(); group_kw != group_stack.rend(); ++group_kw) {
558+
for (auto group_kw = lexer_state->group_stack.rbegin(); group_kw != lexer_state->group_stack.rend(); ++group_kw) {
562559
chars.push_back('@');
563560
for (size_t i = 0; i < strlen(*group_kw); i++)
564561
chars.push_back((*group_kw)[i]);
@@ -573,7 +570,7 @@ identifier_token* lexer::apply_groups(identifier_token* id, bool creating) {
573570
identifier_token* new_id = new identifier_token(id_buf, insecure_hash(id_buf));
574571

575572
if (creating)
576-
to_exclude.push_back(new_id->id_hash);
573+
lexer_state->to_exclude.push_back(new_id->id_hash);
577574

578575
return new_id;
579576
}

src/lexer.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,23 @@
1010
#include "builtins.h"
1111
#include "tokens.h"
1212

13+
struct lexer_state {
14+
std::map<unsigned long, value*> constants;
15+
std::set<unsigned long> namespace_register;
16+
std::list<char*> group_stack;
17+
std::list<unsigned long> to_exclude;
18+
19+
~lexer_state() {
20+
for (auto it = this->constants.begin(); it != this->constants.end(); ++it)
21+
delete (*it).second;
22+
for (auto it = this->group_stack.begin(); it != this->group_stack.end(); ++it)
23+
delete (*it);
24+
}
25+
};
26+
1327
class lexer {
1428
public:
15-
lexer(const char* source, unsigned long source_length, std::map<unsigned long, value*>* constants, std::set<unsigned long>* group_excluded_ids);
29+
lexer(const char* source, unsigned long source_length, lexer_state* lexer_state);
1630
~lexer();
1731

1832
inline bool eos() {
@@ -31,10 +45,7 @@ class lexer {
3145
char last_char;
3246
token* last_tok;
3347

34-
std::map<unsigned long, value*>* constants;
35-
std::list<char*> group_stack;
36-
std::set<unsigned long>* group_excluded_ids;
37-
std::list<unsigned long> to_exclude;
48+
lexer_state* lexer_state;
3849

3950
//reads the next availible character.
4051
char read_char();

src/math.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "builtins.h"
33
#include "math.h"
44

5-
reference_apartment* abs(std::list<value*> arguments, garbage_collector* gc) {
5+
reference_apartment* numabs(std::list<value*> arguments, garbage_collector* gc) {
66
match_arg_len(arguments, 1);
77
match_arg_type(arguments.front(), VALUE_TYPE_NUMERICAL);
88
return gc->new_apartment(new value(VALUE_TYPE_NUMERICAL, new long double(abs(*arguments.front()->get_numerical()))));

src/math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "references.h"
99
#include "value.h"
1010

11-
reference_apartment* abs(std::list<value*> arguments, garbage_collector* gc);
11+
reference_apartment* numabs(std::list<value*> arguments, garbage_collector* gc);
1212
reference_apartment* sine(std::list<value*> arguments, garbage_collector* gc);
1313
reference_apartment* cosine(std::list<value*> arguments, garbage_collector* gc);
1414
reference_apartment* tangent(std::list<value*> arguments, garbage_collector* gc);

src/runtime.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <fstream>
2-
#include "lexer.h"
32
#include "collection.h"
43
#include "operators.h"
54
#include "runtime.h"
@@ -54,17 +53,13 @@ interpreter::~interpreter() {
5453
for (auto it = this->struct_definitions.begin(); it != this->struct_definitions.end(); ++it) {
5554
delete (*it).second;
5655
}
57-
58-
for (auto it = this->constants.begin(); it != this->constants.end(); ++it) {
59-
delete (*it).second;
60-
}
6156
}
6257

6358
long double interpreter::run(const char* source, bool interactive_mode) {
6459
lexer* lexer = nullptr;
6560
std::list<token*> to_execute;
6661
try {
67-
lexer = new class lexer(source, std::strlen(source), &constants, &group_excluded_ids);
62+
lexer = new class lexer(source, std::strlen(source), &lexer_state);
6863
to_execute = lexer->tokenize(interactive_mode);
6964
delete lexer;
7065
}
@@ -88,6 +83,8 @@ long double interpreter::run(const char* source, bool interactive_mode) {
8883
}
8984
catch (int runtime_error){
9085
last_error = runtime_error;
86+
87+
handle_runtime_err(runtime_error, last_tok);
9188
//cleanup
9289
while (call_stack.size() > 1)
9390
{
@@ -96,7 +93,6 @@ long double interpreter::run(const char* source, bool interactive_mode) {
9693
}
9794
ret_val = nullptr;
9895

99-
handle_runtime_err(runtime_error, last_tok);
10096
err = true;
10197
}
10298

@@ -106,7 +102,7 @@ long double interpreter::run(const char* source, bool interactive_mode) {
106102
}
107103

108104
if (err)
109-
return -1;
105+
return -abs(last_error);
110106
if (ret_val == nullptr)
111107
return 0;
112108
long double exit_code = *ret_val->get_value()->get_numerical();
@@ -145,7 +141,7 @@ void interpreter::import_func(const char* identifier, built_in_function function
145141
if (built_in_functions.count(id_hash))
146142
throw ERROR_FUNCTION_PROTO_ALREADY_DEFINED;
147143
built_in_functions[id_hash] = function;
148-
group_excluded_ids.insert(id_hash);
144+
lexer_state.namespace_register.insert(id_hash);
149145
}
150146

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

src/runtime.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "garbage.h"
1616
#include "builtins.h"
1717
#include "structure.h"
18+
#include "lexer.h"
1819
#include "hash.h"
1920

2021
class call_frame {
@@ -88,7 +89,7 @@ class interpreter {
8889
void import_func(const char* identifier, built_in_function function);
8990

9091
inline void new_constant(const char* identifier, value* val) {
91-
this->constants[insecure_hash(identifier)] = val;
92+
this->lexer_state.constants[insecure_hash(identifier)] = val;
9293
}
9394

9495
////you need the garbage collector to allocate non-primitive objects
@@ -106,9 +107,8 @@ class interpreter {
106107
std::map<unsigned long, built_in_function> built_in_functions;
107108

108109
std::set<unsigned long> included_files;
109-
std::map<unsigned long, value*> constants;
110110

111-
std::set<unsigned long> group_excluded_ids;
111+
struct lexer_state lexer_state;
112112

113113
bool break_mode;
114114

0 commit comments

Comments
 (0)