176 lines
4 KiB
C
176 lines
4 KiB
C
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <string/new.h>
|
|
#include <string/free.h>
|
|
|
|
#include <stringtree/println.h>
|
|
#include <stringtree/free.h>
|
|
|
|
/*#include <booleans/new.h>*/
|
|
/*#include <booleans/free.h>*/
|
|
|
|
/*#include <color_factory/new.h>*/
|
|
/*#include <color_factory/free.h>*/
|
|
|
|
/*#include <environment/new.h>*/
|
|
/*#include <environment/declare_builtins.h>*/
|
|
/*#include <environment/free.h>*/
|
|
|
|
#include <parse/position/struct.h>
|
|
#include <parse/position/new.h>
|
|
#include <parse/position/free.h>
|
|
|
|
#include <parse/istream/string/new.h>
|
|
#include <parse/istream/file/new.h>
|
|
#include <parse/istream/read.h>
|
|
#include <parse/istream/free.h>
|
|
|
|
#include <parse/wcistream/istream/new.h>
|
|
#include <parse/wcistream/read.h>
|
|
#include <parse/wcistream/free.h>
|
|
|
|
#include <parse/token/struct.h>
|
|
|
|
#include <parse/tokenizer/struct.h>
|
|
#include <parse/tokenizer/new.h>
|
|
#include <parse/tokenizer/next.h>
|
|
#include <parse/tokenizer/free.h>
|
|
|
|
#include <parse/parse.h>
|
|
|
|
#include <wcostream/ostream/new.h>
|
|
#include <wcostream/free.h>
|
|
|
|
#include <ostream/file/new.h>
|
|
#include <ostream/free.h>
|
|
|
|
#include <statement/prettyprint.h>
|
|
|
|
#include <statement/execute.h>
|
|
#include <statement/prettyprint_errors.h>
|
|
#include <statement/free.h>
|
|
|
|
#include "handle_file.h"
|
|
|
|
void handle_file(
|
|
struct environment** environment,
|
|
struct booleans* booleans,
|
|
const char* input_file,
|
|
bool echo,
|
|
bool print_with_colors)
|
|
{
|
|
ENTER;
|
|
|
|
dpvs(input_file);
|
|
|
|
assert(input_file);
|
|
|
|
struct ostream* b_stdout = new_file_ostream(
|
|
/* file descriptor: */ 1);
|
|
|
|
struct wcostream* wc_stdout = new_ostream_wcostream(
|
|
/* binary stream: */ b_stdout);
|
|
|
|
struct istream* stream = new_file_istream(
|
|
/* path: */ input_file);
|
|
|
|
struct wcistream* wcstream = new_istream_wcistream(
|
|
/* binary stream: */ stream);
|
|
|
|
struct string* filename = new_string_from_ascii(
|
|
/* data: */ input_file,
|
|
/* len: */ (size_t) -1);
|
|
|
|
struct position* position = new_position(
|
|
/* filename: */ filename,
|
|
/* line number: */ 1,
|
|
/* column: */ 1);
|
|
|
|
struct tokenizer* tokenizer = new_tokenizer(
|
|
/* wide-character stream: */ wcstream,
|
|
/* inital position (mutated): */ position);
|
|
|
|
istream_read(stream);
|
|
|
|
wcistream_read(wcstream);
|
|
|
|
tokenizer_next(tokenizer);
|
|
|
|
while (tokenizer->token->kind == tk_newline)
|
|
{
|
|
tokenizer_next(tokenizer);
|
|
}
|
|
|
|
if (tokenizer->token->kind == tk_EOF)
|
|
{
|
|
// error: empty file
|
|
TODO;
|
|
}
|
|
|
|
while (tokenizer->token->kind != tk_EOF)
|
|
{
|
|
struct statement* statement = parse(
|
|
/* tokenizer: */ tokenizer);
|
|
|
|
struct stringtree* error_messages =
|
|
statement_prettyprint_errors(statement);
|
|
|
|
if (error_messages)
|
|
{
|
|
// print one (or more) error expressions
|
|
TODO;
|
|
|
|
// exit(1);
|
|
TODO;
|
|
}
|
|
|
|
if (echo)
|
|
{
|
|
struct stringtree* stree = statement_prettyprint(
|
|
/* chosen color reference: */ NULL,
|
|
/* instance: */ statement,
|
|
/* print with color? */ print_with_colors);
|
|
|
|
stringtree_println(stree, wc_stdout);
|
|
|
|
free_stringtree(stree);
|
|
}
|
|
|
|
statement_execute(
|
|
/* instance: */ statement,
|
|
/* environment: */ environment,
|
|
/* booleans: */ booleans,
|
|
/* wide-character stdout: */ wc_stdout,
|
|
/* print value: */ true,
|
|
/* print with color?: */ print_with_colors);
|
|
|
|
while (tokenizer->token->kind == tk_newline)
|
|
{
|
|
tokenizer_next(tokenizer);
|
|
}
|
|
|
|
free_stringtree(error_messages);
|
|
|
|
free_statement(statement);
|
|
}
|
|
|
|
free_tokenizer(tokenizer);
|
|
|
|
free_position(position);
|
|
|
|
free_string(filename);
|
|
|
|
free_wcostream(wc_stdout);
|
|
|
|
free_wcistream(wcstream);
|
|
|
|
free_istream(stream);
|
|
|
|
free_ostream(b_stdout);
|
|
|
|
EXIT;
|
|
}
|
|
|