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 21, 2022
1 parent a250483 commit 15aed25
Show file tree
Hide file tree
Showing 23 changed files with 167 additions and 142 deletions.
173 changes: 101 additions & 72 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ libloading = { version = "0.7", optional = true }
lazy_static = "1.4"

[features]
default = ["repl", "ffi"]
default = ["repl", "ffi", "std"]
repl = ["linefeed"]
ffi = ["libloading", "libffi"]
std = []

[dev-dependencies]
libc = "0.2"
Expand Down
3 changes: 2 additions & 1 deletion examples/simple-repl.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use jinko::context::Context;
use jinko::io_trait::JkStdReader;
use std::io::{self, Result, Write};

fn main() -> Result<()> {
let stdin = io::stdin();
let mut stdout = io::stdout();

let mut ctx = Context::new();
let mut ctx = Context::new(Box::new(JkStdReader {}));
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(Box::new(jinko::io_trait::JkStdReader {}));

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,
typechecker::CheckedType,
value::JkConstant,
};
Expand Down Expand Up @@ -41,14 +39,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 @@ -82,7 +72,7 @@ impl Repl {
pub fn launch(self) -> InteractResult {
let mut ctx = match self.ctx {
Some(ctx) => ctx,
None => Context::new(Box::new(ReplReader {})),
None => Context::new(Box::new(jinko::io_trait::JkStdReader {})),
};

Repl::setup_context(&mut ctx);
Expand Down
17 changes: 7 additions & 10 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ pub struct Context {
pub entry_point: FunctionDec,
/// Errors being kept by the context
pub error_handler: ErrorHandler,

reader: Box<dyn JkReader>,
}

impl Context {
Expand All @@ -86,12 +84,11 @@ impl Context {
#[cfg(feature = "ffi")]
external_libs: Vec::new(),
scope_map: ScopeMap::new(),
typechecker: TypeCtx::new(reader.clone()),
typechecker: TypeCtx::new(reader),
debug_mode: false,
code: None,
entry_point: Self::new_entry(),
error_handler: ErrorHandler::default(),
reader,
};

ctx.scope_enter();
Expand Down Expand Up @@ -424,7 +421,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(Box::new(crate::io_trait::JkStdReader {}));

assert_eq!(i.add_function(f0), Ok(()));
assert!(i.add_function(f0_copy).is_err());
Expand All @@ -435,15 +432,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(Box::new(crate::io_trait::JkStdReader {}));

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(Box::new(crate::io_trait::JkStdReader {}));
ctx.init_stdlib().unwrap();
ctx.execute().unwrap();

Expand All @@ -456,7 +453,7 @@ mod tests {

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

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

#[test]
fn t_eval() {
let mut ctx = Context::new();
let mut ctx = Context::new(Box::new(crate::io_trait::JkStdReader {}));
let input = String::from("my_var = 1");

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

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

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 @@ -184,7 +184,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(Box::new(crate::io_trait::JkStdReader {}));

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 @@ -203,7 +203,7 @@ mod tests {
Operator::new("-"),
);

let mut i = Context::new();
let mut i = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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

let mut i = Context::new();
let mut i = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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

Expand All @@ -243,7 +243,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(Box::new(crate::io_trait::JkStdReader {}));

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

macro_rules! binop_assert {
($expr:expr) => {{
let mut ctx = Context::new();
let mut ctx = Context::new(Box::new(crate::io_trait::JkStdReader {}));
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 @@ -212,7 +212,7 @@ mod tests {
fn block_execute_empty() {
let b = Block::new();

let mut i = Context::new();
let mut i = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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

let mut i = Context::new();
let mut i = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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

let mut i = Context::new();
let mut i = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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

let mut ctx = Context::new();
let mut ctx = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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 @@ -191,7 +191,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(Box::new(crate::io_trait::JkStdReader {}));

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 @@ -448,7 +448,7 @@ mod tests {
use crate::instance::ToObjectInstance;
use crate::value::JkInt;

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

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

let mut i = Context::new();
let mut i = Context::new(Box::new(crate::io_trait::JkStdReader {}));
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 @@ -426,7 +426,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(Box::new(crate::io_trait::JkStdReader {}));

assert_eq!(ctx.type_check(&mut function).unwrap(), CheckedType::Void);
assert!(!ctx.error_handler.has_errors());
Expand All @@ -441,7 +441,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(Box::new(crate::io_trait::JkStdReader {}));

assert_eq!(ctx.type_check(&mut function).unwrap(), CheckedType::Void);
assert!(!ctx.error_handler.has_errors());
Expand All @@ -460,7 +460,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(Box::new(crate::io_trait::JkStdReader {}));

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 @@ -203,7 +203,7 @@ mod tests {
use crate::instance::ToObjectInstance;
use crate::value::{JkBool, JkInt};

let mut ctx = Context::new();
let mut ctx = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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

let mut ctx = Context::new();
let mut ctx = Context::new(Box::new(crate::io_trait::JkStdReader {}));

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::parser::constructs;
use crate::typechecker::{CheckedType, TypeCheck, TypeCtx};
Expand Down Expand Up @@ -42,8 +43,12 @@ impl Incl {
}
}

fn fetch_instructions(&self, formatted: &Path) -> Result<Vec<Box<dyn Instruction>>, Error> {
let input = std::fs::read_to_string(&formatted)?;
fn fetch_instructions(
&self,
formatted: &Path,
reader: &dyn JkReader,
) -> Result<Vec<Box<dyn Instruction>>, Error> {
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 @@ -192,7 +197,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 @@ -239,7 +244,7 @@ mod tests {

#[test]
fn tc_typecheck_stdlib() {
let mut ctx = Context::new();
let mut ctx = Context::new(Box::new(crate::io_trait::JkStdReader {}));
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 @@ -113,7 +113,7 @@ mod tests {

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

0 comments on commit 15aed25

Please sign in to comment.