Skip to content

Commit

Permalink
io_trait: Add JkStdReader and fix tests
Browse files Browse the repository at this point in the history
JkStdReader is the default JkReader implementation. This way is the user
does not gives a custom reader, we can use this one
  • Loading branch information
Skallwar committed May 13, 2022
1 parent 6f49cfa commit de6c1eb
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 69 deletions.
2 changes: 1 addition & 1 deletion examples/simple-repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() -> Result<()> {
let stdin = io::stdin();
let mut stdout = io::stdout();

let mut ctx = Context::new();
let mut ctx = Context::new(None);
let mut input = String::new();

let mut prompt = "> ";
Expand Down
11 changes: 1 addition & 10 deletions interpreter/jinko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use colored::Colorize;
use jinko::context::Context;
use jinko::error::{ErrKind, Error};
use jinko::instance::{FromObjectInstance, ObjectInstance};
use jinko::io_trait::JkReader;
use jinko::typechecker::CheckedType;
use jinko::value::{JkBool, JkFloat, JkInt};

Expand All @@ -18,14 +17,6 @@ use args::Args;
use repl::Repl;
use std::{fs, path::Path};

#[derive(Clone)]
struct InterpreterReader {}
impl JkReader for InterpreterReader {
fn read_to_string(&self, path: &str) -> Result<String, Error> {
unimplemented!("POV t\'as pas implem");
}
}

// FIXME: Add documentation
pub type InteractResult = Result<(Option<ObjectInstance>, Context), Error>;

Expand Down Expand Up @@ -102,7 +93,7 @@ fn run_tests(ctx: &mut Context) -> Result<Option<ObjectInstance>, Error> {
fn handle_input(args: &Args, file: &Path) -> InteractResult {
let input = fs::read_to_string(file)?;

let mut ctx = Context::new(Box::new(InterpreterReader {}));
let mut ctx = Context::new(None);

if !args.nostdlib() {
ctx.init_stdlib()?;
Expand Down
12 changes: 1 addition & 11 deletions interpreter/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use std::path::PathBuf;

use jinko::{
context::Context,
error::Error,
instance::{FromObjectInstance, ObjectInstance},
instruction::Instruction,
io_trait::JkReader,
log,
typechecker::CheckedType,
value::JkConstant,
Expand Down Expand Up @@ -42,14 +40,6 @@ impl std::fmt::Display for ReplInstance {
}
}

#[derive(Clone)]
pub struct ReplReader {}
impl JkReader for ReplReader {
fn read_to_string(&self, path: &str) -> Result<String, Error> {
unimplemented!("POV t\'as pas implem");
}
}

pub struct Repl {
ctx: Option<Context>,
reader: Interface<DefaultTerminal>,
Expand Down Expand Up @@ -85,7 +75,7 @@ impl Repl {

let mut ctx = match self.ctx {
Some(ctx) => ctx,
None => Context::new(Box::new(ReplReader {})),
None => Context::new(None),
};

Repl::setup_context(&mut ctx);
Expand Down
20 changes: 9 additions & 11 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::builtins::Builtins;
use crate::error::{ErrKind, Error, ErrorHandler};
use crate::instance::ObjectInstance;
use crate::instruction::{Block, FunctionDec, FunctionKind, Instruction, TypeDec, Var};
use crate::io_trait::JkReader;
use crate::io_trait::{JkReader, JkStdReader};
use crate::log;
use crate::parser;
use crate::typechecker::CheckedType;
Expand Down Expand Up @@ -71,8 +71,6 @@ pub struct Context {
/// Errors being kept by the context
pub error_handler: ErrorHandler,

reader: Box<dyn JkReader>,

/// Various passes ran by the context
pub(crate) typechecker: TypeCtx,
}
Expand All @@ -88,7 +86,8 @@ impl Context {
}

/// Create a new empty context without the standard library
pub fn new(reader: Box<dyn JkReader>) -> Context {
pub fn new(custom_reader: Option<Box<dyn JkReader>>) -> Context {
let reader = custom_reader.unwrap_or_else(|| Box::new(JkStdReader {}));
let mut ctx = Context {
debug_mode: false,
code: None,
Expand All @@ -103,7 +102,6 @@ impl Context {
external_libs: Vec::new(),
error_handler: ErrorHandler::default(),
typechecker: TypeCtx::new(reader.clone()),
reader,
};

ctx.scope_enter();
Expand Down Expand Up @@ -443,7 +441,7 @@ mod tests {
let f0 = FunctionDec::new("f0".to_owned(), None, vec![], vec![]);
let f0_copy = FunctionDec::new("f0".to_owned(), None, vec![], vec![]);

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(i.add_function(f0), Ok(()));
assert!(i.add_function(f0_copy).is_err());
Expand All @@ -454,15 +452,15 @@ mod tests {
let v0 = Var::new("v0".to_owned());
let v0_copy = Var::new("v0".to_owned());

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(i.add_variable(v0), Ok(()));
assert!(i.add_variable(v0_copy).is_err());
}

#[test]
fn t_print_scopemap() {
let mut ctx = Context::new();
let mut ctx = Context::new(None);
ctx.init_stdlib().unwrap();
ctx.execute().unwrap();

Expand All @@ -475,7 +473,7 @@ mod tests {

#[test]
fn t_print_eb() {
let mut ctx = Context::new();
let mut ctx = Context::new(None);

ctx.add_variable(Var::new(String::from("a_var_named_a")))
.unwrap();
Expand All @@ -497,7 +495,7 @@ mod tests {

#[test]
fn t_eval() {
let mut ctx = Context::new();
let mut ctx = Context::new(None);
let input = String::from("my_var = 1");

ctx.eval(&input).unwrap();
Expand All @@ -508,7 +506,7 @@ mod tests {

#[test]
fn t_double_eval() {
let mut ctx = Context::new();
let mut ctx = Context::new(None);

let mut input = String::from("my_var = 1");
ctx.eval(&input).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ mod tests {

#[test]
fn create_map_different_size() {
let mut ctx = TypeCtx::new();
use crate::io_trait::JkStdReader;
let mut ctx = TypeCtx::new(Box::new(JkStdReader {}));

assert!(GenericMap::create(&[ty!("int"), ty!("float")], &[ty!("T")], &mut ctx).is_err());
}
Expand Down
2 changes: 1 addition & 1 deletion src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod tests {
use crate::value::JkInt;

fn setup() -> Context {
let mut ctx = Context::new();
let mut ctx = Context::new(None);

let inst = constructs::expr(span!("type Point(x: int, y: int);"))
.unwrap()
Expand Down
8 changes: 4 additions & 4 deletions src/instruction/binary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod tests {
Operator::new("-"),
);

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(
binary_op.rhs().execute(&mut i).unwrap(),
Expand All @@ -230,7 +230,7 @@ mod tests {
Operator::new("-"),
);

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(binary_op.operator(), Operator::Sub);

Expand All @@ -248,7 +248,7 @@ mod tests {
let boxed_output = crate::parser::constructs::expr(input).unwrap().1;
let output = boxed_output.downcast_ref::<BinaryOp>().unwrap();

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(
output.execute(&mut i).unwrap(),
Expand Down Expand Up @@ -310,7 +310,7 @@ mod tests {

macro_rules! binop_assert {
($expr:expr) => {{
let mut ctx = Context::new();
let mut ctx = Context::new(None);
let expr = crate::parser::constructs::expr(nom_locate::LocatedSpan::new_extra(
stringify!($expr),
None,
Expand Down
8 changes: 4 additions & 4 deletions src/instruction/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ mod tests {
fn block_execute_empty() {
let b = Block::new();

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(b.execute(&mut i), None);
assert!(!i.error_handler.has_errors());
Expand All @@ -236,7 +236,7 @@ mod tests {
];
b.set_instructions(instr);

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(b.execute(&mut i), None);
assert!(!i.error_handler.has_errors());
Expand All @@ -256,7 +256,7 @@ mod tests {
b.add_instruction(last);
b.set_statement(false);

let mut i = Context::new();
let mut i = Context::new(None);

assert_eq!(b.execute(&mut i).unwrap(), JkInt::from(18).to_instance());
assert!(!i.error_handler.has_errors());
Expand All @@ -272,7 +272,7 @@ mod tests {
.unwrap()
.1;

let mut ctx = Context::new();
let mut ctx = Context::new(None);

assert_eq!(ctx.type_check(b.as_mut()).unwrap(), CheckedType::Void)
}
Expand Down
2 changes: 1 addition & 1 deletion src/instruction/field_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ mod tests {
#[test]
#[ignore] // FIXME: Do not ignore once we can type instance fields
fn t_valid_multi_field_access() {
let mut ctx = Context::new();
let mut ctx = Context::new(None);

let inst = constructs::expr(span!("type Pair1(x: int, y: int)"))
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions src/instruction/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ mod tests {
use crate::instance::ToObjectInstance;
use crate::value::JkInt;

let mut i = Context::new();
let mut i = Context::new(None);
let func_dec = constructs::expr(span!("func __second(f: int, s: int) -> int { s }"))
.unwrap()
.1;
Expand All @@ -490,7 +490,7 @@ mod tests {
use crate::instance::ToObjectInstance;
use crate::value::JkInt;

let mut i = Context::new();
let mut i = Context::new(None);
let func_dec = constructs::expr(span!("func add(a: int, b: int) -> int { a + b }"))
.unwrap()
.1;
Expand All @@ -509,7 +509,7 @@ mod tests {
use crate::instance::ToObjectInstance;
use crate::value::JkInt;

let mut i = Context::new();
let mut i = Context::new(None);
let func_dec = constructs::expr(span!("func one() -> int { one = 1; one }"))
.unwrap()
.1;
Expand Down
6 changes: 3 additions & 3 deletions src/instruction/function_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ mod tests {
FunctionDec::new("fn".to_owned(), Some(TypeId::from("int")), vec![], vec![]);
function.set_kind(FunctionKind::Ext);

let mut ctx = Context::new();
let mut ctx = Context::new(None);

assert_eq!(ctx.type_check(&mut function).unwrap(), CheckedType::Void);
assert!(!ctx.error_handler.has_errors());
Expand All @@ -445,7 +445,7 @@ mod tests {
let block = constructs::block(span!("{ 15 }")).unwrap().1;
function.set_block(block);

let mut ctx = Context::new();
let mut ctx = Context::new(None);

assert_eq!(ctx.type_check(&mut function).unwrap(), CheckedType::Void);
assert!(!ctx.error_handler.has_errors());
Expand All @@ -464,7 +464,7 @@ mod tests {
let block = constructs::block(span!("{ 15 }")).unwrap().1;
function.set_block(block);

let mut ctx = Context::new();
let mut ctx = Context::new(None);

assert!(ctx.type_check(&mut function).is_err());
assert!(ctx.error_handler.has_errors());
Expand Down
4 changes: 2 additions & 2 deletions src/instruction/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod tests {
use crate::instance::ToObjectInstance;
use crate::value::{JkBool, JkInt};

let mut ctx = Context::new();
let mut ctx = Context::new(None);

let mut if_block = Block::new();
let mut else_block = Block::new();
Expand All @@ -230,7 +230,7 @@ mod tests {
use crate::instance::ToObjectInstance;
use crate::value::{JkBool, JkInt};

let mut ctx = Context::new();
let mut ctx = Context::new(None);

let mut if_block = Block::new();
let mut else_block = Block::new();
Expand Down
13 changes: 9 additions & 4 deletions src/instruction/incl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::error::{ErrKind, Error};
use crate::generics::GenericUser;
use crate::instance::ObjectInstance;
use crate::instruction::{InstrKind, Instruction};
use crate::io_trait::JkReader;
use crate::location::SpanTuple;
use crate::log;
use crate::parser::constructs;
Expand Down Expand Up @@ -43,10 +44,14 @@ impl Incl {
}
}

fn fetch_instructions(&self, formatted: &Path) -> Result<Vec<Box<dyn Instruction>>, Error> {
fn fetch_instructions(
&self,
formatted: &Path,
reader: &dyn JkReader,
) -> Result<Vec<Box<dyn Instruction>>, Error> {
log!("final path: {}", &format!("{:?}", formatted));

let input = std::fs::read_to_string(&formatted)?;
let input = reader.read_to_string(formatted.to_str().unwrap())?;

// We can't just parse the input, since it adds the instructions
// to an entry block in order to execute them. What we can do, is
Expand Down Expand Up @@ -197,7 +202,7 @@ impl TypeCheck for Incl {
return CheckedType::Void;
}

let instructions = match self.fetch_instructions(&final_path) {
let instructions = match self.fetch_instructions(&final_path, ctx.reader()) {
Ok(instructions) => instructions,
Err(e) => {
ctx.error(e);
Expand Down Expand Up @@ -244,7 +249,7 @@ mod tests {

#[test]
fn tc_typecheck_stdlib() {
let mut ctx = Context::new();
let mut ctx = Context::new(None);
ctx.execute().unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion src/instruction/method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ mod tests {

#[test]
fn t_execute() {
let mut ctx = Context::new();
let mut ctx = Context::new(None);
let mut func_dec = constructs::expr(span!("func __first(a: int, b: int) -> int { a }"))
.unwrap()
.1;
Expand Down
Loading

0 comments on commit de6c1eb

Please sign in to comment.