Skip to content

Commit

Permalink
Move Int parsing to i32 for #543 (#700)
Browse files Browse the repository at this point in the history
Note this doesn't support other sizes or types, it just errors on them.

Co-authored-by: Geoff Romer <gromer@google.com>
  • Loading branch information
jonmeow and geoffromer authored Aug 5, 2021
1 parent 7e91fbc commit 2620ba0
Show file tree
Hide file tree
Showing 100 changed files with 212 additions and 202 deletions.
2 changes: 1 addition & 1 deletion executable_semantics/ast/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void Expression::Print(llvm::raw_ostream& out) const {
out << "Bool";
break;
case ExpressionKind::IntTypeLiteral:
out << "Int";
out << "i32";
break;
case ExpressionKind::TypeTypeLiteral:
out << "Type";
Expand Down
2 changes: 1 addition & 1 deletion executable_semantics/interpreter/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void Value::Print(llvm::raw_ostream& out) const {
out << "Bool";
break;
case Value::Kind::IntType:
out << "Int";
out << "i32";
break;
case Value::Kind::TypeType:
out << "Type";
Expand Down
11 changes: 7 additions & 4 deletions executable_semantics/syntax/lexer.lpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -45,7 +46,6 @@ FALSE "false"
FN "fn"
FNTY "fnty"
IF "if"
INT "Int"
MATCH "match"
NOT "not"
OR "or"
Expand All @@ -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]
Expand Down Expand Up @@ -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); }
Expand All @@ -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);
Expand Down Expand Up @@ -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} {
Expand Down
13 changes: 10 additions & 3 deletions executable_semantics/syntax/parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions executable_semantics/testdata/assignment_copy1.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

// Test that assignment performs a copy and does not create an alias.

fn main() -> Int {
var x: Int = -1;
fn main() -> i32 {
var x: i32 = -1;
{
var y: Int = 0;
var y: i32 = 0;
x = y;
// y dies here
}
Expand Down
6 changes: 3 additions & 3 deletions executable_semantics/testdata/assignment_copy2.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

// Test that assignment performs a copy and does not create an alias.

fn main() -> Int {
var x: Int = 0;
var y: Int = x;
fn main() -> i32 {
var x: i32 = 0;
var y: i32 = x;
x = 1;
return y;
}
6 changes: 3 additions & 3 deletions executable_semantics/testdata/block1.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

fn main() -> Int {
var x: Int = 0;
fn main() -> i32 {
var x: i32 = 0;
{
var x: Int = 1;
var x: i32 = 1;
}
return x;
}
4 changes: 2 additions & 2 deletions executable_semantics/testdata/block2.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

fn main() -> Int {
var x: Int = 0;
fn main() -> i32 {
var x: i32 = 0;
{
// empty block
}
Expand Down
4 changes: 2 additions & 2 deletions executable_semantics/testdata/break1.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

fn main() -> Int {
var x: Int = 2;
fn main() -> i32 {
var x: i32 = 2;
while (true) {
if (x == 0) {
break;
Expand Down
10 changes: 5 additions & 5 deletions executable_semantics/testdata/choice1.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

choice Ints {
None,
One(Int),
Two(Int,Int)
One(i32),
Two(i32,i32)
}

fn main() -> Int {
fn main() -> i32 {
var x: auto = Ints.None();
var y: auto = Ints.One(42);
var n: auto = 0;
Expand All @@ -34,6 +34,6 @@ fn main() -> Int {
// Test some alternate syntaxes
choice MoreInts {
None(),
One(Int),
Two(Int,Int),
One(i32),
Two(i32,i32),
}
2 changes: 1 addition & 1 deletion executable_semantics/testdata/continue1.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

fn main() -> Int {
fn main() -> i32 {
var x: auto = 2;
while (not (x == 0)) {
x = x - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// Test that creating a continuation doesn't do anything.

fn main() -> Int {
var x: Int = 0;
fn main() -> i32 {
var x: i32 = 0;
__continuation k {
x = x + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

// Test creating and running a continuation.

fn main() -> Int {
var x: Int = 0;
fn main() -> i32 {
var x: i32 = 0;
__continuation k {
x = x + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Test pausing a continuation with `__await` and restarting it with
// `__run`.

fn main() -> Int {
var x: Int = 0;
fn main() -> i32 {
var x: i32 = 0;
__continuation k {
x = x + 1;
__await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Assignment for continuations is shallow, so `k2` refers to the same
// continuation as `k1`.

fn main() -> Int {
var x: Int = 0;
fn main() -> i32 {
var x: i32 = 0;
__continuation k1 {
x = x + 1;
__await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

// Test access to block-scoped variables upon resuming a continuation.

fn main() -> Int {
var y: Int = 0;
fn main() -> i32 {
var y: i32 = 0;
__continuation k {
var x: Int = 0;
var x: i32 = 0;
x = x + 1;
__await;
x = x + 2;
Expand Down
10 changes: 5 additions & 5 deletions executable_semantics/testdata/experimental_continuation6.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

// Test recursive functions inside continuations.

var current: Int = 0;
var current: i32 = 0;

fn CountUpTo(x: Int) -> Int {
fn CountUpTo(x: i32) -> i32 {
if (x == 0) {
current = 0;
__await;
Expand All @@ -18,12 +18,12 @@ fn CountUpTo(x: Int) -> Int {
}
}

fn main() -> Int {
fn main() -> i32 {
__continuation k {
CountUpTo(5);
}
var sum: Int = 0;
var count: Int = 5;
var sum: i32 = 0;
var count: i32 = 5;
while (not (count == 0)) {
__run k;
sum = sum + current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
// on the stack such as the variable `x`. In this example the copy
// happens after the variable `x` is created.

var y: Int = 0;
var y: i32 = 0;

fn main() -> Int {
fn main() -> i32 {
__continuation k1 {
var x: Int = 0;
var x: i32 = 0;
x = x + 1;
__await;
x = x + 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// happens before the variable `x` is created, so each continuation
// creates a different `x`.

var y: Int = 0;
var y: i32 = 0;

fn main() -> Int {
fn main() -> i32 {
__continuation k1 {
var x: Int = 0;
var x: i32 = 0;
x = x + 1;
__await;
y = x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
// is dangerous and can happen inside continuations.

fn capture() -> __Continuation {
var x: Int = 1;
var x: i32 = 1;
__continuation k {
var y: Int = x;
var y: i32 = x;
}
return k;
}

fn main() -> Int {
fn main() -> i32 {
var k: __Continuation = capture();
__run k; // error, lifetime of x is over
return 0;
Expand Down
4 changes: 2 additions & 2 deletions executable_semantics/testdata/fun1.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

fn f(x: Int) -> Int {
fn f(x: i32) -> i32 {
return x - 1;
}

fn main() -> Int {
fn main() -> i32 {
return f(1);
}
6 changes: 3 additions & 3 deletions executable_semantics/testdata/fun2.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// This makes sure that when the value in `x` dies,
// it does not cause the value in `a` to also die.

fn f(x: Int) -> Int {
fn f(x: i32) -> i32 {
return 0;
}

fn main() -> Int {
var a: Int = 0; var b: Int = 1;
fn main() -> i32 {
var a: i32 = 0; var b: i32 = 1;
f(a);
b = a;
return b;
Expand Down
4 changes: 2 additions & 2 deletions executable_semantics/testdata/fun3.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

// Test multiple arguments
fn f(x: Int, y: Int) -> Int {
fn f(x: i32, y: i32) -> i32 {
return x + y;
}

fn main() -> Int {
fn main() -> i32 {
return f(2,3) - 5;
}
2 changes: 1 addition & 1 deletion executable_semantics/testdata/fun4.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Test empty parameters and return type
fn f() { }

fn main() -> Int {
fn main() -> i32 {
f();
return 0;
}
Loading

0 comments on commit 2620ba0

Please sign in to comment.