aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-06-26 15:56:09 -0500
committerCody <cody@codyq.dev>2023-06-26 15:56:09 -0500
commit4172dc213f88df5025169fa7f43d02f2794fdd70 (patch)
tree35f570b62caecdb921b5414c51de08f18126054b
parent849ba9ffa6ba9ac00bbc5fdb144a48cf7076a46f (diff)
downloadsloth-4172dc213f88df5025169fa7f43d02f2794fdd70.tar.gz
Bugfix with dynamic arrays
-rw-r--r--examples/hello.sloth2
-rw-r--r--sloth/src/codegen/mod.rs58
-rw-r--r--sloth/src/lexer.rs8
-rw-r--r--sloth/src/parser/ast.rs4
-rw-r--r--test.c8
5 files changed, 41 insertions, 39 deletions
diff --git a/examples/hello.sloth b/examples/hello.sloth
index 7378cb1..1bbdbd4 100644
--- a/examples/hello.sloth
+++ b/examples/hello.sloth
@@ -1,5 +1,5 @@
fn test() [Int 3] {
- var list: [Int 3] = [9, 5, 7];
+ var list: [Int 3] = [500, 5, 7];
vpushi(list, 3);
vpushi(list, 3);
diff --git a/sloth/src/codegen/mod.rs b/sloth/src/codegen/mod.rs
index de19116..ad6b451 100644
--- a/sloth/src/codegen/mod.rs
+++ b/sloth/src/codegen/mod.rs
@@ -7,7 +7,7 @@ use inkwell::module::Module;
use inkwell::targets::{
CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine,
};
-use inkwell::types::{BasicMetadataTypeEnum, BasicType, BasicTypeEnum, PointerType};
+use inkwell::types::{BasicMetadataTypeEnum, BasicType, BasicTypeEnum};
use inkwell::values::{
BasicMetadataValueEnum, BasicValue, BasicValueEnum, FunctionValue, PointerValue,
};
@@ -210,8 +210,8 @@ impl<'ctx> Codegen<'ctx> {
let llvm_function_type = match output_typ {
Type::Void => self.context.void_type().fn_type(&inputs_typ, false),
- Type::Integer => self.context.i64_type().fn_type(&inputs_typ, false),
- Type::Float => self.context.f64_type().fn_type(&inputs_typ, false),
+ Type::Integer => self.context.i32_type().fn_type(&inputs_typ, false),
+ Type::Float => self.context.f32_type().fn_type(&inputs_typ, false),
Type::Boolean => self.context.bool_type().fn_type(&inputs_typ, false),
Type::Array { ref typ, .. } => {
let i32_type = self.context.i32_type().as_basic_type_enum();
@@ -345,13 +345,13 @@ impl<'ctx> Codegen<'ctx> {
match value {
Literal::Integer(value) => self
.context
- .i64_type()
+ .i32_type()
.const_int(value as u64, true)
.as_basic_value_enum(),
Literal::Float(value) => self
.context
- .f64_type()
- .const_float(value)
+ .f32_type()
+ .const_float(value as f64)
.as_basic_value_enum(),
Literal::Boolean(value) => self
.context
@@ -372,7 +372,7 @@ impl<'ctx> Codegen<'ctx> {
let value = self.codegen_expr(value).unwrap();
let value_ptr = unsafe {
self.builder.build_gep(
- i32_type,
+ element_type,
inner_ptr,
&[i32_type.const_int(idx as u64, false)],
"",
@@ -394,24 +394,25 @@ impl<'ctx> Codegen<'ctx> {
let vector_ptr = self.builder.build_malloc(vector_type, "vecptr").unwrap();
// Set the size and capacity values
- let size = self
+ let size_ptr = self
.builder
- .build_struct_gep(vector_type, vector_ptr, 0, "gepvec")
+ .build_struct_gep(vector_type, vector_ptr, 0, "size")
.unwrap();
- self.builder
- .build_store(size, i32_type.const_int(values.len() as u64, false));
-
- let cap = self
+ let cap_ptr = self
.builder
- .build_struct_gep(vector_type, vector_ptr, 1, "gepvec")
+ .build_struct_gep(vector_type, vector_ptr, 1, "cap")
.unwrap();
- self.builder
- .build_store(cap, i32_type.const_int(100, false));
-
let inner = self
.builder
- .build_struct_gep(vector_type, vector_ptr, 2, "gepvec")
+ .build_struct_gep(vector_type, vector_ptr, 2, "inner")
.unwrap();
+
+ self.builder
+ .build_store(size_ptr, i32_type.const_int(values.len() as u64, false));
+
+ self.builder
+ .build_store(cap_ptr, i32_type.const_int(100, false));
+
self.builder.build_store(inner, inner_ptr);
vector_ptr.as_basic_value_enum()
@@ -423,8 +424,8 @@ impl<'ctx> Codegen<'ctx> {
fn type_as_basic_type(&self, typ: Type) -> BasicTypeEnum<'ctx> {
// self.context.i64_type().ptr_type(Address)
match typ {
- Type::Integer => self.context.i64_type().into(),
- Type::Float => self.context.f64_type().into(),
+ Type::Integer => self.context.i32_type().into(),
+ Type::Float => self.context.f32_type().into(),
Type::Boolean => self.context.bool_type().into(),
Type::Array { typ, .. } => {
let i32_type = self.context.i32_type().as_basic_type_enum();
@@ -502,15 +503,15 @@ impl<'ctx> Codegen<'ctx> {
// Writing the logic
let element_type = self.type_as_basic_type(typ);
+ let element_ptr_type = element_type.ptr_type(AddressSpace::default());
+
let i32_type = self.context.i32_type();
let vector_type = self.context.struct_type(
&[
i32_type.as_basic_type_enum(),
i32_type.as_basic_type_enum(),
- element_type
- .ptr_type(AddressSpace::default())
- .as_basic_type_enum(),
+ element_ptr_type.as_basic_type_enum(),
],
false,
);
@@ -538,14 +539,15 @@ impl<'ctx> Codegen<'ctx> {
.builder
.build_load(i32_type, cap_ptr, "cap")
.into_int_value();
+ let inner = self
+ .builder
+ .build_load(element_ptr_type, inner_ptr, "inner")
+ .into_pointer_value();
// Put the new element into backing array
- let slot_ptr = unsafe {
- self.builder
- .build_gep(element_type, inner_ptr, &[size], "slot")
- };
-
let element = func.get_nth_param(1).unwrap();
+ let slot_ptr = unsafe { self.builder.build_gep(element_type, inner, &[size], "slot") };
+
self.builder.build_store(slot_ptr, element);
// TODO: Handle going over capacity
diff --git a/sloth/src/lexer.rs b/sloth/src/lexer.rs
index 128b012..20379a3 100644
--- a/sloth/src/lexer.rs
+++ b/sloth/src/lexer.rs
@@ -114,8 +114,8 @@ pub enum TokenType {
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
- Integer(i64),
- Float(f64),
+ Integer(i32),
+ Float(f32),
Boolean(bool),
Character(char),
String(String),
@@ -249,9 +249,9 @@ impl<'a> Lexer<'a> {
value.push(self.advance());
}
- Literal::Float(value.parse::<f64>().expect("Expected float")).into()
+ Literal::Float(value.parse::<f32>().expect("Expected float")).into()
} else {
- Literal::Integer(value.parse::<i64>().expect("Expected integer")).into()
+ Literal::Integer(value.parse::<i32>().expect("Expected integer")).into()
}
}
diff --git a/sloth/src/parser/ast.rs b/sloth/src/parser/ast.rs
index 1624b0d..66bf851 100644
--- a/sloth/src/parser/ast.rs
+++ b/sloth/src/parser/ast.rs
@@ -276,8 +276,8 @@ impl Display for TypeIdentifier {
#[derive(Debug, Clone, PartialEq)]
pub enum Literal {
- Integer(i64),
- Float(f64),
+ Integer(i32),
+ Float(f32),
Boolean(bool),
Character(char),
String(String),
diff --git a/test.c b/test.c
index 605ed13..c7e7f5c 100644
--- a/test.c
+++ b/test.c
@@ -3,7 +3,7 @@
typedef struct {
int size;
int cap;
- long* inner;
+ int* inner;
} IntVec;
IntVec* test();
@@ -13,14 +13,14 @@ int main() {
int size = (*v).size;
int cap = (*v).cap;
- long* inner = (*v).inner;
+ int* inner = (*v).inner;
printf("%d\n", size);
printf("%d\n", cap);
for (int i = 0; i < size; ++i) {
- long value = inner[i];
- printf("%d ", i);
+ int value = inner[i];
+ printf("%d ", value);
}
puts("\n");
}