mirror of
https://github.com/Dummi26/mers.git
synced 2025-12-16 03:57:50 +01:00
assigning to tuples should work properly now
This commit is contained in:
@@ -1,24 +1,85 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::program;
|
||||
use crate::program::{self, parsed::block::Block};
|
||||
|
||||
pub mod errors;
|
||||
pub mod statements;
|
||||
pub mod types;
|
||||
|
||||
pub fn parse(src: &mut Source) -> Result<Box<dyn program::parsed::MersStatement>, ()> {
|
||||
Ok(Box::new(statements::parse_block(src)?))
|
||||
let pos_in_src = src.get_pos();
|
||||
Ok(Box::new(Block {
|
||||
pos_in_src,
|
||||
statements: statements::parse_multiple(src, "")?,
|
||||
}))
|
||||
}
|
||||
|
||||
pub struct Source {
|
||||
src_raw_len: usize,
|
||||
src: String,
|
||||
/// (start, content) of each comment, including start/end (//, \n, /* and */)
|
||||
comments: Vec<(usize, String)>,
|
||||
i: usize,
|
||||
sections: Vec<SectionMarker>,
|
||||
}
|
||||
impl Source {
|
||||
pub fn new(src: String) -> Self {
|
||||
pub fn new(source: String) -> Self {
|
||||
let mut src = String::with_capacity(source.len());
|
||||
let mut comment = (0, String::new());
|
||||
let mut comments = Vec::new();
|
||||
let mut chars = source.char_indices().peekable();
|
||||
let mut in_comment = None;
|
||||
loop {
|
||||
if let Some((i, ch)) = chars.next() {
|
||||
match in_comment {
|
||||
Some(false) => {
|
||||
comment.1.push(ch);
|
||||
if ch == '\n' {
|
||||
in_comment = None;
|
||||
comments.push((
|
||||
comment.0,
|
||||
std::mem::replace(&mut comment.1, String::new()),
|
||||
));
|
||||
}
|
||||
}
|
||||
Some(true) => {
|
||||
comment.1.push(ch);
|
||||
if ch == '*' && matches!(chars.peek(), Some((_, '/'))) {
|
||||
chars.next();
|
||||
comment.1.push('/');
|
||||
in_comment = None;
|
||||
comments.push((
|
||||
comment.0,
|
||||
std::mem::replace(&mut comment.1, String::new()),
|
||||
));
|
||||
}
|
||||
}
|
||||
None => match ch {
|
||||
'/' if matches!(chars.peek(), Some((_, '/'))) => {
|
||||
chars.next();
|
||||
in_comment = Some(false);
|
||||
comment.0 = i;
|
||||
comment.1.push('/');
|
||||
comment.1.push('/');
|
||||
}
|
||||
'/' if matches!(chars.peek(), Some((_, '*'))) => {
|
||||
chars.next();
|
||||
in_comment = Some(true);
|
||||
comment.0 = i;
|
||||
comment.1.push('/');
|
||||
comment.1.push('*');
|
||||
}
|
||||
_ => src.push(ch),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Self {
|
||||
src_raw_len: source.len(),
|
||||
src,
|
||||
comments,
|
||||
i: 0,
|
||||
sections: vec![],
|
||||
}
|
||||
@@ -105,6 +166,37 @@ impl Source {
|
||||
pub fn sections(&self) -> &Vec<SectionMarker> {
|
||||
&self.sections
|
||||
}
|
||||
|
||||
pub fn format(&self, insertions: &Vec<(usize, bool, String)>) -> String {
|
||||
let mut o = String::with_capacity(self.src_raw_len);
|
||||
let mut insertions = insertions.iter().peekable();
|
||||
let mut comments = self.comments.iter().peekable();
|
||||
for (i, ch) in self.src.char_indices() {
|
||||
let insert = if let Some((index, pre, _)) = insertions.peek() {
|
||||
if *index <= i {
|
||||
Some(*pre)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some((index, comment)) = comments.peek() {
|
||||
if *index == i {
|
||||
comments.next();
|
||||
o.push_str(comment);
|
||||
}
|
||||
}
|
||||
if let Some(true) = insert {
|
||||
o.push_str(&insertions.next().unwrap().2);
|
||||
}
|
||||
o.push(ch);
|
||||
if let Some(false) = insert {
|
||||
o.push_str(&insertions.next().unwrap().2);
|
||||
}
|
||||
}
|
||||
o
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Source {
|
||||
@@ -144,7 +236,7 @@ impl SectionMarker {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct SourcePos(usize);
|
||||
impl SourcePos {
|
||||
fn diff(&self, rhs: &Self) -> usize {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::Source;
|
||||
use crate::{
|
||||
data::Data,
|
||||
@@ -16,32 +14,43 @@ pub fn parse(src: &mut Source) -> Result<Option<Box<dyn program::parsed::MersSta
|
||||
src.skip_whitespace();
|
||||
match src.peek_word() {
|
||||
":=" => {
|
||||
let pos_in_src = src.get_pos();
|
||||
src.next_word();
|
||||
first = Box::new(program::parsed::init_to::InitTo {
|
||||
pos_in_src,
|
||||
target: first,
|
||||
source: parse(src)?.expect("todo"),
|
||||
});
|
||||
}
|
||||
"=" => {
|
||||
let pos_in_src = src.get_pos();
|
||||
src.next_word();
|
||||
first = Box::new(program::parsed::assign_to::AssignTo {
|
||||
pos_in_src,
|
||||
target: first,
|
||||
source: parse(src)?.expect("todo"),
|
||||
});
|
||||
}
|
||||
"->" => {
|
||||
let pos_in_src = src.get_pos();
|
||||
src.next_word();
|
||||
first = Box::new(program::parsed::function::Function {
|
||||
pos_in_src,
|
||||
arg: first,
|
||||
run: parse(src)?.expect("err: bad eof, fn needs some statement"),
|
||||
});
|
||||
}
|
||||
_ => loop {
|
||||
let pos_in_src = src.get_pos();
|
||||
src.skip_whitespace();
|
||||
if let Some('.') = src.peek_char() {
|
||||
src.next_char();
|
||||
let chained = parse_no_chain(src)?.expect("err: EOF instead of chain");
|
||||
first = Box::new(program::parsed::chain::Chain { first, chained });
|
||||
first = Box::new(program::parsed::chain::Chain {
|
||||
pos_in_src,
|
||||
first,
|
||||
chained,
|
||||
});
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -52,23 +61,12 @@ pub fn parse(src: &mut Source) -> Result<Option<Box<dyn program::parsed::MersSta
|
||||
}
|
||||
Ok(Some(first))
|
||||
}
|
||||
/// Assumes the { has already been parsed
|
||||
pub fn parse_block(src: &mut Source) -> Result<program::parsed::block::Block, ()> {
|
||||
Ok(program::parsed::block::Block {
|
||||
statements: parse_multiple(src, '}')?,
|
||||
})
|
||||
}
|
||||
pub fn parse_tuple(src: &mut Source) -> Result<program::parsed::tuple::Tuple, ()> {
|
||||
Ok(program::parsed::tuple::Tuple {
|
||||
elems: parse_multiple(src, ')')?,
|
||||
})
|
||||
}
|
||||
pub fn parse_multiple(src: &mut Source, end: char) -> Result<Vec<Box<dyn MersStatement>>, ()> {
|
||||
pub fn parse_multiple(src: &mut Source, end: &str) -> Result<Vec<Box<dyn MersStatement>>, ()> {
|
||||
src.section_begin("block".to_string());
|
||||
let mut statements = vec![];
|
||||
loop {
|
||||
src.skip_whitespace();
|
||||
if matches!(src.peek_char(), Some(e) if e == end) {
|
||||
if src.peek_char().is_some_and(|ch| end.contains(ch)) {
|
||||
src.next_char();
|
||||
break;
|
||||
} else if let Some(s) = parse(src)? {
|
||||
@@ -87,15 +85,24 @@ pub fn parse_no_chain(
|
||||
src.skip_whitespace();
|
||||
match src.peek_char() {
|
||||
Some('{') => {
|
||||
let pos_in_src = src.get_pos();
|
||||
src.next_char();
|
||||
return Ok(Some(Box::new(parse_block(src)?)));
|
||||
return Ok(Some(Box::new(program::parsed::block::Block {
|
||||
pos_in_src,
|
||||
statements: parse_multiple(src, "}")?,
|
||||
})));
|
||||
}
|
||||
Some('(') => {
|
||||
let pos_in_src = src.get_pos();
|
||||
src.next_char();
|
||||
return Ok(Some(Box::new(parse_tuple(src)?)));
|
||||
return Ok(Some(Box::new(program::parsed::tuple::Tuple {
|
||||
pos_in_src,
|
||||
elems: parse_multiple(src, ")")?,
|
||||
})));
|
||||
}
|
||||
Some('"') => {
|
||||
src.section_begin("string literal".to_string());
|
||||
let pos_in_src = src.get_pos();
|
||||
src.next_char();
|
||||
let mut s = String::new();
|
||||
loop {
|
||||
@@ -118,16 +125,19 @@ pub fn parse_no_chain(
|
||||
todo!("err: eof in string")
|
||||
}
|
||||
}
|
||||
return Ok(Some(Box::new(program::parsed::value::Value(Data::new(
|
||||
crate::data::string::String(s),
|
||||
)))));
|
||||
return Ok(Some(Box::new(program::parsed::value::Value {
|
||||
pos_in_src,
|
||||
data: Data::new(crate::data::string::String(s)),
|
||||
})));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let pos_in_src = src.get_pos();
|
||||
Ok(Some(match src.next_word() {
|
||||
"if" => {
|
||||
src.section_begin("if".to_string());
|
||||
Box::new(program::parsed::r#if::If {
|
||||
pos_in_src,
|
||||
condition: parse(src)?.expect("err: EOF instead of condition"),
|
||||
on_true: parse(src)?.expect("err: EOF instead of on_true"),
|
||||
on_false: {
|
||||
@@ -142,16 +152,14 @@ pub fn parse_no_chain(
|
||||
},
|
||||
})
|
||||
}
|
||||
"switch" => {
|
||||
src.section_begin("loop".to_string());
|
||||
todo!()
|
||||
}
|
||||
"true" => Box::new(program::parsed::value::Value(Data::new(
|
||||
crate::data::bool::Bool(true),
|
||||
))),
|
||||
"false" => Box::new(program::parsed::value::Value(Data::new(
|
||||
crate::data::bool::Bool(false),
|
||||
))),
|
||||
"true" => Box::new(program::parsed::value::Value {
|
||||
pos_in_src,
|
||||
data: Data::new(crate::data::bool::Bool(true)),
|
||||
}),
|
||||
"false" => Box::new(program::parsed::value::Value {
|
||||
pos_in_src,
|
||||
data: Data::new(crate::data::bool::Bool(false)),
|
||||
}),
|
||||
"" => return Ok(None),
|
||||
o => {
|
||||
let o = o.to_string();
|
||||
@@ -161,28 +169,33 @@ pub fn parse_no_chain(
|
||||
let here = src.get_pos();
|
||||
src.next_char();
|
||||
if let Ok(num) = format!("{o}.{}", src.next_word()).parse() {
|
||||
Box::new(program::parsed::value::Value(Data::new(
|
||||
crate::data::float::Float(num),
|
||||
)))
|
||||
Box::new(program::parsed::value::Value {
|
||||
pos_in_src,
|
||||
data: Data::new(crate::data::float::Float(num)),
|
||||
})
|
||||
} else {
|
||||
src.set_pos(here);
|
||||
Box::new(program::parsed::value::Value(Data::new(
|
||||
crate::data::int::Int(n),
|
||||
)))
|
||||
Box::new(program::parsed::value::Value {
|
||||
pos_in_src,
|
||||
data: Data::new(crate::data::int::Int(n)),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
Box::new(program::parsed::value::Value(Data::new(
|
||||
crate::data::int::Int(n),
|
||||
)))
|
||||
Box::new(program::parsed::value::Value {
|
||||
pos_in_src,
|
||||
data: Data::new(crate::data::int::Int(n)),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if let Some('&') = o.chars().next() {
|
||||
Box::new(program::parsed::variable::Variable {
|
||||
pos_in_src,
|
||||
is_ref: true,
|
||||
var: o[1..].to_string(),
|
||||
})
|
||||
} else {
|
||||
Box::new(program::parsed::variable::Variable {
|
||||
pos_in_src,
|
||||
is_ref: false,
|
||||
var: o.to_string(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user