aboutsummaryrefslogtreecommitdiffstats
path: root/src/vm.rs
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-01-15 13:59:27 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-01-15 16:58:08 +0100
commite85c4984dbca6cb1bf579accf7d0ad694e619eb8 (patch)
treee0c07242fedb174bd24bc44fd9c217aabba2eb0a /src/vm.rs
parenta3ccc100e0dbc99ace7507694e4934bec2458d6e (diff)
downloadsylt-e85c4984dbca6cb1bf579accf7d0ad694e619eb8.tar.gz
parse function types in function types in...
Diffstat (limited to 'src/vm.rs')
-rw-r--r--src/vm.rs24
1 files changed, 12 insertions, 12 deletions
diff --git a/src/vm.rs b/src/vm.rs
index eb4de31..daf2ead 100644
--- a/src/vm.rs
+++ b/src/vm.rs
@@ -22,7 +22,8 @@ pub enum Value {
Int(i64),
Bool(bool),
String(Rc<String>),
- Function(usize, Rc<Block>),
+ Function(Vec<Type>, Type, Rc<Block>),
+ Nil,
}
impl Debug for Value {
@@ -32,7 +33,8 @@ impl Debug for Value {
Value::Int(i) => write!(fmt, "(int {})", i),
Value::Bool(b) => write!(fmt, "(bool {})", b),
Value::String(s) => write!(fmt, "(string \"{}\")", s),
- Value::Function(arity, block) => write!(fmt, "(func {}-{})", block.name, arity),
+ Value::Function(args, ret, block) => write!(fmt, "(fn {}: {:?} -> {:?})", block.name, args, ret),
+ Value::Nil => write!(fmt, "(nil)"),
}
}
}
@@ -75,9 +77,6 @@ pub enum Op {
#[derive(Debug)]
pub struct Block {
- pub args: Vec<Type>,
- pub ret: Type,
-
pub name: String,
pub file: PathBuf,
pub ops: Vec<Op>,
@@ -89,9 +88,6 @@ pub struct Block {
impl Block {
pub fn new(name: &str, file: &Path, line: usize) -> Self {
Self {
- args: Vec::new(),
- ret: Type::UnkownType,
-
name: String::from(name),
file: file.to_owned(),
ops: Vec::new(),
@@ -105,6 +101,10 @@ impl Block {
(self.file.clone(), self.line)
}
+ pub fn last_op(&self) -> Option<&Op> {
+ self.ops.last()
+ }
+
pub fn add_line(&mut self, token_position: usize) {
if token_position != self.last_line_offset {
self.line_offsets.insert(self.curr(), token_position);
@@ -228,7 +228,7 @@ impl VM {
}
pub fn run(&mut self, block: Rc<Block>) -> Result<(), Error>{
- if let Err(err) = crate::typer::VM::new().print_ops(true).typecheck(Rc::clone(&block)) {
+ if let Err(err) = crate::typer::VM::new().print_ops(true).typecheck(Type::NoType, Rc::clone(&block)) {
println!("TYPE ERROR: {}", err);
}
@@ -404,12 +404,12 @@ impl VM {
Op::Call(num_args) => {
let new_base = self.stack.len() - 1 - num_args;
match &self.stack[new_base] {
- Value::Function(arity, block) => {
- if arity != &num_args {
+ Value::Function(args, ret, block) => {
+ if args.len() != num_args {
error!(self,
ErrorKind::InvalidProgram,
format!("Invalid number of arguments, got {} expected {}.",
- num_args, arity));
+ num_args, args.len()));
}
if self.print_blocks {
block.debug_print();