1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

feat: array types in IR

This commit is contained in:
Natapat Samutpong 2022-02-06 14:16:39 +07:00
parent f939903df2
commit d0555cda52
2 changed files with 30 additions and 9 deletions

View file

@ -2,7 +2,7 @@ use regex::Regex;
use crate::vm::{instr::*, types::Type};
const REGEX: &str = r###"[^\s\$";]+|"[^"]*"|;.*"###;
const REGEX: &str = r###"\([^)]*\)|[^\s\$";]+|"[^"]*"|;.*"###;
macro_rules! value { ($s:expr) => { $s.parse::<Type>().unwrap() }; }
macro_rules! register { ($s:expr) => { $s.parse::<Register>().unwrap() }; }

View file

@ -10,6 +10,7 @@ pub enum Type {
Float(f64),
Boolean(bool),
String(String),
Cons(Vec<Type>),
}
impl Type {
@ -37,6 +38,16 @@ impl Type {
false => "false".to_string(),
},
Type::String(s) => s.clone(),
Type::Cons(v) => {
let mut s = String::new();
s.push('(');
for (i, t) in v.iter().enumerate() {
if i != 0 { s.push(','); }
s.push_str(&t.fmt());
}
s.push(')');
s
}
}
}
}
@ -129,6 +140,15 @@ impl FromStr for Type {
"true" => Ok(Type::Boolean(true)),
"false" => Ok(Type::Boolean(false)),
_ => {
if s.starts_with("(") {
let elems = s[1..s.len() - 1]
.split(',')
.collect::<Vec<&str>>()
.iter()
.map(|s| s.trim().parse::<Type>())
.collect::<Result<Vec<Type>, Self::Err>>()?;
Ok(Type::Cons(elems))
} else {
let i = s.parse::<i64>();
if i.is_ok() {
Ok(Type::Int(i.unwrap()))
@ -143,4 +163,5 @@ impl FromStr for Type {
}
}
}
}
}