new function parsing

This commit is contained in:
2024-10-16 15:30:38 -04:00
parent 34077344ca
commit 7dc88b8fb0
3 changed files with 184 additions and 168 deletions

View File

@@ -232,31 +232,11 @@ where
} }
}, },
ParseTree::FunctionDefinition(func, scope) => { ParseTree::FunctionDefinition(func, scope) => {
let ident = func.name.clone().unwrap(); let locals = locals.to_mut();
let existing = locals.get(&ident) locals.insert(func.name.clone().unwrap(), Object::Function(func));
.or(self.globals.get(&ident));
match existing { self.exec(scope, &mut Cow::Borrowed(&locals))
Some(Object::Function(f)) => {
if f.body.is_some() || f.arg_names.is_some() {
return Err(RuntimeError::ImmutableError(ident.clone()));
}
let new_func = Function::named(func.name.unwrap().as_str(), func.t.clone(), func.arg_names.clone(), func.body.clone());
let locals = locals.to_mut();
locals.insert(ident.clone(), Object::Function(new_func));
self.exec(scope, &mut Cow::Borrowed(&locals))
}
Some(Object::Variable(_)) => Err(RuntimeError::ImmutableError(ident.clone())),
None => {
let locals = locals.to_mut();
locals.insert(ident.clone(), Object::Function(func));
self.exec(scope, &mut Cow::Borrowed(&locals))
}
}
}, },
ParseTree::Compose(x, y) => { ParseTree::Compose(x, y) => {
self.exec(x, locals)?; self.exec(x, locals)?;
@@ -296,6 +276,9 @@ where
Some(Object::Function(f)) => { Some(Object::Function(f)) => {
let locals = locals.to_mut(); let locals = locals.to_mut();
assert!(f.arg_names.is_some());
assert!(f.body.is_some());
for ((t, name), tree) in std::iter::zip(std::iter::zip(f.t.1, f.arg_names.unwrap()), args) { for ((t, name), tree) in std::iter::zip(std::iter::zip(f.t.1, f.arg_names.unwrap()), args) {
let v = self.exec(Box::new(tree), &mut Cow::Borrowed(locals))?; let v = self.exec(Box::new(tree), &mut Cow::Borrowed(locals))?;
@@ -369,18 +352,7 @@ where
Ok(Value::Nil) Ok(Value::Nil)
} }
} }
ParseTree::FunctionDeclaration(func, scope) => { ParseTree::LambdaDefinition(func) => todo!(),
let locals = locals.to_mut();
let name = func.name.clone().unwrap();
if locals.contains_key(&name) {
Err(RuntimeError::ImmutableError(name.clone()))
} else {
locals.insert(name, Object::Function(func));
self.exec(scope, &mut Cow::Borrowed(&locals))
}
}
ParseTree::LambdaDefinition(func) => Ok(Value::Function(func)),
} }
} }
} }

View File

@@ -5,13 +5,14 @@ use std::error;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::borrow::Cow; use std::borrow::Cow;
use std::iter::Peekable;
#[derive(Debug)] #[derive(Debug)]
pub enum ParseError { pub enum ParseError {
NoInput, NoInput,
UnexpectedEndInput, UnexpectedEndInput,
IdentifierUndefined(String), IdentifierUndefined(String),
InvalidIdentifier, InvalidIdentifier(Token),
FunctionUndefined(String), FunctionUndefined(String),
VariableUndefined(String), VariableUndefined(String),
UnmatchedArrayClose, UnmatchedArrayClose,
@@ -25,7 +26,7 @@ impl Display for ParseError {
match self { match self {
ParseError::UnexpectedEndInput => write!(f, "Input ended unexpectedly"), ParseError::UnexpectedEndInput => write!(f, "Input ended unexpectedly"),
ParseError::IdentifierUndefined(name) => write!(f, "Undefined variable `{name}`"), ParseError::IdentifierUndefined(name) => write!(f, "Undefined variable `{name}`"),
ParseError::InvalidIdentifier => write!(f, "Invalid identifier"), ParseError::InvalidIdentifier(t) => write!(f, "Invalid identifier `{t:?}`"),
ParseError::FunctionUndefined(name) => write!(f, "Undefined function `{name}`"), ParseError::FunctionUndefined(name) => write!(f, "Undefined function `{name}`"),
ParseError::VariableUndefined(name) => write!(f, "Undefined variable `{name}`"), ParseError::VariableUndefined(name) => write!(f, "Undefined variable `{name}`"),
ParseError::NoInput => write!(f, "No input given"), ParseError::NoInput => write!(f, "No input given"),
@@ -64,7 +65,6 @@ pub(crate) enum ParseTree {
Equ(String, Box<ParseTree>, Box<ParseTree>), Equ(String, Box<ParseTree>, Box<ParseTree>),
LazyEqu(String, Box<ParseTree>, Box<ParseTree>), LazyEqu(String, Box<ParseTree>, Box<ParseTree>),
FunctionDefinition(Function, Box<ParseTree>), FunctionDefinition(Function, Box<ParseTree>),
FunctionDeclaration(Function, Box<ParseTree>),
LambdaDefinition(Function), LambdaDefinition(Function),
// Functional Operations // Functional Operations
@@ -116,7 +116,7 @@ macro_rules! three_arg {
impl ParseTree { impl ParseTree {
fn parse<I>( fn parse<I>(
tokens: &mut I, tokens: &mut Peekable<I>,
globals: &HashMap<String, Function>, globals: &HashMap<String, Function>,
locals: &mut Cow<HashMap<String, Function>>) -> Result<Self, ParseError> locals: &mut Cow<HashMap<String, Function>>) -> Result<Self, ParseError>
where where
@@ -165,63 +165,32 @@ impl ParseTree {
_ => panic!("Operator literally changed under your nose"), _ => panic!("Operator literally changed under your nose"),
} }
} else { } else {
Err(ParseError::InvalidIdentifier) Err(ParseError::InvalidIdentifier(token))
} }
} }
Op::FunctionDefine(nargs) => { Op::FunctionDefine(arg_count) => {
let token = tokens.next() let mut f = ParseTree::parse_function(tokens, arg_count)?;
.ok_or(ParseError::UnexpectedEndInput)?
.map_err(|e| ParseError::TokenizeError(e))?;
if let Token::Identifier(ident) = token { assert!(f.arg_names.is_some());
let args: Vec<String> = tokens.take(nargs) assert!(f.name.is_some());
.map(|token| match token { assert!(f.body.is_none());
Ok(Token::Identifier(ident)) => Ok(ident),
Ok(_) => Err(ParseError::InvalidIdentifier),
Err(e) => Err(ParseError::TokenizeError(e)),
})
.collect::<Result<Vec<_>, ParseError>>()?;
let f = if locals.contains_key(&ident) { if locals.contains_key(&f.name.clone().unwrap()) {
let locals = locals.to_mut(); return Err(ParseError::ImmutableError(f.name.unwrap()));
let f = locals.get(&ident).unwrap();
let f = f.clone();
// iterate over f's types and push them
for (t, name) in std::iter::zip(f.t.1.clone(), args.clone()) {
match t {
Type::Function(finner) => {
locals.insert(name.clone(), Function::named(&name, finner, None, None));
}
_ => (),
}
}
Function::named(
&ident,
f.t.clone(),
Some(args),
Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?)))
} else {
let f = Function::named(
&ident,
FunctionType(Box::new(Type::Any), args.iter().map(|_| Type::Any).collect()),
Some(args),
Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?)));
let locals = locals.to_mut();
locals.insert(ident.clone(), f.clone());
f
};
Ok(ParseTree::FunctionDefinition(f,
Box::new(ParseTree::parse(tokens, globals, locals)?)))
} else {
Err(ParseError::InvalidIdentifier)
} }
}
let locals = locals.to_mut();
// recursion requires that f's prototype is present in locals
locals.insert(f.name.clone().unwrap(), f.clone());
f.body = Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?));
assert!(f.body.is_some());
println!("{:?} = {:?}", f.name, f);
Ok(ParseTree::FunctionDefinition(f, Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?)))
},
Op::Compose => two_arg!(Compose, tokens, globals, locals), Op::Compose => two_arg!(Compose, tokens, globals, locals),
Op::Id => one_arg!(Id, tokens, globals, locals), Op::Id => one_arg!(Id, tokens, globals, locals),
Op::If => two_arg!(If, tokens, globals, locals), Op::If => two_arg!(If, tokens, globals, locals),
@@ -273,69 +242,9 @@ impl ParseTree {
Op::NotEqualTo => two_arg!(NotEqualTo, tokens, globals, locals), Op::NotEqualTo => two_arg!(NotEqualTo, tokens, globals, locals),
Op::And => two_arg!(And, tokens, globals, locals), Op::And => two_arg!(And, tokens, globals, locals),
Op::Or => two_arg!(Or, tokens, globals, locals), Op::Or => two_arg!(Or, tokens, globals, locals),
Op::FunctionDeclare(arg_count) => { Op::LambdaDefine(_arg_count) => todo!(),
let name = match tokens.next() Op::NonCall => todo!(),
.ok_or(ParseError::UnexpectedEndInput)? op => Err(ParseError::UnwantedToken(Token::Operator(op))),
.map_err(|e| ParseError::TokenizeError(e))?
{
Token::Identifier(x) => x,
_ => return Err(ParseError::InvalidIdentifier),
};
let args: Vec<Type> = (0..arg_count)
.map(|_| Self::parse_type(tokens))
.collect::<Result<_, ParseError>>()?;
let rett = Self::parse_type(tokens)?;
if locals.contains_key(&name) {
println!("{name} already found: {locals:?}");
return Err(ParseError::ImmutableError(name.clone()));
}
let f = Function::named(
&name,
FunctionType(Box::new(rett), args),
None,
None);
let locals = locals.to_mut();
locals.insert(name, f.clone());
Ok(ParseTree::FunctionDeclaration(
f,
Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&*locals))?)))
}
Op::LambdaDefine(arg_count) => {
let args: Vec<String> = tokens.take(arg_count)
.map(|token| match token {
Ok(Token::Identifier(ident)) => Ok(ident),
Ok(_) => Err(ParseError::InvalidIdentifier),
Err(e) => Err(ParseError::TokenizeError(e)),
})
.collect::<Result<Vec<_>, ParseError>>()?;
Ok(ParseTree::LambdaDefinition(
Function::lambda(
FunctionType(Box::new(Type::Any), args.clone().into_iter().map(|_| Type::Any).collect()),
args,
Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&*locals))?)))))
}
Op::NonCall => {
let ident = match tokens.next().ok_or(ParseError::UnexpectedEndInput)?
.map_err(|e| ParseError::TokenizeError(e))?
{
Token::Identifier(x) => x,
_ => return Err(ParseError::InvalidIdentifier),
};
if let Some(f) = locals.clone().get(&ident).or(globals.clone().get(&ident)).cloned() {
Ok(ParseTree::Constant(Value::Function(f)))
} else {
Err(ParseError::FunctionUndefined(ident.clone()))
}
}
} }
} }
t => Err(ParseError::UnwantedToken(t)), t => Err(ParseError::UnwantedToken(t)),
@@ -346,6 +255,92 @@ impl ParseTree {
} }
} }
fn parse_function<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, ParseError>
where
I: Iterator<Item = Result<Token, TokenizeError>>,
{
let name = Self::get_identifier(tokens.next())?;
let (t, args) = Self::parse_function_declaration(tokens, arg_count)?;
Ok(Function::named(&name, t, Some(args), None))
}
fn parse_function_declaration<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<(FunctionType, Vec<String>), ParseError>
where
I: Iterator<Item = Result<Token, TokenizeError>>
{
let args: Vec<(Type, String)> = (0..arg_count)
.map(|_| Self::parse_function_declaration_parameter(tokens))
.collect::<Result<_, _>>()?;
let (types, names): (Vec<_>, Vec<_>) = args.into_iter().unzip();
let mut ret = Type::Any;
if let Some(t) = tokens.next_if(|x| matches!(x, Ok(Token::Operator(Op::Arrow))))
{
if let Err(e) = t {
return Err(ParseError::TokenizeError(e));
}
ret = Self::parse_type(tokens)?;
}
Ok((FunctionType(Box::new(ret), types), names))
}
fn parse_function_declaration_parameter<I>(mut tokens: &mut Peekable<I>) -> Result<(Type, String), ParseError>
where
I: Iterator<Item = Result<Token, TokenizeError>>
{
match tokens.next() {
// untyped variable
Some(Ok(Token::Identifier(x))) => Ok((Type::Any, x)),
// typed variable
Some(Ok(Token::Operator(Op::TypeDeclaration))) => {
let name = Self::get_identifier(tokens.next())?;
let t = Self::parse_type(&mut tokens)?;
Ok((t, name))
}
// untyped function (all args Any, return type Any)
Some(Ok(Token::Operator(Op::FunctionDefine(n)))) => {
let name = Self::get_identifier(tokens.next())?;
let args = (0..n).map(|_| Type::Any).collect();
Ok((Type::Function(FunctionType(Box::new(Type::Any), args)), name))
}
// typed function
Some(Ok(Token::Operator(Op::FunctionDeclare(n)))) => {
let name = Self::get_identifier(tokens.next())?;
let args = (0..n).map(|_| Self::parse_type(&mut tokens)).collect::<Result<_, _>>()?;
let mut ret = Type::Any;
// this is annoying
// inside of the next_if closure, we already can know that its an error
// and return it, but we cannot return out of a closure
if let Some(t) = tokens.next_if(|x| matches!(x, Ok(Token::Operator(Op::Arrow))))
{
// so we just check for an error here. this is the only reason t exists.
if let Err(e) = t {
return Err(ParseError::TokenizeError(e));
}
ret = Self::parse_type(&mut tokens)?;
}
Ok((Type::Function(FunctionType(Box::new(ret), args)), name))
}
Some(Ok(t)) => Err(ParseError::UnwantedToken(t)),
Some(Err(e)) => Err(ParseError::TokenizeError(e)),
None => Err(ParseError::UnexpectedEndInput),
}
}
fn parse_type<I>(tokens: &mut I) -> Result<Type, ParseError> fn parse_type<I>(tokens: &mut I) -> Result<Type, ParseError>
where where
I: Iterator<Item = Result<Token, TokenizeError>>, I: Iterator<Item = Result<Token, TokenizeError>>,
@@ -375,6 +370,16 @@ impl ParseTree {
None => Err(ParseError::UnexpectedEndInput), None => Err(ParseError::UnexpectedEndInput),
} }
} }
fn get_identifier(t: Option<Result<Token, TokenizeError>>) -> Result<String, ParseError> {
match t.ok_or(ParseError::UnexpectedEndInput)?
.map_err(|e| ParseError::TokenizeError(e))
{
Ok(Token::Identifier(ident)) => Ok(ident),
Ok(t) => Err(ParseError::InvalidIdentifier(t)),
Err(e) => Err(e),
}
}
} }
/// Parses input tokens and produces ParseTrees for an Executor /// Parses input tokens and produces ParseTrees for an Executor
@@ -418,7 +423,7 @@ impl<I: Iterator<Item = Result<Token, TokenizeError>>> Iterator for Parser<I> {
type Item = Result<ParseTree, ParseError>; type Item = Result<ParseTree, ParseError>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let tree = ParseTree::parse(&mut self.tokens, &self.globals, &mut Cow::Borrowed(&self.locals)); let tree = ParseTree::parse(&mut self.tokens.by_ref().peekable(), &self.globals, &mut Cow::Borrowed(&self.locals));
match tree { match tree {
Ok(tree) => Some(Ok(tree)), Ok(tree) => Some(Ok(tree)),

View File

@@ -41,7 +41,7 @@ impl Display for TokenizeError {
impl error::Error for TokenizeError {} impl error::Error for TokenizeError {}
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub(crate) enum Op { pub(crate) enum Op {
Add, Add,
Sub, Sub,
@@ -51,9 +51,11 @@ pub(crate) enum Op {
Equ, Equ,
Mod, Mod,
LazyEqu, LazyEqu,
TypeDeclaration,
FunctionDefine(usize), FunctionDefine(usize),
FunctionDeclare(usize), FunctionDeclare(usize),
LambdaDefine(usize), LambdaDefine(usize),
Arrow,
Compose, Compose,
Id, Id,
If, If,
@@ -169,9 +171,11 @@ impl<R: BufRead> Tokenizer<R> {
("%", Op::Mod), ("%", Op::Mod),
("=", Op::Equ), ("=", Op::Equ),
(".", Op::LazyEqu), (".", Op::LazyEqu),
("?.", Op::TypeDeclaration),
(":", Op::FunctionDefine(1)), (":", Op::FunctionDefine(1)),
("?:", Op::FunctionDeclare(1)), ("?:", Op::FunctionDeclare(1)),
(";", Op::LambdaDefine(1)), (";", Op::LambdaDefine(1)),
("->", Op::Arrow),
("~", Op::Compose), ("~", Op::Compose),
(",", Op::Id), (",", Op::Id),
("?", Op::If), ("?", Op::If),
@@ -262,16 +266,6 @@ impl<R: BufRead> Tokenizer<R> {
if let Some(op) = possible.get(token.as_str()) { if let Some(op) = possible.get(token.as_str()) {
self.tokens.push_back(Ok(Token::Operator(match op { self.tokens.push_back(Ok(Token::Operator(match op {
// special handling for "dynamic" operators // special handling for "dynamic" operators
Op::FunctionDeclare(n) => {
let count = match get_dot_count(&mut iter) {
Some(count) => count,
None => {
self.tokens.push_back(Err(TokenizeError::InvalidDynamicOperator(token)));
return;
}
};
Op::FunctionDeclare(n + count)
}
Op::FunctionDefine(n) => { Op::FunctionDefine(n) => {
let count = match get_dot_count(&mut iter) { let count = match get_dot_count(&mut iter) {
Some(count) => count, Some(count) => count,
@@ -282,6 +276,16 @@ impl<R: BufRead> Tokenizer<R> {
}; };
Op::FunctionDefine(n + count) Op::FunctionDefine(n + count)
} }
Op::FunctionDeclare(n) => {
let count = match get_dot_count(&mut iter) {
Some(count) => count,
None => {
self.tokens.push_back(Err(TokenizeError::InvalidDynamicOperator(token)));
return;
}
};
Op::FunctionDeclare(n + count)
}
Op::LambdaDefine(n) => { Op::LambdaDefine(n) => {
let count = match get_dot_count(&mut iter) { let count = match get_dot_count(&mut iter) {
Some(count) => count, Some(count) => count,
@@ -314,7 +318,42 @@ impl<R: BufRead> Tokenizer<R> {
Some(c) => c, Some(c) => c,
None => { None => {
// at this point, token must be in the hashmap possible, otherwise it wouldnt have any matches // at this point, token must be in the hashmap possible, otherwise it wouldnt have any matches
self.tokens.push_back(Ok(Token::Operator(possible.get(token.as_str()).unwrap().clone()))); self.tokens.push_back(Ok(Token::Operator(match possible.get(token.as_str()).unwrap() {
// special handling for "dynamic" operators
Op::FunctionDefine(n) => {
let count = match get_dot_count(&mut iter) {
Some(count) => count,
None => {
self.tokens.push_back(Err(TokenizeError::InvalidDynamicOperator(token)));
return;
}
};
println!("{n} + {count}");
Op::FunctionDefine(n + count)
}
Op::FunctionDeclare(n) => {
let count = match get_dot_count(&mut iter) {
Some(count) => count,
None => {
self.tokens.push_back(Err(TokenizeError::InvalidDynamicOperator(token)));
return;
}
};
Op::FunctionDeclare(n + count)
}
Op::LambdaDefine(n) => {
let count = match get_dot_count(&mut iter) {
Some(count) => count,
None => {
self.tokens.push_back(Err(TokenizeError::InvalidDynamicOperator(token)));
return;
}
};
Op::LambdaDefine(n + count)
}
op => op.clone(),
})));
break; break;
} }
}; };
@@ -374,7 +413,7 @@ mod tests {
#[test] #[test]
fn uwu() { fn uwu() {
let program = "?:. apply : Any Any Any Any :. apply f x f x : id x x apply ; x id x 12"; let program = ":. add x y + x y";
let tokens: Vec<Token> = Tokenizer::from_str(program).unwrap().collect::<Result<_, TokenizeError>>().unwrap(); let tokens: Vec<Token> = Tokenizer::from_str(program).unwrap().collect::<Result<_, TokenizeError>>().unwrap();