Support the remainder operator
This commit is contained in:
parent
005e9dcc52
commit
1f7c1b4cb8
108
example.elna
108
example.elna
@ -15,6 +15,52 @@ type
|
||||
const
|
||||
SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2;
|
||||
|
||||
--
|
||||
-- External procedures.
|
||||
--
|
||||
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 fread(ptr: pointer to Char, size: Int, nmemb: Int, stream: pointer to FILE): Int; extern;
|
||||
proc write(fd: Int, buf: pointer to Char, count: Int): Int; extern;
|
||||
|
||||
proc malloc(size: Int): pointer to Char; extern;
|
||||
proc free(ptr: pointer to Char); extern;
|
||||
proc calloc(nmemb: Int, size: Int): pointer to Char; extern;
|
||||
|
||||
proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern;
|
||||
|
||||
proc strlen(ptr: pointer to Char): Word; extern;
|
||||
|
||||
proc exit(code: Int); extern;
|
||||
|
||||
--
|
||||
-- Standard procedures.
|
||||
--
|
||||
proc write_s(value: String);
|
||||
begin
|
||||
write(0, value, strlen(value))
|
||||
end;
|
||||
|
||||
proc write_b(value: Bool);
|
||||
begin
|
||||
if value then
|
||||
write_s("true")
|
||||
else
|
||||
write_s("false")
|
||||
end
|
||||
end;
|
||||
|
||||
proc write_c(value: Char);
|
||||
begin
|
||||
write(0, @value, 1)
|
||||
end;
|
||||
|
||||
--
|
||||
-- End of standard procedures.
|
||||
--
|
||||
|
||||
proc test_array();
|
||||
var a: T, x: Int;
|
||||
begin
|
||||
@ -44,31 +90,17 @@ begin
|
||||
end;
|
||||
|
||||
proc test_primitive();
|
||||
var c: Char, z: Float;
|
||||
var u: Word, z: Float;
|
||||
begin
|
||||
c := 'x';
|
||||
u := 25u;
|
||||
z := 8.2;
|
||||
|
||||
writei("");
|
||||
writei("Test primitives:");
|
||||
writei(c);
|
||||
writei(u);
|
||||
writei(z)
|
||||
end;
|
||||
|
||||
proc test_if();
|
||||
var x: Bool, y: Bool;
|
||||
begin
|
||||
x := true;
|
||||
y := false;
|
||||
|
||||
writei("");
|
||||
if x and y then
|
||||
writei("Test if: True")
|
||||
else
|
||||
writei("Test if: False")
|
||||
end
|
||||
end;
|
||||
|
||||
proc test_param(d: Int, e: Int);
|
||||
begin
|
||||
writei("");
|
||||
@ -77,14 +109,6 @@ begin
|
||||
writei(e)
|
||||
end;
|
||||
|
||||
proc test_const_char();
|
||||
const x = 'u';
|
||||
begin
|
||||
writei("");
|
||||
writei("Test constant character");
|
||||
writei(x)
|
||||
end;
|
||||
|
||||
proc test_return_int(): Int;
|
||||
begin
|
||||
writei("");
|
||||
@ -92,35 +116,6 @@ begin
|
||||
return 5
|
||||
end;
|
||||
|
||||
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 fread(ptr: pointer to Char, size: Int, nmemb: Int, stream: pointer to FILE): Int; extern;
|
||||
proc write(fd: Int, buf: pointer to Char, count: Int): Int; extern;
|
||||
|
||||
proc malloc(size: Int): pointer to Char; extern;
|
||||
proc free(ptr: pointer to Char); extern;
|
||||
proc calloc(nmemb: Int, size: Int): pointer to Char; extern;
|
||||
|
||||
proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern;
|
||||
|
||||
proc read_source(filename: String): pointer to Char;
|
||||
var
|
||||
input_file: pointer to FILE,
|
||||
@ -161,11 +156,10 @@ begin
|
||||
test_primitive();
|
||||
test_array();
|
||||
test_record();
|
||||
test_if();
|
||||
test_param(8, 7);
|
||||
test_const_char();
|
||||
writei(test_return_int());
|
||||
test_add_pointer();
|
||||
write_b(true);
|
||||
write_c('\n');
|
||||
|
||||
compile();
|
||||
|
||||
|
@ -50,14 +50,6 @@ namespace gcc
|
||||
{
|
||||
format_number = "%f\n";
|
||||
}
|
||||
else if (argument_type == elna_char_type_node)
|
||||
{
|
||||
format_number = "%c\n";
|
||||
}
|
||||
else if (is_string_type(argument_type))
|
||||
{
|
||||
format_number = "%s\n";
|
||||
}
|
||||
else if (is_pointer_type(argument_type))
|
||||
{
|
||||
format_number = "%p\n";
|
||||
@ -357,6 +349,10 @@ namespace gcc
|
||||
operator_code = TRUNC_DIV_EXPR;
|
||||
target_type = left_type;
|
||||
break;
|
||||
case source::binary_operator::remainder:
|
||||
operator_code = TRUNC_MOD_EXPR;
|
||||
target_type = left_type;
|
||||
break;
|
||||
case source::binary_operator::multiplication:
|
||||
operator_code = MULT_EXPR;
|
||||
target_type = left_type;
|
||||
@ -737,18 +733,18 @@ namespace gcc
|
||||
|
||||
void generic_visitor::visit(source::if_statement *statement)
|
||||
{
|
||||
statement->prerequisite().accept(this);
|
||||
statement->body().prerequisite().accept(this);
|
||||
|
||||
if (TREE_TYPE(this->current_expression) != boolean_type_node)
|
||||
{
|
||||
error_at(get_location(&statement->prerequisite().position()),
|
||||
error_at(get_location(&statement->body().prerequisite().position()),
|
||||
"expected expression of boolean type but its type is %s",
|
||||
print_type(TREE_TYPE(this->current_expression)));
|
||||
this->current_expression = error_mark_node;
|
||||
return;
|
||||
}
|
||||
auto then_location = get_location(&statement->body().position());
|
||||
auto prerequisite_location = get_location(&statement->prerequisite().position());
|
||||
auto then_location = get_location(&statement->position());
|
||||
auto prerequisite_location = get_location(&statement->body().prerequisite().position());
|
||||
|
||||
auto then_label_decl = build_label_decl("then", then_location);
|
||||
auto endif_label_decl = build_label_decl("end_if", then_location);
|
||||
@ -762,7 +758,7 @@ namespace gcc
|
||||
tree goto_else_or_endif = NULL_TREE;
|
||||
if (statement->alternative() != nullptr)
|
||||
{
|
||||
auto else_location = get_location(&statement->alternative()->position());
|
||||
auto else_location = get_location(&statement->position());
|
||||
else_label_decl = build_label_decl("else", else_location);
|
||||
goto_else_or_endif = build1_loc(else_location, GOTO_EXPR, void_type_node, else_label_decl);
|
||||
}
|
||||
@ -779,8 +775,10 @@ namespace gcc
|
||||
void_type_node, then_label_decl);
|
||||
append_to_statement_list(then_label_expr, &this->current_statements);
|
||||
|
||||
statement->body().accept(this);
|
||||
|
||||
for (const auto body_statement : statement->body().statements)
|
||||
{
|
||||
body_statement->accept(this);
|
||||
}
|
||||
if (statement->alternative() != nullptr)
|
||||
{
|
||||
append_to_statement_list(goto_endif, &this->current_statements);
|
||||
@ -788,7 +786,10 @@ namespace gcc
|
||||
auto else_label_expr = build1(LABEL_EXPR, void_type_node, else_label_decl);
|
||||
append_to_statement_list(else_label_expr, &this->current_statements);
|
||||
|
||||
statement->alternative()->accept(this);
|
||||
for (const auto body_statement : *statement->alternative())
|
||||
{
|
||||
body_statement->accept(this);
|
||||
}
|
||||
}
|
||||
auto endif_label_expr = build1(LABEL_EXPR, void_type_node, endif_label_decl);
|
||||
append_to_statement_list(endif_label_expr, &this->current_statements);
|
||||
@ -808,18 +809,18 @@ namespace gcc
|
||||
|
||||
void generic_visitor::visit(source::while_statement *statement)
|
||||
{
|
||||
statement->prerequisite().accept(this);
|
||||
statement->body().prerequisite().accept(this);
|
||||
|
||||
if (TREE_TYPE(this->current_expression) != boolean_type_node)
|
||||
{
|
||||
error_at(get_location(&statement->prerequisite().position()),
|
||||
error_at(get_location(&statement->body().prerequisite().position()),
|
||||
"expected expression of boolean type but its type is %s",
|
||||
print_type(TREE_TYPE(this->current_expression)));
|
||||
this->current_expression = error_mark_node;
|
||||
return;
|
||||
}
|
||||
auto prerequisite_location = get_location(&statement->prerequisite().position());
|
||||
auto body_location = get_location(&statement->body().position());
|
||||
auto prerequisite_location = get_location(&statement->body().prerequisite().position());
|
||||
auto body_location = get_location(&statement->position());
|
||||
|
||||
auto prerequisite_label_decl = build_label_decl("while_check", prerequisite_location);
|
||||
auto prerequisite_label_expr = build1_loc(prerequisite_location, LABEL_EXPR,
|
||||
@ -842,8 +843,10 @@ namespace gcc
|
||||
void_type_node, body_label_decl);
|
||||
append_to_statement_list(body_label_expr, &this->current_statements);
|
||||
|
||||
statement->body().accept(this);
|
||||
|
||||
for (const auto body_statement : statement->body().statements)
|
||||
{
|
||||
body_statement->accept(this);
|
||||
}
|
||||
auto goto_check = build1(GOTO_EXPR, void_type_node, prerequisite_label_decl);
|
||||
append_to_statement_list(goto_check, &this->current_statements);
|
||||
|
||||
|
@ -19,6 +19,7 @@ namespace source
|
||||
subtraction,
|
||||
multiplication,
|
||||
division,
|
||||
remainder,
|
||||
equals,
|
||||
not_equals,
|
||||
less,
|
||||
@ -40,7 +41,6 @@ namespace source
|
||||
class procedure_definition;
|
||||
class type_definition;
|
||||
class call_expression;
|
||||
class compound_statement;
|
||||
class assign_statement;
|
||||
class if_statement;
|
||||
class while_statement;
|
||||
@ -75,7 +75,6 @@ namespace source
|
||||
virtual void visit(type_definition *) = 0;
|
||||
virtual void visit(call_expression *) = 0;
|
||||
virtual void visit(expression_statement *) = 0;
|
||||
virtual void visit(compound_statement *) = 0;
|
||||
virtual void visit(assign_statement *) = 0;
|
||||
virtual void visit(if_statement *) = 0;
|
||||
virtual void visit(while_statement *) = 0;
|
||||
@ -112,7 +111,6 @@ namespace source
|
||||
virtual void visit(type_definition *definition) override;
|
||||
virtual void visit(call_expression *statement) override;
|
||||
virtual void visit(expression_statement *statement) override;
|
||||
virtual void visit(compound_statement *statement) override;
|
||||
virtual void visit(assign_statement *statement) override;
|
||||
virtual void visit(if_statement *) override;
|
||||
virtual void visit(while_statement *) override;
|
||||
@ -484,15 +482,21 @@ namespace source
|
||||
virtual ~expression_statement() override;
|
||||
};
|
||||
|
||||
class compound_statement : public node
|
||||
/**
|
||||
* List of statements paired with a condition.
|
||||
*/
|
||||
class conditional_statements
|
||||
{
|
||||
expression *m_prerequisite;
|
||||
|
||||
public:
|
||||
std::vector<statement *> statements;
|
||||
|
||||
compound_statement(const struct position position, std::vector<statement *>&& statements);
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
conditional_statements(expression *prerequisite);
|
||||
|
||||
virtual ~compound_statement() override;
|
||||
expression& prerequisite();
|
||||
|
||||
virtual ~conditional_statements();
|
||||
};
|
||||
|
||||
class return_statement : public statement
|
||||
@ -609,24 +613,16 @@ namespace source
|
||||
*/
|
||||
class if_statement : public statement
|
||||
{
|
||||
expression *m_prerequisite;
|
||||
compound_statement *m_body;
|
||||
compound_statement *m_alternative;
|
||||
conditional_statements *m_body;
|
||||
std::vector<statement *> *m_alternative;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \param position Source code position.
|
||||
* \param prerequisite Condition.
|
||||
* \param body Statement executed if the condition is met.
|
||||
* \param alternative Statement executed if the condition is not met.
|
||||
*/
|
||||
if_statement(const struct position position, expression *prerequisite,
|
||||
compound_statement *body, compound_statement *alternative = nullptr);
|
||||
if_statement(const struct position position, conditional_statements *body,
|
||||
std::vector<statement *> *alternative = nullptr);
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
|
||||
expression& prerequisite();
|
||||
compound_statement& body();
|
||||
compound_statement *alternative();
|
||||
conditional_statements& body();
|
||||
std::vector<statement *> *alternative();
|
||||
|
||||
virtual ~if_statement() override;
|
||||
};
|
||||
@ -636,21 +632,13 @@ namespace source
|
||||
*/
|
||||
class while_statement : public statement
|
||||
{
|
||||
expression *m_prerequisite;
|
||||
compound_statement *m_body;
|
||||
conditional_statements *m_body;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \param position Source code position.
|
||||
* \param prerequisite Condition.
|
||||
* \param body Statement executed while the condition is met.
|
||||
*/
|
||||
while_statement(const struct position position, expression *prerequisite,
|
||||
compound_statement *body);
|
||||
while_statement(const struct position position, conditional_statements *body);
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
|
||||
expression& prerequisite();
|
||||
compound_statement& body();
|
||||
conditional_statements& body();
|
||||
|
||||
virtual ~while_statement() override;
|
||||
};
|
||||
@ -721,7 +709,7 @@ namespace source
|
||||
|
||||
public:
|
||||
binary_expression(const struct position position, expression *lhs,
|
||||
expression *rhs, const unsigned char operation);
|
||||
expression *rhs, const binary_operator operation);
|
||||
|
||||
virtual void accept(parser_visitor *visitor) override;
|
||||
expression& lhs();
|
||||
|
@ -1,116 +0,0 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License
|
||||
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
// obtain one at http://mozilla.org/MPL/2.0/.
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace elna
|
||||
{
|
||||
namespace source
|
||||
{
|
||||
class primitive_type;
|
||||
class pointer_type;
|
||||
class procedure_type;
|
||||
|
||||
/**
|
||||
* Type representation.
|
||||
*/
|
||||
class type
|
||||
{
|
||||
const std::size_t byte_size;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* \param byte_size The type size in bytes.
|
||||
*/
|
||||
explicit type(const std::size_t byte_size);
|
||||
|
||||
public:
|
||||
/**
|
||||
* \return The type size in bytes.
|
||||
*/
|
||||
virtual std::size_t size() const noexcept;
|
||||
|
||||
/**
|
||||
* \return Unique type representation.
|
||||
*/
|
||||
virtual std::string type_name() const = 0;
|
||||
|
||||
virtual const pointer_type *is_pointer_type() const;
|
||||
|
||||
friend bool operator==(const type& lhs, const type& rhs) noexcept;
|
||||
friend bool operator!=(const type& lhs, const type& rhs) noexcept;
|
||||
};
|
||||
|
||||
/**
|
||||
* Built-in type representation.
|
||||
*/
|
||||
class primitive_type final : public type
|
||||
{
|
||||
/// Type name.
|
||||
const std::string m_type_name;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* \param type_name Type name.
|
||||
* \param byte_size The type size in bytes.
|
||||
*/
|
||||
primitive_type(const std::string& type_name, const std::size_t byte_size);
|
||||
|
||||
virtual std::string type_name() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Typed pointer.
|
||||
*/
|
||||
struct pointer_type final : public type
|
||||
{
|
||||
/// Pointer target type.
|
||||
std::shared_ptr<const type> base_type;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* \param base_type Pointer target type.
|
||||
* \param byte_size The type size in bytes.
|
||||
*/
|
||||
pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size);
|
||||
|
||||
virtual std::string type_name() const override;
|
||||
|
||||
virtual const pointer_type *is_pointer_type() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Type of a procedure.
|
||||
*/
|
||||
struct procedure_type final : public type
|
||||
{
|
||||
/// Argument types.
|
||||
std::vector<std::shared_ptr<const type>> arguments;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* \param arguments Argument types.
|
||||
* \param byte_size Function pointer size.
|
||||
*/
|
||||
procedure_type(std::vector<std::shared_ptr<const type>> arguments, const std::size_t byte_size);
|
||||
|
||||
virtual std::string type_name() const override;
|
||||
};
|
||||
|
||||
bool operator==(const type& lhs, const type& rhs) noexcept;
|
||||
bool operator!=(const type& lhs, const type& rhs) noexcept;
|
||||
|
||||
extern const primitive_type boolean_type;
|
||||
extern const primitive_type int_type;
|
||||
}
|
||||
}
|
110
source/ast.cc
110
source/ast.cc
@ -46,14 +46,6 @@ namespace source
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(compound_statement *statement)
|
||||
{
|
||||
for (const auto nested_statement : statement->statements)
|
||||
{
|
||||
nested_statement->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void empty_visitor::visit(assign_statement *statement)
|
||||
{
|
||||
statement->rvalue().accept(this);
|
||||
@ -61,14 +53,20 @@ namespace source
|
||||
|
||||
void empty_visitor::visit(if_statement *statement)
|
||||
{
|
||||
statement->prerequisite().accept(this);
|
||||
statement->body().accept(this);
|
||||
statement->body().prerequisite().accept(this);
|
||||
for (const auto body_statement : statement->body().statements)
|
||||
{
|
||||
body_statement->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void empty_visitor::visit(while_statement *statement)
|
||||
{
|
||||
statement->prerequisite().accept(this);
|
||||
statement->body().accept(this);
|
||||
statement->body().prerequisite().accept(this);
|
||||
for (const auto body_statement : statement->body().statements)
|
||||
{
|
||||
body_statement->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void empty_visitor::visit(return_statement *statement)
|
||||
@ -683,50 +681,9 @@ namespace source
|
||||
}
|
||||
|
||||
binary_expression::binary_expression(const struct position position, expression *lhs,
|
||||
expression *rhs, const unsigned char operation)
|
||||
: expression(position), m_lhs(std::move(lhs)), m_rhs(std::move(rhs))
|
||||
expression *rhs, const binary_operator operation)
|
||||
: expression(position), m_lhs(lhs), m_rhs(rhs), m_operator(operation)
|
||||
{
|
||||
switch (operation)
|
||||
{
|
||||
case '+':
|
||||
this->m_operator = binary_operator::sum;
|
||||
break;
|
||||
case '-':
|
||||
this->m_operator = binary_operator::subtraction;
|
||||
break;
|
||||
case '*':
|
||||
this->m_operator = binary_operator::multiplication;
|
||||
break;
|
||||
case '/':
|
||||
this->m_operator = binary_operator::division;
|
||||
break;
|
||||
case '=':
|
||||
this->m_operator = binary_operator::equals;
|
||||
break;
|
||||
case 'n':
|
||||
this->m_operator = binary_operator::not_equals;
|
||||
break;
|
||||
case '<':
|
||||
this->m_operator = binary_operator::less;
|
||||
break;
|
||||
case 'l':
|
||||
this->m_operator = binary_operator::less_equal;
|
||||
break;
|
||||
case '>':
|
||||
this->m_operator = binary_operator::greater;
|
||||
break;
|
||||
case 'g':
|
||||
this->m_operator = binary_operator::greater_equal;
|
||||
break;
|
||||
case 'o':
|
||||
this->m_operator = binary_operator::disjunction;
|
||||
break;
|
||||
case 'a':
|
||||
this->m_operator = binary_operator::conjunction;
|
||||
break;
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
void binary_expression::accept(parser_visitor *visitor)
|
||||
@ -829,18 +786,19 @@ namespace source
|
||||
delete m_body;
|
||||
}
|
||||
|
||||
compound_statement::compound_statement(const struct position position, std::vector<statement *>&& statements)
|
||||
: node(position), statements(std::move(statements))
|
||||
conditional_statements::conditional_statements(expression *prerequisite)
|
||||
: m_prerequisite(prerequisite)
|
||||
{
|
||||
}
|
||||
|
||||
void compound_statement::accept(parser_visitor *visitor)
|
||||
expression& conditional_statements::prerequisite()
|
||||
{
|
||||
visitor->visit(this);
|
||||
return *m_prerequisite;
|
||||
}
|
||||
|
||||
compound_statement::~compound_statement()
|
||||
conditional_statements::~conditional_statements()
|
||||
{
|
||||
delete m_prerequisite;
|
||||
for (auto statement : statements)
|
||||
{
|
||||
delete statement;
|
||||
@ -916,10 +874,9 @@ namespace source
|
||||
delete m_rvalue;
|
||||
}
|
||||
|
||||
if_statement::if_statement(const struct position position, expression *prerequisite,
|
||||
compound_statement *body, compound_statement *alternative)
|
||||
: statement(position), m_prerequisite(prerequisite), m_body(body),
|
||||
m_alternative(alternative)
|
||||
if_statement::if_statement(const struct position position, conditional_statements *body,
|
||||
std::vector<statement *> *alternative)
|
||||
: statement(position), m_body(body), m_alternative(alternative)
|
||||
{
|
||||
}
|
||||
|
||||
@ -928,24 +885,18 @@ namespace source
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
expression& if_statement::prerequisite()
|
||||
{
|
||||
return *m_prerequisite;
|
||||
}
|
||||
|
||||
compound_statement& if_statement::body()
|
||||
conditional_statements& if_statement::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
compound_statement *if_statement::alternative()
|
||||
std::vector<statement *> *if_statement::alternative()
|
||||
{
|
||||
return m_alternative;
|
||||
}
|
||||
|
||||
if_statement::~if_statement()
|
||||
{
|
||||
delete m_prerequisite;
|
||||
delete m_body;
|
||||
|
||||
if (m_alternative != nullptr)
|
||||
@ -954,9 +905,8 @@ namespace source
|
||||
}
|
||||
}
|
||||
|
||||
while_statement::while_statement(const struct position position, expression *prerequisite,
|
||||
compound_statement *body)
|
||||
: statement(position), m_prerequisite(prerequisite), m_body(body)
|
||||
while_statement::while_statement(const struct position position, conditional_statements *body)
|
||||
: statement(position), m_body(body)
|
||||
{
|
||||
}
|
||||
|
||||
@ -965,19 +915,13 @@ namespace source
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
expression& while_statement::prerequisite()
|
||||
{
|
||||
return *m_prerequisite;
|
||||
}
|
||||
|
||||
compound_statement& while_statement::body()
|
||||
conditional_statements& while_statement::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
while_statement::~while_statement()
|
||||
{
|
||||
delete m_prerequisite;
|
||||
delete m_body;
|
||||
}
|
||||
|
||||
@ -993,6 +937,8 @@ namespace source
|
||||
return "*";
|
||||
case binary_operator::division:
|
||||
return "/";
|
||||
case binary_operator::remainder:
|
||||
return "%";
|
||||
case binary_operator::equals:
|
||||
return "=";
|
||||
case binary_operator::not_equals:
|
||||
|
@ -40,6 +40,9 @@ then {
|
||||
else {
|
||||
return yy::parser::make_ELSE(this->location);
|
||||
}
|
||||
elsif {
|
||||
return yy::parser::make_ELSIF(this->location);
|
||||
}
|
||||
while {
|
||||
return yy::parser::make_WHILE(this->location);
|
||||
}
|
||||
@ -237,6 +240,9 @@ return {
|
||||
\/ {
|
||||
return yy::parser::make_DIVISION(this->location);
|
||||
}
|
||||
% {
|
||||
return yy::parser::make_REMAINDER(this->location);
|
||||
}
|
||||
:= {
|
||||
return yy::parser::make_ASSIGNMENT(this->location);
|
||||
}
|
||||
|
@ -63,13 +63,13 @@
|
||||
%token <std::string> CHARACTER "character"
|
||||
%token <std::string> STRING "string"
|
||||
%token <bool> BOOLEAN
|
||||
%token IF WHILE DO THEN ELSE RETURN
|
||||
%token IF WHILE DO THEN ELSE ELSIF RETURN
|
||||
%token CONST VAR PROCEDURE ARRAY OF TYPE RECORD POINTER TO UNION
|
||||
%token BEGIN_BLOCK END_BLOCK EXTERN
|
||||
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
|
||||
%token AND OR NOT
|
||||
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
|
||||
%token PLUS MINUS MULTIPLICATION DIVISION
|
||||
%token PLUS MINUS MULTIPLICATION DIVISION REMAINDER
|
||||
%token ASSIGNMENT COLON HAT AT
|
||||
|
||||
%type <elna::source::literal *> literal;
|
||||
@ -184,20 +184,23 @@ call_expression: IDENTIFIER actual_parameter_list
|
||||
}
|
||||
while_statement: WHILE expression DO optional_statements END_BLOCK
|
||||
{
|
||||
auto body = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
|
||||
$$ = new elna::source::while_statement(elna::source::make_position(@1), $2, body);
|
||||
auto body = new elna::source::conditional_statements($2);
|
||||
std::swap($4, body->statements);
|
||||
$$ = new elna::source::while_statement(elna::source::make_position(@1), body);
|
||||
}
|
||||
if_statement:
|
||||
IF expression THEN optional_statements END_BLOCK
|
||||
{
|
||||
auto then = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
|
||||
$$ = new elna::source::if_statement(elna::source::make_position(@1), $2, then);
|
||||
auto then = new elna::source::conditional_statements($2);
|
||||
std::swap($4, then->statements);
|
||||
$$ = new elna::source::if_statement(elna::source::make_position(@1), then);
|
||||
}
|
||||
| IF expression THEN optional_statements ELSE optional_statements END_BLOCK
|
||||
{
|
||||
auto then = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
|
||||
auto _else = new elna::source::compound_statement(elna::source::make_position(@5), std::move($6));
|
||||
$$ = new elna::source::if_statement(elna::source::make_position(@1), $2, then, _else);
|
||||
auto then = new elna::source::conditional_statements($2);
|
||||
std::swap($4, then->statements);
|
||||
auto _else = new std::vector<elna::source::statement *>(std::move($6));
|
||||
$$ = new elna::source::if_statement(elna::source::make_position(@1), then, _else);
|
||||
}
|
||||
return_statement:
|
||||
RETURN expression
|
||||
@ -238,13 +241,18 @@ summand:
|
||||
factor { $$ = std::move($1); }
|
||||
| factor MULTIPLICATION factor
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1),
|
||||
$1, $3, '*');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::multiplication);
|
||||
}
|
||||
| factor DIVISION factor
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1),
|
||||
$1, $3, '/');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::division);
|
||||
}
|
||||
| factor REMAINDER factor
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::remainder);
|
||||
}
|
||||
factor:
|
||||
AT pointer
|
||||
@ -261,47 +269,57 @@ factor:
|
||||
comparand:
|
||||
summand PLUS summand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '+');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::sum);
|
||||
}
|
||||
| summand MINUS summand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '-');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::subtraction);
|
||||
}
|
||||
| summand { $$ = std::move($1); }
|
||||
logical_operand:
|
||||
comparand EQUALS comparand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '=');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::equals);
|
||||
}
|
||||
| comparand NOT_EQUAL comparand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'n');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::not_equals);
|
||||
}
|
||||
| comparand LESS_THAN comparand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '<');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::less);
|
||||
}
|
||||
| comparand GREATER_THAN comparand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '>');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::greater);
|
||||
}
|
||||
| comparand LESS_EQUAL comparand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '<');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::less_equal);
|
||||
}
|
||||
| comparand GREATER_EQUAL comparand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '>');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::greater_equal);
|
||||
}
|
||||
| comparand { $$ = $1; }
|
||||
expression:
|
||||
logical_operand AND logical_operand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'a');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::conjunction);
|
||||
}
|
||||
| logical_operand OR logical_operand
|
||||
{
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'o');
|
||||
$$ = new elna::source::binary_expression(elna::source::make_position(@2), $1, $3,
|
||||
elna::source::binary_operator::disjunction);
|
||||
}
|
||||
| logical_operand { $$ = $1; }
|
||||
expressions:
|
||||
|
@ -1,83 +0,0 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License
|
||||
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
// obtain one at http://mozilla.org/MPL/2.0/.
|
||||
#include <elna/source/types.h>
|
||||
|
||||
namespace elna
|
||||
{
|
||||
namespace source
|
||||
{
|
||||
type::type(const std::size_t byte_size)
|
||||
: byte_size(byte_size)
|
||||
{
|
||||
}
|
||||
|
||||
std::size_t type::size() const noexcept
|
||||
{
|
||||
return this->byte_size;
|
||||
}
|
||||
|
||||
const pointer_type *type::is_pointer_type() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
primitive_type::primitive_type(const std::string& type_name, const std::size_t byte_size)
|
||||
: type(byte_size), m_type_name(type_name)
|
||||
{
|
||||
}
|
||||
|
||||
std::string primitive_type::type_name() const
|
||||
{
|
||||
return m_type_name;
|
||||
}
|
||||
|
||||
pointer_type::pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size)
|
||||
: type(byte_size), base_type(base_type)
|
||||
{
|
||||
}
|
||||
|
||||
const pointer_type *pointer_type::is_pointer_type() const
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
std::string pointer_type::type_name() const
|
||||
{
|
||||
return '^' + base_type->type_name();
|
||||
}
|
||||
|
||||
procedure_type::procedure_type(std::vector<std::shared_ptr<const type>> arguments, const std::size_t byte_size)
|
||||
:type(byte_size), arguments(std::move(arguments))
|
||||
{
|
||||
}
|
||||
|
||||
std::string procedure_type::type_name() const
|
||||
{
|
||||
std::string result{ "proc(" };
|
||||
for (const auto& argument : arguments)
|
||||
{
|
||||
result += argument->type_name() + ',';
|
||||
}
|
||||
result.at(result.size() - 1) = ')';
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const type& lhs, const type& rhs) noexcept
|
||||
{
|
||||
auto lhs_type = lhs.type_name();
|
||||
auto rhs_type = rhs.type_name();
|
||||
|
||||
return lhs_type == rhs_type;
|
||||
}
|
||||
|
||||
bool operator!=(const type& lhs, const type& rhs) noexcept
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
const primitive_type boolean_type{ "Boolean", 1 };
|
||||
const primitive_type int_type{ "Int", 4 };
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user