Skip to content

Commit

Permalink
compile macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Master-Bw3 committed Jul 25, 2023
1 parent 9b88415 commit 0b63230
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 36 deletions.
61 changes: 41 additions & 20 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, rc::Rc};
use crate::{
interpreter::{mishap::Mishap, ops::EmbedType},
iota::{hex_casting::pattern::PatternIota, Iota},
parser::{AstNode, OpName},
parser::{AstNode, Macros, OpName},
pattern_registry::{PatternRegistry, PatternRegistryExt},
};

Expand All @@ -18,46 +18,65 @@ pub mod ops;

pub fn compile_to_iotas(
node: &AstNode,
pattern_registry: &PatternRegistry,
heap: Option<&mut HashMap<String, i32>>,
pattern_registry: &PatternRegistry,
macros: &Macros,
) -> CompileResult {
let mut empty_heap = HashMap::new();
let mut heap = heap.unwrap_or(&mut empty_heap);
compile_node(&node, &mut heap, 0, pattern_registry)
compile_node(&node, &mut heap, 0, pattern_registry, macros)
}

fn compile_node(
node: &AstNode,
heap: &mut HashMap<String, i32>,
depth: u32,
pattern_registry: &PatternRegistry,
macros: &Macros,
) -> CompileResult {
match node {
AstNode::File(file) => {
let mut result = vec![];
for node in file {
result.append(&mut compile_node(node, heap, depth, pattern_registry)?)
result.append(&mut compile_node(
node,
heap,
depth,
pattern_registry,
macros,
)?)
}
Ok(result)
}

AstNode::Action { line, name, value } => Ok(vec![{
let pattern = pattern_registry
.find(name, value)
.ok_or((Mishap::InvalidPattern, *line))?;

//remove output values used by the interpreter
//once signature generation exists for number, all values can be ignored
let new_value = if pattern.internal_name == "number" || pattern.internal_name == "mask"
{
value.clone()
AstNode::Action { line, name, value } => {
if let Some((_, AstNode::Hex(macro_hex))) = macros.get(name) {
compile_to_iotas(
&AstNode::File(macro_hex.clone()),
Some(heap),
pattern_registry,
macros,
)
} else {
None
};
Rc::new(PatternIota::from_sig(&pattern.signature, new_value, None))
}]),
Ok(vec![{
let pattern = pattern_registry
.find(name, value)
.ok_or((Mishap::InvalidPattern, *line))?;

//remove output values used by the interpreter
//once signature generation exists for number, all values can be ignored
let new_value =
if pattern.internal_name == "number" || pattern.internal_name == "mask" {
value.clone()
} else {
None
};
Rc::new(PatternIota::from_sig(&pattern.signature, new_value, None))
}])
}
}

AstNode::Hex(hex) => compile_hex_node(hex, heap, depth, pattern_registry),
AstNode::Hex(hex) => compile_hex_node(hex, heap, depth, pattern_registry, macros),

AstNode::Op { line, name, arg } => match name {
OpName::Store => compile_op_store(heap, pattern_registry, arg),
Expand Down Expand Up @@ -87,6 +106,7 @@ fn compile_node(
depth,
heap,
pattern_registry,
macros
),
}
}
Expand All @@ -98,14 +118,15 @@ fn compile_hex_node(
heap: &mut HashMap<String, i32>,
mut depth: u32,
pattern_registry: &PatternRegistry,
macros: &Macros
) -> CompileResult {
depth += 1;

let mut result: Vec<Rc<dyn Iota>> = vec![];

let mut inner = vec![];
for node in hex {
inner.append(&mut compile_node(node, heap, depth, pattern_registry)?)
inner.append(&mut compile_node(node, heap, depth, pattern_registry, macros)?)
}

result.push(Rc::new(
Expand Down
37 changes: 26 additions & 11 deletions src/compiler/if_block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::HashMap, rc::Rc};

use crate::{
iota::{Iota, hex_casting::pattern::PatternIota},
parser::AstNode,
iota::{hex_casting::pattern::PatternIota, Iota},
parser::{AstNode, Macros},
pattern_registry::PatternRegistry,
};

Expand All @@ -16,18 +16,31 @@ pub fn compile_if_block(
depth: u32,
heap: &mut HashMap<String, i32>,
pattern_registry: &PatternRegistry,
macros: &Macros,
) -> CompileResult {
let mut result: Vec<Rc<dyn Iota>> = vec![];

//append condition to result
if let AstNode::Hex(condition_hex) = (*condition).clone() {
for node in condition_hex {
result.append(&mut compile_node(&node, heap, depth, pattern_registry)?)
result.append(&mut compile_node(
&node,
heap,
depth,
pattern_registry,
macros,
)?)
}
};

//push success hex to result
result.append(&mut compile_node(succeed, heap, depth, pattern_registry)?);
result.append(&mut compile_node(
succeed,
heap,
depth,
pattern_registry,
macros,
)?);
//push fail hex to result (if there is one)
match fail {
Some(fail_node) => match **fail_node {
Expand All @@ -38,6 +51,7 @@ pub fn compile_if_block(
heap,
depth,
pattern_registry,
macros,
)?);
}
// "if else"
Expand All @@ -52,21 +66,22 @@ pub fn compile_if_block(
heap,
depth,
pattern_registry,
macros,
)?);
result.push(
Rc::new(PatternIota::from_name(pattern_registry, "eval", None, None).unwrap()),
);
result.push(Rc::new(
PatternIota::from_name(pattern_registry, "eval", None, None).unwrap(),
));
}
_ => unreachable!(),
},
None => {
compile_node(&AstNode::Hex(vec![]), heap, depth, pattern_registry)?;
compile_node(&AstNode::Hex(vec![]), heap, depth, pattern_registry, macros)?;
}
}
//push augur's to buffer
result.push(
Rc::new(PatternIota::from_name(pattern_registry, "if", None, None).unwrap()),
);
result.push(Rc::new(
PatternIota::from_name(pattern_registry, "if", None, None).unwrap(),
));

Ok(result)
}
4 changes: 3 additions & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ fn interpret_node<'a>(
calc_buffer_depth(pattern_registry, &Some(buffer.clone())),
&mut state.heap,
pattern_registry,
macros
)?
.iter()
.map(|x| (x.clone(), false))
Expand Down Expand Up @@ -256,8 +257,9 @@ pub fn interpret_action<'a>(
if let Some(ref mut buffer) = state.buffer {
let compiled = compile_to_iotas(
&AstNode::File(macro_hex.clone()),
pattern_registry,
Some(&mut state.heap),
pattern_registry,
macros,
)
.unwrap()
.into_iter()
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ pub fn run() {
let parse_result = parser::parse(&source, &config.great_spell_sigs, &mut config.entities);
let (ast, macros) = match parse_result {
Ok(result) => result,
Err(err) => {eprintln!("{}\n{}", "Parsing Error:".red().bold(), err);
return;
},
Err(err) => {
eprintln!("{}\n{}", "Parsing Error:".red().bold(), err);
return;
}
};

if let Command::Run = args.command {
Expand All @@ -93,7 +94,7 @@ pub fn run() {
};
} else if let Command::Build = args.command {
let pattern_registry = PatternRegistry::construct(&config.great_spell_sigs);
let compile_result = compile_to_iotas(&ast, &pattern_registry, None);
let compile_result = compile_to_iotas(&ast, None, &pattern_registry, &macros);
match compile_result {
// Ok(result) => println!("\nresult: {}", Vector::from(result).display()),
Ok(result) => println!("\nresult: {}", gen_give_cmd(result)),
Expand Down

0 comments on commit 0b63230

Please sign in to comment.