Replace type expression with traits
This commit is contained in:
101
boot/ast.cc
101
boot/ast.cc
@ -44,30 +44,25 @@ namespace boot
|
||||
definition->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(call_expression *expression)
|
||||
void empty_visitor::visit(procedure_call *call)
|
||||
{
|
||||
for (struct expression *const argument : expression->arguments)
|
||||
for (expression *const argument : call->arguments)
|
||||
{
|
||||
argument->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void empty_visitor::visit(traits_expression *trait)
|
||||
{
|
||||
trait->type().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(cast_expression *expression)
|
||||
{
|
||||
expression->target().accept(this);
|
||||
expression->value().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(type_expression *expression)
|
||||
{
|
||||
expression->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(call_statement *statement)
|
||||
{
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(assign_statement *statement)
|
||||
{
|
||||
statement->rvalue().accept(this);
|
||||
@ -249,13 +244,11 @@ namespace boot
|
||||
return this->source_position;
|
||||
}
|
||||
|
||||
statement::statement(const struct position position)
|
||||
: node(position)
|
||||
statement::statement()
|
||||
{
|
||||
}
|
||||
|
||||
expression::expression(const struct position position)
|
||||
: node(position)
|
||||
expression::expression()
|
||||
{
|
||||
}
|
||||
|
||||
@ -539,13 +532,12 @@ namespace boot
|
||||
}
|
||||
}
|
||||
|
||||
literal::literal(const struct position position)
|
||||
: expression(position)
|
||||
literal::literal()
|
||||
{
|
||||
}
|
||||
|
||||
defer_statement::defer_statement(const struct position position)
|
||||
: statement(position)
|
||||
: node(position)
|
||||
{
|
||||
}
|
||||
|
||||
@ -562,13 +554,12 @@ namespace boot
|
||||
}
|
||||
}
|
||||
|
||||
designator_expression::designator_expression(const struct position position)
|
||||
: expression(position)
|
||||
designator_expression::designator_expression()
|
||||
{
|
||||
}
|
||||
|
||||
variable_expression::variable_expression(const struct position position, const std::string& name)
|
||||
: designator_expression(position), m_name(name)
|
||||
: node(position), name(name)
|
||||
{
|
||||
}
|
||||
|
||||
@ -577,11 +568,6 @@ namespace boot
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
const std::string& variable_expression::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
variable_expression *variable_expression::is_variable()
|
||||
{
|
||||
return this;
|
||||
@ -589,7 +575,7 @@ namespace boot
|
||||
|
||||
array_access_expression::array_access_expression(const struct position position,
|
||||
expression *base, expression *index)
|
||||
: designator_expression(position), m_base(base), m_index(index)
|
||||
: node(position), m_base(base), m_index(index)
|
||||
{
|
||||
}
|
||||
|
||||
@ -621,7 +607,7 @@ namespace boot
|
||||
|
||||
field_access_expression::field_access_expression(const struct position position,
|
||||
expression *base, const std::string& field)
|
||||
: designator_expression(position), m_base(base), m_field(field)
|
||||
: node(position), m_base(base), m_field(field)
|
||||
{
|
||||
}
|
||||
|
||||
@ -652,7 +638,7 @@ namespace boot
|
||||
|
||||
dereference_expression::dereference_expression(const struct position position,
|
||||
expression *base)
|
||||
: designator_expression(position), m_base(base)
|
||||
: node(position), m_base(base)
|
||||
{
|
||||
}
|
||||
|
||||
@ -678,7 +664,7 @@ namespace boot
|
||||
|
||||
binary_expression::binary_expression(const struct position position, expression *lhs,
|
||||
expression *rhs, const binary_operator operation)
|
||||
: expression(position), m_lhs(lhs), m_rhs(rhs), m_operator(operation)
|
||||
: node(position), m_lhs(lhs), m_rhs(rhs), m_operator(operation)
|
||||
{
|
||||
}
|
||||
|
||||
@ -710,7 +696,7 @@ namespace boot
|
||||
|
||||
unary_expression::unary_expression(const struct position position, expression *operand,
|
||||
const unary_operator operation)
|
||||
: expression(position), m_operand(std::move(operand)), m_operator(operation)
|
||||
: node(position), m_operand(std::move(operand)), m_operator(operation)
|
||||
{
|
||||
}
|
||||
|
||||
@ -734,22 +720,22 @@ namespace boot
|
||||
delete m_operand;
|
||||
}
|
||||
|
||||
call_expression::call_expression(const struct position position, designator_expression *callable)
|
||||
: expression(position), m_callable(callable)
|
||||
procedure_call::procedure_call(const struct position position, designator_expression *callable)
|
||||
: node(position), m_callable(callable)
|
||||
{
|
||||
}
|
||||
|
||||
void call_expression::accept(parser_visitor *visitor)
|
||||
void procedure_call::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
designator_expression& call_expression::callable()
|
||||
designator_expression& procedure_call::callable()
|
||||
{
|
||||
return *m_callable;
|
||||
}
|
||||
|
||||
call_expression::~call_expression()
|
||||
procedure_call::~procedure_call()
|
||||
{
|
||||
for (expression *const argument : arguments)
|
||||
{
|
||||
@ -760,7 +746,7 @@ namespace boot
|
||||
|
||||
cast_expression::cast_expression(const struct position position,
|
||||
std::shared_ptr<top_type> target, expression *value)
|
||||
: expression(position), m_target(target), m_value(value)
|
||||
: node(position), m_target(target), m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
@ -784,39 +770,20 @@ namespace boot
|
||||
delete m_value;
|
||||
}
|
||||
|
||||
type_expression::type_expression(const struct position position, std::shared_ptr<top_type> body)
|
||||
: expression(position), m_body(body)
|
||||
traits_expression::traits_expression(const struct position position,
|
||||
const std::string& name, std::shared_ptr<top_type> type)
|
||||
: node(position), m_type(type), name(name)
|
||||
{
|
||||
}
|
||||
|
||||
void type_expression::accept(parser_visitor *visitor)
|
||||
void traits_expression::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
top_type& type_expression::body()
|
||||
top_type& traits_expression::type()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
call_statement::call_statement(const struct position position, call_expression *body)
|
||||
: statement(position), m_body(body)
|
||||
{
|
||||
}
|
||||
|
||||
void call_statement::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
call_expression& call_statement::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
call_statement::~call_statement()
|
||||
{
|
||||
delete m_body;
|
||||
return *m_type;
|
||||
}
|
||||
|
||||
conditional_statements::conditional_statements(expression *prerequisite)
|
||||
@ -839,7 +806,7 @@ namespace boot
|
||||
}
|
||||
|
||||
return_statement::return_statement(const struct position position, expression *return_expression)
|
||||
: statement(position), m_return_expression(return_expression)
|
||||
: node(position), m_return_expression(return_expression)
|
||||
{
|
||||
}
|
||||
|
||||
@ -865,7 +832,7 @@ namespace boot
|
||||
|
||||
assign_statement::assign_statement(const struct position position, designator_expression *lvalue,
|
||||
expression *rvalue)
|
||||
: statement(position), m_lvalue(lvalue), m_rvalue(rvalue)
|
||||
: node(position), m_lvalue(lvalue), m_rvalue(rvalue)
|
||||
{
|
||||
}
|
||||
|
||||
@ -906,7 +873,7 @@ namespace boot
|
||||
|
||||
if_statement::if_statement(const struct position position, conditional_statements *body,
|
||||
std::vector<statement *> *alternative)
|
||||
: statement(position), m_body(body), m_alternative(alternative)
|
||||
: node(position), m_body(body), m_alternative(alternative)
|
||||
{
|
||||
}
|
||||
|
||||
@ -936,7 +903,7 @@ namespace boot
|
||||
}
|
||||
|
||||
while_statement::while_statement(const struct position position, conditional_statements *body)
|
||||
: statement(position), m_body(body)
|
||||
: node(position), m_body(body)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -131,6 +131,9 @@ defer {
|
||||
[A-Za-z_][A-Za-z0-9_]* {
|
||||
return yy::parser::make_IDENTIFIER(yytext, this->location);
|
||||
}
|
||||
#[A-Za-z_][A-Za-z0-9_]* {
|
||||
return yy::parser::make_TRAIT(yytext + 1, this->location);
|
||||
}
|
||||
[0-9]+u {
|
||||
return yy::parser::make_WORD(strtoul(yytext, NULL, 10), this->location);
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ along with GCC; see the file COPYING3. If not see
|
||||
%start program;
|
||||
|
||||
%token <std::string> IDENTIFIER
|
||||
%token <std::string> TRAIT
|
||||
%token <std::int32_t> INTEGER
|
||||
%token <std::uint32_t> WORD
|
||||
%token <float> FLOAT
|
||||
@ -125,11 +126,12 @@ along with GCC; see the file COPYING3. If not see
|
||||
formal_parameters formal_parameter_list;
|
||||
%type <elna::boot::variable_declaration *> formal_parameter
|
||||
%type <std::shared_ptr<elna::boot::top_type>> type_expression;
|
||||
%type <elna::boot::traits_expression *> traits_expression;
|
||||
%type <elna::boot::expression *> expression operand unary;
|
||||
%type <std::vector<elna::boot::expression *>> expressions actual_parameter_list;
|
||||
%type <elna::boot::designator_expression *> designator_expression;
|
||||
%type <elna::boot::assign_statement *> assign_statement;
|
||||
%type <elna::boot::call_expression *> call_expression;
|
||||
%type <elna::boot::procedure_call*> call_expression;
|
||||
%type <elna::boot::while_statement *> while_statement;
|
||||
%type <elna::boot::if_statement *> if_statement;
|
||||
%type <elna::boot::return_statement *> return_statement;
|
||||
@ -227,7 +229,7 @@ assign_statement: designator_expression ":=" expression
|
||||
}
|
||||
call_expression: designator_expression actual_parameter_list
|
||||
{
|
||||
$$ = new elna::boot::call_expression(elna::boot::make_position(@1), $1);
|
||||
$$ = new elna::boot::procedure_call(elna::boot::make_position(@1), $1);
|
||||
std::swap($$->arguments, $2);
|
||||
}
|
||||
cast_expression: "cast" "(" expression ":" type_expression ")"
|
||||
@ -313,10 +315,15 @@ literal:
|
||||
{
|
||||
$$ = new elna::boot::number_literal<std::string>(elna::boot::make_position(@1), $1);
|
||||
}
|
||||
traits_expression:
|
||||
TRAIT "(" type_expression ")"
|
||||
{
|
||||
$$ = new elna::boot::traits_expression(elna::boot::make_position(@1), $1, $3);
|
||||
}
|
||||
operand:
|
||||
literal { $$ = $1; }
|
||||
| designator_expression { $$ = $1; }
|
||||
| "(" type_expression ")" { $$ = new elna::boot::type_expression(elna::boot::make_position(@1), $2); }
|
||||
| traits_expression { $$ = $1; }
|
||||
| cast_expression { $$ = $1; }
|
||||
| call_expression { $$ = $1; }
|
||||
| "(" expression ")" { $$ = $2; }
|
||||
@ -448,10 +455,7 @@ statement:
|
||||
| while_statement { $$ = $1; }
|
||||
| if_statement { $$ = $1; }
|
||||
| return_statement { $$ = $1; }
|
||||
| call_expression
|
||||
{
|
||||
$$ = new elna::boot::call_statement(elna::boot::make_position(@1), $1);
|
||||
}
|
||||
| call_expression { $$ = $1; }
|
||||
| defer_statement { $$ = $1; }
|
||||
statements:
|
||||
statement statements
|
||||
|
Reference in New Issue
Block a user