diff --git a/src/front/glsl/ast.rs b/src/front/glsl/ast.rs index cd7abfdeaa..a4acf3a91b 100644 --- a/src/front/glsl/ast.rs +++ b/src/front/glsl/ast.rs @@ -42,8 +42,8 @@ impl Program { pub fn binary_expr( &mut self, op: BinaryOperator, - left: ExpressionRule, - right: ExpressionRule, + left: &ExpressionRule, + right: &ExpressionRule, ) -> ExpressionRule { ExpressionRule::from_expression(self.context.expressions.append(Expression::Binary { op, diff --git a/src/front/glsl/parser.rs b/src/front/glsl/parser.rs index bf10899fc2..5e6a16d341 100644 --- a/src/front/glsl/parser.rs +++ b/src/front/glsl/parser.rs @@ -8,7 +8,7 @@ pomelo! { use crate::{proc::Typifier, Arena, BinaryOperator, Binding, Block, Constant, ConstantInner, EntryPoint, Expression, FallThrough, FastHashMap, Function, GlobalVariable, Handle, Interpolation, LocalVariable, MemberOrigin, SampleLevel, ScalarKind, Statement, StorageAccess, - StorageClass, StructMember, Type, TypeInner}; + StorageClass, StructMember, Type, TypeInner, UnaryOperator}; } %token #[derive(Debug)] #[cfg_attr(test, derive(PartialEq))] pub enum Token {}; %parser pub struct Parser<'a> {}; @@ -150,9 +150,7 @@ pomelo! { let var = extra.lookup_variable(&v.1)?; match var { Some(expression) => { - ExpressionRule::from_expression( - expression - ) + ExpressionRule::from_expression(expression) }, None => { return Err(ErrorKind::UnknownVariable(v.0, v.1)); @@ -246,11 +244,11 @@ pomelo! { function_call ::= function_call_or_method(fc) { match fc.kind { FunctionCallKind::TypeConstructor(ty) => { - let h = extra.context.expressions.append(Expression::Compose{ + let h = extra.context.expressions.append(Expression::Compose { ty, components: fc.args.iter().map(|a| a.expression).collect(), }); - ExpressionRule{ + ExpressionRule { expression: h, statements: fc.args.into_iter().map(|a| a.statements).flatten().collect(), sampler: None @@ -270,7 +268,7 @@ pomelo! { //TODO: check args len if let Some(sampler) = fc.args[0].sampler { ExpressionRule{ - expression: extra.context.expressions.append(Expression::ImageSample{ + expression: extra.context.expressions.append(Expression::ImageSample { image: fc.args[0].expression, sampler, coordinate: fc.args[1].expression, @@ -358,73 +356,76 @@ pomelo! { unary_operator ::= Tilde; multiplicative_expression ::= unary_expression; multiplicative_expression ::= multiplicative_expression(left) Star unary_expression(right) { - extra.binary_expr(BinaryOperator::Multiply, left, right) + extra.binary_expr(BinaryOperator::Multiply, &left, &right) } multiplicative_expression ::= multiplicative_expression(left) Slash unary_expression(right) { - extra.binary_expr(BinaryOperator::Divide, left, right) + extra.binary_expr(BinaryOperator::Divide, &left, &right) } multiplicative_expression ::= multiplicative_expression(left) Percent unary_expression(right) { - extra.binary_expr(BinaryOperator::Modulo, left, right) + extra.binary_expr(BinaryOperator::Modulo, &left, &right) } additive_expression ::= multiplicative_expression; additive_expression ::= additive_expression(left) Plus multiplicative_expression(right) { - extra.binary_expr(BinaryOperator::Add, left, right) + extra.binary_expr(BinaryOperator::Add, &left, &right) } additive_expression ::= additive_expression(left) Dash multiplicative_expression(right) { - extra.binary_expr(BinaryOperator::Subtract, left, right) + extra.binary_expr(BinaryOperator::Subtract, &left, &right) } shift_expression ::= additive_expression; shift_expression ::= shift_expression(left) LeftOp additive_expression(right) { - extra.binary_expr(BinaryOperator::ShiftLeft, left, right) + extra.binary_expr(BinaryOperator::ShiftLeft, &left, &right) } shift_expression ::= shift_expression(left) RightOp additive_expression(right) { - extra.binary_expr(BinaryOperator::ShiftRight, left, right) + extra.binary_expr(BinaryOperator::ShiftRight, &left, &right) } relational_expression ::= shift_expression; relational_expression ::= relational_expression(left) LeftAngle shift_expression(right) { - extra.binary_expr(BinaryOperator::Less, left, right) + extra.binary_expr(BinaryOperator::Less, &left, &right) } relational_expression ::= relational_expression(left) RightAngle shift_expression(right) { - extra.binary_expr(BinaryOperator::Greater, left, right) + extra.binary_expr(BinaryOperator::Greater, &left, &right) } relational_expression ::= relational_expression(left) LeOp shift_expression(right) { - extra.binary_expr(BinaryOperator::LessEqual, left, right) + extra.binary_expr(BinaryOperator::LessEqual, &left, &right) } relational_expression ::= relational_expression(left) GeOp shift_expression(right) { - extra.binary_expr(BinaryOperator::GreaterEqual, left, right) + extra.binary_expr(BinaryOperator::GreaterEqual, &left, &right) } equality_expression ::= relational_expression; equality_expression ::= equality_expression(left) EqOp relational_expression(right) { - extra.binary_expr(BinaryOperator::Equal, left, right) + extra.binary_expr(BinaryOperator::Equal, &left, &right) } equality_expression ::= equality_expression(left) NeOp relational_expression(right) { - extra.binary_expr(BinaryOperator::NotEqual, left, right) + extra.binary_expr(BinaryOperator::NotEqual, &left, &right) } and_expression ::= equality_expression; and_expression ::= and_expression(left) Ampersand equality_expression(right) { - extra.binary_expr(BinaryOperator::And, left, right) + extra.binary_expr(BinaryOperator::And, &left, &right) } exclusive_or_expression ::= and_expression; exclusive_or_expression ::= exclusive_or_expression(left) Caret and_expression(right) { - extra.binary_expr(BinaryOperator::ExclusiveOr, left, right) + extra.binary_expr(BinaryOperator::ExclusiveOr, &left, &right) } inclusive_or_expression ::= exclusive_or_expression; inclusive_or_expression ::= inclusive_or_expression(left) VerticalBar exclusive_or_expression(right) { - extra.binary_expr(BinaryOperator::InclusiveOr, left, right) + extra.binary_expr(BinaryOperator::InclusiveOr, &left, &right) } logical_and_expression ::= inclusive_or_expression; logical_and_expression ::= logical_and_expression(left) AndOp inclusive_or_expression(right) { - extra.binary_expr(BinaryOperator::LogicalAnd, left, right) + extra.binary_expr(BinaryOperator::LogicalAnd, &left, &right) } logical_xor_expression ::= logical_and_expression; logical_xor_expression ::= logical_xor_expression(left) XorOp logical_and_expression(right) { - return Err(ErrorKind::NotImplemented("logical xor")) - //TODO: naga doesn't have BinaryOperator::LogicalXor - // extra.context.expressions.append(Expression::Binary{op: BinaryOperator::LogicalXor, left, right}) + let exp1 = extra.binary_expr(BinaryOperator::LogicalOr, &left, &right); + let exp2 = { + let tmp = extra.binary_expr(BinaryOperator::LogicalAnd, &left, &right).expression; + ExpressionRule::from_expression(extra.context.expressions.append(Expression::Unary { op: UnaryOperator::Not, expr: tmp })) + }; + extra.binary_expr(BinaryOperator::LogicalAnd, &exp1, &exp2) } logical_or_expression ::= logical_xor_expression; logical_or_expression ::= logical_or_expression(left) OrOp logical_xor_expression(right) { - extra.binary_expr(BinaryOperator::LogicalOr, left, right) + extra.binary_expr(BinaryOperator::LogicalOr, &left, &right) } conditional_expression ::= logical_or_expression; @@ -498,7 +499,7 @@ pomelo! { expression ::= assignment_expression; expression ::= expression(e) Comma assignment_expression(mut ae) { ae.statements.extend(e.statements); - ExpressionRule{ + ExpressionRule { expression: e.expression, statements: ae.statements, sampler: None, @@ -521,7 +522,7 @@ pomelo! { declaration ::= type_qualifier(t) Identifier(i) LeftBrace struct_declaration_list(sdl) RightBrace Semicolon { - VarDeclaration{ + VarDeclaration { type_qualifiers: t, ids_initializers: vec![(None, None)], ty: extra.module.types.fetch_or_append(Type{ @@ -535,7 +536,7 @@ pomelo! { declaration ::= type_qualifier(t) Identifier(i1) LeftBrace struct_declaration_list(sdl) RightBrace Identifier(i2) Semicolon { - VarDeclaration{ + VarDeclaration { type_qualifiers: t, ids_initializers: vec![(Some(i2.1), None)], ty: extra.module.types.fetch_or_append(Type{ @@ -565,7 +566,7 @@ pomelo! { single_declaration ::= fully_specified_type(t) { let ty = t.1.ok_or(ErrorKind::SemanticError("Empty type for declaration"))?; - VarDeclaration{ + VarDeclaration { type_qualifiers: t.0, ids_initializers: vec![], ty, @@ -574,7 +575,7 @@ pomelo! { single_declaration ::= fully_specified_type(t) Identifier(i) { let ty = t.1.ok_or(ErrorKind::SemanticError("Empty type for declaration"))?; - VarDeclaration{ + VarDeclaration { type_qualifiers: t.0, ids_initializers: vec![(Some(i.1), None)], ty, @@ -585,7 +586,7 @@ pomelo! { single_declaration ::= fully_specified_type(t) Identifier(i) Equal initializer(init) { let ty = t.1.ok_or(ErrorKind::SemanticError("Empty type for declaration"))?; - VarDeclaration{ + VarDeclaration { type_qualifiers: t.0, ids_initializers: vec![(Some(i.1), Some(init))], ty, @@ -715,7 +716,7 @@ pomelo! { struct_declaration ::= type_specifier(t) struct_declarator_list(sdl) Semicolon { if let Some(ty) = t { - sdl.iter().map(|name| StructMember{ + sdl.iter().map(|name| StructMember { name: Some(name.clone()), origin: MemberOrigin::Empty, ty, @@ -1007,7 +1008,7 @@ pomelo! { Statement::Break } jump_statement ::= Return Semicolon { - Statement::Return{ value: None } + Statement::Return { value: None } } jump_statement ::= Return expression(mut e) Semicolon { let ret = Statement::Return{ value: Some(e.expression) }; @@ -1093,7 +1094,6 @@ pomelo! { } } } - } }