Revert WIP

This commit is contained in:
Pranav Gaddamadugu 2023-06-23 10:24:10 -04:00
parent ccb4c0f38f
commit 5cce8b4a0e
15 changed files with 6 additions and 159 deletions

View File

@ -1,38 +0,0 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use crate::Type;
/// A cast expression, e.g. `42u8 as u16`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CastExpression {
/// The expression to be casted, e.g.`42u8` in `42u8 as u16`.
pub expression: Box<Expression>,
/// The type to be casted to, e.g. `u16` in `42u8 as u16`.
pub type_: Type,
/// Span of the entire cast `42u8 as u16`.
pub span: Span,
}
impl fmt::Display for CastExpression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({} as {})", self.expression, self.type_)
}
}
crate::simple_node_impl!(CastExpression);

View File

@ -29,9 +29,6 @@ pub use binary::*;
mod call;
pub use call::*;
mod cast;
pub use cast::*;
mod struct_init;
pub use struct_init::*;
@ -62,8 +59,6 @@ pub enum Expression {
Binary(BinaryExpression),
/// A call expression, e.g., `my_fun(args)`.
Call(CallExpression),
/// A cast expression, e.g., `42u32 as u8`.
Cast(CastExpression),
/// An expression constructing a struct like `Foo { bar: 42, baz }`.
Struct(StructExpression),
/// An expression of type "error".
@ -90,7 +85,6 @@ impl Node for Expression {
Access(n) => n.span(),
Binary(n) => n.span(),
Call(n) => n.span(),
Cast(n) => n.span(),
Struct(n) => n.span(),
Err(n) => n.span(),
Identifier(n) => n.span(),
@ -108,7 +102,6 @@ impl Node for Expression {
Access(n) => n.set_span(span),
Binary(n) => n.set_span(span),
Call(n) => n.set_span(span),
Cast(n) => n.set_span(span),
Struct(n) => n.set_span(span),
Identifier(n) => n.set_span(span),
Literal(n) => n.set_span(span),
@ -128,7 +121,6 @@ impl fmt::Display for Expression {
Access(n) => n.fmt(f),
Binary(n) => n.fmt(f),
Call(n) => n.fmt(f),
Cast(n) => n.fmt(f),
Struct(n) => n.fmt(f),
Err(n) => n.fmt(f),
Identifier(n) => n.fmt(f),

View File

@ -28,7 +28,6 @@ pub trait ExpressionConsumer {
Expression::Access(access) => self.consume_access(access),
Expression::Binary(binary) => self.consume_binary(binary),
Expression::Call(call) => self.consume_call(call),
Expression::Cast(cast) => self.consume_cast(cast),
Expression::Struct(struct_) => self.consume_struct_init(struct_),
Expression::Err(err) => self.consume_err(err),
Expression::Identifier(identifier) => self.consume_identifier(identifier),
@ -46,8 +45,6 @@ pub trait ExpressionConsumer {
fn consume_call(&mut self, _input: CallExpression) -> Self::Output;
fn consume_cast(&mut self, _input: CastExpression) -> Self::Output;
fn consume_struct_init(&mut self, _input: StructExpression) -> Self::Output;
fn consume_err(&mut self, _input: ErrExpression) -> Self::Output {

View File

@ -29,7 +29,6 @@ pub trait ExpressionReconstructor {
Expression::Access(access) => self.reconstruct_access(access),
Expression::Binary(binary) => self.reconstruct_binary(binary),
Expression::Call(call) => self.reconstruct_call(call),
Expression::Cast(cast) => self.reconstruct_cast(cast),
Expression::Struct(struct_) => self.reconstruct_struct_init(struct_),
Expression::Err(err) => self.reconstruct_err(err),
Expression::Identifier(identifier) => self.reconstruct_identifier(identifier),
@ -96,17 +95,6 @@ pub trait ExpressionReconstructor {
)
}
fn reconstruct_cast(&mut self, input: CastExpression) -> (Expression, Self::AdditionalOutput) {
(
Expression::Cast(CastExpression {
expression: Box::new(self.reconstruct_expression(*input.expression).0),
type_: input.type_,
span: input.span,
}),
Default::default(),
)
}
fn reconstruct_struct_init(&mut self, input: StructExpression) -> (Expression, Self::AdditionalOutput) {
(Expression::Struct(input), Default::default())
}

View File

@ -30,7 +30,6 @@ pub trait ExpressionVisitor<'a> {
Expression::Access(access) => self.visit_access(access, additional),
Expression::Binary(binary) => self.visit_binary(binary, additional),
Expression::Call(call) => self.visit_call(call, additional),
Expression::Cast(cast) => self.visit_cast(cast, additional),
Expression::Struct(struct_) => self.visit_struct_init(struct_, additional),
Expression::Err(err) => self.visit_err(err, additional),
Expression::Identifier(identifier) => self.visit_identifier(identifier, additional),
@ -74,11 +73,6 @@ pub trait ExpressionVisitor<'a> {
Default::default()
}
fn visit_cast(&mut self, input: &'a CastExpression, additional: &Self::AdditionalInput) -> Self::Output {
self.visit_expression(&input.expression, additional);
Default::default()
}
fn visit_struct_init(&mut self, _input: &'a StructExpression, _additional: &Self::AdditionalInput) -> Self::Output {
Default::default()
}

View File

@ -221,21 +221,13 @@ impl ParserContext<'_> {
/// Returns an [`Expression`] AST node if the next tokens represent a
/// binary exponentiation expression.
///
/// Otherwise, tries to parse the next token using [`parse_cast_expression`].
fn parse_exponential_expression(&mut self) -> Result<Expression> {
self.parse_bin_expr(&[Token::Pow], Self::parse_cast_expression)
}
/// Returns an [`Expression`] AST node if the next tokens represent a
/// cast expression.
///
/// Otherwise, tries to parse the next token using [`parse_unary_expression`].
fn parse_cast_expression(&mut self) -> Result<Expression> {
fn parse_exponential_expression(&mut self) -> Result<Expression> {
let mut expr = self.parse_unary_expression()?;
if self.eat(&Token::As) {
let (type_, end_span) = self.parse_primitive_type()?;
let span = expr.span() + end_span;
expr = Expression::Cast(CastExpression { expression: Box::new(expr), type_, span });
if let Some(op) = self.eat_bin_op(&[Token::Pow]) {
let right = self.parse_exponential_expression()?;
expr = Self::bin_expr(expr, right, op);
}
Ok(expr)

View File

@ -376,7 +376,6 @@ impl Token {
match &*identifier {
x if x.starts_with("aleo1") => Token::AddressLit(identifier),
"address" => Token::Address,
"as" => Token::As,
"assert" => Token::Assert,
"assert_eq" => Token::AssertEq,
"assert_neq" => Token::AssertNeq,

View File

@ -83,7 +83,6 @@ mod tests {
test_ident
12345
address
as
assert
assert_eq
assert_neq
@ -169,7 +168,7 @@ mod tests {
assert_eq!(
output,
r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" test_ident 12345 address as assert assert_eq assert_neq async bool const else false field finalize for function group i128 i64 i32 i16 i8 if in inline input let mut private program public return scalar self string struct test then transition true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> => _ . .. / : ; < <= = == > >= [ ] { { } } || ? @ // test
r#""test" "test{}test" "test{}" "{}test" "test{" "test}" "test{test" "test}test" "te{{}}" test_ident 12345 address assert assert_eq assert_neq async bool const else false field finalize for function group i128 i64 i32 i16 i8 if in inline input let mut private program public return scalar self string struct test then transition true u128 u64 u32 u16 u8 console ! != && ( ) * ** + , - -> => _ . .. / : ; < <= = == > >= [ ] { { } } || ? @ // test
/* test */ // "#
);
});

View File

@ -108,7 +108,6 @@ pub enum Token {
Record,
// Regular Keywords
As,
Assert,
AssertEq,
AssertNeq,
@ -147,7 +146,6 @@ pub enum Token {
/// because true and false are also boolean literals, which are different tokens from keywords.
pub const KEYWORD_TOKENS: &[Token] = &[
Token::Address,
Token::As,
Token::Assert,
Token::AssertEq,
Token::AssertNeq,
@ -201,7 +199,6 @@ impl Token {
pub fn keyword_to_symbol(&self) -> Option<Symbol> {
Some(match self {
Token::Address => sym::address,
Token::As => sym::As,
Token::Assert => sym::assert,
Token::AssertEq => sym::assert_eq,
Token::AssertNeq => sym::assert_neq,
@ -334,7 +331,6 @@ impl fmt::Display for Token {
U128 => write!(f, "u128"),
Record => write!(f, "record"),
As => write!(f, "as"),
Assert => write!(f, "assert"),
AssertEq => write!(f, "assert_eq"),
AssertNeq => write!(f, "assert_neq"),

View File

@ -22,7 +22,6 @@ use leo_ast::{
BinaryExpression,
BinaryOperation,
CallExpression,
CastExpression,
ErrExpression,
Expression,
Identifier,
@ -51,7 +50,6 @@ impl<'a> CodeGenerator<'a> {
Expression::Access(expr) => self.visit_access(expr),
Expression::Binary(expr) => self.visit_binary(expr),
Expression::Call(expr) => self.visit_call(expr),
Expression::Cast(expr) => self.visit_cast(expr),
Expression::Struct(expr) => self.visit_struct_init(expr),
Expression::Err(expr) => self.visit_err(expr),
Expression::Identifier(expr) => self.visit_identifier(expr),
@ -129,23 +127,6 @@ impl<'a> CodeGenerator<'a> {
(destination_register, instructions)
}
fn visit_cast(&mut self, input: &'a CastExpression) -> (String, String) {
let (expression_operand, expression_instructions) = self.visit_expression(&input.expression);
let destination_register = format!("r{}", self.next_register);
let cast_instruction =
format!(" cast {expression_operand} into {destination_register} as {};\n", input.type_);
// Increment the register counter.
self.next_register += 1;
// Concatenate the instructions.
let mut instructions = expression_instructions;
instructions.push_str(&cast_instruction);
(destination_register, instructions)
}
fn visit_unary(&mut self, input: &'a UnaryExpression) -> (String, String) {
let (expression_operand, expression_instructions) = self.visit_expression(&input.receiver);

View File

@ -21,7 +21,6 @@ use leo_ast::{
AssociatedFunction,
BinaryExpression,
CallExpression,
CastExpression,
Expression,
ExpressionConsumer,
Identifier,
@ -156,22 +155,6 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> {
(Expression::Identifier(place), statements)
}
/// Consumes a cast expression, accumulating any statements that are generated.
fn consume_cast(&mut self, input: CastExpression) -> Self::Output {
// Reconstruct the expression being casted.
let (expression, mut statements) = self.consume_expression(*input.expression);
// Construct and accumulate a unique assignment statement storing the result of the cast expression.
let (place, statement) = self.assigner.unique_simple_assign_statement(Expression::Cast(CastExpression {
expression: Box::new(expression),
type_: input.type_,
span: input.span,
}));
statements.push(statement);
(Expression::Identifier(place), statements)
}
/// Consumes a struct initialization expression with renamed variables, accumulating any statements that are generated.
fn consume_struct_init(&mut self, input: StructExpression) -> Self::Output {
let mut statements = Vec::new();

View File

@ -512,20 +512,6 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> {
}
}
fn visit_cast(&mut self, input: &'a CastExpression, expected: &Self::AdditionalInput) -> Self::Output {
// Check that the target type of the cast expression is a primitive type.
if !Self::is_primitive_type(&input.type_) {
self.emit_err(TypeCheckerError::cast_must_be_to_primitive(input.span()));
}
// Check that the expression being casted matches the target type.
let target_type = input.type_.clone();
let expression_type = self.visit_expression(&input.expression, &None);
self.assert_type(&expression_type, &target_type, input.expression.span());
// Check that the expected type matches the target type.
Some(self.assert_and_return_type(target_type, expected, input.span()))
}
fn visit_struct_init(&mut self, input: &'a StructExpression, additional: &Self::AdditionalInput) -> Self::Output {
let struct_ = self.symbol_table.borrow().lookup_struct(input.name.name).cloned();
if let Some(struct_) = struct_ {

View File

@ -163,20 +163,6 @@ impl<'a> TypeChecker<'a> {
}
}
/// Returns whether the given type is a primitive type.
pub(crate) fn is_primitive_type(type_: &Type) -> bool {
match type_ {
Type::Address
| Type::Boolean
| Type::Field
| Type::Group
| Type::Integer(_)
| Type::Scalar
| Type::String => true,
Type::Identifier(_) | Type::Mapping(_) | Type::Tuple(_) | Type::Unit | Type::Err => false,
}
}
/// Use this method when you know the actual type.
/// Emits an error to the handler if the `actual` type is not equal to the `expected` type.
pub(crate) fn assert_and_return_type(&self, actual: Type, expected: &Option<Type>, span: Span) -> Type {

View File

@ -201,7 +201,6 @@ symbols! {
True: "true",
// general keywords
As: "as",
assert,
assert_eq,
assert_neq,

View File

@ -635,11 +635,4 @@ create_messages!(
msg: format!("`{operation}` is not a valid operand in a finalize context."),
help: None,
}
@formatted
cast_must_be_to_primitive {
args: (),
msg: format!("A cast must be to a primitive type."),
help: None,
}
);