From 0cef7e7f25b3bcd49f6525bc80ab0ad8297f27b2 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Wed, 13 Sep 2023 14:58:28 -0500 Subject: pre-clippy --- examples/struct.sloth | 8 ++++++++ sloth/src/codegen/mod.rs | 22 ++++++++++++++++++++++ sloth/src/lexer.rs | 1 + sloth/src/parser/graph.rs | 7 +++++++ sloth/src/parser/stmt.rs | 18 +++++++++++------- 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 examples/struct.sloth diff --git a/examples/struct.sloth b/examples/struct.sloth new file mode 100644 index 0000000..a9f9750 --- /dev/null +++ b/examples/struct.sloth @@ -0,0 +1,8 @@ +fn main() Int { + struct Time { + var hr: Int; + var mn: Int; + } + println("Hello Struct"); + return 0; +} diff --git a/sloth/src/codegen/mod.rs b/sloth/src/codegen/mod.rs index 91bc078..52c0432 100644 --- a/sloth/src/codegen/mod.rs +++ b/sloth/src/codegen/mod.rs @@ -220,6 +220,15 @@ impl<'ctx> Codegen<'ctx> { // Position the builder at the end of the loop self.builder.position_at_end(after_bb); } + 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, .. } => { @@ -652,6 +661,19 @@ impl<'ctx> Codegen<'ctx> { .ptr_type(AddressSpace::default()) .as_basic_type_enum() } + Type::Struct { properties, .. } => { + let strct = self.context.struct_type( + &properties + .iter() + .map(|it| self.type_as_basic_type(it.1.clone())) + .collect::>(), + false, + ).as_basic_type_enum(); + + + let ptr = strct.ptr_type(AddressSpace::default()); + ptr.as_basic_type_enum() + } _ => todo!(), } } diff --git a/sloth/src/lexer.rs b/sloth/src/lexer.rs index 7b71377..bca2cc3 100644 --- a/sloth/src/lexer.rs +++ b/sloth/src/lexer.rs @@ -499,6 +499,7 @@ impl<'a> Iterator for Lexer<'a> { "loop" => TokenType::Loop, "break" => TokenType::Break, "continue" => TokenType::Continue, + "struct" => TokenType::Struct, "as" => TokenType::As, "foreign" => TokenType::Foreign, "true" => Literal::Boolean(true).into(), diff --git a/sloth/src/parser/graph.rs b/sloth/src/parser/graph.rs index a76bf4c..fd381a3 100644 --- a/sloth/src/parser/graph.rs +++ b/sloth/src/parser/graph.rs @@ -47,6 +47,12 @@ impl GraphBuilder { )?; self.traverse_expr0(expr)?; } + StmtKind::DefineStruct { + identifier, + properties, + } => { + todo!(); + } StmtKind::IfStmt { condition, if_then, @@ -218,6 +224,7 @@ impl GraphBuilder { fn traverse_stmt(&mut self, stmt: &Stmt) -> Result<(), Error> { match &stmt.kind { + 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 9496d88..05212f4 100644 --- a/sloth/src/parser/stmt.rs +++ b/sloth/src/parser/stmt.rs @@ -137,7 +137,7 @@ impl<'a> AstParser<'a> { self.consume(TokenType::Eq, "Expected '='")?; let value = self.expression()?; - self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?; + self.consume(TokenType::SemiColon, "Expected ';' at end of statement VAR")?; let kind = StmtKind::DefineVariable { identifier, @@ -159,16 +159,20 @@ impl<'a> AstParser<'a> { self.consume(TokenType::Struct, "Expected struct")?; let identifier = self.consume_identifier()?; + self.consume(TokenType::OpeningBrace, "Expected '{' after identifier")?; - while self.peek().tt != TokenType::ClosingBracket { - self.consume(TokenType::Val, "Expected val in struct!")?; + while self.peek().tt != TokenType::ClosingBrace { + self.consume(TokenType::Var, "Expected var in struct!")?; let ident = self.consume_identifier()?; + 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::ClosingBracket, "Expected '}' at end of struct"); + self.consume(TokenType::ClosingBrace, "Expected '}' at end of struct"); let kind = StmtKind::DefineStruct { identifier, @@ -199,7 +203,7 @@ impl<'a> AstParser<'a> { self.consume(TokenType::Eq, "Expected '='")?; let value = self.expression()?; - self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?; + self.consume(TokenType::SemiColon, "Expected ';' at end of statement VAL")?; let kind = StmtKind::DefineValue { identifier, @@ -296,7 +300,7 @@ 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")?; + self.consume(TokenType::SemiColon, "Expected ';' at end of statement ASSIGN")?; let kind = StmtKind::AssignVariable { identifier, value }; Ok(Stmt::new( self.reserve_id(), @@ -308,7 +312,7 @@ impl<'a> AstParser<'a> { fn expression_stmt(&mut self) -> Result { let expr = self.expression()?; - self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?; + self.consume(TokenType::SemiColon, "Expected ';' at end of statement EXPR")?; let kind = StmtKind::ExprStmt(expr); Ok(Stmt::new( self.reserve_id(), -- cgit v1.2.3