Add string parsing and a print builtin (#721)

It was in my mind to add String in order to support libraries in `package`.  `print` is added in order to have a String go to stdout. I've tried to do `print` in a way that won't be too hard to add other printable types, but it's probably also somewhat optional here -- that is, if desired, I could remove it. But it was a lot easier to doublecheck `\n` behavior with it, and I suspect it'll be helpful in other tests if it supports more value types.

On the side, this also fixes dereferencing in Pattern/Expression Print() calls, which I was noticing printing pointers instead of values. This may be another argument for moving away from passing pointers, since this seems to be a difficult-to-catch error.

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Jon Meow
2021-08-11 13:14:05 -07:00
committed by GitHub
co-authored by Geoff Romer
parent ecb5a611e5
commit 250ce4ab00
36 changed files with 549 additions and 33 deletions
+6
View File
@@ -90,6 +90,7 @@ void Carbon::Parser::error(const location_type&, const std::string& message) {
%token <int> integer_literal
%token <std::string> identifier
%token <std::string> sized_type_literal
%token <std::string> string_literal
%type <std::string> designator
%type <const Declaration*> declaration
%type <FunctionDefinition> function_declaration
@@ -131,6 +132,7 @@ void Carbon::Parser::error(const location_type&, const std::string& message) {
%token AND
%token OR
%token NOT
%token STRING
%token BOOL
%token TYPE
%token FN
@@ -218,6 +220,8 @@ expression:
{ $$ = global_arena->New<IndexExpression>(yylineno, $1, $3); }
| integer_literal
{ $$ = global_arena->New<IntLiteral>(yylineno, $1); }
| string_literal
{ $$ = global_arena->New<StringLiteral>(yylineno, $1); }
| TRUE
{ $$ = global_arena->New<BoolLiteral>(yylineno, true); }
| FALSE
@@ -229,6 +233,8 @@ expression:
CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1;
$$ = global_arena->New<IntTypeLiteral>(yylineno);
}
| STRING
{ $$ = global_arena->New<StringTypeLiteral>(yylineno); }
| BOOL
{ $$ = global_arena->New<BoolTypeLiteral>(yylineno); }
| TYPE