cl-interpret: Give the interpreter a little love

And stop copying strings around.
This commit is contained in:
John 2024-04-19 10:49:25 -05:00
parent 90a3818ca0
commit 9dc0cc7841
5 changed files with 139 additions and 65 deletions

View File

@ -6,7 +6,10 @@ use super::{
temp_type_impl::ConValue, temp_type_impl::ConValue,
BuiltIn, Callable, BuiltIn, Callable,
}; };
use std::io::{stdout, Write}; use std::{
io::{stdout, Write},
rc::Rc,
};
builtins! { builtins! {
const MISC; const MISC;
@ -65,7 +68,7 @@ builtins! {
Ok(match (lhs, rhs) { Ok(match (lhs, rhs) {
(ConValue::Empty, ConValue::Empty) => ConValue::Empty, (ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a + b), (ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a + b),
(ConValue::String(a), ConValue::String(b)) => ConValue::String(a.to_string() + b), (ConValue::String(a), ConValue::String(b)) => (a.to_string() + b).into(),
_ => Err(Error::TypeError)? _ => Err(Error::TypeError)?
}) })
} }
@ -184,6 +187,12 @@ builtins! {
_ => Err(Error::TypeError)?, _ => Err(Error::TypeError)?,
}) })
} }
pub fn deref(tail) -> IResult<ConValue> {
Ok(match tail {
ConValue::Ref(v) => Rc::as_ref(v).clone(),
_ => tail.clone(),
})
}
} }
/// Turns an argument slice into an array with the (inferred) correct number of elements /// Turns an argument slice into an array with the (inferred) correct number of elements

View File

@ -5,7 +5,7 @@
//! meaningless to get a pointer to one, and would be undefined behavior to dereference a pointer to //! meaningless to get a pointer to one, and would be undefined behavior to dereference a pointer to
//! one in any situation. //! one in any situation.
use std::borrow::Borrow; use std::{borrow::Borrow, rc::Rc};
use super::*; use super::*;
use cl_ast::*; use cl_ast::*;
@ -40,18 +40,21 @@ impl Interpret for Item {
} }
} }
impl Interpret for Alias { impl Interpret for Alias {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
todo!("Interpret type alias in {env}") println!("TODO: {self}");
Ok(ConValue::Empty)
} }
} }
impl Interpret for Const { impl Interpret for Const {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
todo!("interpret const in {env}") println!("TODO: {self}");
Ok(ConValue::Empty)
} }
} }
impl Interpret for Static { impl Interpret for Static {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
todo!("interpret static in {env}") println!("TODO: {self}");
Ok(ConValue::Empty)
} }
} }
impl Interpret for Module { impl Interpret for Module {
@ -71,18 +74,22 @@ impl Interpret for Function {
} }
} }
impl Interpret for Struct { impl Interpret for Struct {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
todo!("Interpret structs in {env}") println!("TODO: {self}");
Ok(ConValue::Empty)
} }
} }
impl Interpret for Enum { impl Interpret for Enum {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
todo!("Interpret enums in {env}") println!("TODO: {self}");
Ok(ConValue::Empty)
} }
} }
impl Interpret for Impl { impl Interpret for Impl {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
todo!("Enter a struct's namespace and insert function definitions into it in {env}"); println!("TODO: {self}");
let Self { target: _, body } = self;
body.interpret(env)
} }
} }
impl Interpret for Stmt { impl Interpret for Stmt {
@ -229,24 +236,24 @@ impl Interpret for Binary {
} }
let tail = tail.interpret(env)?; let tail = tail.interpret(env)?;
match kind { match kind {
BinaryKind::Mul => env.call("mul", &[head, tail]), BinaryKind::Lt => head.lt(&tail),
BinaryKind::Div => env.call("div", &[head, tail]), BinaryKind::LtEq => head.lt_eq(&tail),
BinaryKind::Rem => env.call("rem", &[head, tail]), BinaryKind::Equal => head.eq(&tail),
BinaryKind::Add => env.call("add", &[head, tail]), BinaryKind::NotEq => head.neq(&tail),
BinaryKind::Sub => env.call("sub", &[head, tail]), BinaryKind::GtEq => head.gt_eq(&tail),
BinaryKind::Shl => env.call("shl", &[head, tail]), BinaryKind::Gt => head.gt(&tail),
BinaryKind::Shr => env.call("shr", &[head, tail]), BinaryKind::RangeExc => head.range_exc(tail),
BinaryKind::BitAnd => env.call("and", &[head, tail]), BinaryKind::RangeInc => head.range_inc(tail),
BinaryKind::BitOr => env.call("or", &[head, tail]), BinaryKind::BitAnd => head & tail,
BinaryKind::BitXor => env.call("xor", &[head, tail]), BinaryKind::BitOr => head | tail,
BinaryKind::RangeExc => env.call("range_exc", &[head, tail]), BinaryKind::BitXor => head ^ tail,
BinaryKind::RangeInc => env.call("range_inc", &[head, tail]), BinaryKind::Shl => head << tail,
BinaryKind::Lt => env.call("lt", &[head, tail]), BinaryKind::Shr => head >> tail,
BinaryKind::LtEq => env.call("lt_eq", &[head, tail]), BinaryKind::Add => head + tail,
BinaryKind::Equal => env.call("eq", &[head, tail]), BinaryKind::Sub => head - tail,
BinaryKind::NotEq => env.call("neq", &[head, tail]), BinaryKind::Mul => head * tail,
BinaryKind::GtEq => env.call("gt_eq", &[head, tail]), BinaryKind::Div => head / tail,
BinaryKind::Gt => env.call("gt", &[head, tail]), BinaryKind::Rem => head % tail,
BinaryKind::Dot => todo!("search within a type's namespace!"), BinaryKind::Dot => todo!("search within a type's namespace!"),
BinaryKind::Call => match tail { BinaryKind::Call => match tail {
ConValue::Empty => head.call(env, &[]), ConValue::Empty => head.call(env, &[]),
@ -255,6 +262,36 @@ impl Interpret for Binary {
}, },
_ => Ok(head), _ => Ok(head),
} }
// // Temporarily disabled, to avoid function dispatch overhead while I screw around
// // Not like it helped much in the first place!
// match kind {
// BinaryKind::Mul => env.call("mul", &[head, tail]),
// BinaryKind::Div => env.call("div", &[head, tail]),
// BinaryKind::Rem => env.call("rem", &[head, tail]),
// BinaryKind::Add => env.call("add", &[head, tail]),
// BinaryKind::Sub => env.call("sub", &[head, tail]),
// BinaryKind::Shl => env.call("shl", &[head, tail]),
// BinaryKind::Shr => env.call("shr", &[head, tail]),
// BinaryKind::BitAnd => env.call("and", &[head, tail]),
// BinaryKind::BitOr => env.call("or", &[head, tail]),
// BinaryKind::BitXor => env.call("xor", &[head, tail]),
// BinaryKind::RangeExc => env.call("range_exc", &[head, tail]),
// BinaryKind::RangeInc => env.call("range_inc", &[head, tail]),
// BinaryKind::Lt => env.call("lt", &[head, tail]),
// BinaryKind::LtEq => env.call("lt_eq", &[head, tail]),
// BinaryKind::Equal => env.call("eq", &[head, tail]),
// BinaryKind::NotEq => env.call("neq", &[head, tail]),
// BinaryKind::GtEq => env.call("gt_eq", &[head, tail]),
// BinaryKind::Gt => env.call("gt", &[head, tail]),
// BinaryKind::Dot => todo!("search within a type's namespace!"),
// BinaryKind::Call => match tail {
// ConValue::Empty => head.call(env, &[]),
// ConValue::Tuple(args) => head.call(env, &args),
// _ => Err(Error::TypeError),
// },
// _ => Ok(head),
// }
} }
} }
@ -291,7 +328,7 @@ impl Interpret for Path {
if parts.len() == 1 { if parts.len() == 1 {
match parts.last().expect("parts should not be empty") { match parts.last().expect("parts should not be empty") {
PathPart::SuperKw | PathPart::SelfKw => todo!("Path navigation"), PathPart::SuperKw | PathPart::SelfKw => todo!("Path navigation"),
PathPart::Ident(Identifier(s)) => env.get(s).cloned(), PathPart::Ident(Identifier(s)) => env.get(s),
} }
} else { } else {
todo!("Path navigation!") todo!("Path navigation!")
@ -316,7 +353,7 @@ impl Interpret for Array {
for expr in values { for expr in values {
out.push(expr.interpret(env)?) out.push(expr.interpret(env)?)
} }
Ok(ConValue::Array(out)) Ok(ConValue::Array(out.into()))
} }
} }
impl Interpret for ArrayRep { impl Interpret for ArrayRep {
@ -327,14 +364,21 @@ impl Interpret for ArrayRep {
_ => Err(Error::TypeError)?, _ => Err(Error::TypeError)?,
}; };
let value = value.interpret(env)?; let value = value.interpret(env)?;
Ok(ConValue::Array(vec![value; repeat as usize])) Ok(ConValue::Array(vec![value; repeat as usize].into()))
} }
} }
impl Interpret for AddrOf { impl Interpret for AddrOf {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { count: _, mutable: _, expr } = self; let Self { count: _, mutable: _, expr } = self;
// this is stupid match expr.as_ref() {
todo!("Create reference\nfrom expr: {expr}\nin env:\n{env}\n") ExprKind::Index(_) => todo!("AddrOf array index"),
// ExprKind::Path(Path { absolute: false, parts }) => match parts.as_slice() {
// [PathPart::Ident(Identifier(id))] => env.get_ref(id),
// _ => todo!("Path traversal in addrof"),
// },
ExprKind::Path(_) => todo!("Path traversal in addrof"),
_ => Ok(ConValue::Ref(Rc::new(expr.interpret(env)?))),
}
} }
} }
impl Interpret for Block { impl Interpret for Block {
@ -357,13 +401,15 @@ impl Interpret for Group {
impl Interpret for Tuple { impl Interpret for Tuple {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { exprs } = self; let Self { exprs } = self;
Ok(ConValue::Tuple(exprs.iter().try_fold( Ok(ConValue::Tuple(
vec![], exprs
|mut out, element| { .iter()
.try_fold(vec![], |mut out, element| {
out.push(element.interpret(env)?); out.push(element.interpret(env)?);
Ok(out) Ok(out)
}, })?
)?)) .into(),
))
} }
} }
impl Interpret for Loop { impl Interpret for Loop {

View File

@ -30,7 +30,7 @@ pub mod temp_type_impl {
function::Function, function::Function,
BuiltIn, Callable, Environment, BuiltIn, Callable, Environment,
}; };
use std::ops::*; use std::{ops::*, rc::Rc};
type Integer = isize; type Integer = isize;
@ -50,11 +50,13 @@ pub mod temp_type_impl {
/// A unicode character /// A unicode character
Char(char), Char(char),
/// A string /// A string
String(String), String(Rc<str>),
/// A reference
Ref(Rc<ConValue>),
/// An Array /// An Array
Array(Vec<ConValue>), Array(Rc<[ConValue]>),
/// A tuple /// A tuple
Tuple(Vec<ConValue>), Tuple(Rc<[ConValue]>),
/// An exclusive range /// An exclusive range
RangeExc(Integer, Integer), RangeExc(Integer, Integer),
/// An inclusive range /// An inclusive range
@ -166,6 +168,7 @@ pub mod temp_type_impl {
char => ConValue::Char, char => ConValue::Char,
&str => ConValue::String, &str => ConValue::String,
String => ConValue::String, String => ConValue::String,
Rc<str> => ConValue::String,
Function => ConValue::Function, Function => ConValue::Function,
Vec<ConValue> => ConValue::Tuple, Vec<ConValue> => ConValue::Tuple,
&'static dyn BuiltIn => ConValue::BuiltIn, &'static dyn BuiltIn => ConValue::BuiltIn,
@ -199,7 +202,7 @@ pub mod temp_type_impl {
Add: add = [ Add: add = [
(ConValue::Empty, ConValue::Empty) => ConValue::Empty, (ConValue::Empty, ConValue::Empty) => ConValue::Empty,
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a + b), (ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a + b),
(ConValue::String(a), ConValue::String(b)) => ConValue::String(a + &b), (ConValue::String(a), ConValue::String(b)) => (a.to_string() + &b).into(),
_ => Err(Error::TypeError)? _ => Err(Error::TypeError)?
] ]
BitAnd: bitand = [ BitAnd: bitand = [
@ -259,6 +262,7 @@ pub mod temp_type_impl {
ConValue::Bool(v) => v.fmt(f), ConValue::Bool(v) => v.fmt(f),
ConValue::Char(v) => v.fmt(f), ConValue::Char(v) => v.fmt(f),
ConValue::String(v) => v.fmt(f), ConValue::String(v) => v.fmt(f),
ConValue::Ref(v) => write!(f, "&{v}"),
ConValue::Array(array) => { ConValue::Array(array) => {
'['.fmt(f)?; '['.fmt(f)?;
for (idx, element) in array.iter().enumerate() { for (idx, element) in array.iter().enumerate() {
@ -296,13 +300,15 @@ pub mod interpret;
pub mod function { pub mod function {
//! Represents a block of code which lives inside the Interpreter //! Represents a block of code which lives inside the Interpreter
use super::{Callable, ConValue, Environment, Error, IResult, Interpret}; use super::{Callable, ConValue, Environment, Error, IResult, Interpret};
use cl_ast::{Function as FnDecl, Identifier, Param}; use cl_ast::{Function as FnDecl, Identifier, Param};
use std::rc::Rc;
/// Represents a block of code which persists inside the Interpreter /// Represents a block of code which persists inside the Interpreter
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Function { pub struct Function {
/// Stores the contents of the function declaration /// Stores the contents of the function declaration
decl: Box<FnDecl>, decl: Rc<FnDecl>,
// /// Stores the enclosing scope of the function // /// Stores the enclosing scope of the function
// env: Box<Environment>, // env: Box<Environment>,
} }
@ -322,19 +328,17 @@ pub mod function {
name name
} }
fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> { fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
let FnDecl { name: Identifier(name), bind: declargs, body, sign: _ } = &*self.decl; let FnDecl { name: Identifier(name), bind, body, sign: _ } = &*self.decl;
// Check arg mapping // Check arg mapping
if args.len() != declargs.len() { if args.len() != bind.len() {
return Err(Error::ArgNumber { want: declargs.len(), got: args.len() }); return Err(Error::ArgNumber { want: bind.len(), got: args.len() });
} }
let Some(body) = body else { let Some(body) = body else {
return Err(Error::NotDefined(name.into())); return Err(Error::NotDefined(name.into()));
}; };
// TODO: completely refactor data storage // TODO: completely refactor data storage
let mut frame = env.frame("fn args"); let mut frame = env.frame("fn args");
for (Param { mutability: _, name: Identifier(name) }, value) in for (Param { mutability: _, name: Identifier(name) }, value) in bind.iter().zip(args) {
declargs.iter().zip(args)
{
frame.insert(name, Some(value.clone())); frame.insert(name, Some(value.clone()));
} }
match body.interpret(&mut frame) { match body.interpret(&mut frame) {
@ -364,10 +368,12 @@ pub mod env {
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
}; };
type StackFrame = HashMap<String, Option<ConValue>>;
/// Implements a nested lexical scope /// Implements a nested lexical scope
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Environment { pub struct Environment {
frames: Vec<(HashMap<String, Option<ConValue>>, &'static str)>, frames: Vec<(StackFrame, &'static str)>,
} }
impl Display for Environment { impl Display for Environment {
@ -444,10 +450,10 @@ pub mod env {
/// Resolves a variable immutably. /// Resolves a variable immutably.
/// ///
/// Returns a reference to the variable's contents, if it is defined and initialized. /// Returns a reference to the variable's contents, if it is defined and initialized.
pub fn get(&self, id: &str) -> IResult<&ConValue> { pub fn get(&self, id: &str) -> IResult<ConValue> {
for (frame, _) in self.frames.iter().rev() { for (frame, _) in self.frames.iter().rev() {
match frame.get(id) { match frame.get(id) {
Some(Some(var)) => return Ok(var), Some(Some(var)) => return Ok(var.clone()),
Some(None) => return Err(Error::NotInitialized(id.into())), Some(None) => return Err(Error::NotInitialized(id.into())),
_ => (), _ => (),
} }

View File

@ -1,16 +1,29 @@
// Calculate Fibonacci numbers // Calculate Fibonacci numbers
fn main() { fn main() {
for num in 0..=30 {
print("fib(", num, ") = ", fib_iterative(num))
}
for num in 0..=30 { for num in 0..=30 {
print("fib(", num, ") = ", fib(num)) print("fib(", num, ") = ", fib(num))
} }
} }
/// Implements the classic recursive definition of fib() /// The classic recursive definition of fib()
fn fib(a: i64) -> i64 { fn fib(a: i64) -> i64 {
if a > 1 { if a > 1 {
fib(a - 1) + fib(a - 2) fib(a - 1) + fib(a - 2)
} else { } else a
1
} }
/// The classic iterative algorithm for fib()
fn fib_iterative(n: i64) -> i64 {
let mut a = 0;
let mut b = 1;
let mut c = 1;
for _ in 0..n {
a = b;
b = c;
c = a + b;
} else a
} }

View File

@ -1,11 +1,11 @@
// FizzBuzz, using the unstable variadic-`print` builtin // FizzBuzz, using the unstable variadic-`print` builtin
fn main() { fn main() {
fizz_buzz(10, 20) fizzbuzz(10, 20)
} }
// Outputs FizzBuzz for numbers between `start` and `end`, inclusive // Outputs FizzBuzz for numbers between `start` and `end`, inclusive
fn fizz_buzz(start: i128, end: i128) { fn fizzbuzz(start: i128, end: i128) {
for x in start..=end { for x in start..=end {
print(if x % 15 == 0 { print(if x % 15 == 0 {
"FizzBuzz" "FizzBuzz"