WIP graph

This commit is contained in:
Dominic Grimm 2022-10-26 11:17:19 +02:00
parent e020b8b78a
commit b607b6f8ed
No known key found for this signature in database
GPG key ID: 77C3333A8FAD191A
7 changed files with 109 additions and 83 deletions

View file

@ -73,7 +73,7 @@ impl Data {
origin,
self.words
.get(&x)
.context(format!("Could not get referenced word: {}", x))?
.context(format!("Could not get already resolved referenced word: {}", x))?
.callable_graph_node,
(),
);
@ -83,12 +83,15 @@ impl Data {
origin,
self.conditions
.get(x)
.context(format!("Could not get referenced condition: {}", x))?
.context(format!("Could not get already resolved referenced condition: {}", x))?
.callable_graph_node,
(),
);
}
Instruction::Multiple { instruction, count: _ } => {
Instruction::Multiple {
instruction,
count: _,
} => {
self.add_graph_edge(origin, *instruction)?;
}
_ => {}
@ -142,7 +145,7 @@ impl Data {
});
}
parser::ast::Node::Number(x) => {
instructions.push(instruction::Instruction::Push(x as u16));
instructions.push(instruction::Instruction::Push(x));
}
parser::ast::Node::WordDefinition {
name,

View file

@ -7,7 +7,7 @@ pub enum Instruction {
Nop,
Debug,
Quit,
Push(u16),
Push(i32),
Drop,
Depth,
Pick,
@ -63,7 +63,7 @@ pub enum Instruction {
TwoMinus,
Times,
Divide,
Mod,
Mod,
AsmQuote(String),
Multiple {
@ -92,7 +92,7 @@ impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruct
}]),
Instruction::Push(x) => Ok(vec![hence::parser::ast::Node::Call {
name: "push".to_string(),
arg: Some(hence::arg::Arg::Number(*x)),
arg: Some(hence::arg::Arg::Number(*x as u16)),
}]),
Instruction::Drop => Ok(vec![hence::parser::ast::Node::Call {
name: "pop".to_string(),
@ -1244,7 +1244,12 @@ impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruct
// },
Instruction::Call(x) => bail!("Unknown word: {}", x),
Instruction::AsmQuote(x) => Ok(hence::parser::parse(hence::lexer::lex(x)?)?.body),
Instruction::Condition(x) => Ok(vec![]),
// Instruction::Condition(x) => Ok(vec![]),
Instruction::Condition(x) => {
dbg!(x);
Ok(vec![])
}
Instruction::Multiple { instruction, count } => {
if *count == 0 {
@ -1255,6 +1260,18 @@ impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruct
match **instruction {
Instruction::Nop => Ok(Instruction::Nop.compile(data)?),
Instruction::Quit => Ok(Instruction::Quit.compile(data)?),
Instruction::Push(x) => Ok([
Instruction::Push(x).compile(data)?,
[hence::parser::ast::Node::Call {
name: "tls".to_string(),
arg: None,
}]
.into_iter()
.cycle()
.take(count - 1)
.collect(),
]
.concat()),
Instruction::Depth => Ok([
Instruction::Depth.compile(data)?,
Instruction::Multiple {
@ -1371,7 +1388,7 @@ impl compiler::Compilable<compiler::Data, hence::parser::ast::Body> for Instruct
arg: None,
},
]),
_ => bail!("Remainder of count % 3 cannot be higher than 3"),
x => bail!("Remainder of count % 3 cannot be higher than 3: {}", x),
},
Instruction::Nip => Ok([
vec![hence::parser::ast::Node::MacroCall {

View file

@ -61,4 +61,3 @@ return_call_stack_jump:
tsr CORE_REG_C
get
tlr CORE_REG_PC

View file

@ -4,8 +4,8 @@ use itertools::Itertools;
#[derive(Debug)]
pub enum Token {
Newline(String),
Whitespace(String),
Newline(usize),
Whitespace(usize),
ParenComment(String),
BackslashComment(String),
@ -19,7 +19,8 @@ pub enum Token {
impl ToCode for Token {
fn to_code(&self) -> String {
match self {
Token::Newline(x) | Token::Whitespace(x) => x.clone(),
Token::Newline(x) => ["\n"].into_iter().cycle().take(*x).join(""),
Token::Whitespace(x) => [" "].into_iter().cycle().take(*x).join(""),
Token::ParenComment(x) => format!("( {})", x),
Token::BackslashComment(x) => format!("\\{}", x),
Token::DoubleDashComment(x) => format!("-- {}", x),
@ -37,21 +38,19 @@ pub fn lex(source: &str) -> Result<Vec<Token>> {
let mut chars = source.chars().peekable();
let mut tokens: Vec<Token> = vec![];
while let Some(&c) = chars.peek() {
while let Some(c) = chars.peek() {
tokens.push(match c {
'\n' => Token::Newline(chars.peeking_take_while(|&c| c == '\n').collect()),
'\n' => Token::Newline(chars.peeking_take_while(|&c| c == '\n').count()),
_ if c.is_whitespace() => {
Token::Whitespace(chars.peeking_take_while(|&c| c.is_whitespace()).collect())
}
'\\' => {
chars.next();
Token::BackslashComment(chars.peeking_take_while(|&c| c != '\n').collect())
Token::Whitespace(chars.peeking_take_while(|&c| c.is_whitespace()).count())
}
'\\' => Token::BackslashComment(chars.peeking_take_while(|&c| c != '\n').collect()),
_ if c.is_numeric() => {
Token::Number(chars.peeking_take_while(|&c| !is_space(c)).collect())
}
_ => {
let x: String = chars.peeking_take_while(|&c| !is_space(c)).collect();
let mut iter = x.chars();
match x.as_str() {
"(" => Token::ParenComment(
@ -64,6 +63,17 @@ pub fn lex(source: &str) -> Result<Vec<Token>> {
mode: x.chars().take(x.len() - 1).collect(),
string: chars.by_ref().skip(1).take_while(|&c| c != '"').collect(),
},
_ if iter.next() == Some('-') => {
if let Some(c) = iter.next() {
if c.is_numeric() {
Token::Number(x)
} else {
Token::Word(x)
}
} else {
Token::Word(x)
}
}
_ => Token::Word(x),
}
}