Skip to content

Commit

Permalink
try stupid nested expression; bump 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Sep 18, 2021
1 parent 1f8e10a commit bd4253d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ jobs:
with:
toolchain: stable

- run: cargo run examples/hello.cirru
- run: cargo run examples/sum.cirru
- run: cargo run examples/assert.cirru
- run: cargo run -- -S examples/hello.cirru
- run: cargo run -- -S examples/sum.cirru
- run: cargo run -- -S examples/assert.cirru
- run: cargo run -- -S examples/nested.cirru

# - run: cargo test

Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
with:
toolchain: stable

- run: cargo run examples/hello.cirru
- run: cargo run examples/sum.cirru
- run: cargo run examples/assert.cirru
- run: cargo run -- -S examples/hello.cirru
- run: cargo run -- -S examples/sum.cirru
- run: cargo run -- -S examples/assert.cirru
- run: cargo run -- -S examples/nested.cirru
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calx_vm"
version = "0.1.0"
version = "0.1.1"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ calx -S hello.cirru
calx -D hello.cirru
```

### Syntax Sugar

Code of:

```cirru
fn main ()
i.add
const 1
i.mul
const 2
const 3
echo
dup
assert "|expected 7"
i.eq
const 7
```

is desugared to:

```cirru
fn main ()
const 2
const 3
i.mul
const 1
i.add
dup
echo
const 7
i.eq
assert "|expected 7"
```

### Instructions

Highly inspired by:
Expand Down
14 changes: 14 additions & 0 deletions examples/nested.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

fn main ()
i.add
const 1
i.mul
const 2
const 3

echo
dup

assert "|expected 7"
i.eq
const 7
54 changes: 50 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ pub fn parse_function(nodes: &[Cirru]) -> Result<CalxFunc, String> {
let mut ptr_base: usize = 0;
for (idx, line) in nodes.iter().enumerate() {
if idx >= 3 {
let instrs = parse_instr(ptr_base, line)?;
for expanded in extract_nested(line)? {
// println!("expanded {}", expanded);
let instrs = parse_instr(ptr_base, &expanded)?;

for instr in instrs {
ptr_base += 1;
body.push(instr);
for instr in instrs {
ptr_base += 1;
body.push(instr);
}
}
}
}
Expand Down Expand Up @@ -423,3 +426,46 @@ pub fn parse_types(xs: &Cirru) -> Result<(Vec<CalxType>, Vec<CalxType>), String>
}
}
}

/// rather stupid function to extract nested calls before current call
/// TODO better have some tests
pub fn extract_nested(xs: &Cirru) -> Result<Vec<Cirru>, String> {
match xs {
Cirru::Leaf(x) => Err(format!("not extracting leaf: {}", x)),
Cirru::List(ys) => match ys.get(0) {
None => Err(String::from("unexpected empty expression")),
Some(Cirru::List(zs)) => Err(format!("unexpected nested instruction name: {:?}", zs)),
Some(Cirru::Leaf(zs)) => match zs.as_str() {
"block" | "loop" => {
let mut chunk: Vec<Cirru> = vec![Cirru::Leaf(zs.to_string())];
for (idx, y) in ys.iter().enumerate() {
if idx > 0 {
for e in extract_nested(y)? {
chunk.push(e);
}
}
}
Ok(vec![Cirru::List(chunk)])
}
_ => {
let mut pre: Vec<Cirru> = vec![];
let mut chunk: Vec<Cirru> = vec![Cirru::Leaf(zs.to_string())];
for (idx, y) in ys.iter().enumerate() {
if idx > 0 {
match y {
Cirru::Leaf(_) => chunk.push(y.to_owned()),
Cirru::List(_) => {
for e in extract_nested(y)? {
pre.push(e);
}
}
}
}
}
pre.push(Cirru::List(chunk));
Ok(pre)
}
},
},
}
}

0 comments on commit bd4253d

Please sign in to comment.