Skip to content

Commit

Permalink
import
Browse files Browse the repository at this point in the history
  • Loading branch information
todesking committed Apr 19, 2024
1 parent be9ec69 commit 2aa461b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ pub enum Expr {
},
#[serde(rename = "expr_neg")]
Negate { expr: Box<Expr> },
#[serde(rename = "expr_import")]
Import(ExprStr),
}
#[derive(Debug, PartialEq, Eq, Clone, Deserialize)]
#[serde(rename = "expr_str")]
pub struct ExprStr {
#[serde(deserialize_with = "deserialize_string_content_opt")]
pub content: Rc<String>,
}

fn deserialize_string_content_opt<'de, D: serde::Deserializer<'de>>(
Expand Down
6 changes: 5 additions & 1 deletion src/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ast::ArrayItem;
use crate::ast::ExprStr;
use crate::ast::Ident;
use crate::ast::ObjItem;
use crate::ast::TopTerm;
Expand All @@ -22,7 +23,6 @@ use crate::Program;
use crate::Value;
use gc::Gc;

use std::borrow::Borrow;
use std::collections::HashMap;
use std::path::PathBuf;

Expand Down Expand Up @@ -384,6 +384,10 @@ impl<L: ModuleLoader> RuntimeContext<L> {
let index = self.eval_expr(index, local_env, current_module)?;
self.get_index(&value, &index)
}
Expr::Import(ExprStr { content }) => {
let module = self.load_module(&ModulePath::new((**content).to_owned()))?;
Ok(module.pub_object().clone())
}
}
}

Expand Down
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ mod test {
use crate::module::{ModulePath, NullModuleLoader};
use crate::value::ObjectKey;

use self::module::FsModuleLoader;
use self::module::{FsModuleLoader, Module};

use super::*;
use gc::Gc;
use pretty_assertions::assert_eq;

#[ctor::ctor]
Expand Down Expand Up @@ -99,6 +100,14 @@ mod test {
};
}

fn new_rt() -> (RuntimeContext<FsModuleLoader>, Gc<Module>) {
let mut ctx = RuntimeContext::with_paths(vec!["lib"]);
let module = ctx
.new_module(crate::module::ModulePath::new("__test__"))
.unwrap();
(ctx, module)
}

#[test]
fn int_lit() {
assert_eval_ok!("1", 1);
Expand Down Expand Up @@ -403,4 +412,20 @@ mod test {
);
assert_eq!(m.pub_object(), &Value::from(object_value! {a: 1}));
}

#[test]
fn import() {
let (mut rt, m) = new_rt();

rt.eval_program_in_module(
&parse_program(r#"let prelude = import "std/prelude";"#).unwrap(),
&m,
)
.unwrap();
assert_eq!(
rt.eval_expr_in_module(&parse_expr("prelude.true").unwrap(), &m)
.unwrap(),
Value::bool(true)
);
}
}
5 changes: 1 addition & 4 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ impl TryFrom<&Value> for String {

fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
Value::Atom(a) => match a {
AtomValue::Str(s) => Ok((**s).clone()),
_ => Err(EvalError::type_error("String", value.clone())),
},
Value::Atom(AtomValue::Str(s)) => Ok((**s).clone()),
_ => Err(EvalError::type_error("String", value.clone())),
}
}
Expand Down
5 changes: 5 additions & 0 deletions tree-sitter-borlang/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const common_rules = {
$.expr_prop,
$.expr_prop_opt,
$.expr_index,
$.expr_import,
),
expr_int: _ => /[0-9]+/,
expr_str: $ => seq(
Expand Down Expand Up @@ -163,6 +164,10 @@ const common_rules = {
field('index', $._expr),
']',
)),
expr_import: $ => seq(
'import',
$.expr_str
),
ident: _ => /[A-Za-z_][a-z0-9_]*/,
op_plus: _ => '+',
op_minus: _ => '-',
Expand Down

0 comments on commit 2aa461b

Please sign in to comment.