Skip to content

Commit

Permalink
vector
Browse files Browse the repository at this point in the history
  • Loading branch information
playXE committed May 13, 2019
1 parent 2d68a63 commit 1ab6401
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/gccjit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,8 @@ impl<'a> Codegen<'a>
{
let lval = self.expr_to_lvalue(lval).unwrap();
let rval = self.gen_expr(rval);
self.cur_block.unwrap().add_assignment(None, lval, rval);
let casted_val = self.ctx.new_cast(None, rval, lval.to_rvalue().get_type());
self.cur_block.unwrap().add_assignment(None, lval, casted_val);

self.ctx.new_rvalue_zero(self.ctx.new_type::<i32>()) // todo: something better than this?
}
Expand Down
11 changes: 6 additions & 5 deletions src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::Context;
use super::*;
use crate::ast::*;
use colored::Colorize;
use std::collections::HashSet;


pub struct SemCheck<'a>
{
Expand All @@ -20,7 +20,8 @@ pub struct SemCheck<'a>
types: HashMap<NodeId, Type>,
aliases: HashMap<Name, Type>,
imported: HashMap<Name,Elem>,
imported_funs: HashMap<Name,Vec<Function>>
imported_funs: HashMap<Name,Vec<Function>>,

}


Expand Down Expand Up @@ -206,8 +207,8 @@ impl<'a> SemCheck<'a>
}
else
{

format!("{}/{}", self.ctx.file.root, import)
let path = std::path::Path::new(&self.ctx.file.root);
format!("{}/{}", path.parent().unwrap().display(), import)
};

let mut file = File {
Expand Down Expand Up @@ -241,7 +242,7 @@ impl<'a> SemCheck<'a>
Elem::Func(f) =>
{

if f.public && !f.static_

{
let funs = self.imported_funs.get(&f.name).clone();
if funs.is_none() {
Expand Down
103 changes: 103 additions & 0 deletions std/vector.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import "libc.jazz"

constexpr VECTOR_MIN_CAP = 2 as usize
constexpr VECTOR_GROWTH_FACTOR = 2 as usize
constexpr VECTOR_SHRINK_TRESHOLD = (1 / 4) as usize
constexpr VECTOR_ERROR = -1
constexpr VECTOR_SUCCESS = 0

pub struct Vector {
size: usize,
capacity: usize,
element_size: usize,
data: *u8
}

pub struct VectorIterator {
pointer: *u8,
element_size: usize
}

pub func vector_setup(vector: *Vector,capacity: usize,element_size: usize) i32 {



vector.size = 0 as usize;
if VECTOR_MIN_CAP > capacity {
vector.capacity = VECTOR_MIN_CAP;
} else {
vector.capacity = capacity;
}

vector.element_size = element_size;
vector.data = malloc(vector.capacity * vector.element_size);

return VECTOR_SUCCESS;
}

pub func vector_push_back(vector: *Vector,element: *u8) void {
if _vector_should_grow(vector) {
_vector_adjust_capacity(vector);
}
_vector_assign(vector,vector.size,element);
vector.size = vector.size + 1;
return;
}

pub func vector_pop_back(vector: *Vector) *u8 {
vector.size = vector.size - 1;

return &vector.data[vector.size];
}

pub func vector_byte_size(vector: *Vector) usize {
return vector.element_size * vector.size;
}

pub func _vector_should_grow(vector: *Vector) bool {
return vector.size == vector.capacity;
}

pub func _vector_offset(vector: *Vector,index: usize) *u8 {
var data = (vector.data)
return &(data + (index * vector.element_size));
}

pub func _vector_assign(vector: *Vector,index: usize,element: *u8) void {
var offset = _vector_offset(vector,index);
memcpy(offset,element,vector.element_size);

return;
}

pub func _vector_reallocate(vector: *Vector,new_cap: usize) void {
var new_cap_in_bytes: usize;


if new_cap < VECTOR_MIN_CAP {
if vector.capacity > VECTOR_MIN_CAP {
new_cap = VECTOR_MIN_CAP;
} else {
return;
}
}

new_cap_in_bytes = new_cap * vector.element_size;
var old = vector.data;
vector.data = malloc(new_cap_in_bytes);

memcpy(vector.data,old,vector.size * vector.element_size);
return;
}

pub func _vector_adjust_capacity(vector: *Vector) void {
var reloc_size: usize;

if vector.size * VECTOR_GROWTH_FACTOR == 1 {
reloc_size = 1;
} else {
vector.size * VECTOR_GROWTH_FACTOR
}
_vector_reallocate(vector,reloc_size,)
return;
}

0 comments on commit 1ab6401

Please sign in to comment.