Implement adding an integer to a pointer
This commit is contained in:
parent
7b36a3803f
commit
e15f6924b1
68
example.elna
68
example.elna
@ -4,6 +4,9 @@ type
|
|||||||
x: Int;
|
x: Int;
|
||||||
y: Int
|
y: Int
|
||||||
end,
|
end,
|
||||||
|
FILE = record
|
||||||
|
x: Int
|
||||||
|
end,
|
||||||
U = union
|
U = union
|
||||||
a: Int;
|
a: Int;
|
||||||
b: Int
|
b: Int
|
||||||
@ -34,18 +37,6 @@ begin
|
|||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
proc test_pointer();
|
|
||||||
var x: Int, p: pointer to Int;
|
|
||||||
begin
|
|
||||||
x := 5;
|
|
||||||
p := @x;
|
|
||||||
|
|
||||||
writei("");
|
|
||||||
writei("Test pointer:");
|
|
||||||
writei(p);
|
|
||||||
writei(p^)
|
|
||||||
end;
|
|
||||||
|
|
||||||
proc test_record();
|
proc test_record();
|
||||||
var r: R;
|
var r: R;
|
||||||
begin
|
begin
|
||||||
@ -135,13 +126,60 @@ begin
|
|||||||
writei(x)
|
writei(x)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
proc test_return_int(): Int;
|
||||||
|
begin
|
||||||
|
writei("");
|
||||||
|
writei("Test return int:");
|
||||||
|
return 5
|
||||||
|
end;
|
||||||
|
|
||||||
proc exit(code: Int); extern;
|
proc exit(code: Int); extern;
|
||||||
|
|
||||||
|
proc test_add_pointer();
|
||||||
|
var x: Int, p1: pointer to Int;
|
||||||
|
begin
|
||||||
|
writei("");
|
||||||
|
writei("Test add pointer:");
|
||||||
|
|
||||||
|
x := 5;
|
||||||
|
p1 := @x;
|
||||||
|
writei(p1);
|
||||||
|
|
||||||
|
p1 := p1 + 2;
|
||||||
|
writei(p1)
|
||||||
|
end;
|
||||||
|
|
||||||
|
proc fopen(pathname: String, mode: String): pointer to FILE; extern;
|
||||||
|
proc fclose(stream: pointer to FILE): Int; extern;
|
||||||
|
proc fseek(stream: pointer to FILE, off: Int, whence: Int): Int; extern;
|
||||||
|
proc ftell(stream: pointer to FILE): Int; extern;
|
||||||
|
|
||||||
|
proc compile();
|
||||||
|
const
|
||||||
|
SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2;
|
||||||
|
var
|
||||||
|
input_file: pointer to FILE,
|
||||||
|
source_size: Int, ret: Int;
|
||||||
|
begin
|
||||||
|
input_file := fopen("example.elna", "rb");
|
||||||
|
|
||||||
|
// Bug: Functions as statements aren't called.
|
||||||
|
ret := fseek(input_file, 0, SEEK_END);
|
||||||
|
source_size := ftell(input_file);
|
||||||
|
ret := fseek(input_file, 0, SEEK_SET);
|
||||||
|
|
||||||
|
writei("");
|
||||||
|
writei("File size: ");
|
||||||
|
writei(input_file);
|
||||||
|
writei(source_size);
|
||||||
|
|
||||||
|
ret := fclose(input_file)
|
||||||
|
end;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
test_primitive();
|
test_primitive();
|
||||||
test_string();
|
test_string();
|
||||||
test_array();
|
test_array();
|
||||||
test_pointer();
|
|
||||||
test_record();
|
test_record();
|
||||||
test_const();
|
test_const();
|
||||||
test_if();
|
test_if();
|
||||||
@ -149,6 +187,10 @@ begin
|
|||||||
test_param(8, 7);
|
test_param(8, 7);
|
||||||
test_const_char();
|
test_const_char();
|
||||||
test_union();
|
test_union();
|
||||||
|
writei(test_return_int());
|
||||||
|
test_add_pointer();
|
||||||
|
|
||||||
|
compile();
|
||||||
|
|
||||||
exit(0)
|
exit(0)
|
||||||
end.
|
end.
|
||||||
|
@ -20,6 +20,10 @@ namespace gcc
|
|||||||
{
|
{
|
||||||
return "Int";
|
return "Int";
|
||||||
}
|
}
|
||||||
|
else if (type == unsigned_type_node)
|
||||||
|
{
|
||||||
|
return "Word";
|
||||||
|
}
|
||||||
else if (type == boolean_type_node)
|
else if (type == boolean_type_node)
|
||||||
{
|
{
|
||||||
return "Bool";
|
return "Bool";
|
||||||
|
@ -42,6 +42,10 @@ namespace gcc
|
|||||||
{
|
{
|
||||||
format_number = "%d\n";
|
format_number = "%d\n";
|
||||||
}
|
}
|
||||||
|
else if (argument_type == unsigned_type_node)
|
||||||
|
{
|
||||||
|
format_number = "%u\n";
|
||||||
|
}
|
||||||
else if (argument_type == double_type_node)
|
else if (argument_type == double_type_node)
|
||||||
{
|
{
|
||||||
format_number = "%f\n";
|
format_number = "%f\n";
|
||||||
@ -316,6 +320,16 @@ namespace gcc
|
|||||||
tree_code operator_code = ERROR_MARK;
|
tree_code operator_code = ERROR_MARK;
|
||||||
tree target_type = error_mark_node;
|
tree target_type = error_mark_node;
|
||||||
|
|
||||||
|
if (is_pointer_type(left_type)
|
||||||
|
&& (right_type == integer_type_node || right_type == unsigned_type_node)
|
||||||
|
&& expression->operation() == source::binary_operator::sum)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (left_type != right_type)
|
if (left_type != right_type)
|
||||||
{
|
{
|
||||||
error_at(expression_location,
|
error_at(expression_location,
|
||||||
|
@ -74,6 +74,7 @@ namespace gcc
|
|||||||
std::make_shared<elna::source::symbol_table<tree>>();
|
std::make_shared<elna::source::symbol_table<tree>>();
|
||||||
|
|
||||||
initial_table->enter("Int", source::make_info(integer_type_node));
|
initial_table->enter("Int", source::make_info(integer_type_node));
|
||||||
|
initial_table->enter("Word", source::make_info(unsigned_type_node));
|
||||||
initial_table->enter("Bool", source::make_info(boolean_type_node));
|
initial_table->enter("Bool", source::make_info(boolean_type_node));
|
||||||
initial_table->enter("Float", source::make_info(double_type_node));
|
initial_table->enter("Float", source::make_info(double_type_node));
|
||||||
initial_table->enter("Char", source::make_info(elna_char_type_node));
|
initial_table->enter("Char", source::make_info(elna_char_type_node));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user