Skip to content

Commit

Permalink
Merge pull request #5 from Master-Bw3/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Master-Bw3 authored Jul 25, 2023
2 parents ea60cf7 + ceed58e commit 61dd33d
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 170 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
72 changes: 46 additions & 26 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 @@ -13,52 +13,70 @@ use self::{
};

pub mod if_block;
pub mod ops;
pub mod nbt;
pub mod ops;

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

let pattern_registry = PatternRegistry::construct(great_sigs);

compile_node(&node, &mut heap, 0, &pattern_registry)
let mut empty_heap = HashMap::new();
let mut heap = heap.unwrap_or(&mut empty_heap);
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 @@ -88,6 +106,7 @@ fn compile_node(
depth,
heap,
pattern_registry,
macros
),
}
}
Expand All @@ -99,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)?;
result.append(&mut 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)
}
12 changes: 8 additions & 4 deletions src/grammar.pest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
File = _{ SOI ~ Hex ~ EOI }

Hex = _{ (( IfBlock | Embed | Op | Var | Action | Term ) ~ Newline? | Newline)* }
Hex = _{ (( Macro | IfBlock | Embed | Op | Var | Action | Term ) ~ Newline? | Newline)* }

IfBlock = {If ~ Then ~ (ElseIf ~ Then ~ Newline? )* ~ Else?}

Expand Down Expand Up @@ -48,17 +48,17 @@ Influence = @{("Null" | "Garbage") ~ !(WHITESPACE? ~ ASCII_ALPHA)}

Bool = @{("True" | "False") ~ !(WHITESPACE? ~ ASCII_ALPHA)}

Pattern = ${ IntroRetro | PatternName | Action }
Pattern = ${ IntroRetro | PatternRaw | Action }

String = @{("\"" ~ ("\\\"" | (!"\"" ~ ANY))* ~ "\"")}

Matrix = !{ "[" ~ "(" ~ Int ~ "," ~ Int ~ ")" ~ ("|" ~ (Number ~ ("," | ";"))* ~ Iota)? ~ "]"}

Int = {ASCII_DIGIT+}

PatternName = ${ PatternDirection ~ " " ~ PatternSignature}
PatternRaw = ${ PatternDirection ~ " " ~ PatternSignature}

PatternDirection = @{("NORTHEAST" | "EAST" | "SOUTHEAST" | "SOUTHWEST" | "WEST" | "NORTHWEST")}
PatternDirection = @{("NORTH_EAST" | "EAST" | "SOUTH_EAST" | "SOUTH_WEST" | "WEST" | "NORTH_WEST" | "NE" | "SE" | "E" |"W" | "NW" | "SW" )}

PatternSignature = @{("q" | "a" | "w" | "e" | "d")+}

Expand All @@ -80,6 +80,10 @@ Op = ${OpName ~ "(" ~ (Iota | Var)? ~ ")"}

OpName = {ASCII_ALPHA_UPPER ~ ASCII_ALPHA_LOWER*}

Macro = {"#define" ~ MacroName ~ "(" ~ PatternRaw ~ ")" ~ (!Newline ~ !Term ~ ANY)* ~ Newline? ~ Term}

MacroName = {(!"(" ~ ANY)*}

WHITESPACE = _{ " " | "\t" }

COMMENT = _{ ("/*" ~ (!"*/" ~ ANY)* ~ "*/") | ("//" ~ (!Newline ~ ANY)*) }
Expand Down
Loading

0 comments on commit 61dd33d

Please sign in to comment.