Skip to content

Commit

Permalink
Structs compile
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Sep 13, 2023
1 parent 0cef7e7 commit 2ce282b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
8 changes: 4 additions & 4 deletions sloth/src/analysis/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ impl Populator {

fn build_struct_symbol(
&mut self,
line: u32,
_line: u32,
table: &SymbolTable,
properties: HashMap<String, TypeIdentifier>,
) -> Result<Symbol, AnalysisError> {
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::<HashMap<String, Type>>();

Ok(Symbol::Value(ValueSymbol {
Expand Down Expand Up @@ -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)?;
Expand Down
16 changes: 10 additions & 6 deletions sloth/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ..
Expand Down Expand Up @@ -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::<Vec::<BasicTypeEnum>>(),
.collect::<Vec<BasicTypeEnum>>(),
false,
).as_basic_type_enum();

)
.as_basic_type_enum();

let ptr = strct.ptr_type(AddressSpace::default());
ptr.as_basic_type_enum()
Expand Down
4 changes: 2 additions & 2 deletions sloth/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
9 changes: 6 additions & 3 deletions sloth/src/parser/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl GraphBuilder {
self.traverse_expr0(expr)?;
}
StmtKind::DefineStruct {
identifier,
properties,
identifier: _,
properties: _,
} => {
todo!();
}
Expand Down Expand Up @@ -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)?;
Expand Down
19 changes: 12 additions & 7 deletions sloth/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -312,7 +314,10 @@ 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 EXPR")?;
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 2ce282b

Please sign in to comment.