Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add unit literals and unit types to parser #1960

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/nargo_cli/tests/test_data_ssa_refactor/unit/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
14 changes: 14 additions & 0 deletions crates/nargo_cli/tests/test_data_ssa_refactor/unit/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
let _a = ();
let _b: () = _a;
let _c: () = ();
let _d = f1();
let _e: () = f2();
let _f: () = f3();
let _g = f4();
}

fn f1() {}
fn f2() { () }
fn f3() -> () {}
fn f4() -> () { () }
2 changes: 1 addition & 1 deletion crates/noirc_evaluator/src/ssa/ssa_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ impl IrGenerator {

//Parse a block of AST statements into ssa form
fn ssa_gen_block(&mut self, block: &[Expression]) -> Result<Value, RuntimeError> {
let mut last_value = Value::dummy();
let mut last_value = Value::Node(self.context.zero());
for expr in block {
last_value = self.ssa_gen_expression(expr)?;
}
Expand Down
2 changes: 2 additions & 0 deletions crates/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ pub enum Literal {
Bool(bool),
Integer(FieldElement),
Str(String),
Unit,
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -465,6 +466,7 @@ impl Display for Literal {
Literal::Bool(boolean) => write!(f, "{}", if *boolean { "true" } else { "false" }),
Literal::Integer(integer) => write!(f, "{}", integer.to_u128()),
Literal::Str(string) => write!(f, "\"{string}\""),
Literal::Unit => write!(f, "()"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ impl<'a> Resolver<'a> {
}
Literal::Integer(integer) => HirLiteral::Integer(integer),
Literal::Str(str) => HirLiteral::Str(str),
Literal::Unit => HirLiteral::Unit,
}),
ExpressionKind::Variable(path) => {
// If the Path is being used as an Expression, then it is referring to a global from a separate module
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl<'interner> TypeChecker<'interner> {
let len = Type::Constant(string.len() as u64);
Type::String(Box::new(len))
}
HirLiteral::Unit => Type::Unit,
}
}
HirExpression::Infix(infix_expr) => {
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/hir_def/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub enum HirLiteral {
Bool(bool),
Integer(FieldElement),
Str(String),
Unit,
}

#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ impl<'interner> Monomorphizer<'interner> {
self.repeated_array(repeated_element, length)
}
},
HirExpression::Literal(HirLiteral::Unit) => ast::Expression::Block(vec![]),
HirExpression::Block(block) => self.block(block.0),

HirExpression::Prefix(prefix) => ast::Expression::Unary(ast::Unary {
Expand Down
25 changes: 19 additions & 6 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use crate::parser::{force, ignore_then_commit, statement_recovery};
use crate::token::{Attribute, Keyword, Token, TokenKind};
use crate::{
BinaryOp, BinaryOpKind, BlockExpression, CompTime, ConstrainStatement, FunctionDefinition,
Ident, IfExpression, InfixExpression, LValue, Lambda, NoirFunction, NoirStruct, NoirTrait,
Path, PathKind, Pattern, Recoverable, TraitConstraint, TraitImpl, TraitImplItem, TraitItem,
TypeImpl, UnaryOp, UnresolvedTypeExpression, UseTree, UseTreeKind,
Ident, IfExpression, InfixExpression, LValue, Lambda, Literal, NoirFunction, NoirStruct,
NoirTrait, Path, PathKind, Pattern, Recoverable, TraitConstraint, TraitImpl, TraitImplItem,
TraitItem, TypeImpl, UnaryOp, UnresolvedTypeExpression, UseTree, UseTreeKind,
};

use chumsky::prelude::*;
Expand Down Expand Up @@ -788,6 +788,7 @@ fn parse_type_inner(
string_type(),
named_type(recursive_type_parser.clone()),
array_type(recursive_type_parser.clone()),
recursive_type_parser.clone().delimited_by(just(Token::LeftParen), just(Token::RightParen)),
tuple_type(recursive_type_parser.clone()),
function_type(recursive_type_parser.clone()),
mutable_reference_type(recursive_type_parser),
Expand Down Expand Up @@ -893,7 +894,13 @@ where
T: NoirParser<UnresolvedType>,
{
let fields = type_parser.separated_by(just(Token::Comma)).allow_trailing();
parenthesized(fields).map(UnresolvedType::Tuple)
parenthesized(fields).map(|fields| {
if fields.is_empty() {
UnresolvedType::Unit
} else {
UnresolvedType::Tuple(fields)
}
})
}

fn function_type<T>(type_parser: T) -> impl NoirParser<UnresolvedType>
Expand Down Expand Up @@ -1303,8 +1310,14 @@ fn tuple<P>(expr_parser: P) -> impl NoirParser<Expression>
where
P: ExprParser,
{
parenthesized(expression_list(expr_parser))
.map_with_span(|elements, span| Expression::new(ExpressionKind::Tuple(elements), span))
parenthesized(expression_list(expr_parser)).map_with_span(|elements, span| {
let kind = if elements.is_empty() {
ExpressionKind::Literal(Literal::Unit)
} else {
ExpressionKind::Tuple(elements)
};
Expression::new(kind, span)
})
}

fn field_name() -> impl NoirParser<Ident> {
Expand Down