Skip to content

Commit

Permalink
Parse strings of the "a" "b" "c" kind
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed May 17, 2022
1 parent bb41e2d commit fed1c9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ void yyerror(YYLTYPE *yyloc, LFortran::Parser &p, const std::string &msg)
%type <ast> primary
%type <vec_ast> sep
%type <ast> sep_one
%type <ast> string

// Precedence

Expand Down Expand Up @@ -664,10 +665,14 @@ function_call
| primary "(" keyword_items ")" { $$ = CALL_03($1, $3, @$); }
;

string
: string TK_STRING { $$ = STRING2($1, $2, @$); } // TODO
| TK_STRING { $$ = STRING1($1, @$); }

expr
: id { $$ = $1; }
| TK_INTEGER { $$ = INTEGER($1, @$); }
| TK_STRING { $$ = STRING($1, @$); }
| string { $$ = $1; }
| TK_REAL { $$ = FLOAT($1, @$); }
| TK_IMAG_NUM { $$ = COMPLEX($1, @$); }
| TK_TRUE { $$ = BOOL(true, @$); }
Expand All @@ -679,7 +684,7 @@ expr
| "{" expr_list "}" { $$ = SET($2, @$); }
| "{" expr_list "," "}" { $$ = SET($2, @$); }
| id "[" tuple_list "]" { $$ = SUBSCRIPT_01($1, $3, @$); }
| TK_STRING "[" tuple_list "]" { $$ = SUBSCRIPT_02($1, $3, @$); }
| string "[" tuple_list "]" { $$ = SUBSCRIPT_02($1, $3, @$); }
| expr "." id { $$ = ATTRIBUTE_REF($1, $3, @$); }
| "{" "}" { $$ = DICT_01(@$); }
| "{" dict_list "}" { $$ = DICT_02($2, @$); }
Expand Down
10 changes: 8 additions & 2 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,17 @@ Vec<ast_t*> MERGE_EXPR(Allocator &al, ast_t *x, ast_t *y) {
#define COMPARE(x, op, y, l) make_Compare_t(p.m_a, l, \
EXPR(x), cmpopType::op, EXPRS(A2LIST(p.m_a, y)), 1)

char* concat_string(Allocator &al, ast_t *a, char *b) {
char *s = down_cast2<ConstantStr_t>(a)->m_value;
return LFortran::s2c(al, std::string(s) + std::string(b));
}

#define SYMBOL(x, l) make_Name_t(p.m_a, l, \
x.c_str(p.m_a), expr_contextType::Load)
// `x.int_n` is of type BigInt but we store the int64_t directly in AST
#define INTEGER(x, l) make_ConstantInt_t(p.m_a, l, x, nullptr)
#define STRING(x, l) make_ConstantStr_t(p.m_a, l, x.c_str(p.m_a), nullptr)
#define STRING1(x, l) make_ConstantStr_t(p.m_a, l, x.c_str(p.m_a), nullptr)
#define STRING2(x, y, l) make_ConstantStr_t(p.m_a, l, concat_string(p.m_a, x, y.c_str(p.m_a)), nullptr)
#define FLOAT(x, l) make_ConstantFloat_t(p.m_a, l, x, nullptr)
#define COMPLEX(x, l) make_ConstantComplex_t(p.m_a, l, 0, x, nullptr)
#define BOOL(x, l) make_ConstantBool_t(p.m_a, l, x, nullptr)
Expand Down Expand Up @@ -432,7 +438,7 @@ expr_t* CHECK_TUPLE(expr_t *x) {
#define SUBSCRIPT_01(value, slice, l) make_Subscript_t(p.m_a, l, \
EXPR(value), CHECK_TUPLE(EXPR(slice)), expr_contextType::Load)
#define SUBSCRIPT_02(s, slice, l) make_Subscript_t(p.m_a, l, \
EXPR(STRING(s, l)), CHECK_TUPLE(EXPR(slice)), expr_contextType::Load)
EXPR(s), CHECK_TUPLE(EXPR(slice)), expr_contextType::Load)

static inline ast_t* SLICE(Allocator &al, Location &l,
ast_t *lower, ast_t *upper, ast_t *_step) {
Expand Down

0 comments on commit fed1c9f

Please sign in to comment.