mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
replace struct -> circuit in pest parsing
This commit is contained in:
parent
d410dc6d9f
commit
5046db8e2c
@ -1,3 +1,7 @@
|
||||
circuit Circ {
|
||||
x: u32
|
||||
}
|
||||
|
||||
function main() {
|
||||
let g = 0group;
|
||||
let c = Circ { x: 4 };
|
||||
}
|
@ -174,8 +174,8 @@ pub struct BooleanType<'ast> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::type_struct))]
|
||||
pub struct StructType<'ast> {
|
||||
#[pest_ast(rule(Rule::type_circuit))]
|
||||
pub struct CircuitType<'ast> {
|
||||
pub variable: Variable<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
@ -190,13 +190,6 @@ pub enum BasicType<'ast> {
|
||||
Boolean(BooleanType<'ast>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[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::type_array))]
|
||||
pub struct ArrayType<'ast> {
|
||||
@ -211,7 +204,7 @@ pub struct ArrayType<'ast> {
|
||||
pub enum Type<'ast> {
|
||||
Basic(BasicType<'ast>),
|
||||
Array(ArrayType<'ast>),
|
||||
Struct(StructType<'ast>),
|
||||
Circuit(CircuitType<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Type<'ast> {
|
||||
@ -219,7 +212,7 @@ impl<'ast> fmt::Display for Type<'ast> {
|
||||
match *self {
|
||||
Type::Basic(ref _type) => write!(f, "basic"),
|
||||
Type::Array(ref _type) => write!(f, "array"),
|
||||
Type::Struct(ref _type) => write!(f, "struct"),
|
||||
Type::Circuit(ref _type) => write!(f, "struct"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -539,11 +532,11 @@ pub struct ArrayInitializerExpression<'ast> {
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
// Structs
|
||||
// Circuits
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::struct_field))]
|
||||
pub struct StructField<'ast> {
|
||||
#[pest_ast(rule(Rule::circuit_object))]
|
||||
pub struct CircuitObject<'ast> {
|
||||
pub variable: Variable<'ast>,
|
||||
pub _type: Type<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
@ -551,17 +544,17 @@ pub struct StructField<'ast> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::struct_definition))]
|
||||
pub struct Struct<'ast> {
|
||||
#[pest_ast(rule(Rule::circuit_definition))]
|
||||
pub struct Circuit<'ast> {
|
||||
pub variable: Variable<'ast>,
|
||||
pub fields: Vec<StructField<'ast>>,
|
||||
pub fields: Vec<CircuitObject<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::inline_struct_member))]
|
||||
pub struct InlineStructMember<'ast> {
|
||||
#[pest_ast(rule(Rule::inline_circuit_member))]
|
||||
pub struct InlineCircuitMember<'ast> {
|
||||
pub variable: Variable<'ast>,
|
||||
pub expression: Expression<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
@ -569,10 +562,10 @@ pub struct InlineStructMember<'ast> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_inline_struct))]
|
||||
pub struct StructInlineExpression<'ast> {
|
||||
#[pest_ast(rule(Rule::expression_inline_circuit))]
|
||||
pub struct CircuitInlineExpression<'ast> {
|
||||
pub variable: Variable<'ast>,
|
||||
pub members: Vec<InlineStructMember<'ast>>,
|
||||
pub members: Vec<InlineCircuitMember<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
@ -633,7 +626,7 @@ pub enum Expression<'ast> {
|
||||
Ternary(TernaryExpression<'ast>),
|
||||
ArrayInline(ArrayInlineExpression<'ast>),
|
||||
ArrayInitializer(ArrayInitializerExpression<'ast>),
|
||||
StructInline(StructInlineExpression<'ast>),
|
||||
CircuitInline(CircuitInlineExpression<'ast>),
|
||||
Postfix(PostfixExpression<'ast>),
|
||||
}
|
||||
|
||||
@ -677,7 +670,7 @@ impl<'ast> Expression<'ast> {
|
||||
Expression::Ternary(expression) => &expression.span,
|
||||
Expression::ArrayInline(expression) => &expression.span,
|
||||
Expression::ArrayInitializer(expression) => &expression.span,
|
||||
Expression::StructInline(expression) => &expression.span,
|
||||
Expression::CircuitInline(expression) => &expression.span,
|
||||
Expression::Postfix(expression) => &expression.span,
|
||||
}
|
||||
}
|
||||
@ -711,8 +704,8 @@ impl<'ast> fmt::Display for Expression<'ast> {
|
||||
Expression::ArrayInitializer(ref expression) => {
|
||||
write!(f, "[{} ; {}]", expression.expression, expression.count)
|
||||
}
|
||||
Expression::StructInline(ref expression) => {
|
||||
write!(f, "inline struct display not impl {}", expression.variable)
|
||||
Expression::CircuitInline(ref expression) => {
|
||||
write!(f, "inline circuit display not impl {}", expression.variable)
|
||||
}
|
||||
Expression::Postfix(ref expression) => {
|
||||
write!(f, "Postfix display not impl {}", expression.variable)
|
||||
@ -746,9 +739,9 @@ fn parse_term(pair: Pair<Rule>) -> Box<Expression> {
|
||||
let next = clone.into_inner().next().unwrap();
|
||||
match next.as_rule() {
|
||||
Rule::expression => Expression::from_pest(&mut pair.into_inner()).unwrap(), // Parenthesis case
|
||||
Rule::expression_inline_struct => {
|
||||
Expression::StructInline(
|
||||
StructInlineExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
Rule::expression_inline_circuit => {
|
||||
Expression::CircuitInline(
|
||||
CircuitInlineExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
)
|
||||
},
|
||||
Rule::expression_array_inline => {
|
||||
@ -1150,7 +1143,7 @@ pub struct Import<'ast> {
|
||||
#[pest_ast(rule(Rule::file))]
|
||||
pub struct File<'ast> {
|
||||
pub imports: Vec<Import<'ast>>,
|
||||
pub structs: Vec<Struct<'ast>>,
|
||||
pub structs: Vec<Circuit<'ast>>,
|
||||
pub functions: Vec<Function<'ast>>,
|
||||
pub eoi: EOI,
|
||||
#[pest_ast(outer())]
|
||||
|
@ -73,10 +73,9 @@ type_field = {"field"}
|
||||
type_group = {"group"}
|
||||
type_bool = {"bool"}
|
||||
type_basic = { type_field | type_group | type_bool | type_integer }
|
||||
type_struct = { variable }
|
||||
type_basic_or_struct = {type_basic | type_struct }
|
||||
type_circuit = { variable }
|
||||
type_array = {type_basic ~ ("[" ~ value ~ "]")+ }
|
||||
_type = {type_array | type_basic | type_struct}
|
||||
_type = {type_array | type_basic | type_circuit}
|
||||
type_list = _{(_type ~ ("," ~ _type)*)?}
|
||||
|
||||
/// Values
|
||||
@ -95,7 +94,7 @@ value = { value_field | value_group | value_boolean | value_integer }
|
||||
protected_name = { visibility | "let" | "for"| "if" | "else" | "as" | "return" }
|
||||
|
||||
// keyword = @{ "as" | "in" | "return" | "export" | "false" |
|
||||
// "def" | "in" | "return" | "struct" | "true" }
|
||||
// "def" | "in" | "return" | "circuit" | "true" }
|
||||
|
||||
variable = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
|
||||
@ -131,15 +130,15 @@ inline_array_inner = _{(spread_or_expression ~ ("," ~ NEWLINE* ~ spread_or_expre
|
||||
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}
|
||||
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ value ~ "]" }
|
||||
|
||||
/// Structs
|
||||
/// Circuits
|
||||
|
||||
struct_field = { variable ~ ":" ~ _type }
|
||||
struct_field_list = _{(struct_field ~ (NEWLINE+ ~ struct_field)*)? }
|
||||
struct_definition = { "struct" ~ variable ~ "{" ~ NEWLINE* ~ struct_field_list ~ NEWLINE* ~ "}" ~ NEWLINE* }
|
||||
circuit_object = { variable ~ ":" ~ _type }
|
||||
circuit_object_list = _{(circuit_object ~ (NEWLINE+ ~ circuit_object)*)? }
|
||||
circuit_definition = { "circuit" ~ variable ~ "{" ~ NEWLINE* ~ circuit_object_list ~ NEWLINE* ~ "}" ~ NEWLINE* }
|
||||
|
||||
inline_struct_member = { variable ~ ":" ~ expression }
|
||||
inline_struct_member_list = _{(inline_struct_member ~ ("," ~ NEWLINE* ~ inline_struct_member)*)? ~ ","? }
|
||||
expression_inline_struct = { variable ~ "{" ~ NEWLINE* ~ inline_struct_member_list ~ NEWLINE* ~ "}" }
|
||||
inline_circuit_member = { variable ~ ":" ~ expression }
|
||||
inline_circuit_member_list = _{(inline_circuit_member ~ ("," ~ NEWLINE* ~ inline_circuit_member)*)? ~ ","? }
|
||||
expression_inline_circuit = { variable ~ "{" ~ NEWLINE* ~ inline_circuit_member_list ~ NEWLINE* ~ "}" }
|
||||
|
||||
/// Conditionals
|
||||
|
||||
@ -149,7 +148,7 @@ expression_conditional = { "if" ~ expression ~ "?" ~ expression ~ ":" ~ expressi
|
||||
|
||||
expression_term = {
|
||||
("(" ~ expression ~ ")")
|
||||
| expression_inline_struct
|
||||
| expression_inline_circuit
|
||||
| expression_conditional
|
||||
| expression_postfix
|
||||
| expression_primitive
|
||||
@ -221,4 +220,4 @@ import = { "from" ~ "\"" ~ import_source ~ "\"" ~ "import" ~ ("*" | ("{" ~ NEWLI
|
||||
|
||||
/// Program File
|
||||
|
||||
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ struct_definition* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ EOI }
|
||||
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit_definition* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ EOI }
|
||||
|
@ -263,10 +263,10 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInitializerExpression
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::InlineStructMember<'ast>>
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::InlineCircuitMember<'ast>>
|
||||
for types::StructMember<F, G>
|
||||
{
|
||||
fn from(member: ast::InlineStructMember<'ast>) -> Self {
|
||||
fn from(member: ast::InlineCircuitMember<'ast>) -> Self {
|
||||
types::StructMember {
|
||||
variable: types::Variable::from(member.variable),
|
||||
expression: types::Expression::from(member.expression),
|
||||
@ -274,10 +274,10 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::InlineStructMember<'ast>>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructInlineExpression<'ast>>
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitInlineExpression<'ast>>
|
||||
for types::Expression<F, G>
|
||||
{
|
||||
fn from(expression: ast::StructInlineExpression<'ast>) -> Self {
|
||||
fn from(expression: ast::CircuitInlineExpression<'ast>) -> Self {
|
||||
let variable = types::Variable::from(expression.variable);
|
||||
let members = expression
|
||||
.members
|
||||
@ -340,7 +340,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Expression<'ast>>
|
||||
ast::Expression::Ternary(expression) => types::Expression::from(expression),
|
||||
ast::Expression::ArrayInline(expression) => types::Expression::from(expression),
|
||||
ast::Expression::ArrayInitializer(expression) => types::Expression::from(expression),
|
||||
ast::Expression::StructInline(expression) => types::Expression::from(expression),
|
||||
ast::Expression::CircuitInline(expression) => types::Expression::from(expression),
|
||||
ast::Expression::Postfix(expression) => types::Expression::from(expression),
|
||||
}
|
||||
}
|
||||
@ -665,8 +665,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayType<'ast>> for types
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructType<'ast>> for types::Type<F, G> {
|
||||
fn from(struct_type: ast::StructType<'ast>) -> Self {
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitType<'ast>> for types::Type<F, G> {
|
||||
fn from(struct_type: ast::CircuitType<'ast>) -> Self {
|
||||
types::Type::Struct(types::Variable::from(struct_type.variable))
|
||||
}
|
||||
}
|
||||
@ -676,17 +676,17 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Type<'ast>> for types::Typ
|
||||
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),
|
||||
ast::Type::Circuit(_type) => types::Type::from(_type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> types::Struct
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructField<'ast>>
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitObject<'ast>>
|
||||
for types::StructField<F, G>
|
||||
{
|
||||
fn from(struct_field: ast::StructField<'ast>) -> Self {
|
||||
fn from(struct_field: ast::CircuitObject<'ast>) -> Self {
|
||||
types::StructField {
|
||||
variable: types::Variable::from(struct_field.variable),
|
||||
_type: types::Type::from(struct_field._type),
|
||||
@ -694,8 +694,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructField<'ast>>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Struct<'ast>> for types::Struct<F, G> {
|
||||
fn from(struct_definition: ast::Struct<'ast>) -> Self {
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Circuit<'ast>> for types::Struct<F, G> {
|
||||
fn from(struct_definition: ast::Circuit<'ast>) -> Self {
|
||||
let variable = types::Variable::from(struct_definition.variable);
|
||||
let fields = struct_definition
|
||||
.fields
|
||||
|
Loading…
Reference in New Issue
Block a user