Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19 vector primitive datatype #23

Merged
merged 4 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/bytecode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,40 @@ Perform fast arithmetic on two loaded items.

---

### `vec_op ! [vector_function, [arguments, ...]?]`
Perform native manipulation on a Vector primitive. Requires a function name, and optionally, arguments.

Available interfaces:
* `vec_op [idx]`
Will retrieve an element at `idx`.
```
int 1
int 2
int 3
make_vec
vec_op [1]
printn * > Outputs 2
```
* `vec_op reverse`
Reverses the vector
* `vec_op mut idx`
Mutate the vector by replacing the `Primitive` at `idx` with the item on top of the local operating stack.
```
int 5
int 10
make_vec

int 20
vec_op mut 0
printn * > Outputs [20, 10]
```

| ! | Reason |
| - | - |
| 1 | Each vector function can fail uniquely. |

---

### `nop`
No operation.

Expand Down
28 changes: 28 additions & 0 deletions src/bytecode/bin/test.mmm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function add_n
end

function main
bool false
if
int 50
call src/bytecode/bin/test.mmm#add_n
store add_fifty
Expand All @@ -30,6 +32,32 @@ function main

printn *

void
endif

call src/bytecode/bin/test.mmm#test_vecs

void
ret
end

function test_vecs
int 5
int 0
string "Hello, World!"
float 3.14159
constexpr 0b101
make_vector

printn *

int 10
vec_op mut 0

printn *

stack_dump

void
ret
end
27 changes: 20 additions & 7 deletions src/bytecode/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::Cell, fmt::Debug, sync::Arc};
use std::{cell::Cell, fmt::Debug, sync::Arc, collections::VecDeque};

use anyhow::{bail, Result};

Expand All @@ -12,7 +12,7 @@ use super::{
};

pub struct Ctx<'a> {
stack: Vec<Primitive>,
stack: VecDeque<Primitive>,
function: &'a Function,
call_stack: Arc<Cell<Stack>>,
pub active_if_stmts: Vec<(IfStatement, bool)>,
Expand All @@ -35,7 +35,7 @@ impl<'a> Ctx<'a> {
callback_state: Option<Arc<VariableMapping>>,
) -> Self {
Self {
stack: vec![],
stack: VecDeque::new(),
active_if_stmts: vec![],
function,
call_stack,
Expand Down Expand Up @@ -77,12 +77,21 @@ impl<'a> Ctx<'a> {
self.stack.get_mut(n)
}

pub fn get_last_op_item(&mut self) -> Option<&Primitive> {
self.stack.get(self.stack_size() - 1)
}

pub fn get_last_op_item_mut(&mut self) -> Option<&mut Primitive> {
let last_idx = self.stack_size() - 1;
self.stack.get_mut(last_idx)
}

pub fn get_call_stack(&self) -> &mut Stack {
use std::borrow::BorrowMut;
unsafe { (*self.call_stack.as_ptr()).borrow_mut() }
}

pub fn get_local_operating_stack(&self) -> Vec<Primitive> {
pub fn get_local_operating_stack(&self) -> VecDeque<Primitive> {
self.stack.clone()
}

Expand Down Expand Up @@ -116,19 +125,23 @@ impl<'a> Ctx<'a> {

pub(crate) fn clear_and_set_stack(&mut self, var: Primitive) {
self.stack.clear();
self.stack.push(var);
self.stack.push_back(var);
}

pub(crate) fn stack_size(&self) -> usize {
self.stack.len()
}

pub(crate) fn push(&mut self, var: Primitive) {
self.stack.push(var);
self.stack.push_back(var);
}

pub(crate) fn pop_front(&mut self) -> Option<Primitive> {
self.stack.pop_front()
}

pub(crate) fn pop(&mut self) -> Option<Primitive> {
self.stack.pop()
self.stack.pop_back()
}

pub(crate) fn register_variable(&self, name: String, var: Primitive) {
Expand Down
20 changes: 10 additions & 10 deletions src/bytecode/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ impl<'a> Function {
.expect("this if has not been mapped.");

let IfStatement::If(..) = if_stmt else {
bail!("expected if statment, found {if_stmt:?}");
};
bail!("expected if statment, found {if_stmt:?}");
};

let next = *if_stmt.next_pos();

Expand Down Expand Up @@ -296,18 +296,18 @@ pub struct Functions {

impl<'a> Functions {
pub fn get(&self, signature: &str) -> Result<&Function> {
let Some(result) = self.map.get(signature) else {
bail!("unknown function ({signature})");
};

let result = self
.map
.get(signature)
.with_context(|| format!("unknown function ({signature})"))?;
Ok(result)
}

pub fn get_mut(&'a mut self, signature: &str) -> Result<&'a mut Function> {
let Some(result) = self.map.get_mut(signature) else {
bail!("unknown function ({signature})");
};

let result = self
.map
.get_mut(signature)
.with_context(|| format!("unknown function ({signature})"))?;
Ok(result)
}
}
Expand Down
Loading