Skip to content

Commit

Permalink
parse CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Sep 17, 2021
1 parent 91d58fd commit d2704b9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ jobs:
with:
toolchain: stable

- run: cargo run
- run: cargo run examples/hello.cirru
- run: cargo run examples/sum.cirru
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2018"
cirru_parser = "0.1.1"
regex = "1"
lazy_static = "1.4.0"
clap = "3.0.0-beta.4"

[profile.release]
debug = true
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
### Usages

TODO
```bash
cargo install calx-vm
calx hello.cirru
```

it starts with a `main` function:

```cirru
fn main ()
const "|hello world"
echo
```

### Instructions

Expand Down
4 changes: 4 additions & 0 deletions examples/hello.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

fn main ()
const "|hello world"
echo
2 changes: 1 addition & 1 deletion examples/demo.cirru → examples/sum.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn demo (-> i64)
const 0
local.set 0
const 0
loop (i64 -> i64)
loop (i64)
const 1
i.add
dup
Expand Down
45 changes: 35 additions & 10 deletions src/bin/calx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@ use std::fs;
use std::time::Instant;

use cirru_parser::{parse, Cirru};
use clap::{App, Arg};

use calx_vm::{parse_function, Calx, CalxFunc, CalxImportsDict, CalxVM};

fn log2_calx_value(xs: Vec<Calx>) -> Result<Calx, String> {
println!("log: {:?}", xs);
Ok(Calx::Nil)
}

fn main() -> Result<(), String> {
let contents = fs::read_to_string("examples/demo.cirru").expect("Cirru file for instructions");
let matches = App::new("Calx VM")
.version("0.1.0")
.author("Jon Chen <jiyinyiyong@gmail.com>")
.about("A toy VM")
.arg(
Arg::new("SHOW_CODE")
.short('S')
.long("show-code")
.value_name("show-code")
.about("Sets a custom config file")
.takes_value(false),
)
.arg(
Arg::new("SOURCE")
.about("A *.cirru file for loading code")
.required(true)
.index(1),
)
.get_matches();

let source = matches.value_of("SOURCE").unwrap();
let show_code = matches.is_present("SHOW_CODE");

let contents = fs::read_to_string(source).expect("Cirru file for instructions");
let code = parse(&contents).expect("Some Cirru content");

if let Cirru::List(xs) = code {
Expand All @@ -30,10 +49,11 @@ fn main() -> Result<(), String> {
imports.insert(String::from("log2"), (log2_calx_value, 2));

let mut vm = CalxVM::new(fns, vec![], imports);

// for func in vm.funcs.to_owned() {
// println!("loaded fn: {}", func);
// }
if show_code {
for func in vm.funcs.to_owned() {
println!("loaded fn: {}", func);
}
}
let now = Instant::now();

match vm.run() {
Expand All @@ -53,3 +73,8 @@ fn main() -> Result<(), String> {
Err(String::from("TODO not cirru code"))
}
}

fn log2_calx_value(xs: Vec<Calx>) -> Result<Calx, String> {
println!("log: {:?}", xs);
Ok(Calx::Nil)
}
5 changes: 5 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ pub fn parse_block(ptr_base: usize, xs: &[Cirru], looped: bool) -> Result<Vec<Ca
}
}
chunk.push(CalxInstr::BlockEnd);

if looped && !ret_types.is_empty() {
println!("return types for loop actuall not checked: {:?}", ret_types);
}

chunk.insert(
0,
CalxInstr::Block {
Expand Down
32 changes: 13 additions & 19 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,7 @@ impl CalxVM {
}
match self.top_frame.instrs[self.top_frame.pointer].to_owned() {
CalxInstr::Br(size) => {
if self.top_frame.blocks_track.len() <= size {
return Err(format!(
"stack size {} eq/smaller than br size {}",
self.top_frame.blocks_track.len(),
size
));
}

self.shrink_blocks_by(size);
self.shrink_blocks_by(size)?;

let last_idx = self.top_frame.blocks_track.len() - 1;
if self.top_frame.blocks_track[last_idx].looped {
Expand All @@ -83,15 +75,7 @@ impl CalxVM {
CalxInstr::BrIf(size) => {
let v = self.stack_pop()?;
if v == Calx::Bool(true) || v == Calx::I64(1) {
if self.top_frame.blocks_track.len() <= size {
return Err(format!(
"stack size {} eq/smaller than br size {}",
self.top_frame.blocks_track.len(),
size
));
}

self.shrink_blocks_by(size);
self.shrink_blocks_by(size)?;

let last_idx = self.top_frame.blocks_track.len() - 1;
if self.top_frame.blocks_track[last_idx].looped {
Expand Down Expand Up @@ -514,12 +498,22 @@ impl CalxVM {

/// assumed that the size already checked
#[inline(always)]
fn shrink_blocks_by(&mut self, size: usize) {
fn shrink_blocks_by(&mut self, size: usize) -> Result<(), String> {
if self.top_frame.blocks_track.len() <= size {
return Err(format!(
"stack size {} eq/smaller than br size {}",
self.top_frame.blocks_track.len(),
size
));
}

let mut i = size;
while i > 0 {
self.top_frame.blocks_track.pop();
i -= 1;
}

Ok(())
}
}

Expand Down

0 comments on commit d2704b9

Please sign in to comment.