mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
Notes versus what jsiek wrote:
- This adopts Bazel for building.
- System-local versions of bison/flex are used. I found https://github.com/jmillikin/rules_bison, but those print a lot of warnings (things like -Wsign-compare IIRC) which makes builds hard to read. Plus I think the underlying bison_cc_library rule didn't work, so this would really only get a hermetic bison/flex build (helpful, but didn't seem worth more time).
- I'm adding in a .bazeliskrc to push a somewhat more standard choice of bazel versions. I noticed I was getting unstable versions by default, possible Google-specific, but seemed good to include.
- The `-lpthread` kludge.
- Turn all of the examples into golden tests.
- Including adding a golden test rule.
- Fixed various style guide issues. For example:
- Fixing function names to be CamelCase instead of snake_case (https://google.github.io/styleguide/cppguide.html#Function_Names)
- Removed exception use (https://google.github.io/styleguide/cppguide.html#Exceptions)
- File name fixes (https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#file-names)
- Dropped `using` of `std` names -- I believe this is preferred (maybe we should be explicit about this in the Carbon style guide)
- Switched `enum` uses to `enum class` for ease-of-identification.
- Spent some time breaking out files to hopefully be easier to read/edit pieces, and understand relations between structs.
- Added `code requires` to `syntax.ypp` to address include issues
Possibly other things -- but the fundamental structure is, I believe, unchanged. I put in the golden tests pretty early to ensure I wasn't mutating output/results.
315 lines
8.4 KiB
Plaintext
315 lines
8.4 KiB
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
%code top {
|
|
#include <algorithm>
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <list>
|
|
|
|
#include "executable_semantics/syntax_helpers.h"
|
|
}
|
|
|
|
%code requires {
|
|
#include "executable_semantics/ast/declaration.h"
|
|
#include "executable_semantics/ast/expression_or_field_list.h"
|
|
#include "executable_semantics/ast/function_definition.h"
|
|
}
|
|
|
|
%code {
|
|
extern int yylineno;
|
|
extern int yylex();
|
|
|
|
void yyerror(char* error) {
|
|
Carbon::PrintSyntaxError(error, yylineno);
|
|
}
|
|
// void yyerror(char* error, ...);
|
|
}
|
|
|
|
%union {
|
|
char* str;
|
|
int num;
|
|
Carbon::Expression* expression;
|
|
std::list<std::pair<std::string, Carbon::Expression*>>* field_types;
|
|
Carbon::Statement* statement;
|
|
Carbon::Statement* statement_list;
|
|
Carbon::FunctionDefinition* function_definition;
|
|
Carbon::Declaration* declaration;
|
|
std::list<Carbon::Declaration*>* declaration_list;
|
|
Carbon::Member* member;
|
|
std::list<Carbon::Member*>* member_list;
|
|
Carbon::ExpOrFieldList* field_list;
|
|
std::pair<std::string, Carbon::Expression*>* alternative;
|
|
std::list<std::pair<std::string, Carbon::Expression*>>* alternative_list;
|
|
std::pair<Carbon::Expression*, Carbon::Statement*>* clause;
|
|
std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>* clause_list;
|
|
Carbon::Expression* fun_type;
|
|
};
|
|
|
|
%token <num> integer_literal
|
|
%token <str> identifier
|
|
%type <str> designator
|
|
%type <declaration> declaration
|
|
%type <function_definition> function_declaration
|
|
%type <function_definition> function_definition
|
|
%type <declaration_list> declaration_list
|
|
%type <statement> statement
|
|
%type <statement_list> statement_list
|
|
%type <expression> expression
|
|
%type <expression> pattern
|
|
%type <expression> return_type
|
|
%type <expression> tuple
|
|
%type <member> member
|
|
%type <member_list> member_list
|
|
%type <field_list> field
|
|
%type <field_list> field_list
|
|
%type <alternative> alternative
|
|
%type <alternative_list> alternative_list
|
|
%type <clause> clause
|
|
%type <clause_list> clause_list
|
|
%token AND
|
|
%token OR
|
|
%token NOT
|
|
%token INT
|
|
%token BOOL
|
|
%token TYPE
|
|
%token FN
|
|
%token FNTY
|
|
%token ARROW
|
|
%token VAR
|
|
%token EQUAL
|
|
%token IF
|
|
%token ELSE
|
|
%token WHILE
|
|
%token BREAK
|
|
%token CONTINUE
|
|
%token RETURN
|
|
%token TRUE
|
|
%token FALSE
|
|
%token STRUCT
|
|
%token CHOICE
|
|
%token MATCH
|
|
%token CASE
|
|
%token DBLARROW
|
|
%token DEFAULT
|
|
%token AUTO
|
|
%nonassoc '{' '}'
|
|
%nonassoc ':' ',' DBLARROW
|
|
%left OR AND
|
|
%nonassoc EQUAL NOT
|
|
%left '+' '-'
|
|
%left '.' ARROW
|
|
%nonassoc '(' ')' '[' ']'
|
|
%start input
|
|
%locations
|
|
%%
|
|
input: declaration_list
|
|
{ Carbon::ExecProgram($1); }
|
|
;
|
|
pattern:
|
|
expression
|
|
{ $$ = $1; }
|
|
;
|
|
expression:
|
|
identifier
|
|
{ $$ = Carbon::MakeVar(yylineno, $1); }
|
|
| expression designator
|
|
{ $$ = Carbon::MakeGetField(yylineno, $1, $2); }
|
|
| expression '[' expression ']'
|
|
{ $$ = Carbon::MakeIndex(yylineno, $1, $3); }
|
|
| expression ':' identifier
|
|
{ $$ = Carbon::MakeVarPat(yylineno, $3, $1); }
|
|
| integer_literal
|
|
{ $$ = Carbon::MakeInt(yylineno, $1); }
|
|
| TRUE
|
|
{ $$ = Carbon::MakeBool(yylineno, true); }
|
|
| FALSE
|
|
{ $$ = Carbon::MakeBool(yylineno, false); }
|
|
| INT
|
|
{ $$ = Carbon::MakeIntType(yylineno); }
|
|
| BOOL
|
|
{ $$ = Carbon::MakeBoolType(yylineno); }
|
|
| TYPE
|
|
{ $$ = Carbon::MakeTypeType(yylineno); }
|
|
| AUTO
|
|
{ $$ = Carbon::MakeAutoType(yylineno); }
|
|
| tuple { $$ = $1; }
|
|
| expression EQUAL expression
|
|
{ $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Eq, $1, $3); }
|
|
| expression '+' expression
|
|
{ $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Add, $1, $3); }
|
|
| expression '-' expression
|
|
{ $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Sub, $1, $3); }
|
|
| expression AND expression
|
|
{ $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::And, $1, $3); }
|
|
| expression OR expression
|
|
{ $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Or, $1, $3); }
|
|
| NOT expression
|
|
{ $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Not, $2); }
|
|
| '-' expression
|
|
{ $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Neg, $2); }
|
|
| expression tuple
|
|
{
|
|
if ($2->tag == Carbon::ExpressionKind::Tuple) {
|
|
$$ = Carbon::MakeCall(yylineno, $1, $2);
|
|
} else {
|
|
auto vec =
|
|
new std::vector<std::pair<std::string, Carbon::Expression*>>();
|
|
vec->push_back(std::make_pair("", $2));
|
|
$$ = Carbon::MakeCall(yylineno, $1, Carbon::MakeTuple(yylineno, vec));
|
|
}
|
|
}
|
|
| FNTY tuple return_type
|
|
{ $$ = Carbon::MakeFunType(yylineno, $2, $3); }
|
|
;
|
|
designator: '.' identifier { $$ = $2; }
|
|
;
|
|
tuple: '(' field_list ')'
|
|
{
|
|
switch ($2->tag) {
|
|
case Carbon::ExpOrFieldListKind::Exp:
|
|
$$ = $2->u.exp;
|
|
break;
|
|
case Carbon::ExpOrFieldListKind::FieldList:
|
|
auto vec = new std::vector<std::pair<std::string,Carbon::Expression*>>(
|
|
$2->u.fields->begin(), $2->u.fields->end());
|
|
$$ = Carbon::MakeTuple(yylineno, vec);
|
|
break;
|
|
}
|
|
}
|
|
;
|
|
field:
|
|
pattern
|
|
{ $$ = Carbon::MakeExp($1); }
|
|
| designator '=' pattern
|
|
{
|
|
auto fields =
|
|
new std::list<std::pair<std::string, Carbon::Expression*>>();
|
|
fields->push_back(std::make_pair($1, $3));
|
|
$$ = Carbon::MakeFieldList(fields);
|
|
}
|
|
;
|
|
field_list:
|
|
// Empty
|
|
{
|
|
$$ = Carbon::MakeFieldList(
|
|
new std::list<std::pair<std::string, Carbon::Expression*>>());
|
|
}
|
|
| field
|
|
{ $$ = $1; }
|
|
| field ',' field_list
|
|
{ $$ = Carbon::MakeConsField($1, $3); }
|
|
;
|
|
clause:
|
|
CASE pattern DBLARROW statement
|
|
{ $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>($2, $4); }
|
|
| DEFAULT DBLARROW statement
|
|
{
|
|
auto vp = Carbon::MakeVarPat(yylineno, "_",
|
|
Carbon::MakeAutoType(yylineno));
|
|
$$ = new std::pair<Carbon::Expression*, Carbon::Statement*>(vp, $3);
|
|
}
|
|
;
|
|
clause_list:
|
|
// Empty
|
|
{
|
|
$$ = new std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>();
|
|
}
|
|
| clause clause_list
|
|
{ $$ = $2; $$->push_front(*$1); }
|
|
;
|
|
statement:
|
|
expression '=' expression ';'
|
|
{ $$ = Carbon::MakeAssign(yylineno, $1, $3); }
|
|
| VAR pattern '=' expression ';'
|
|
{ $$ = Carbon::MakeVarDef(yylineno, $2, $4); }
|
|
| expression ';'
|
|
{ $$ = Carbon::MakeExpStmt(yylineno, $1); }
|
|
| IF '(' expression ')' statement ELSE statement
|
|
{ $$ = Carbon::MakeIf(yylineno, $3, $5, $7); }
|
|
| WHILE '(' expression ')' statement
|
|
{ $$ = Carbon::MakeWhile(yylineno, $3, $5); }
|
|
| BREAK ';'
|
|
{ $$ = Carbon::MakeBreak(yylineno); }
|
|
| CONTINUE ';'
|
|
{ $$ = Carbon::MakeContinue(yylineno); }
|
|
| RETURN expression ';'
|
|
{ $$ = Carbon::MakeReturn(yylineno, $2); }
|
|
| '{' statement_list '}'
|
|
{ $$ = Carbon::MakeBlock(yylineno, $2); }
|
|
| MATCH '(' expression ')' '{' clause_list '}'
|
|
{ $$ = Carbon::MakeMatch(yylineno, $3, $6); }
|
|
;
|
|
statement_list:
|
|
// Empty
|
|
{ $$ = 0; }
|
|
| statement statement_list
|
|
{ $$ = Carbon::MakeSeq(yylineno, $1, $2); }
|
|
;
|
|
return_type:
|
|
// Empty
|
|
{
|
|
$$ = Carbon::MakeTuple(
|
|
yylineno,
|
|
new std::vector<std::pair<std::string, Carbon::Expression*>>());
|
|
}
|
|
| ARROW expression
|
|
{ $$ = $2; }
|
|
;
|
|
function_definition:
|
|
FN identifier tuple return_type '{' statement_list '}'
|
|
{ $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
|
|
| FN identifier tuple DBLARROW expression ';'
|
|
{
|
|
$$ = Carbon::MakeFunDef(yylineno, $2, Carbon::MakeAutoType(yylineno), $3,
|
|
Carbon::MakeReturn(yylineno, $5));
|
|
}
|
|
;
|
|
function_declaration:
|
|
FN identifier tuple return_type ';'
|
|
{ $$ = MakeFunDef(yylineno, $2, $4, $3, 0); }
|
|
;
|
|
member:
|
|
VAR expression ':' identifier ';'
|
|
{ $$ = MakeField(yylineno, $4, $2); }
|
|
;
|
|
member_list:
|
|
// Empty
|
|
{ $$ = new std::list<Carbon::Member*>(); }
|
|
| member member_list
|
|
{ $$ = $2; $$->push_front($1); }
|
|
;
|
|
alternative:
|
|
identifier tuple ';'
|
|
{ $$ = new std::pair<std::string, Carbon::Expression*>($1, $2); }
|
|
;
|
|
alternative_list:
|
|
// Empty
|
|
{ $$ = new std::list<std::pair<std::string, Carbon::Expression*>>(); }
|
|
| alternative alternative_list
|
|
{ $$ = $2; $$->push_front(*$1); }
|
|
;
|
|
declaration:
|
|
function_definition
|
|
{ $$ = Carbon::MakeFunDecl($1); }
|
|
| function_declaration
|
|
{ $$ = Carbon::MakeFunDecl($1); }
|
|
| STRUCT identifier '{' member_list '}'
|
|
{ $$ = Carbon::MakeStructDecl(yylineno, $2, $4); }
|
|
| CHOICE identifier '{' alternative_list '}'
|
|
{ $$ = Carbon::MakeChoiceDecl(yylineno, $2, $4); }
|
|
;
|
|
declaration_list:
|
|
// Empty
|
|
{ $$ = new std::list<Carbon::Declaration*>(); }
|
|
| declaration declaration_list
|
|
{
|
|
$$ = $2;
|
|
$$->push_front($1);
|
|
}
|
|
;
|
|
%%
|