1+ #include " errors.h"
2+ #include " structure.h"
3+ #include " collection.h"
4+ #include " tokens.h"
5+ #include " builtins.h"
6+ #include " io.h"
7+
8+ #include < fstream>
9+ #include < iostream>
10+ #include < stack>
11+
12+ void handle_syntax_err (int syntax_error, unsigned int pos, const char * source) {
13+ std::cout << std::endl << " ***Syntax Error: " << get_err_info (syntax_error) << " ***" << std::endl;
14+ std::cout << " Error Code: " << syntax_error << ' \t ' << " Lexer Index: " << pos << std::endl;
15+ for (int i = (int )pos - (int )17 > 0 ? pos - 17 : 0 ; i < strlen (source) && i < pos + 17 ; i++) {
16+ std::cout << source[i];
17+ }
18+ std::cout << " " << std::endl;
19+ }
20+
21+ void handle_runtime_err (int runtime_error, token* err_tok) {
22+ std::cout << std::endl << " ***Runtime Error: " << get_err_info (runtime_error) << " ***" << std::endl;
23+ std::cout << " Error Code: " << runtime_error << ' \t ' << " Error Tok Type: " << (int )err_tok->type << std::endl;
24+ }
25+
26+ void print_call_stack (std::stack<function_prototype*> call_stack) {
27+ std::cout << std::endl << " Stack Trace:" ;
28+ if (call_stack.empty ()) {
29+ std::cout << " Empty" ;
30+ return ;
31+ }
32+ while (!call_stack.empty ())
33+ {
34+ std::cout << std::endl << " \t in " << call_stack.top ()->identifier ->get_identifier ();
35+ call_stack.pop ();
36+ }
37+ }
38+
39+ void print_value (value* val);
40+
41+ inline void print_indent (unsigned int indent) {
42+ for (size_t i = 0 ; i < indent; i++)
43+ std::cout << ' \t ' ;
44+ }
45+
46+ void xprint (structure* structure, unsigned int indent) {
47+ print_indent (indent);
48+ std::cout << ' <' << structure->get_identifier ()->get_identifier () << ' >' ;
49+ reference_apartment** children = structure->get_children ();
50+ for (size_t i = 0 ; i < structure->get_size (); i++)
51+ {
52+ std::cout << std::endl;
53+ if (children[i]->value ->type != VALUE_TYPE_STRUCT) {
54+ print_indent (indent + 1 );
55+ print_value (children[i]->value );
56+ }
57+ else {
58+ xprint ((class structure *)children[i]->value ->ptr , indent + 1 );
59+ }
60+ }
61+ }
62+
63+ void print_array (collection* collection) {
64+ bool is_str = true ;
65+ for (size_t i = 0 ; i < collection->size ; i++)
66+ {
67+ if (collection->get_value (i)->type != VALUE_TYPE_CHAR) {
68+ is_str = false ;
69+ break ;
70+ }
71+ }
72+ if (is_str) {
73+ for (size_t i = 0 ; i < collection->size ; i++)
74+ {
75+ std::cout << *collection->get_value (i)->get_char ();
76+ }
77+ }
78+ else {
79+ if (collection->size > 25 ) {
80+ std::cout << " <array>" ;
81+ return ;
82+ }
83+ std::cout << ' [' ;
84+ for (size_t i = 0 ; i < collection->size ; i++)
85+ {
86+ print_value (collection->get_value (i));
87+ if (i != collection->size - 1 )
88+ std::cout << " , " ;
89+ }
90+ std::cout << ' ]' ;
91+ }
92+ }
93+
94+ void print_value (value* val) {
95+ switch (val->type )
96+ {
97+ case VALUE_TYPE_NULL:
98+ std::cout << " null" ;
99+ break ;
100+ case VALUE_TYPE_CHAR:
101+ std::cout << *val->get_char ();
102+ break ;
103+ case VALUE_TYPE_NUMERICAL:
104+ std::cout << *val->get_numerical ();
105+ break ;
106+ case VALUE_TYPE_COLLECTION:
107+ print_array ((collection*)val->ptr );
108+ break ;
109+ case VALUE_TYPE_STRUCT:
110+ // std::cout << '<' << ((structure*)val->ptr)->get_identifier()->get_identifier() << '>';
111+ xprint ((structure*)val->ptr , 0 );
112+ break ;
113+ default :
114+ throw ERROR_INVALID_VALUE_TYPE;
115+ }
116+ }
117+
118+ reference_apartment* print (std::list<value*> arguments, garbage_collector* gc) {
119+ for (auto it = arguments.begin (); it != arguments.end (); ++it) {
120+ print_value (*it);
121+ }
122+ return gc->new_apartment (new value (VALUE_TYPE_NULL, nullptr ));
123+ }
124+
125+ reference_apartment* print_line (std::list<value*> arguments, garbage_collector* gc) {
126+ reference_apartment* appt = print (arguments, gc);
127+ std::cout << std::endl;
128+ return appt;
129+ }
130+
131+ reference_apartment* get_input (std::list<value*> arguments, garbage_collector* gc) {
132+ char * input = new char [250 ];
133+ std::cin.getline (input, 250 );
134+ collection* str = from_c_str (input, gc);
135+ return str->get_parent_ref ();
136+ }
137+
138+ reference_apartment* file_read_text (std::list<value*> args, garbage_collector* gc) {
139+ match_arg_len (args, 1 );
140+ match_arg_type (args.front (), VALUE_TYPE_COLLECTION);
141+
142+ char * file_path = to_c_str (args.front ());
143+
144+ std::ifstream infile (file_path, std::ifstream::binary);
145+
146+ if (!infile.is_open ())
147+ return gc->new_apartment (new value (VALUE_TYPE_NUMERICAL, new long double (0 )));
148+ delete[] file_path;
149+
150+ infile.seekg (0 , std::ios::end);
151+ int buffer_length = infile.tellg ();
152+ infile.seekg (0 , std::ios::beg);
153+ char * buffer = new char [buffer_length + 1 ];
154+ infile.read (buffer, buffer_length);
155+ infile.close ();
156+ buffer[buffer_length] = 0 ;
157+
158+ collection* strcol = from_c_str (buffer, gc);
159+ delete[] buffer;
160+ return strcol->get_parent_ref ();
161+ }
162+
163+ reference_apartment* file_write_text (std::list<value*> args, garbage_collector* gc) {
164+ match_arg_len (args, 2 );
165+ match_arg_type (args.front (), VALUE_TYPE_COLLECTION);
166+ match_arg_type (args.back (), VALUE_TYPE_COLLECTION);
167+
168+ char * file_path = to_c_str (args.front ());
169+
170+ std::ofstream infile (file_path, std::ofstream::binary);
171+
172+ if (!infile.is_open ())
173+ return gc->new_apartment (new value (VALUE_TYPE_NUMERICAL, new long double (0 )));
174+ delete[] file_path;
175+
176+ char * buffer = to_c_str (args.back ());
177+ infile.write (buffer, strlen (buffer));
178+ infile.close ();
179+ delete[] buffer;
180+
181+ return gc->new_apartment (new value (VALUE_TYPE_NUMERICAL, new long double (1 )));
182+ }
0 commit comments