figured out the output/input stuff better
This commit is contained in:
parent
053aceec4d
commit
d9b4dcd984
2 changed files with 157 additions and 56 deletions
|
@ -3,11 +3,11 @@
|
||||||
// See README.md and COPYING for details.
|
// See README.md and COPYING for details.
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use synfx_dsp_jit::ASTNode;
|
|
||||||
use crate::blocklang::*;
|
use crate::blocklang::*;
|
||||||
|
use synfx_dsp_jit::ASTNode;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct JASTNode {
|
struct JASTNode {
|
||||||
|
@ -51,17 +51,18 @@ impl ASTNodeRef {
|
||||||
pub fn walk_dump(&self, input: &str, output: &str, indent: usize) -> String {
|
pub fn walk_dump(&self, input: &str, output: &str, indent: usize) -> String {
|
||||||
let indent_str = " ".repeat(indent + 1);
|
let indent_str = " ".repeat(indent + 1);
|
||||||
|
|
||||||
let out_port =
|
let out_port = if output.len() > 0 { format!("(out: {})", output) } else { "".to_string() };
|
||||||
if output.len() > 0 { format!("(out: {})", output) }
|
let in_port = if input.len() > 0 { format!("(in: {})", input) } else { "".to_string() };
|
||||||
else { "".to_string() };
|
|
||||||
let in_port =
|
|
||||||
if input.len() > 0 { format!("(in: {})", input) }
|
|
||||||
else { "".to_string() };
|
|
||||||
|
|
||||||
let mut s = format!(
|
let mut s = format!(
|
||||||
"{}{}#{}[{}] {}{}\n",
|
"{}{}#{}[{}] {}{}\n",
|
||||||
indent_str, self.0.borrow().id, self.0.borrow().typ,
|
indent_str,
|
||||||
self.0.borrow().lbl, out_port, in_port);
|
self.0.borrow().id,
|
||||||
|
self.0.borrow().typ,
|
||||||
|
self.0.borrow().lbl,
|
||||||
|
out_port,
|
||||||
|
in_port
|
||||||
|
);
|
||||||
|
|
||||||
for (inp, out, n) in &self.0.borrow().nodes {
|
for (inp, out, n) in &self.0.borrow().nodes {
|
||||||
s += &n.walk_dump(&inp, &out, indent + 1);
|
s += &n.walk_dump(&inp, &out, indent + 1);
|
||||||
|
@ -75,14 +76,75 @@ type BlkASTRef = Rc<RefCell<BlkASTNode>>;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum BlkASTNode {
|
enum BlkASTNode {
|
||||||
Root { child: BlkASTRef },
|
Root {
|
||||||
Area { childs: Vec<BlkASTRef> },
|
child: BlkASTRef,
|
||||||
Set { var: String, expr: BlkASTRef },
|
},
|
||||||
Get { id: usize, use_count: usize, var: String, expr: BlkASTRef },
|
Area {
|
||||||
Node { id: usize, use_count: usize, typ: String, lbl: String, childs: Vec<BlkASTRef> },
|
childs: Vec<BlkASTRef>,
|
||||||
|
},
|
||||||
|
Set {
|
||||||
|
var: String,
|
||||||
|
expr: BlkASTRef,
|
||||||
|
},
|
||||||
|
Get {
|
||||||
|
id: usize,
|
||||||
|
use_count: usize,
|
||||||
|
var: String,
|
||||||
|
},
|
||||||
|
Node {
|
||||||
|
id: usize,
|
||||||
|
out: Option<String>,
|
||||||
|
use_count: usize,
|
||||||
|
typ: String,
|
||||||
|
lbl: String,
|
||||||
|
childs: Vec<(Option<String>, BlkASTRef)>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlkASTNode {
|
impl BlkASTNode {
|
||||||
|
pub fn dump(&self, indent: usize, inp: Option<&str>) -> String {
|
||||||
|
let mut indent_str = " ".repeat(indent + 1);
|
||||||
|
|
||||||
|
if let Some(inp) = inp {
|
||||||
|
indent_str += &format!("{}<= ", inp);
|
||||||
|
}
|
||||||
|
|
||||||
|
match self {
|
||||||
|
BlkASTNode::Root { child } => {
|
||||||
|
format!("{}* Root\n", indent_str) + &child.borrow().dump(indent + 1, None)
|
||||||
|
}
|
||||||
|
BlkASTNode::Area { childs } => {
|
||||||
|
let mut s = format!("{}* Area\n", indent_str);
|
||||||
|
for c in childs.iter() {
|
||||||
|
s += &c.borrow().dump(indent + 1, None);
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
BlkASTNode::Set { var, expr } => {
|
||||||
|
format!("{}set '{}'=\n", indent_str, var) + &expr.borrow().dump(indent + 1, None)
|
||||||
|
}
|
||||||
|
BlkASTNode::Get { id, use_count, var } => {
|
||||||
|
format!("{}get '{}' (id={}, use={})\n", indent_str, var, id, use_count)
|
||||||
|
}
|
||||||
|
BlkASTNode::Node { id, out, use_count, typ, lbl, childs } => {
|
||||||
|
let lbl = if *typ == *lbl { "".to_string() } else { format!("[{}]", lbl) };
|
||||||
|
|
||||||
|
let mut s = if let Some(out) = out {
|
||||||
|
format!(
|
||||||
|
"{}{}{} (id={}/{}, use={})\n",
|
||||||
|
indent_str, typ, lbl, id, out, use_count
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!("{}{}{} (id={}, use={})\n", indent_str, typ, lbl, id, use_count)
|
||||||
|
};
|
||||||
|
for (inp, c) in childs.iter() {
|
||||||
|
s += &format!("{}", c.borrow().dump(indent + 1, inp.as_ref().map(|s| &s[..])));
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_root(child: BlkASTRef) -> BlkASTRef {
|
pub fn new_root(child: BlkASTRef) -> BlkASTRef {
|
||||||
Rc::new(RefCell::new(BlkASTNode::Root { child }))
|
Rc::new(RefCell::new(BlkASTNode::Root { child }))
|
||||||
}
|
}
|
||||||
|
@ -95,12 +157,28 @@ impl BlkASTNode {
|
||||||
Rc::new(RefCell::new(BlkASTNode::Set { var: var.to_string(), expr }))
|
Rc::new(RefCell::new(BlkASTNode::Set { var: var.to_string(), expr }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_node(id: usize, typ: &str, lbl: &str, childs: Vec<BlkASTRef>) -> BlkASTRef {
|
pub fn new_get(id: usize, var: &str) -> BlkASTRef {
|
||||||
Rc::new(RefCell::new(BlkASTNode::Node { id, typ: typ.to_string(), lbl: lbl.to_string(), use_count: 1, childs }))
|
Rc::new(RefCell::new(BlkASTNode::Get { id, var: var.to_string(), use_count: 1 }))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_node(
|
||||||
|
id: usize,
|
||||||
|
out: Option<String>,
|
||||||
|
typ: &str,
|
||||||
|
lbl: &str,
|
||||||
|
childs: Vec<(Option<String>, BlkASTRef)>,
|
||||||
|
) -> BlkASTRef {
|
||||||
|
Rc::new(RefCell::new(BlkASTNode::Node {
|
||||||
|
id,
|
||||||
|
out,
|
||||||
|
typ: typ.to_string(),
|
||||||
|
lbl: lbl.to_string(),
|
||||||
|
use_count: 1,
|
||||||
|
childs,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum BlkJITCompileError {
|
pub enum BlkJITCompileError {
|
||||||
UnknownError,
|
UnknownError,
|
||||||
|
@ -117,12 +195,14 @@ pub struct Block2JITCompiler {
|
||||||
|
|
||||||
impl Block2JITCompiler {
|
impl Block2JITCompiler {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self { id_node_map: HashMap::new() }
|
||||||
id_node_map: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trans2bjit(&self, node: &ASTNodeRef) -> Result<BlkASTRef, BlkJITCompileError> {
|
pub fn trans2bjit(
|
||||||
|
&self,
|
||||||
|
node: &ASTNodeRef,
|
||||||
|
my_out: Option<String>,
|
||||||
|
) -> Result<BlkASTRef, BlkJITCompileError> {
|
||||||
// TODO: Deal with multiple outputs.
|
// TODO: Deal with multiple outputs.
|
||||||
// If we encounter a node with multiple outputs, assign each output
|
// If we encounter a node with multiple outputs, assign each output
|
||||||
// to a temporary variable and save that.
|
// to a temporary variable and save that.
|
||||||
|
@ -145,8 +225,9 @@ impl Block2JITCompiler {
|
||||||
|
|
||||||
match &node.0.borrow().typ[..] {
|
match &node.0.borrow().typ[..] {
|
||||||
"<r>" => {
|
"<r>" => {
|
||||||
if let Some(first) = node.first_child_ref() {
|
if let Some((_in, out, first)) = node.first_child() {
|
||||||
let child = self.trans2bjit(&first)?;
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
|
let child = self.trans2bjit(&first, out)?;
|
||||||
Ok(BlkASTNode::new_root(child))
|
Ok(BlkASTNode::new_root(child))
|
||||||
} else {
|
} else {
|
||||||
Err(BlkJITCompileError::BadTree(node.clone()))
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
||||||
|
@ -156,8 +237,9 @@ impl Block2JITCompiler {
|
||||||
let mut childs = vec![];
|
let mut childs = vec![];
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while let Some((_in, _out, child)) = node.nth_child(i) {
|
while let Some((_in, out, child)) = node.nth_child(i) {
|
||||||
let child = self.trans2bjit(&child)?;
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
|
let child = self.trans2bjit(&child, out)?;
|
||||||
childs.push(child);
|
childs.push(child);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
@ -168,39 +250,58 @@ impl Block2JITCompiler {
|
||||||
// TODO: handle results properly, like remembering the most recent result
|
// TODO: handle results properly, like remembering the most recent result
|
||||||
// and append it to the end of the statements block. so that a temporary
|
// and append it to the end of the statements block. so that a temporary
|
||||||
// variable is created.
|
// variable is created.
|
||||||
if let Some(first) = node.first_child_ref() {
|
if let Some((_in, out, first)) = node.first_child() {
|
||||||
self.trans2bjit(&first)
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
|
self.trans2bjit(&first, out)
|
||||||
} else {
|
} else {
|
||||||
Err(BlkJITCompileError::BadTree(node.clone()))
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"set" => {
|
"set" => {
|
||||||
if let Some(first) = node.first_child_ref() {
|
if let Some((_in, out, first)) = node.first_child() {
|
||||||
let expr = self.trans2bjit(&first)?;
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
|
let expr = self.trans2bjit(&first, out)?;
|
||||||
Ok(BlkASTNode::new_set(&node.0.borrow().lbl, expr))
|
Ok(BlkASTNode::new_set(&node.0.borrow().lbl, expr))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Err(BlkJITCompileError::BadTree(node.clone()))
|
Err(BlkJITCompileError::BadTree(node.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"get" => Ok(BlkASTNode::new_get(node.0.borrow().id, &node.0.borrow().lbl)),
|
||||||
optype => {
|
optype => {
|
||||||
let mut childs = vec![];
|
let mut childs = vec![];
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while let Some((_in, _out, child)) = node.nth_child(i) {
|
while let Some((inp, out, child)) = node.nth_child(i) {
|
||||||
let child = self.trans2bjit(&child)?;
|
let out = if out.len() > 0 { Some(out) } else { None };
|
||||||
childs.push(child);
|
|
||||||
|
let child = self.trans2bjit(&child, out)?;
|
||||||
|
if inp.len() > 0 {
|
||||||
|
childs.push((Some(inp.to_string()), child));
|
||||||
|
} else {
|
||||||
|
childs.push((None, child));
|
||||||
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
|
||||||
|
// TODO: Check here if the optype has multiple outputs.
|
||||||
|
// when it has, make a sub-collection of statements
|
||||||
|
// and make temporary variables with ::Set
|
||||||
|
// then return the output with a final ::Get to the
|
||||||
|
// output "my_out".
|
||||||
|
// If no output is given in "my_out" it's an error!
|
||||||
|
//""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
|
||||||
// TODO: Reorder the childs/arguments according to the input
|
// TODO: Reorder the childs/arguments according to the input
|
||||||
// order in the BlockLanguage
|
// order in the BlockLanguage
|
||||||
|
|
||||||
Ok(BlkASTNode::new_node(
|
Ok(BlkASTNode::new_node(
|
||||||
node.0.borrow().id,
|
node.0.borrow().id,
|
||||||
|
my_out,
|
||||||
&node.0.borrow().typ,
|
&node.0.borrow().typ,
|
||||||
&node.0.borrow().lbl,
|
&node.0.borrow().lbl,
|
||||||
childs))
|
childs,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -209,8 +310,8 @@ impl Block2JITCompiler {
|
||||||
let tree = fun.generate_tree::<ASTNodeRef>("zero").unwrap();
|
let tree = fun.generate_tree::<ASTNodeRef>("zero").unwrap();
|
||||||
println!("{}", tree.walk_dump("", "", 0));
|
println!("{}", tree.walk_dump("", "", 0));
|
||||||
|
|
||||||
let blkast = self.trans2bjit(&tree);
|
let blkast = self.trans2bjit(&tree, None);
|
||||||
println!("R: {:#?}", blkast);
|
println!("R: {}", blkast.unwrap().borrow().dump(0, None));
|
||||||
|
|
||||||
Err(BlkJITCompileError::UnknownError)
|
Err(BlkJITCompileError::UnknownError)
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,19 +26,19 @@ fn check_blocklang_1() {
|
||||||
{
|
{
|
||||||
let mut block_fun = block_fun.lock().expect("matrix lock");
|
let mut block_fun = block_fun.lock().expect("matrix lock");
|
||||||
|
|
||||||
block_fun.instanciate_at(0, 0, 0, "get", Some("in1".to_string()));
|
// block_fun.instanciate_at(0, 0, 0, "get", Some("in1".to_string()));
|
||||||
block_fun.instanciate_at(0, 0, 1, "value", Some("0.3".to_string()));
|
// block_fun.instanciate_at(0, 0, 1, "value", Some("0.3".to_string()));
|
||||||
block_fun.instanciate_at(0, 1, 0, "+", None);
|
// block_fun.instanciate_at(0, 1, 0, "+", None);
|
||||||
block_fun.instanciate_at(0, 2, 0, "set", Some("&sig1".to_string()));
|
// block_fun.instanciate_at(0, 2, 0, "set", Some("&sig1".to_string()));
|
||||||
|
//
|
||||||
block_fun.instanciate_at(0, 3, 0, "get", Some("in1".to_string()));
|
// block_fun.instanciate_at(0, 3, 0, "get", Some("in1".to_string()));
|
||||||
block_fun.instanciate_at(0, 3, 1, "get", Some("in2".to_string()));
|
// block_fun.instanciate_at(0, 3, 1, "get", Some("in2".to_string()));
|
||||||
block_fun.instanciate_at(0, 4, 0, "-", None);
|
// block_fun.instanciate_at(0, 4, 0, "-", None);
|
||||||
block_fun.instanciate_at(0, 5, 0, "->3", None);
|
// block_fun.instanciate_at(0, 5, 0, "->3", None);
|
||||||
block_fun.instanciate_at(0, 6, 1, "set", Some("*a".to_string()));
|
// block_fun.instanciate_at(0, 6, 1, "set", Some("*a".to_string()));
|
||||||
block_fun.instanciate_at(0, 6, 2, "set", Some("x".to_string()));
|
// block_fun.instanciate_at(0, 6, 2, "set", Some("x".to_string()));
|
||||||
block_fun.instanciate_at(0, 6, 0, "->", None);
|
// block_fun.instanciate_at(0, 6, 0, "->", None);
|
||||||
block_fun.instanciate_at(0, 7, 0, "->2", None);
|
// block_fun.instanciate_at(0, 7, 0, "->2", None);
|
||||||
|
|
||||||
block_fun.instanciate_at(0, 0, 3, "get", Some("in1".to_string()));
|
block_fun.instanciate_at(0, 0, 3, "get", Some("in1".to_string()));
|
||||||
block_fun.instanciate_at(0, 0, 4, "get", Some("in2".to_string()));
|
block_fun.instanciate_at(0, 0, 4, "get", Some("in2".to_string()));
|
||||||
|
|
Loading…
Reference in a new issue