fix syntax for _type, struct fields, protected names

This commit is contained in:
collin 2020-05-05 18:04:25 -07:00
parent 983cd6675a
commit d7167f5b2b
7 changed files with 71 additions and 57 deletions

View File

@ -1,5 +1,15 @@
struct Foo {
x: u32
y: u32
}
function main(a: private fe) {
let b = a + 1fe;
assert_eq(b, 2fe);
let c = Foo {
x: 4,
y: 5,
};
}

View File

@ -127,28 +127,28 @@ pub enum OperationAssign {
// Types
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty_u32))]
#[pest_ast(rule(Rule::type_u32))]
pub struct U32Type<'ast> {
#[pest_ast(outer())]
pub span: Span<'ast>,
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty_field))]
#[pest_ast(rule(Rule::type_field))]
pub struct FieldType<'ast> {
#[pest_ast(outer())]
pub span: Span<'ast>,
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty_bool))]
#[pest_ast(rule(Rule::type_bool))]
pub struct BooleanType<'ast> {
#[pest_ast(outer())]
pub span: Span<'ast>,
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty_struct))]
#[pest_ast(rule(Rule::type_struct))]
pub struct StructType<'ast> {
pub variable: Variable<'ast>,
#[pest_ast(outer())]
@ -156,7 +156,7 @@ pub struct StructType<'ast> {
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty_basic))]
#[pest_ast(rule(Rule::type_basic))]
pub enum BasicType<'ast> {
U32(U32Type<'ast>),
Field(FieldType<'ast>),
@ -164,23 +164,23 @@ pub enum BasicType<'ast> {
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty_basic_or_struct))]
#[pest_ast(rule(Rule::type_basic_or_struct))]
pub enum BasicOrStructType<'ast> {
Struct(StructType<'ast>),
Basic(BasicType<'ast>),
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty_array))]
#[pest_ast(rule(Rule::type_array))]
pub struct ArrayType<'ast> {
pub ty: BasicType<'ast>,
pub _type: BasicType<'ast>,
pub count: Value<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::ty))]
#[pest_ast(rule(Rule::_type))]
pub enum Type<'ast> {
Basic(BasicType<'ast>),
Array(ArrayType<'ast>),
@ -190,9 +190,9 @@ pub enum Type<'ast> {
impl<'ast> fmt::Display for Type<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Type::Basic(ref _ty) => write!(f, "basic"),
Type::Array(ref _ty) => write!(f, "array"),
Type::Struct(ref _ty) => write!(f, "struct"),
Type::Basic(ref _type) => write!(f, "basic"),
Type::Array(ref _type) => write!(f, "array"),
Type::Struct(ref _type) => write!(f, "struct"),
}
}
}
@ -217,7 +217,7 @@ impl<'ast> fmt::Display for Number<'ast> {
#[pest_ast(rule(Rule::value_u32))]
pub struct U32<'ast> {
pub number: Number<'ast>,
pub ty: Option<U32Type<'ast>>,
pub _type: Option<U32Type<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
@ -232,7 +232,7 @@ impl<'ast> fmt::Display for U32<'ast> {
#[pest_ast(rule(Rule::value_field))]
pub struct Field<'ast> {
pub number: Number<'ast>,
pub ty: FieldType<'ast>,
pub _type: FieldType<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
@ -306,7 +306,7 @@ impl<'ast> fmt::Display for Variable<'ast> {
#[derive(Debug, FromPest, PartialEq, Clone)]
#[pest_ast(rule(Rule::optionally_typed_variable))]
pub struct OptionallyTypedVariable<'ast> {
pub ty: Option<Type<'ast>>,
pub _type: Option<Type<'ast>>,
pub id: Variable<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
@ -499,8 +499,8 @@ pub struct ArrayInitializerExpression<'ast> {
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::struct_field))]
pub struct StructField<'ast> {
pub ty: Type<'ast>,
pub variable: Variable<'ast>,
pub _type: Type<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
@ -880,7 +880,7 @@ pub struct MultipleAssignmentStatement<'ast> {
#[pest_ast(rule(Rule::statement_definition))]
pub struct DefinitionStatement<'ast> {
pub variable: Variable<'ast>,
pub ty: Option<Type<'ast>>,
pub _type: Option<Type<'ast>>,
pub expression: Expression<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
@ -990,8 +990,12 @@ impl<'ast> fmt::Display for MultipleAssignmentStatement<'ast> {
impl<'ast> fmt::Display for DefinitionStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.ty {
Some(ref ty) => write!(f, "let {} : {} = {};", self.variable, ty, self.expression),
match self._type {
Some(ref _type) => write!(
f,
"let {} : {} = {};",
self.variable, _type, self.expression
),
None => write!(f, "let {} = {}", self.variable, self.expression),
}
}
@ -1035,7 +1039,7 @@ impl<'ast> fmt::Display for Statement<'ast> {
pub struct Parameter<'ast> {
pub variable: Variable<'ast>,
pub visibility: Option<Visibility>,
pub ty: Type<'ast>,
pub _type: Type<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -58,7 +58,7 @@ impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ResolvedProgram<F, CS> {
.zip(arguments.clone().into_iter())
.for_each(|(parameter, argument)| {
// Check that argument is correct type
match parameter.ty.clone() {
match parameter._type.clone() {
Type::U32 => {
match self.enforce_argument(
cs,
@ -164,7 +164,7 @@ impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ResolvedProgram<F, CS> {
.zip(parameters.into_iter())
.for_each(|(parameter_model, parameter_value)| {
// append each variable to arguments vector
arguments.push(Expression::Variable(match parameter_model.ty {
arguments.push(Expression::Variable(match parameter_model._type {
Type::U32 => self.u32_from_parameter(
cs,
function_name.clone(),

View File

@ -1,4 +1,4 @@
/// Visibility
/// Visibili_type
visibility_public = { "public" }
visibility_private = { "private" }
@ -55,23 +55,23 @@ operation_assign = {
operation_mul_assign | operation_div_assign | operation_pow_assign
}
/// Types
/// types
ty_u32 = {"u32"}
ty_field = {"fe"}
ty_bool = {"bool"}
ty_basic = { ty_u32 | ty_field | ty_bool }
ty_struct = { variable }
ty_basic_or_struct = {ty_basic | ty_struct }
ty_array = {ty_basic ~ ("[" ~ value ~ "]")+ }
ty = {ty_array | ty_basic | ty_struct}
type_list = _{(ty ~ ("," ~ ty)*)?}
type_u32 = {"u32"}
type_field = {"fe"}
type_bool = {"bool"}
type_basic = { type_u32 | type_field | type_bool }
type_struct = { variable }
type_basic_or_struct = {type_basic | type_struct }
type_array = {type_basic ~ ("[" ~ value ~ "]")+ }
_type = {type_array | type_basic | type_struct}
type_list = _{(_type ~ ("," ~ _type)*)?}
/// Values
value_number = @{ "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* }
value_u32 = { value_number ~ ty_u32? }
value_field = { value_number ~ ty_field }
value_u32 = { value_number ~ type_u32? }
value_field = { value_number ~ type_field }
value_boolean = { "true" | "false" }
value = { value_field | value_boolean | value_u32 }
@ -79,14 +79,14 @@ value = { value_field | value_boolean | value_u32 }
// TODO: Include "import" and "conditional"
protected_name = { visibility | value_boolean | "return" }
protected_name = { visibility | "let" | "for"| "if" | "else" | "as" | "return" }
// keyword = @{ "as" | "in" | "return" | "export" | "false" |
// "def" | "in" | "return" | "struct" | "true" }
variable = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
optionally_typed_variable = { (ty ~ variable) | (variable)}
optionally_typed_variable = { (_type ~ variable) | (variable)}
optionally_typed_variable_tuple = _{ optionally_typed_variable ~ ("," ~ optionally_typed_variable)* }
expression_primitive = { value | variable }
@ -120,7 +120,7 @@ expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ value ~ "]"
/// Structs
struct_field = { ty ~ variable }
struct_field = { variable ~ ":" ~ _type }
struct_field_list = _{(struct_field ~ (NEWLINE+ ~ struct_field)*)? }
struct_definition = { "struct" ~ variable ~ "{" ~ NEWLINE* ~ struct_field_list ~ NEWLINE* ~ "}" ~ NEWLINE* }
@ -160,7 +160,7 @@ conditional_nested_or_end = { statement_conditional | "{" ~ NEWLINE* ~ statement
/// Statements
statement_return = { "return" ~ expression_tuple }
statement_definition = { "let" ~ variable ~ (":" ~ ty)? ~ "=" ~ expression }
statement_definition = { "let" ~ variable ~ (":" ~ _type)? ~ "=" ~ expression }
statement_assign = { assignee ~ operation_assign ~ expression }
statement_multiple_assignment = { "let" ~ "(" ~ optionally_typed_variable_tuple ~ ")" ~ "=" ~ variable ~ "(" ~ expression_tuple ~ ")" }
statement_conditional = {"if" ~ "(" ~ expression ~ ")" ~ "{" ~ NEWLINE* ~ statement+ ~ "}" ~ ("else" ~ conditional_nested_or_end)?}
@ -186,11 +186,11 @@ statement = {
/// Functions
parameter = {variable ~ ":" ~ visibility? ~ ty}
parameter = {variable ~ ":" ~ visibility? ~ _type}
parameter_list = _{(parameter ~ ("," ~ parameter)*)?}
function_name = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
function_definition = {"function" ~ function_name ~ "(" ~ parameter_list ~ ")" ~ ("->" ~ (ty | "(" ~ type_list ~ ")"))? ~ "{" ~ NEWLINE* ~ statement* ~ NEWLINE* ~ "}" ~ NEWLINE* }
function_definition = {"function" ~ function_name ~ "(" ~ parameter_list ~ ")" ~ ("->" ~ (_type | "(" ~ type_list ~ ")"))? ~ "{" ~ NEWLINE* ~ statement* ~ NEWLINE* ~ "}" ~ NEWLINE* }
/// Utilities

View File

@ -143,7 +143,7 @@ pub struct StructMember<F: Field + PrimeField> {
#[derive(Clone, PartialEq, Eq)]
pub struct StructField<F: Field + PrimeField> {
pub variable: Variable<F>,
pub ty: Type<F>,
pub _type: Type<F>,
}
#[derive(Clone, PartialEq, Eq)]
@ -157,7 +157,7 @@ pub struct Struct<F: Field + PrimeField> {
#[derive(Clone, PartialEq, Eq)]
pub struct ParameterModel<F: Field + PrimeField> {
pub private: bool,
pub ty: Type<F>,
pub _type: Type<F>,
pub variable: Variable<F>,
}

View File

@ -231,7 +231,7 @@ impl<F: Field + PrimeField> fmt::Display for Type<F> {
impl<F: Field + PrimeField> fmt::Display for StructField<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} : {}", self.ty, self.variable)
write!(f, "{}: {}", self.variable, self._type)
}
}
@ -260,7 +260,7 @@ impl<F: Field + PrimeField> fmt::Debug for Struct<F> {
impl<F: Field + PrimeField> fmt::Display for ParameterModel<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let visibility = if self.private { "private" } else { "public" };
write!(f, "{}: {} {}", self.variable, visibility, self.ty,)
write!(f, "{}: {} {}", self.variable, visibility, self._type,)
}
}

View File

@ -373,7 +373,7 @@ impl<'ast, F: Field + PrimeField> From<ast::DefinitionStatement<'ast>> for types
fn from(statement: ast::DefinitionStatement<'ast>) -> Self {
types::Statement::Definition(
types::Assignee::from(statement.variable),
statement.ty.map(|ty| types::Type::<F>::from(ty)),
statement._type.map(|_type| types::Type::<F>::from(_type)),
types::Expression::from(statement.expression),
)
}
@ -568,7 +568,7 @@ impl<'ast, F: Field + PrimeField> From<ast::BasicType<'ast>> for types::Type<F>
impl<'ast, F: Field + PrimeField> From<ast::ArrayType<'ast>> for types::Type<F> {
fn from(array_type: ast::ArrayType<'ast>) -> Self {
let element_type = Box::new(types::Type::from(array_type.ty));
let element_type = Box::new(types::Type::from(array_type._type));
let count = types::Expression::<F>::get_count(array_type.count);
types::Type::Array(element_type, count)
@ -582,11 +582,11 @@ impl<'ast, F: Field + PrimeField> From<ast::StructType<'ast>> for types::Type<F>
}
impl<'ast, F: Field + PrimeField> From<ast::Type<'ast>> for types::Type<F> {
fn from(ty: ast::Type<'ast>) -> Self {
match ty {
ast::Type::Basic(ty) => types::Type::from(ty),
ast::Type::Array(ty) => types::Type::from(ty),
ast::Type::Struct(ty) => types::Type::from(ty),
fn from(_type: ast::Type<'ast>) -> Self {
match _type {
ast::Type::Basic(_type) => types::Type::from(_type),
ast::Type::Array(_type) => types::Type::from(_type),
ast::Type::Struct(_type) => types::Type::from(_type),
}
}
}
@ -597,7 +597,7 @@ impl<'ast, F: Field + PrimeField> From<ast::StructField<'ast>> for types::Struct
fn from(struct_field: ast::StructField<'ast>) -> Self {
types::StructField {
variable: types::Variable::from(struct_field.variable),
ty: types::Type::from(struct_field.ty),
_type: types::Type::from(struct_field._type),
}
}
}
@ -619,7 +619,7 @@ impl<'ast, F: Field + PrimeField> From<ast::Struct<'ast>> for types::Struct<F> {
impl<'ast, F: Field + PrimeField> From<ast::Parameter<'ast>> for types::ParameterModel<F> {
fn from(parameter: ast::Parameter<'ast>) -> Self {
let ty = types::Type::from(parameter.ty);
let _type = types::Type::from(parameter._type);
let variable = types::Variable::from(parameter.variable);
if parameter.visibility.is_some() {
@ -629,13 +629,13 @@ impl<'ast, F: Field + PrimeField> From<ast::Parameter<'ast>> for types::Paramete
};
types::ParameterModel {
private,
ty,
_type,
variable,
}
} else {
types::ParameterModel {
private: true,
ty,
_type,
variable,
}
}