Read the input filename from the command line

This commit is contained in:
Eugen Wissner 2025-02-02 08:22:40 +01:00
parent b41d6fb907
commit b417cb973f
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
6 changed files with 156 additions and 136 deletions

View File

@ -36,10 +36,6 @@ namespace gcc
{
return "Char";
}
else if (is_string_type(type))
{
return "String";
}
else if (is_pointer_type(type))
{
return "pointer";

View File

@ -86,6 +86,12 @@ namespace gcc
|| type == this->symbol_map->lookup("Word")->payload;
}
bool generic_visitor::is_numeric_type(tree type)
{
return is_integral_type(type)
|| type == this->symbol_map->lookup("Float")->payload;
}
void generic_visitor::visit(boot::program *program)
{
for (const auto definition : program->value_definitions)
@ -278,49 +284,57 @@ namespace gcc
this->current_expression = build_string_literal(string->string().size() + 1, string->string().c_str());
}
void generic_visitor::build_binary_operation(bool condition, boot::binary_expression *expression,
tree_code operator_code, tree left, tree right, tree target_type)
tree generic_visitor::build_arithmetic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
auto expression_location = get_location(&expression->position());
auto left_type = TREE_TYPE(left);
auto right_type = TREE_TYPE(right);
return build_binary_operation(is_numeric_type(TREE_TYPE(left)),
expression, operator_code, left, right, TREE_TYPE(left));
}
if (condition)
tree generic_visitor::build_comparison_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
this->current_expression = build2_loc(expression_location,
operator_code, target_type, left, right);
return build_binary_operation(is_numeric_type(TREE_TYPE(left)) || is_pointer_type(TREE_TYPE(left)),
expression, operator_code, left, right, this->symbol_map->lookup("Bool")->payload);
}
else
tree generic_visitor::build_logic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
error_at(expression_location,
"invalid operands of type %s and %s for operator %s",
print_type(left_type), print_type(right_type),
elna::boot::print_binary_operator(expression->operation()));
this->current_expression = error_mark_node;
auto symbol = this->symbol_map->lookup("Bool");
return build_binary_operation(TREE_TYPE(left) == symbol->payload,
expression, operator_code, left, right, symbol->payload);
}
tree generic_visitor::build_equality_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right)
{
return build_binary_operation(true, expression,
operator_code, left, right, this->symbol_map->lookup("Bool")->payload);
}
void generic_visitor::visit(boot::binary_expression *expression)
{
expression->lhs().accept(this);
auto left = this->current_expression;
auto left_type = TREE_TYPE(left);
tree left = this->current_expression;
tree left_type = TREE_TYPE(left);
expression->rhs().accept(this);
auto right = this->current_expression;
auto right_type = TREE_TYPE(right);
tree right = this->current_expression;
tree right_type = TREE_TYPE(right);
auto expression_location = get_location(&expression->position());
tree_code operator_code = ERROR_MARK;
tree target_type = error_mark_node;
location_t expression_location = get_location(&expression->position());
if (is_pointer_type(left_type) && is_integral_type(right_type)
&& expression->operation() == boot::binary_operator::sum)
if (is_pointer_type(left_type) && is_integral_type(right_type))
{
tree convert_expression = build1_loc(expression_location, CONVERT_EXPR,
sizetype, right);
this->current_expression = build2_loc(expression_location,
POINTER_PLUS_EXPR, left_type, left, convert_expression);
this->current_expression = do_pointer_arithmetic(expression->operation(), left, right);
if (this->current_expression == error_mark_node)
{
error_at(expression_location,
"invalid operation %s on a pointer and an integral type",
boot::print_binary_operator(expression->operation()));
}
return;
}
if (left_type != right_type)
@ -335,87 +349,45 @@ namespace gcc
switch (expression->operation())
{
case boot::binary_operator::sum:
operator_code = PLUS_EXPR;
target_type = left_type;
this->current_expression = build_arithmetic_operation(expression, PLUS_EXPR, left, right);
break;
case boot::binary_operator::subtraction:
operator_code = MINUS_EXPR;
target_type = left_type;
this->current_expression = build_arithmetic_operation(expression, MINUS_EXPR, left, right);
break;
case boot::binary_operator::division:
operator_code = TRUNC_DIV_EXPR;
target_type = left_type;
this->current_expression = build_arithmetic_operation(expression, TRUNC_DIV_EXPR, left, right);
break;
case boot::binary_operator::remainder:
operator_code = TRUNC_MOD_EXPR;
target_type = left_type;
this->current_expression = build_arithmetic_operation(expression, TRUNC_MOD_EXPR, left, right);
break;
case boot::binary_operator::multiplication:
operator_code = MULT_EXPR;
target_type = left_type;
this->current_expression = build_arithmetic_operation(expression, MULT_EXPR, left, right);
break;
case boot::binary_operator::less:
operator_code = LT_EXPR;
target_type = boolean_type_node;
this->current_expression = build_comparison_operation(expression, LT_EXPR, left, right);
break;
case boot::binary_operator::greater:
operator_code = GT_EXPR;
target_type = boolean_type_node;
this->current_expression = build_comparison_operation(expression, GT_EXPR, left, right);
break;
case boot::binary_operator::less_equal:
operator_code = LE_EXPR;
target_type = boolean_type_node;
this->current_expression = build_comparison_operation(expression, LE_EXPR, left, right);
break;
case boot::binary_operator::greater_equal:
operator_code = GE_EXPR;
target_type = boolean_type_node;
this->current_expression = build_comparison_operation(expression, GE_EXPR, left, right);
break;
default:
break;
}
if (operator_code != ERROR_MARK) // An arithmetic operation.
{
build_binary_operation(is_integral_type(left_type) || left_type == double_type_node,
expression, operator_code, left, right, target_type);
return;
}
switch (expression->operation())
{
case boot::binary_operator::conjunction:
operator_code = TRUTH_ANDIF_EXPR;
target_type = boolean_type_node;
this->current_expression = build_logic_operation(expression, TRUTH_ANDIF_EXPR, left, right);
break;
case boot::binary_operator::disjunction:
operator_code = TRUTH_ORIF_EXPR;
target_type = boolean_type_node;
this->current_expression = build_logic_operation(expression, TRUTH_ORIF_EXPR, left, right);
break;
default:
break;
}
if (operator_code != ERROR_MARK) // A logical operation.
{
build_binary_operation(left_type == boolean_type_node,
expression, operator_code, left, right, target_type);
return;
}
switch (expression->operation())
{
case boot::binary_operator::equals:
operator_code = EQ_EXPR;
target_type = boolean_type_node;
this->current_expression = build_equality_operation(expression, EQ_EXPR, left, right);
break;
case boot::binary_operator::not_equals:
operator_code = NE_EXPR;
target_type = boolean_type_node;
break;
default:
this->current_expression = build_equality_operation(expression, NE_EXPR, left, right);
break;
}
gcc_assert(operator_code != ERROR_MARK);
gcc_assert(target_type != error_mark_node);
this->current_expression = build2_loc(expression_location,
operator_code, target_type, left, right);
}
void generic_visitor::visit(boot::unary_expression *expression)

View File

@ -1,6 +1,9 @@
#include "elna/gcc/elna-tree.h"
#include "elna/gcc/elna-diagnostic.h"
#include "stor-layout.h"
#include "fold-const.h"
#include "diagnostic-core.h"
tree elna_global_trees[ELNA_TI_MAX];
@ -20,12 +23,6 @@ namespace gcc
return TREE_CODE(type) == POINTER_TYPE;
}
bool is_string_type(tree type)
{
return is_pointer_type(type)
&& TYPE_MAIN_VARIANT(TREE_TYPE(type)) == char_type_node;
}
tree tree_chain_base::head()
{
return first;
@ -81,5 +78,43 @@ namespace gcc
return initial_table;
}
tree do_pointer_arithmetic(boot::binary_operator binary_operator, tree left, tree right)
{
tree result = error_mark_node;
tree convert_expression = fold_convert(sizetype, right);
if (binary_operator == boot::binary_operator::sum)
{
result = fold_build2(POINTER_PLUS_EXPR, TREE_TYPE(left), left, convert_expression);
}
else if (binary_operator == boot::binary_operator::subtraction)
{
convert_expression = fold_build1(NEGATE_EXPR, sizetype, convert_expression);
result = fold_build2(POINTER_PLUS_EXPR, TREE_TYPE(left), left, convert_expression);
}
return result;
}
tree build_binary_operation(bool condition, boot::binary_expression *expression,
tree_code operator_code, tree left, tree right, tree target_type)
{
location_t expression_location = get_location(&expression->position());
tree left_type = TREE_TYPE(left);
tree right_type = TREE_TYPE(right);
if (condition)
{
return build2_loc(expression_location, operator_code, target_type, left, right);
}
else
{
error_at(expression_location,
"invalid operands of type %s and %s for operator %s",
print_type(left_type), print_type(right_type),
elna::boot::print_binary_operator(expression->operation()));
return error_mark_node;
}
}
}
}

View File

@ -31,12 +31,19 @@ namespace gcc
void enter_scope();
tree_symbol_mapping leave_scope();
void build_binary_operation(bool condition, boot::binary_expression *expression,
tree_code operator_code, tree left, tree right, tree target_type);
void make_if_branch(boot::conditional_statements& branch, tree goto_endif);
bool is_integral_type(tree type);
bool is_numeric_type(tree type);
tree build_arithmetic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right);
tree build_comparison_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right);
tree build_logic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right);
tree build_equality_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right);
public:
generic_visitor(std::shared_ptr<boot::symbol_table<tree>> symbol_table);

View File

@ -4,8 +4,8 @@
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "tree.h"
#include "elna/boot/ast.h"
#include "elna/boot/symbol.h"
enum elna_tree_index
@ -24,7 +24,6 @@ namespace gcc
{
void init_ttree();
bool is_pointer_type(tree type);
bool is_string_type(tree type);
class tree_chain_base
{
@ -58,5 +57,11 @@ namespace gcc
};
std::shared_ptr<boot::symbol_table<tree>> builtin_symbol_table();
tree do_pointer_arithmetic(boot::binary_operator binary_operator, tree left, tree right);
tree build_binary_operation(bool condition, boot::binary_expression *expression,
tree_code operator_code, tree left, tree right, tree target_type);
tree build_arithmetic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right);
}
}

View File

@ -20,6 +20,9 @@ type
end,
FILE = record
dummy: Int
end,
CommandLine = record
input: pointer to Char
end;
const
@ -141,19 +144,6 @@ end;
End of standard procedures.
*)
proc test_primitive();
begin
write_s("\nTest primitives:\n");
write_u(25u);
write_c('\n');
write_i(8);
write_c('\n');
write_b(true);
write_c('\n')
end;
proc read_source(filename: String): pointer to Char;
var
input_file: pointer to FILE,
@ -272,7 +262,7 @@ var
is_valid: Bool;
begin
token_end := input;
previous := cast(cast(input as Word) - 1u as pointer to Char);
previous := input - 1;
while token_end^ <> '\0' and not (previous^ <> '\\' and token_end^ = '"') do
previous := token_end;
@ -286,7 +276,7 @@ begin
is_valid := true;
constructed_string := current_token^.value.string_value;
while cast(input as Word) < cast(token_end as Word) and is_valid do
while input < token_end and is_valid do
if input^ = '\\' then
input := input + 1;
@ -677,32 +667,51 @@ begin
return tokens
end;
proc command_line(argc: Int, argv: pointer to pointer to Char);
proc parse_command_line(argc: Int, argv: pointer to pointer to Char): pointer to CommandLine;
var
parameter: pointer to pointer to Char,
i: Int;
i: Int,
result: pointer to CommandLine;
begin
write_s("Argument count: ");
write_i(argc - 1);
write_s("\nArguments:");
if argc < 2 then
write_s("Fatal error: no input files.\n");
return nil
end;
if argc > 2 then
write_s("Fatal error: Unknown command line options:");
i := 1;
i := 2;
while i < argc do
parameter := argv + i * cast(sizeof(pointer to Char) as Int);
write_c(' ');
write_s(parameter^);
i := i + 1
end
end;
write_s(".\n");
return nil
end;
parameter := argv + cast(sizeof(pointer to Char) as Int);
result := cast(malloc(sizeof(CommandLine)) as pointer to CommandLine);
result^.input := parameter^;
return result
end;
proc compile();
proc process(argc: Int, argv: pointer to pointer to Char): Int;
var
input: pointer to Char,
tokens: pointer to Token,
tokens_size: Word;
tokens_size: Word,
command_line: pointer to CommandLine;
begin
input := read_source("example.elna");
command_line := parse_command_line(argc, argv);
if cast(command_line as Word) = 0u then
return 2
end;
input := read_source(command_line^.input);
tokens := tokenize(input, @tokens_size);
free(input);
@ -711,9 +720,5 @@ begin
end;
begin
command_line(count, parameters);
compile();
test_primitive();
exit(0)
exit(process(count, parameters))
end.