Skip to content

Commit cf569b5

Browse files
committed
extra stack check for block; tag 0.1.4
1 parent 629287a commit cf569b5

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

.github/workflows/publish.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
- run: cargo run -- -S examples/sum.cirru
1919
- run: cargo run -- -S examples/assert.cirru
2020
- run: cargo run -- -S examples/nested.cirru
21+
- run: cargo run -- -S examples/named.cirru
22+
- run: cargo run -- --emit-binary target/a.calx examples/named.cirru && cargo run -- --eval-binary target/a.calx
2123

2224
# - run: cargo test
2325

.github/workflows/test.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- uses: actions/checkout@v2
2121
- uses: actions-rs/toolchain@v1
2222
with:
23-
toolchain: stable
23+
toolchain: nightly
2424
components: clippy
2525
override: true
2626

@@ -35,3 +35,8 @@ jobs:
3535
with:
3636
token: ${{ secrets.GITHUB_TOKEN }}
3737
args: --all-features
38+
39+
- uses: actions-rs/clippy-check@v1
40+
with:
41+
token: ${{ secrets.GITHUB_TOKEN }}
42+
args: --all-features

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "calx_vm"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
55
edition = "2018"
66
license = "MIT"

src/parser.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ fn parse_local_idx(x: &Cirru, collector: &mut LocalsCollector) -> Result<usize,
271271
}
272272
None => Err(String::from("invalid empty name")),
273273
},
274-
Cirru::List(_) => {
275-
return Err(format!("expected token, got {}", x));
276-
}
274+
Cirru::List(_) => Err(format!("expected token, got {}", x)),
277275
}
278276
}
279277

@@ -358,7 +356,7 @@ pub fn parse_fn_types(xs: &Cirru, collector: &mut LocalsCollector) -> Result<(Ve
358356
if &**t == "->" {
359357
ret_mode = true;
360358
} else {
361-
let ty = parse_type_name(&**t)?;
359+
let ty = parse_type_name(t)?;
362360
if ret_mode {
363361
returns.push(ty);
364362
} else {
@@ -382,7 +380,7 @@ pub fn parse_fn_types(xs: &Cirru, collector: &mut LocalsCollector) -> Result<(Ve
382380
Cirru::List(_) => return Err(format!("invalid syntax, expected name, got {:?}", x)),
383381
};
384382
let ty = match &zs[1] {
385-
Cirru::Leaf(s) => parse_type_name(&**s)?,
383+
Cirru::Leaf(s) => parse_type_name(s)?,
386384
Cirru::List(_) => return Err(format!("invalid syntax, expected type, got {:?}", x)),
387385
};
388386
collector.track(&name_str);
@@ -410,7 +408,7 @@ pub fn parse_block_types(xs: &Cirru) -> Result<(Vec<CalxType>, Vec<CalxType>), S
410408
if &**t == "->" {
411409
ret_mode = true;
412410
} else {
413-
let ty = parse_type_name(&**t)?;
411+
let ty = parse_type_name(t)?;
414412
if ret_mode {
415413
returns.push(ty);
416414
} else {

src/primes.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,28 @@ pub enum Calx {
1919
// Link(Box<Calx>, Box<Calx>, Box<Calx>),
2020
}
2121

22-
#[derive(Debug, Clone, PartialEq, PartialOrd, Decode, Encode)]
22+
impl Calx {
23+
// for runtime type checking
24+
pub fn typed_as(&self, t: CalxType) -> bool {
25+
match self {
26+
Calx::Nil => t == CalxType::Nil,
27+
Calx::Bool(_) => t == CalxType::Bool,
28+
Calx::I64(_) => t == CalxType::I64,
29+
Calx::F64(_) => t == CalxType::F64,
30+
Calx::Str(_) => t == CalxType::Str,
31+
Calx::List(_) => t == CalxType::List,
32+
// Calx::Link(_, _, _) => t == CalxType::Link,
33+
}
34+
}
35+
}
36+
37+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Decode, Encode)]
2338
pub enum CalxType {
2439
Nil,
2540
Bool,
2641
I64,
2742
F64,
43+
Str,
2844
List,
2945
Link,
3046
}
@@ -193,7 +209,7 @@ impl CalxError {
193209
}
194210
}
195211

196-
#[derive(Debug, Clone, PartialEq, PartialOrd)]
212+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
197213
pub struct BlockData {
198214
pub looped: bool,
199215
pub ret_types: Vec<CalxType>,

src/vm.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::primes::{BlockData, Calx, CalxError, CalxFrame, CalxFunc, CalxInstr};
1+
use crate::primes::{BlockData, Calx, CalxError, CalxFrame, CalxFunc, CalxInstr, CalxType};
22
use std::collections::hash_map::HashMap;
33
use std::fmt;
44
use std::ops::Rem;
@@ -118,7 +118,7 @@ impl CalxVM {
118118
to,
119119
initial_stack_size: self.stack.len() - params_types.len(),
120120
});
121-
println!("TODO check params type: {:?}", params_types);
121+
self.check_stack_for_block(&params_types)?;
122122
}
123123
CalxInstr::BlockEnd(looped) => {
124124
if looped {
@@ -391,9 +391,6 @@ impl CalxVM {
391391
ret_types: f.ret_types,
392392
};
393393

394-
// TODO check params type
395-
println!("TODO check args: {:?}", f.params_types);
396-
397394
// start in new frame
398395
continue;
399396
}
@@ -579,6 +576,20 @@ impl CalxVM {
579576
Ok(())
580577
}
581578

579+
/// checks is given parameters on stack top
580+
fn check_stack_for_block(&self, params: &[CalxType]) -> Result<(), CalxError> {
581+
if self.stack.len() < params.len() {
582+
return Err(self.gen_err(format!("stack size does not match given params: {:?} {:?}", self.stack, params)));
583+
}
584+
for (idx, t) in params.iter().enumerate() {
585+
if self.stack[self.stack.len() - params.len() - 1 + idx].typed_as(t.to_owned()) {
586+
continue;
587+
}
588+
return Err(self.gen_err(format!("stack type does not match given params: {:?} {:?}", self.stack, params)));
589+
}
590+
Ok(())
591+
}
592+
582593
#[inline(always)]
583594
fn check_func_return(&mut self) -> Result<(), CalxError> {
584595
if self.stack.len() != self.top_frame.initial_stack_size + self.top_frame.ret_types.len() {

0 commit comments

Comments
 (0)