Skip to content

Commit

Permalink
pre-clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Sep 13, 2023
1 parent 2ab284b commit 0cef7e7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
8 changes: 8 additions & 0 deletions examples/struct.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() Int {
struct Time {
var hr: Int;
var mn: Int;
}
println("Hello Struct");
return 0;
}
22 changes: 22 additions & 0 deletions sloth/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ..
} => {
Expand Down Expand Up @@ -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::<Vec::<BasicTypeEnum>>(),
false,
).as_basic_type_enum();


let ptr = strct.ptr_type(AddressSpace::default());
ptr.as_basic_type_enum()
}
_ => todo!(),
}
}
Expand Down
1 change: 1 addition & 0 deletions sloth/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
7 changes: 7 additions & 0 deletions sloth/src/parser/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ impl GraphBuilder {
)?;
self.traverse_expr0(expr)?;
}
StmtKind::DefineStruct {
identifier,
properties,
} => {
todo!();
}
StmtKind::IfStmt {
condition,
if_then,
Expand Down Expand Up @@ -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)?;
Expand Down
18 changes: 11 additions & 7 deletions sloth/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -308,7 +312,7 @@ impl<'a> AstParser<'a> {

fn expression_stmt(&mut self) -> Result<Stmt, ParsingError> {
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(),
Expand Down

0 comments on commit 0cef7e7

Please sign in to comment.