summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNagi5Yeq <wu@x-nagi.com>2020-05-30 16:14:12 +0800
committerNagi5Yeq <wu@x-nagi.com>2020-05-30 16:14:12 +0800
commitfb1fed0f275d1673cf6fb6c3887bc8919e1849d7 (patch)
treef9fe14b95c53760b5bea25b5931eb52b147d05d6
parent30726a1872c92d40424d496fc52043ca452d935f (diff)
full support of pointer access
-rw-r--r--Parser.y22
-rw-r--r--t/trivial.pas4
2 files changed, 16 insertions, 10 deletions
diff --git a/Parser.y b/Parser.y
index b3ecc5b..2d859c2 100644
--- a/Parser.y
+++ b/Parser.y
@@ -62,10 +62,10 @@ YY_DECL;
%type <int> INTEGER_LITERAL
%type <float> REAL_LITERAL
%type <std::vector<char>> STRING_LITERAL
-%type <std::shared_ptr<pcc::ExprNode>> literal lvalue
+%type <std::shared_ptr<pcc::ExprNode>> literal lvalue rvalue standalone
%type <std::list<std::shared_ptr<pcc::ExprNode>>> arguments
%type <std::shared_ptr<pcc::FunctionCallNode>> function_call
-%type <std::shared_ptr<pcc::ExprNode>> expression l1_expression l2_expression l3_expression standalone_expression
+%type <std::shared_ptr<pcc::ExprNode>> expression l1_expression l2_expression l3_expression
%type <pcc::BinaryOperator> l1_operator l2_operator l3_operator
%type <pcc::UnaryOperator> l4_operator
@@ -229,20 +229,24 @@ l2_expression
l3_expression
: l4_operator l3_expression {$$=std::make_shared<pcc::UnaryExprNode>(ctx,$1,$2);}
| AT lvalue {$$=$2;}
- | standalone_expression {$$=$1;}
+ | standalone {$$=$1;}
+
+standalone
+ : rvalue {$$=$1;}
+ | lvalue {$$=std::make_shared<pcc::L2RCastingNode>(ctx, $1);}
;
-standalone_expression
- : lvalue {$$=std::make_shared<pcc::L2RCastingNode>(ctx, $1);}
- | function_call {$$=$1;}
+rvalue
+ : function_call {$$=$1;}
| literal {$$=$1;}
| LPARENTHESIS expression RPARENTHESIS {$$=$2;}
;
lvalue
- : IDENTIFIER {$$=std::make_shared<pcc::IdentifierNode>(ctx, $1);}
- | lvalue LBRACKET expression RBRACKET {$$=std::make_shared<pcc::ArrayAccessNode>(ctx, $1, $3);}
- | standalone_expression DOT IDENTIFIER
+ : IDENTIFIER {$$=std::make_shared<pcc::IdentifierNode>(ctx, $1);}
+ | lvalue LBRACKET expression RBRACKET {$$=std::make_shared<pcc::ArrayAccessNode>(ctx, $1, $3);}
+ | rvalue LBRACKET expression RBRACKET {$$=std::make_shared<pcc::PointerAccessNode>(ctx, $1, $3);}
+ | standalone DOT IDENTIFIER
;
function_call
diff --git a/t/trivial.pas b/t/trivial.pas
index 147505a..fb97adc 100644
--- a/t/trivial.pas
+++ b/t/trivial.pas
@@ -64,7 +64,7 @@ end;
function pointer_test():integer;
var
a,b:integer;
- c:array[0..10] of integer;
+ c:array[0..100] of integer;
p,q:^integer;
s:string;
ps:^char;
@@ -75,6 +75,8 @@ begin
b:=q-p;
p[0]:=a;
q[b]:=a;
+ (p+1)[0]:=a+1;
+ b:=p[0]+p[1];
s:='testabcd';
ps:=s+2;
pointer_test:=b+ps[3];