[AST] Adding indexing operations

This commit is contained in:
Philippe Tillet
2018-12-29 17:06:48 -05:00
parent 1b8199b82d
commit 8f9e6a3655
4 changed files with 55 additions and 3 deletions

View File

@@ -89,11 +89,45 @@ private:
std::vector<T> values_;
};
enum range_enum_t{
ALL,
NEWAXIS
};
class range: public node{
public:
range(range_enum_t type)
: type_(type){}
range_enum_t type() const{
return type_;
}
public:
const range_enum_t type_;
};
class expression: public node{
public:
virtual llvm::Value* codegen(module *) const = 0;
};
class postfix_expression: public expression{
};
class indexing_expression: public postfix_expression{
public:
indexing_expression(node *id, node *ranges)
: id_((const identifier*)id), ranges_((const list<range*>*)ranges) {}
llvm::Value* codegen(module *) const;
private:
const identifier* id_;
const list<range*>* ranges_;
};
class unary_expression: public node{
public:
unary_expression(node *id): id_((const identifier*)id) {}

View File

@@ -49,7 +49,7 @@ TYPE_T get_type_spec(node *op) { return ((token*)op)->type; }
%token XOR_ASSIGN OR_ASSIGN TYPE_NAME
%token VOID UINT8 UINT16 UINT32 UINT64 INT8 INT16 INT32 INT64 FP32 FP64
%token IF ELSE FOR
%token DEF
%token NEWAXIS
%start translation_unit
%%
@@ -115,8 +115,21 @@ primary_expression
| '(' expression ')' { $$ = $1; }
;
unary_expression
range
: ':' { $$ = new range(tdl::ast::ALL); }
| NEWAXIS { $$ = new range(tdl::ast::NEWAXIS); }
range_list
: range { $$ = new list<range*>((range*)$1); }
| range_list ',' range { $$ = append_ptr_list<range>($1, $2); }
postfix_expression
: primary_expression { $$ = $1;}
| identifier '[' range_list ']' { $$ = new indexing_expression($1, $2);}
;
unary_expression
: postfix_expression { $$ = $1; }
| INC_OP unary_expression { $$ = new unary_operator(INC, $2); }
| DEC_OP unary_expression { $$ = new unary_operator(DEC, $2); }
| unary_operator cast_expression { $$ = new unary_operator(get_unary_op($1), $2); }

View File

@@ -16,7 +16,7 @@ int comment();
%}
%%
"def" { count(); return(DEF); }
"newaxis" { count(); return(NEWAXIS); }
"if" { count(); return(IF); }
"else" { count(); return(ELSE); }
"for" { count(); return(FOR); }

View File

@@ -482,6 +482,11 @@ Value* binary_operator::codegen(module *mod) const{
return result;
}
/* Postfix expression */
Value* indexing_expression::codegen(module *mod) const{
return nullptr;
}
/* Unary operator */
Value *unary_operator::llvm_op(llvm::IRBuilder<> &builder, Value *arg, const std::string &name) const{
Type *atype = arg->getType();