microcad_lang/lower/lower/
attribute.rs1use crate::lower::{Lower, LowerContext, LowerError, ir};
5
6use microcad_lang_base::Identifier;
7use microcad_lang_parse::ast;
8
9impl Lower for ir::AttributeCommand {
10 type AstNode = ast::AttributeCommand;
11
12 fn lower(node: &Self::AstNode, context: &mut LowerContext) -> Result<Self, LowerError> {
13 Ok(match node {
14 ast::AttributeCommand::Ident(i) => {
15 ir::AttributeCommand::Ident(Identifier::lower(i, context)?)
16 }
17 ast::AttributeCommand::Assignment(a) => ir::AttributeCommand::Assignment {
18 name: Identifier::lower(&a.id, context)?,
19 value: ir::Expression::lower(&a.expr, context)?,
20 src_ref: context.src_ref(&a.span),
21 },
22 ast::AttributeCommand::Call(c) => {
23 ir::AttributeCommand::Call(ir::Call::lower(c, context)?)
24 }
25 })
26 }
27}
28
29impl Lower for ir::Attribute {
30 type AstNode = ast::Attribute;
31
32 fn lower(node: &Self::AstNode, context: &mut LowerContext) -> Result<Self, LowerError> {
33 Ok(ir::Attribute {
34 src_ref: context.src_ref(&node.span),
35 is_inner: node.is_inner,
36 commands: node
37 .commands
38 .iter()
39 .map(|c| ir::AttributeCommand::lower(c, context))
40 .collect::<Result<Vec<_>, LowerError>>()?,
41 })
42 }
43}
44
45impl Lower for ir::AttributeList {
46 type AstNode = Vec<ast::Attribute>;
47
48 fn lower(node: &Self::AstNode, context: &mut LowerContext) -> Result<Self, LowerError> {
49 node.iter()
50 .map(|a| ir::Attribute::lower(a, context))
51 .collect::<Result<Vec<_>, _>>()
52 .map(ir::AttributeList::from)
53 }
54}