[WIP] Callable graph
This commit is contained in:
parent
072653935e
commit
040ab13911
5 changed files with 143 additions and 95 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -250,6 +250,7 @@ dependencies = [
|
|||
"itertools",
|
||||
"lazy_static",
|
||||
"parse_int",
|
||||
"petgraph",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -24,4 +24,5 @@ parse_int = "0.6.0"
|
|||
indexmap = "1.9.1"
|
||||
lazy_static = "1.4.0"
|
||||
dependency-graph = "0.1.5"
|
||||
petgraph = "0.6.2"
|
||||
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
: test1 40 ;
|
||||
: test2 test1 2 ;
|
||||
|
||||
1 1 = debug
|
||||
if 69 else 42 then debug
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use anyhow::{bail, Result};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use indexmap::IndexSet;
|
||||
use itertools::Itertools;
|
||||
use lazy_static::lazy_static;
|
||||
use petgraph::Graph;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::parser;
|
||||
|
@ -9,31 +10,6 @@ use crate::parser;
|
|||
mod instruction;
|
||||
pub use crate::compiler::instruction::Instruction;
|
||||
|
||||
pub trait Compilable<T, U> {
|
||||
fn compile(&self, data: &T) -> Result<U>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Word {
|
||||
pub id: usize,
|
||||
pub instructions: Vec<Instruction>,
|
||||
pub times_used: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Condition {
|
||||
pub if_instructions: Vec<Instruction>,
|
||||
pub else_instructions: Vec<Instruction>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Data {
|
||||
pub words: HashMap<String, Word>,
|
||||
// pub word_graph:
|
||||
pub conditions: IndexSet<Condition>,
|
||||
pub strings: IndexSet<String>,
|
||||
}
|
||||
|
||||
pub const TEMPLATE_ASM: &str = include_str!("compiler/template.asm");
|
||||
|
||||
lazy_static! {
|
||||
|
@ -45,12 +21,50 @@ lazy_static! {
|
|||
.body;
|
||||
}
|
||||
|
||||
pub trait Compilable<T, U> {
|
||||
fn compile(&self, data: &T) -> Result<U>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Word {
|
||||
pub id: usize,
|
||||
pub instructions: Vec<Instruction>,
|
||||
pub times_used: usize,
|
||||
pub callable_graph_node: petgraph::graph::NodeIndex,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Condition {
|
||||
pub if_instructions: Vec<Instruction>,
|
||||
pub else_instructions: Vec<Instruction>,
|
||||
pub callable_graph_node: petgraph::graph::NodeIndex,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CallableId {
|
||||
Word(String),
|
||||
Condition(usize),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Data {
|
||||
// pub words: HashMap<String, Word>,
|
||||
// pub conditions: IndexSet<Condition>,
|
||||
pub strings: IndexSet<String>,
|
||||
pub callable_graph: Graph<CallableId, ()>,
|
||||
pub words: HashMap<String, Word>,
|
||||
pub conditions: Vec<Condition>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub fn default() -> Self {
|
||||
Self {
|
||||
words: HashMap::new(),
|
||||
conditions: IndexSet::new(),
|
||||
// words: HashMap::new(),
|
||||
// conditions: IndexSet::new(),
|
||||
strings: IndexSet::new(),
|
||||
callable_graph: Graph::new(),
|
||||
words: HashMap::new(),
|
||||
conditions: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,28 +118,55 @@ impl Data {
|
|||
bail!("Word already exists as user word definition: {}", name);
|
||||
}
|
||||
|
||||
let ins = self.generate_instructions(body, optimize)?;
|
||||
|
||||
let origin = self
|
||||
.callable_graph
|
||||
.add_node(CallableId::Word(name.to_string()));
|
||||
self.words.insert(
|
||||
name,
|
||||
name.to_string(),
|
||||
Word {
|
||||
id: self.words.len(),
|
||||
instructions: ins,
|
||||
instructions: vec![],
|
||||
times_used: 0,
|
||||
callable_graph_node: origin,
|
||||
},
|
||||
);
|
||||
let ins = self.generate_instructions(body, optimize)?;
|
||||
self.words
|
||||
.get_mut(&name)
|
||||
.context(format!("Could not get word: {}", name))?
|
||||
.instructions = ins.clone();
|
||||
|
||||
for instruction in ins {
|
||||
match instruction {
|
||||
Instruction::Call(x) => {
|
||||
self.callable_graph.add_edge(
|
||||
origin,
|
||||
self.words
|
||||
.get(&x)
|
||||
.context(format!("Could not get referenced word: {}", x))?
|
||||
.callable_graph_node,
|
||||
(),
|
||||
);
|
||||
}
|
||||
Instruction::Condition(x) => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
dbg!(&self);
|
||||
}
|
||||
parser::ast::Node::Condition { if_body, else_body } => {
|
||||
let if_instructions = self.generate_instructions(if_body, optimize)?;
|
||||
let else_instructions = self.generate_instructions(else_body, optimize)?;
|
||||
let id = self
|
||||
.conditions
|
||||
.insert_full(Condition {
|
||||
if_instructions,
|
||||
else_instructions,
|
||||
})
|
||||
.0;
|
||||
let id = self.conditions.len();
|
||||
self.conditions.push(Condition {
|
||||
if_instructions,
|
||||
else_instructions,
|
||||
callable_graph_node: self
|
||||
.callable_graph
|
||||
.add_node(CallableId::Condition(id)),
|
||||
});
|
||||
instructions.push(Instruction::Condition(id));
|
||||
self.callable_graph.add_node(CallableId::Condition(id));
|
||||
}
|
||||
parser::ast::Node::Word(x) => {
|
||||
if let Some(ins) = Instruction::from_word(&x) {
|
||||
|
@ -159,42 +200,42 @@ impl Data {
|
|||
}
|
||||
|
||||
// conditions
|
||||
for (id, c) in self.conditions.iter().enumerate() {
|
||||
x.push(hence::parser::ast::Node::Label(format!(
|
||||
"conditions_if_{}",
|
||||
id
|
||||
)));
|
||||
x.extend(c.if_instructions.iter().map(|ins| ins.compile(self)).collect::<Result<Vec<_>>>()?.into_iter().flatten());
|
||||
x.push(hence::parser::ast::Node::Label(format!("conditions_else_{}", id)));
|
||||
x.extend(c.else_instructions.iter().map(|ins| ins.compile(self)).collect::<Result<Vec<_>>>()?.into_iter().flatten());
|
||||
}
|
||||
// for (id, c) in self.conditions.iter().enumerate() {
|
||||
// x.push(hence::parser::ast::Node::Label(format!(
|
||||
// "conditions_if_{}",
|
||||
// id
|
||||
// )));
|
||||
// x.extend(c.if_instructions.iter().map(|ins| ins.compile(self)).collect::<Result<Vec<_>>>()?.into_iter().flatten());
|
||||
// x.push(hence::parser::ast::Node::Label(format!("conditions_else_{}", id)));
|
||||
// x.extend(c.else_instructions.iter().map(|ins| ins.compile(self)).collect::<Result<Vec<_>>>()?.into_iter().flatten());
|
||||
// }
|
||||
|
||||
// words
|
||||
for (name, word) in &self
|
||||
.words
|
||||
.iter()
|
||||
.filter(|(_, w)| w.times_used > 1)
|
||||
.sorted_by(|a, b| Ord::cmp(&a.1.id, &b.1.id))
|
||||
.collect::<Vec<_>>()
|
||||
{
|
||||
x.extend(vec![
|
||||
hence::parser::ast::Node::Label(format!("words_{}", word.id)),
|
||||
hence::parser::ast::Node::Comment(format!("word: \"{}\"", name)),
|
||||
]);
|
||||
x.extend(
|
||||
word.instructions
|
||||
.iter()
|
||||
.map(|ins| ins.compile(self))
|
||||
.collect::<Result<Vec<hence::parser::ast::Body>>>()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.flatten(),
|
||||
);
|
||||
x.push(hence::parser::ast::Node::MacroCall {
|
||||
name: "return_call_stack_jump".to_string(),
|
||||
args: vec![],
|
||||
});
|
||||
}
|
||||
// for (name, word) in &self
|
||||
// .words
|
||||
// .iter()
|
||||
// .filter(|(_, w)| w.times_used > 1)
|
||||
// .sorted_by(|a, b| Ord::cmp(&a.1.id, &b.1.id))
|
||||
// .collect::<Vec<_>>()
|
||||
// {
|
||||
// x.extend(vec![
|
||||
// hence::parser::ast::Node::Label(format!("words_{}", word.id)),
|
||||
// hence::parser::ast::Node::Comment(format!("word: \"{}\"", name)),
|
||||
// ]);
|
||||
// x.extend(
|
||||
// word.instructions
|
||||
// .iter()
|
||||
// .map(|ins| ins.compile(self))
|
||||
// .collect::<Result<Vec<hence::parser::ast::Body>>>()
|
||||
// .unwrap()
|
||||
// .into_iter()
|
||||
// .flatten(),
|
||||
// );
|
||||
// x.push(hence::parser::ast::Node::MacroCall {
|
||||
// name: "return_call_stack_jump".to_string(),
|
||||
// args: vec![],
|
||||
// });
|
||||
// }
|
||||
|
||||
x.extend([
|
||||
hence::parser::ast::Node::Label("main".to_string()),
|
||||
|
|
|
@ -64,14 +64,15 @@ pub enum Instruction {
|
|||
Times,
|
||||
Divide,
|
||||
Mod,
|
||||
Call(String),
|
||||
AsmQuote(String),
|
||||
Condition(usize),
|
||||
|
||||
Multiple {
|
||||
instruction: Box<Self>,
|
||||
count: usize,
|
||||
},
|
||||
|
||||
Call(String),
|
||||
Condition(usize),
|
||||
}
|
||||
|
||||
impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruction {
|
||||
|
@ -1222,25 +1223,26 @@ impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruct
|
|||
},
|
||||
]),
|
||||
Instruction::Mod => Ok(vec![]),
|
||||
Instruction::Call(x) => match data.words.get(x) {
|
||||
Some(w) => {
|
||||
if w.times_used > 1 {
|
||||
Ok(vec![hence::parser::ast::Node::MacroCall {
|
||||
name: "call_stack_jump".to_string(),
|
||||
args: vec![hence::arg::Arg::Variable(format!("words_{}", w.id)), hence::arg::Arg::Variable("OFFSET".to_string())],
|
||||
}])
|
||||
} else {
|
||||
Ok(w.instructions
|
||||
.iter()
|
||||
.map(|ins| ins.compile(data))
|
||||
.collect::<Result<Vec<hence::parser::ast::Body>>>()?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
None => bail!("Unknown word: {}", x),
|
||||
},
|
||||
// Instruction::Call(x) => match data.words.get(x) {
|
||||
// Some(w) => {
|
||||
// if w.times_used > 1 {
|
||||
// Ok(vec![hence::parser::ast::Node::MacroCall {
|
||||
// name: "call_stack_jump".to_string(),
|
||||
// args: vec![hence::arg::Arg::Variable(format!("words_{}", w.id)), hence::arg::Arg::Variable("OFFSET".to_string())],
|
||||
// }])
|
||||
// } else {
|
||||
// Ok(w.instructions
|
||||
// .iter()
|
||||
// .map(|ins| ins.compile(data))
|
||||
// .collect::<Result<Vec<hence::parser::ast::Body>>>()?
|
||||
// .into_iter()
|
||||
// .flatten()
|
||||
// .collect())
|
||||
// }
|
||||
// }
|
||||
// None => bail!("Unknown word: {}", x),
|
||||
// },
|
||||
Instruction::Call(x) => bail!("Unknown word: {}", x),
|
||||
Instruction::AsmQuote(x) => Ok(hence::parser::parse(hence::lexer::lex(x)?)?.body),
|
||||
Instruction::Condition(x) => Ok(vec![]),
|
||||
|
||||
|
|
Loading…
Reference in a new issue