From efe8e181606f98b8fdcb03d6b8611f36ca42f5e4 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Mon, 15 Feb 2021 09:49:16 +0100 Subject: [PATCH] feat: infer struct type --- src/generator/c.rs | 1 + src/generator/llvm.rs | 1 + src/parser/infer.rs | 1 + src/parser/node_type.rs | 1 + 4 files changed, 4 insertions(+) diff --git a/src/generator/c.rs b/src/generator/c.rs index 09b8f68..d921940 100644 --- a/src/generator/c.rs +++ b/src/generator/c.rs @@ -51,6 +51,7 @@ pub(super) fn generate_type(t: Either>) -> String { Type::Str => "char *".into(), Type::Any => "void *".into(), Type::Bool => "bool".into(), + Type::Struct(_) => todo!(), Type::Array(_) => match name { Some(n) => format!( "{T} {N}[]", diff --git a/src/generator/llvm.rs b/src/generator/llvm.rs index 0d1deb9..210e6a0 100644 --- a/src/generator/llvm.rs +++ b/src/generator/llvm.rs @@ -31,6 +31,7 @@ impl<'ctx> LLVMGenerator<'ctx> { Some(Type::Any) => todo!(), Some(Type::Str) => todo!(), Some(Type::Array(_)) => todo!(), + Some(Type::Struct(_)) => todo!(), None => panic!("Function argument has no type"), }) .collect(); diff --git a/src/parser/infer.rs b/src/parser/infer.rs index c964ad5..819e167 100644 --- a/src/parser/infer.rs +++ b/src/parser/infer.rs @@ -32,6 +32,7 @@ fn infer_expression(expr: &Expression, table: &SymbolTable) -> Option { Expression::Int(_) => Some(Type::Int), Expression::Bool(_) => Some(Type::Bool), Expression::Str(_) => Some(Type::Str), + Expression::StructInitialization(name, _) => Some(Type::Struct(name.to_string())), Expression::FunctionCall(name, _) => infer_function_call(name, table), Expression::Array(els) => infer_array(els, table), _ => None, diff --git a/src/parser/node_type.rs b/src/parser/node_type.rs index 39a755d..5c647dd 100644 --- a/src/parser/node_type.rs +++ b/src/parser/node_type.rs @@ -71,6 +71,7 @@ pub enum Type { Str, Bool, Array(Box), + Struct(String), } impl TryFrom for Type {