Skip to content

Commit

Permalink
Add auto names for registered structs and remove Bool wrapper
Browse files Browse the repository at this point in the history
Also fixed registered objects possibly having a size not divisable by 4
  • Loading branch information
GsLogimaker committed Dec 13, 2023
1 parent 1ce55b6 commit abc46d7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 73 deletions.
15 changes: 7 additions & 8 deletions qu/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::import::QuRegistered;
use crate::import::QuStruct;
use crate::import::ClassId;
use crate::import::Registerer;
use crate::objects::Bool;
use crate::parser::KEYWORD_BOOL_FALSE;
use crate::parser::KEYWORD_BOOL_TRUE;
use crate::parser::KEYWORD_IF;
Expand All @@ -37,7 +36,7 @@ use std::mem::size_of;
use std::hash::Hash;
use std::sync::RwLock;

pub(crate) const CONSTRUCTOR_NAME:&str = ".new";
pub const CONSTRUCTOR_NAME:&str = ".new";
// TODO: Fix compiler's documentation

// TODO: Make bank store which definitions obj the mappings are for
Expand Down Expand Up @@ -2249,17 +2248,17 @@ pub struct QuCompiler {
definitions: &mut Definitions,
) -> Result<QuAsmBuilder, QuMsg> {

// WARNING: Value takes an isize, which can be 4 or 8 bytes, but Bool is only 4 bytes
// TODO: Add proper way of adding Bool
// WARNING: Value takes an isize, which can be 4 or 8 bytes, but bool is only 4 bytes
// TODO: Add proper way of adding bool
if value.slice == KEYWORD_BOOL_TRUE {
let mut b = QuAsmBuilder::new();
b.add_op(Value(1, output_reg));
b.return_type = definitions.class_id::<Bool>()?;
b.return_type = definitions.class_id::<bool>()?;
return Ok(b);
} else if value.slice == KEYWORD_BOOL_FALSE {
let mut b = QuAsmBuilder::new();
b.add_op(Value(0, output_reg));
b.return_type = definitions.class_id::<Bool>()?;
b.return_type = definitions.class_id::<bool>()?;
return Ok(b);
}

Expand Down Expand Up @@ -2373,7 +2372,7 @@ pub struct QuCompiler {
condition,
QuStackId::new(
expr_reg.index(),
definitions.class_id::<Bool>()?,
definitions.class_id::<bool>()?,
),
definitions
)
Expand Down Expand Up @@ -2418,7 +2417,7 @@ pub struct QuCompiler {
condition,
QuStackId::new(
if_expr_reg.index(),
definitions.class_id::<Bool>()?,
definitions.class_id::<bool>()?,
),
definitions
)
Expand Down
19 changes: 16 additions & 3 deletions qu/src/import.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

use std::alloc::Layout;
use std::collections::HashMap;
use std::fmt::Debug;
use std::mem::size_of;
Expand Down Expand Up @@ -27,12 +28,18 @@ pub struct ArgsAPI<'a> {
pub(crate) out_id: QuStackId,
} impl<'a> ArgsAPI<'a> {
/// Gets a reference to the value of the function argument at `index`.
pub fn get<T:QuRegisterStruct>(&self, index:usize) -> Result<&T, QuMsg> {
pub fn get<T: QuRegisterStruct + 'static>(
&self,
index:usize,
) -> Result<&T, QuMsg> {
self.vm.read::<T>(self.arg_ids[index])
}

/// Sets the return value of the function to `value`.
pub fn set<T:QuRegisterStruct>(&mut self, value:T) {
pub fn set<T: QuRegisterStruct + 'static>(
&mut self,
value:T,
) {
self.vm.write::<T>(self.out_id, value);
}

Expand Down Expand Up @@ -287,10 +294,16 @@ pub struct QuStruct {
) -> Self {
let name = name.into();
assert!(size < u8::MAX as usize);
let aligned_size = if size != 0 {
Layout::from_size_align(size, 4)
.unwrap()
.pad_to_align()
.size()
} else { 0 };
Self {
name,
register_fn: fn_registerer,
size: size as u8,
size: aligned_size as u8,
constants_map: Default::default(),
external_functions_map: Default::default(),
functions_map: Default::default(),
Expand Down
30 changes: 18 additions & 12 deletions qu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,17 @@ pub struct Qu<'a> {

#[cfg(test)]
mod lib {
use crate::{Qu, Module, Bool};
use crate::{Qu, Module, QuRegisterStruct};

// TODO: Test what happens when a function overrides a class name
// TODO: Test what happens when a class constructor that doesn't exist is called

#[test]
fn names() {
dbg!(bool::name());
let x = 0;
}

#[test]
fn constants() {
let mut qu = Qu::new();
Expand All @@ -295,16 +301,16 @@ mod lib {
fn bool_literals() {
let mut qu = Qu::new();

let result:bool = qu.run_and_get::<Bool>("
let result:bool = *qu.run_and_get::<bool>("
var value bool = true
return value
").unwrap().to_owned().into();
").unwrap();
assert_eq!(result, true);

let result:bool = qu.run_and_get::<Bool>("
let result:bool = *qu.run_and_get::<bool>("
var value bool = false
return value
").unwrap().to_owned().into();
").unwrap();
assert_eq!(result, false);
}

Expand All @@ -327,19 +333,19 @@ mod lib {
").unwrap();
assert_eq!(result, 1+1+0+0);

let result:bool = qu.run_and_get::<Bool>("
let result:bool = *qu.run_and_get::<bool>("
return bool()
").unwrap().to_owned().into();
").unwrap();
assert_eq!(result, false);

let result:bool = qu.run_and_get::<Bool>("
let result:bool = *qu.run_and_get::<bool>("
return bool(0==0)
").unwrap().to_owned().into();
").unwrap();
assert_eq!(result, true);

let result:bool = qu.run_and_get::<Bool>("
let result:bool = *qu.run_and_get::<bool>("
return bool(1)
").unwrap().to_owned().into();
").unwrap();
assert_eq!(result, true);
}

Expand Down Expand Up @@ -634,7 +640,7 @@ mod lib {
#[test]
fn variable_dot_notation_2() {
let mut qu = Qu::new();
let result:Bool = *qu.run_and_get("
let result:bool = *qu.run_and_get("
var count int = 5
return count.add(1) == add(count, 1)
").unwrap();
Expand Down
Loading

0 comments on commit abc46d7

Please sign in to comment.