Skip to content

Commit d2704b9

Browse files
committed
parse CLI args
1 parent 91d58fd commit d2704b9

File tree

8 files changed

+73
-32
lines changed

8 files changed

+73
-32
lines changed

.github/workflows/test.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ jobs:
1616
with:
1717
toolchain: stable
1818

19-
- run: cargo run
19+
- run: cargo run examples/hello.cirru
20+
- run: cargo run examples/sum.cirru

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ edition = "2018"
1010
cirru_parser = "0.1.1"
1111
regex = "1"
1212
lazy_static = "1.4.0"
13+
clap = "3.0.0-beta.4"
1314

1415
[profile.release]
1516
debug = true

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
55
### Usages
66

7-
TODO
7+
```bash
8+
cargo install calx-vm
9+
calx hello.cirru
10+
```
11+
12+
it starts with a `main` function:
13+
14+
```cirru
15+
fn main ()
16+
const "|hello world"
17+
echo
18+
```
819

920
### Instructions
1021

examples/hello.cirru

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
fn main ()
3+
const "|hello world"
4+
echo

examples/demo.cirru renamed to examples/sum.cirru

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn demo (-> i64)
1717
const 0
1818
local.set 0
1919
const 0
20-
loop (i64 -> i64)
20+
loop (i64)
2121
const 1
2222
i.add
2323
dup

src/bin/calx.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,35 @@ use std::fs;
33
use std::time::Instant;
44

55
use cirru_parser::{parse, Cirru};
6+
use clap::{App, Arg};
67

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

9-
fn log2_calx_value(xs: Vec<Calx>) -> Result<Calx, String> {
10-
println!("log: {:?}", xs);
11-
Ok(Calx::Nil)
12-
}
13-
1410
fn main() -> Result<(), String> {
15-
let contents = fs::read_to_string("examples/demo.cirru").expect("Cirru file for instructions");
11+
let matches = App::new("Calx VM")
12+
.version("0.1.0")
13+
.author("Jon Chen <jiyinyiyong@gmail.com>")
14+
.about("A toy VM")
15+
.arg(
16+
Arg::new("SHOW_CODE")
17+
.short('S')
18+
.long("show-code")
19+
.value_name("show-code")
20+
.about("Sets a custom config file")
21+
.takes_value(false),
22+
)
23+
.arg(
24+
Arg::new("SOURCE")
25+
.about("A *.cirru file for loading code")
26+
.required(true)
27+
.index(1),
28+
)
29+
.get_matches();
30+
31+
let source = matches.value_of("SOURCE").unwrap();
32+
let show_code = matches.is_present("SHOW_CODE");
33+
34+
let contents = fs::read_to_string(source).expect("Cirru file for instructions");
1635
let code = parse(&contents).expect("Some Cirru content");
1736

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

3251
let mut vm = CalxVM::new(fns, vec![], imports);
33-
34-
// for func in vm.funcs.to_owned() {
35-
// println!("loaded fn: {}", func);
36-
// }
52+
if show_code {
53+
for func in vm.funcs.to_owned() {
54+
println!("loaded fn: {}", func);
55+
}
56+
}
3757
let now = Instant::now();
3858

3959
match vm.run() {
@@ -53,3 +73,8 @@ fn main() -> Result<(), String> {
5373
Err(String::from("TODO not cirru code"))
5474
}
5575
}
76+
77+
fn log2_calx_value(xs: Vec<Calx>) -> Result<Calx, String> {
78+
println!("log: {:?}", xs);
79+
Ok(Calx::Nil)
80+
}

src/parser.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ pub fn parse_block(ptr_base: usize, xs: &[Cirru], looped: bool) -> Result<Vec<Ca
327327
}
328328
}
329329
chunk.push(CalxInstr::BlockEnd);
330+
331+
if looped && !ret_types.is_empty() {
332+
println!("return types for loop actuall not checked: {:?}", ret_types);
333+
}
334+
330335
chunk.insert(
331336
0,
332337
CalxInstr::Block {

src/vm.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,7 @@ impl CalxVM {
6161
}
6262
match self.top_frame.instrs[self.top_frame.pointer].to_owned() {
6363
CalxInstr::Br(size) => {
64-
if self.top_frame.blocks_track.len() <= size {
65-
return Err(format!(
66-
"stack size {} eq/smaller than br size {}",
67-
self.top_frame.blocks_track.len(),
68-
size
69-
));
70-
}
71-
72-
self.shrink_blocks_by(size);
64+
self.shrink_blocks_by(size)?;
7365

7466
let last_idx = self.top_frame.blocks_track.len() - 1;
7567
if self.top_frame.blocks_track[last_idx].looped {
@@ -83,15 +75,7 @@ impl CalxVM {
8375
CalxInstr::BrIf(size) => {
8476
let v = self.stack_pop()?;
8577
if v == Calx::Bool(true) || v == Calx::I64(1) {
86-
if self.top_frame.blocks_track.len() <= size {
87-
return Err(format!(
88-
"stack size {} eq/smaller than br size {}",
89-
self.top_frame.blocks_track.len(),
90-
size
91-
));
92-
}
93-
94-
self.shrink_blocks_by(size);
78+
self.shrink_blocks_by(size)?;
9579

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

515499
/// assumed that the size already checked
516500
#[inline(always)]
517-
fn shrink_blocks_by(&mut self, size: usize) {
501+
fn shrink_blocks_by(&mut self, size: usize) -> Result<(), String> {
502+
if self.top_frame.blocks_track.len() <= size {
503+
return Err(format!(
504+
"stack size {} eq/smaller than br size {}",
505+
self.top_frame.blocks_track.len(),
506+
size
507+
));
508+
}
509+
518510
let mut i = size;
519511
while i > 0 {
520512
self.top_frame.blocks_track.pop();
521513
i -= 1;
522514
}
515+
516+
Ok(())
523517
}
524518
}
525519

0 commit comments

Comments
 (0)