Skip to content

Commit

Permalink
macro embedding stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Master-Bw3 committed Jul 25, 2023
1 parent 0b63230 commit 5e75927
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 60 deletions.
43 changes: 43 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,49 @@ if {<\True>} then {
...
}
Hermes' Gambit //evaluates the 'then' branch
```

### Macros
Macros are defined using this syntax:
```
#define Macro Name (DIRECTION signature) ... {
...
}
```
As an example:
```
#define Duplicate Thrice (SOUTH_EAST edd) = num, num -> num {
Numerical Reflection: 3
Gemini Gambit
}
```

Note: everything between the signature and first curly bracket is ignored, so the following is also valid:
```
#define Duplicate Thrice (SOUTH_EAST edd)
{
Numerical Reflection: 3
Gemini Gambit
}
```

When macros are used in a hex, they get expanded, not evaluated.
```
Mind's Reflection
Duplicate Thrice //expand a macro
{
Duplicate Thrice //expand a macro into a list
}
<\Duplicate Thrice> //embed the pattern associated with the macro
<Duplicate Thrice> //will cause a mishap
```

## Config
Expand Down
15 changes: 9 additions & 6 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn interpret_node<'a>(
Ok(state)
}
AstNode::Op { name, arg, line } => {
interpret_op(name, arg, state, pattern_registry).map_err(|err| (err, line))
interpret_op(name, arg, state, pattern_registry, macros).map_err(|err| (err, line))
}
AstNode::IfBlock {
condition,
Expand All @@ -145,7 +145,7 @@ fn interpret_node<'a>(
calc_buffer_depth(pattern_registry, &Some(buffer.clone())),
&mut state.heap,
pattern_registry,
macros
macros,
)?
.iter()
.map(|x| (x.clone(), false))
Expand Down Expand Up @@ -183,6 +183,7 @@ pub fn interpret_op<'a>(
arg: Option<OpValue>,
state: &'a mut State,
pattern_registry: &PatternRegistry,
macros: &Macros,
) -> Result<&'a mut State, Mishap> {
if state.consider_next {
return Err(Mishap::OpCannotBeConsidered);
Expand Down Expand Up @@ -228,15 +229,17 @@ pub fn interpret_op<'a>(
crate::parser::OpName::Store => store(&arg, state, false),
crate::parser::OpName::Copy => store(&arg, state, true),
crate::parser::OpName::Push => push(&arg, state),
crate::parser::OpName::Embed => embed(&arg, state, pattern_registry, EmbedType::Normal),
crate::parser::OpName::Embed => {
embed(&arg, state, pattern_registry, EmbedType::Normal, macros)
}
crate::parser::OpName::SmartEmbed => {
embed(&arg, state, pattern_registry, EmbedType::Smart)
embed(&arg, state, pattern_registry, EmbedType::Smart, macros)
}
crate::parser::OpName::ConsiderEmbed => {
embed(&arg, state, pattern_registry, EmbedType::Consider)
embed(&arg, state, pattern_registry, EmbedType::Consider, macros)
}
crate::parser::OpName::IntroEmbed => {
embed(&arg, state, pattern_registry, EmbedType::IntroRetro)
embed(&arg, state, pattern_registry, EmbedType::IntroRetro, macros)
}
}?;
}
Expand Down
5 changes: 3 additions & 2 deletions src/interpreter/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
Iota,
},
parser::OpValue,
parser::{OpValue, Macros},
pattern_registry::{PatternRegistry, PatternRegistryExt},
};

Expand Down Expand Up @@ -149,6 +149,7 @@ pub fn embed<'a>(
state: &'a mut State,
pattern_registry: &PatternRegistry,
embed_type: EmbedType,
macros: &Macros
) -> Result<(), Mishap> {
let val = match value {
Some(val) => val,
Expand All @@ -167,7 +168,7 @@ pub fn embed<'a>(
*pat.value.clone(),
state,
pattern_registry,
&HashMap::new(),
macros,
None,
)
.map_err(|err| err.0)?;
Expand Down
2 changes: 2 additions & 0 deletions src/parse_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn parse_library(library: &mut Map<String, Value>, config: &mut Config) {
.unwrap(),
&PatternRegistry::construct(&config.great_spell_sigs),
&mut config.entities,
&HashMap::new()
);
contents.insert(Signature::from_sig(key), iota);
}
Expand Down Expand Up @@ -129,6 +130,7 @@ fn parse_entity(entity: &Map<String, Value>, config: &mut Config) {
pair,
&PatternRegistry::construct(&config.great_spell_sigs),
&mut config.entities,
&HashMap::new()
)
});

Expand Down
Loading

0 comments on commit 5e75927

Please sign in to comment.