From 2ce282bc0045afb64814ba86735fcf5f0d3a6071 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Wed, 13 Sep 2023 15:00:04 -0500 Subject: [PATCH] Structs compile --- sloth/src/analysis/setup.rs | 8 ++++---- sloth/src/codegen/mod.rs | 16 ++++++++++------ sloth/src/parser/ast.rs | 4 ++-- sloth/src/parser/graph.rs | 9 ++++++--- sloth/src/parser/stmt.rs | 19 ++++++++++++------- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/sloth/src/analysis/setup.rs b/sloth/src/analysis/setup.rs index 69e7efb..bee0da3 100644 --- a/sloth/src/analysis/setup.rs +++ b/sloth/src/analysis/setup.rs @@ -136,13 +136,13 @@ impl Populator { fn build_struct_symbol( &mut self, - line: u32, + _line: u32, table: &SymbolTable, properties: HashMap, ) -> Result { let properties = properties .iter() - .map(|it| (it.0.clone(), table.get_type(&it.1).unwrap())) + .map(|it| (it.0.clone(), table.get_type(it.1).unwrap())) .collect::>(); Ok(Symbol::Value(ValueSymbol { @@ -223,8 +223,8 @@ pub(super) fn propagate_types_stmt(node: &mut Stmt) -> Result<(), AnalysisError> propagate_types(value)?; } StmtKind::DefineStruct { - identifier, - properties, + identifier: _, + properties: _, } => (), StmtKind::AssignVariable { value, .. } => { propagate_types(value)?; diff --git a/sloth/src/codegen/mod.rs b/sloth/src/codegen/mod.rs index 52c0432..e2c8082 100644 --- a/sloth/src/codegen/mod.rs +++ b/sloth/src/codegen/mod.rs @@ -220,14 +220,16 @@ impl<'ctx> Codegen<'ctx> { // Position the builder at the end of the loop self.builder.position_at_end(after_bb); } - StmtKind::DefineStruct { identifier, properties } => { + StmtKind::DefineStruct { + identifier, + properties: _, + } => { let table = code.symtable.clone(); let symbol = table.get_value(identifier).unwrap(); let ptr = self.codegen_alloca(self.type_as_basic_type(symbol.typ), identifier); self.references.insert(symbol.id, ptr); - } StmtKind::DefineVariable { identifier, value, .. @@ -662,14 +664,16 @@ impl<'ctx> Codegen<'ctx> { .as_basic_type_enum() } Type::Struct { properties, .. } => { - let strct = self.context.struct_type( + let strct = self + .context + .struct_type( &properties .iter() .map(|it| self.type_as_basic_type(it.1.clone())) - .collect::>(), + .collect::>(), false, - ).as_basic_type_enum(); - + ) + .as_basic_type_enum(); let ptr = strct.ptr_type(AddressSpace::default()); ptr.as_basic_type_enum() diff --git a/sloth/src/parser/ast.rs b/sloth/src/parser/ast.rs index f830761..b35c516 100644 --- a/sloth/src/parser/ast.rs +++ b/sloth/src/parser/ast.rs @@ -169,8 +169,8 @@ impl Stmt { match &self.kind { StmtKind::DefineStruct { - identifier, - properties, + identifier: _, + properties: _, } => (), StmtKind::Block(inner) => { children.extend(inner.iter().map(Self::as_node)); diff --git a/sloth/src/parser/graph.rs b/sloth/src/parser/graph.rs index fd381a3..187c98b 100644 --- a/sloth/src/parser/graph.rs +++ b/sloth/src/parser/graph.rs @@ -48,8 +48,8 @@ impl GraphBuilder { self.traverse_expr0(expr)?; } StmtKind::DefineStruct { - identifier, - properties, + identifier: _, + properties: _, } => { todo!(); } @@ -224,7 +224,10 @@ impl GraphBuilder { fn traverse_stmt(&mut self, stmt: &Stmt) -> Result<(), Error> { match &stmt.kind { - StmtKind::DefineStruct { identifier, properties } => todo!(), + StmtKind::DefineStruct { + identifier: _, + properties: _, + } => todo!(), StmtKind::Block(children) => { for child in children { writeln!(&mut self.graph, "N{} -> N{};", stmt.id, child.id)?; diff --git a/sloth/src/parser/stmt.rs b/sloth/src/parser/stmt.rs index 05212f4..873595b 100644 --- a/sloth/src/parser/stmt.rs +++ b/sloth/src/parser/stmt.rs @@ -165,14 +165,13 @@ impl<'a> AstParser<'a> { self.consume(TokenType::Var, "Expected var in struct!")?; let ident = self.consume_identifier()?; - self.consume(TokenType::Colon, "Expected :"); + self.consume(TokenType::Colon, "Expected :")?; let typ = self.consume_type()?; - self.consume(TokenType::SemiColon, "Expected ;"); - - properties.insert(ident, typ).ok_or(0); + self.consume(TokenType::SemiColon, "Expected ;")?; + properties.insert(ident, typ); } - self.consume(TokenType::ClosingBrace, "Expected '}' at end of struct"); + self.consume(TokenType::ClosingBrace, "Expected '}' at end of struct")?; let kind = StmtKind::DefineStruct { identifier, @@ -300,7 +299,10 @@ impl<'a> AstParser<'a> { let identifier = self.consume_identifier()?; self.consume(TokenType::Eq, "Expected '='")?; let value = self.expression()?; - self.consume(TokenType::SemiColon, "Expected ';' at end of statement ASSIGN")?; + self.consume( + TokenType::SemiColon, + "Expected ';' at end of statement ASSIGN", + )?; let kind = StmtKind::AssignVariable { identifier, value }; Ok(Stmt::new( self.reserve_id(), @@ -312,7 +314,10 @@ impl<'a> AstParser<'a> { fn expression_stmt(&mut self) -> Result { let expr = self.expression()?; - self.consume(TokenType::SemiColon, "Expected ';' at end of statement EXPR")?; + self.consume( + TokenType::SemiColon, + "Expected ';' at end of statement EXPR", + )?; let kind = StmtKind::ExprStmt(expr); Ok(Stmt::new( self.reserve_id(),