mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 09:24:46 +01:00
Note this doesn't support other sizes or types, it just errors on them. Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
parent
7e91fbc276
commit
2620ba066d
@@ -8,6 +8,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "executable_semantics/common/tracing_flag.h"
|
||||
#include "executable_semantics/syntax/parse_and_lex_context.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
@@ -45,7 +46,6 @@ FALSE "false"
|
||||
FN "fn"
|
||||
FNTY "fnty"
|
||||
IF "if"
|
||||
INT "Int"
|
||||
MATCH "match"
|
||||
NOT "not"
|
||||
OR "or"
|
||||
@@ -62,6 +62,7 @@ AWAIT "__await"
|
||||
UNDERSCORE "_"
|
||||
|
||||
identifier [A-Za-z_][A-Za-z0-9_]*
|
||||
sized_type_literal [iuf][1-9][0-9]*
|
||||
integer_literal [0-9]+
|
||||
horizontal_whitespace [ \t\r]
|
||||
whitespace [ \t\r\n]
|
||||
@@ -106,7 +107,6 @@ operand_start [(A-Za-z0-9_"]
|
||||
{FN} { return Carbon::Parser::make_FN(context.current_token_position); }
|
||||
{FNTY} { return Carbon::Parser::make_FNTY(context.current_token_position); }
|
||||
{IF} { return Carbon::Parser::make_IF(context.current_token_position); }
|
||||
{INT} { return Carbon::Parser::make_INT(context.current_token_position); }
|
||||
{MATCH} { return Carbon::Parser::make_MATCH(context.current_token_position); }
|
||||
{NOT} { return Carbon::Parser::make_NOT(context.current_token_position); }
|
||||
{OR} { return Carbon::Parser::make_OR(context.current_token_position); }
|
||||
@@ -122,6 +122,8 @@ operand_start [(A-Za-z0-9_"]
|
||||
{AWAIT} { return Carbon::Parser::make_AWAIT(context.current_token_position); }
|
||||
{UNDERSCORE} { return Carbon::Parser::make_UNDERSCORE(context.current_token_position); }
|
||||
|
||||
{sized_type_literal} { return Carbon::Parser::make_sized_type_literal(yytext, context.current_token_position); }
|
||||
|
||||
"=" return Carbon::Parser::make_EQUAL(context.current_token_position);
|
||||
"-" return Carbon::Parser::make_MINUS(context.current_token_position);
|
||||
"+" return Carbon::Parser::make_PLUS(context.current_token_position);
|
||||
@@ -183,8 +185,9 @@ operator and its operand, leading to three more cases:
|
||||
|
||||
{integer_literal} {
|
||||
BEGIN(AFTER_OPERAND);
|
||||
auto r = atof(yytext);
|
||||
return Carbon::Parser::make_integer_literal(r, context.current_token_position);
|
||||
int val;
|
||||
CHECK(llvm::to_integer(yytext, val));
|
||||
return Carbon::Parser::make_integer_literal(val, context.current_token_position);
|
||||
}
|
||||
|
||||
{ONE_LINE_COMMENT} {
|
||||
|
||||
@@ -55,8 +55,10 @@
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "executable_semantics/syntax/syntax_helpers.h"
|
||||
#include "executable_semantics/syntax/parse_and_lex_context.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
} // %code top
|
||||
|
||||
%code requires {
|
||||
@@ -88,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
|
||||
%type <std::string> designator
|
||||
%type <Declaration> declaration
|
||||
%type <FunctionDefinition> function_declaration
|
||||
@@ -129,7 +132,6 @@ void Carbon::Parser::error(const location_type&, const std::string& message) {
|
||||
%token AND
|
||||
%token OR
|
||||
%token NOT
|
||||
%token INT
|
||||
%token BOOL
|
||||
%token TYPE
|
||||
%token FN
|
||||
@@ -221,8 +223,13 @@ expression:
|
||||
{ $$ = Expression::MakeBoolLiteral(yylineno, true); }
|
||||
| FALSE
|
||||
{ $$ = Expression::MakeBoolLiteral(yylineno, false); }
|
||||
| INT
|
||||
{ $$ = Expression::MakeIntTypeLiteral(yylineno); }
|
||||
| sized_type_literal
|
||||
{
|
||||
int val;
|
||||
CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
|
||||
CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1;
|
||||
$$ = Expression::MakeIntTypeLiteral(yylineno);
|
||||
}
|
||||
| BOOL
|
||||
{ $$ = Expression::MakeBoolTypeLiteral(yylineno); }
|
||||
| TYPE
|
||||
|
||||
Reference in New Issue
Block a user