aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-01-12 21:14:25 +0100
committerEdvard Thörnros <edvard.thornros@gmail.com>2021-01-12 21:14:25 +0100
commit2bed29253c2cf79b125b57324b48c0a0f917365c (patch)
treeb1279d8688ad439875ca627b6e33aaed918f1570 /src
parent6718b634844a7db28ef01374701d1f77c4fe1716 (diff)
downloadsylt-2bed29253c2cf79b125b57324b48c0a0f917365c.tar.gz
Reference counted strings
Diffstat (limited to 'src')
-rw-r--r--src/compiler.rs3
-rw-r--r--src/vm.rs19
2 files changed, 16 insertions, 6 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index df094ae..d3fc1fd 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -1,4 +1,5 @@
use std::path::{Path, PathBuf};
+use std::rc::Rc;
use std::convert::TryFrom;
use crate::tokenizer::{Token, TokenStream};
@@ -225,7 +226,7 @@ impl Compiler {
Token::Float(f) => { Value::Float(f) },
Token::Int(i) => { Value::Int(i) }
Token::Bool(b) => { Value::Bool(b) }
- Token::String(s) => { Value::String(s.clone()) }
+ Token::String(s) => { Value::String(Rc::from(s)) }
_ => { error!(self, "Cannot parse value."); Value::Bool(false) }
};
block.add(Op::Constant(value), self.line());
diff --git a/src/vm.rs b/src/vm.rs
index d0b9190..2962b7f 100644
--- a/src/vm.rs
+++ b/src/vm.rs
@@ -1,4 +1,5 @@
use std::path::{Path, PathBuf};
+use std::rc::Rc;
use std::collections::HashMap;
use crate::error::{Error, ErrorKind};
@@ -17,7 +18,7 @@ pub enum Value {
Float(f64),
Int(i64),
Bool(bool),
- String(String),
+ String(Rc<String>),
}
#[derive(Debug, Clone)]
@@ -153,7 +154,7 @@ impl VM {
}
pub fn run(&mut self) -> Result<(), Error>{
- const PRINT_WHILE_RUNNING: bool = false;
+ const PRINT_WHILE_RUNNING: bool = true;
const PRINT_BLOCK: bool = true;
if PRINT_BLOCK {
@@ -167,8 +168,14 @@ impl VM {
loop {
if PRINT_WHILE_RUNNING {
print!(" [");
- for s in self.stack.iter() {
- print!("{:?} ", s);
+ for (i, s) in self.stack.iter().enumerate() {
+ if i != 0 {
+ print!(" ");
+ }
+ match s {
+ Value::String(rc) => print!("{:?}<{}>", rc, Rc::strong_count(rc)),
+ _ => print!("{:?}", s),
+ }
}
println!("]");
@@ -205,7 +212,9 @@ impl VM {
match self.pop_twice() {
(Value::Float(a), Value::Float(b)) => self.stack.push(Value::Float(b + a)),
(Value::Int(a), Value::Int(b)) => self.stack.push(Value::Int(b + a)),
- (Value::String(a), Value::String(b)) => self.stack.push(Value::String(format!("{}{}", a, b))),
+ (Value::String(a), Value::String(b)) => {
+ self.stack.push(Value::String(Rc::from(format!("{}{}", a, b))))
+ }
(a, b) => error!(self, ErrorKind::TypeError(op.clone(), vec![a, b])),
}
}