Skip to content

Commit

Permalink
Interpreter for validated
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Nov 25, 2023
1 parent 8fcabaa commit b775b16
Show file tree
Hide file tree
Showing 19 changed files with 295 additions and 276 deletions.
21 changes: 11 additions & 10 deletions compiler/src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::passes::validate::TLit;
use crate::utils::gen_sym::UniqueSym;
use derive_more::Display;
use std::collections::HashMap;
use std::fmt::Display;
Expand Down Expand Up @@ -41,33 +42,33 @@ impl IO for TestIO {
}

#[derive(Eq, PartialEq, Clone, Debug, Display)]
pub enum Val<'p, A: Copy + Hash + Eq + Display> {
pub enum Val<'p> {
#[display(fmt = "{val}")]
Int { val: i64 },
#[display(fmt = "{}", r#"if *val { "true" } else { "false" }"#)]
Bool { val: bool },
#[display(fmt = "unit")]
Unit,
#[display(fmt = "fn pointer `{sym}`")]
Function { sym: A },
Function { sym: UniqueSym<'p> },
#[display(fmt = "stdlib function `{sym}`")]
StdlibFunction { sym: &'p str, },
#[display(fmt = "struct instance")]
StructInstance {
fields: HashMap<&'p str, Val<'p, A>>,
},
StructInstance { fields: HashMap<&'p str, Val<'p>> },
}

impl<'p, A: Copy + Hash + Eq + Display> From<TLit> for Val<'p, A> {
impl<'p> From<TLit> for Val<'p> {
fn from(value: TLit) -> Self {
match value {
TLit::I64 { val } => Val::Int { val: val },
TLit::I64 { val } => Val::Int { val },
TLit::U64 { val } => Val::Int { val: val as i64 },
TLit::Bool { val } => Val::Bool { val },
TLit::Unit => Val::Unit,
}
}
}

impl<'p, A: Copy + Hash + Eq + Display> Val<'p, A> {
impl<'p> Val<'p> {
pub fn int(&self) -> i64 {
match self {
Val::Int { val } => *val,
Expand All @@ -82,14 +83,14 @@ impl<'p, A: Copy + Hash + Eq + Display> Val<'p, A> {
}
}

pub fn fun(&self) -> A {
pub fn fun(&self) -> UniqueSym<'p> {
match self {
Val::Function { sym } => *sym,
_ => panic!(),
}
}

pub fn strct(&self) -> &HashMap<&'p str, Val<'p, A>> {
pub fn strct(&self) -> &HashMap<&'p str, Val<'p>> {
match self {
Val::StructInstance { fields } => fields,
_ => panic!(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/eliminate/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'p> EVal<'p> {
}
}

impl<'p> From<EVal<'p>> for Val<'p, UniqueSym<'p>> {
impl<'p> From<EVal<'p>> for Val<'p> {
fn from(value: EVal<'p>) -> Self {
match value {
EVal::Int { val } => Val::Int { val },
Expand Down
14 changes: 7 additions & 7 deletions compiler/src/passes/explicate/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ use crate::utils::gen_sym::UniqueSym;
use crate::utils::push_map::PushMap;

impl<'p> PrgExplicated<'p> {
pub fn interpret(&self, io: &mut impl IO) -> Val<'p, UniqueSym<'p>> {
pub fn interpret(&self, io: &mut impl IO) -> Val<'p> {
self.interpret_tail(&self.blocks[&self.entry], &mut PushMap::default(), io)
}

fn interpret_tail(
&self,
tail: &CTail<'p>,
scope: &mut PushMap<UniqueSym<'p>, Val<'p, UniqueSym<'p>>>,
scope: &mut PushMap<UniqueSym<'p>, Val<'p>>,
io: &mut impl IO,
) -> Val<'p, UniqueSym<'p>> {
) -> Val<'p> {
match tail {
CTail::Return { expr, .. } => self.interpret_atom(expr, scope),
CTail::Seq { sym, bnd, tail } => {
Expand All @@ -36,9 +36,9 @@ impl<'p> PrgExplicated<'p> {
pub fn interpret_expr(
&self,
_expr: &CExpr<'p>,
_scope: &mut PushMap<UniqueSym<'p>, Val<'p, UniqueSym<'p>>>,
_scope: &mut PushMap<UniqueSym<'p>, Val<'p>>,
_io: &mut impl IO,
) -> Val<'p, UniqueSym<'p>> {
) -> Val<'p> {
todo!()
// match expr {
// CExpr::Prim { op, args, .. } => match (op, args.as_slice()) {
Expand Down Expand Up @@ -168,8 +168,8 @@ impl<'p> PrgExplicated<'p> {
pub fn interpret_atom(
&self,
atom: &Atom<'p>,
scope: &PushMap<UniqueSym<'p>, Val<'p, UniqueSym<'p>>>,
) -> Val<'p, UniqueSym<'p>> {
scope: &PushMap<UniqueSym<'p>, Val<'p>>,
) -> Val<'p> {
match atom {
Atom::Val { val } => (*val).into(),
Atom::Var { sym } => scope[sym].clone(),
Expand Down
219 changes: 0 additions & 219 deletions compiler/src/passes/parse/interpreter.rs

This file was deleted.

1 change: 0 additions & 1 deletion compiler/src/passes/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#[allow(clippy::all, clippy::pedantic)]
mod grammar;
mod display;
pub mod interpreter;
pub mod parse;
#[cfg(test)]
mod tests;
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/passes/parse/parse.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::passes::parse::grammar::ProgramParser;
use crate::passes::parse::PrgParsed;
#[cfg(test)]
use derive_name::VariantName;
use itertools::Itertools;
use lalrpop_util::lexer::Token;
use lalrpop_util::ParseError;
use miette::Diagnostic;
use thiserror::Error;

#[cfg_attr(test, derive(VariantName))]
#[derive(Error, Debug, Diagnostic)]
pub enum PrettyParseError {
#[error("Parse error: Invalid token.")]
Expand Down
19 changes: 6 additions & 13 deletions compiler/src/passes/parse/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::passes::parse::parse::{parse_program, PrettyParseError};
use crate::utils::split_test::split_test;
use derive_name::VariantName;
use test_each_file::test_each_file;

fn parse([test]: [&str; 1]) {
Expand All @@ -9,23 +10,15 @@ fn parse([test]: [&str; 1]) {

match (result, expected_error) {
(Ok(_), None) => {}
(Ok(_), Some(expected_error)) => {
panic!("Expected error `{expected_error}`, but succeeded instead.")
}
(Err(error), None) => {
panic!("Should have succeeded, but panicked with `{error}` instead")
}
(Ok(_), Some(expected_error)) => {
panic!("Expected error `{expected_error}`, but succeeded instead.")
(Err(error), Some(expected_error)) => {
assert_eq!(error.variant_name(), expected_error);
}
(Err(error), Some(expected_error)) => match error {
PrettyParseError::InvalidToken { .. } => {
assert_eq!(expected_error, "InvalidToken")
}
PrettyParseError::UnexpectedToken { .. } => {
assert_eq!(expected_error, "UnexpectedToken")
}
PrettyParseError::UnexpectedEOF { .. } => {
assert_eq!(expected_error, "UnexpectedEOF")
}
},
}
}

Expand Down
Loading

0 comments on commit b775b16

Please sign in to comment.