Skip to content

Commit

Permalink
Consistency with calling and auto-calling (#59)
Browse files Browse the repository at this point in the history
This is NOT a code-consistency or refactor PR. This is just to get the
following working:

- [x] Make lists be lists (no auto-calling for them anymore)
- [x] Add a method to `Engine` specifically for calling an Expr, like a
list or function
- [x] Make s-expressions be auto-called just like functions
  • Loading branch information
Vandesm14 authored Jun 30, 2024
1 parent 6750f92 commit 388641d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
34 changes: 24 additions & 10 deletions stack-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,18 @@ impl Engine {
Ok(context)
}

#[allow(clippy::only_used_in_recursion)]
pub fn call_expr(
&self,
mut context: Context,
expr: Expr,
) -> Result<Context, RunError> {
let expr = context.scan_expr(expr)?;
match expr.kind {
ExprKind::List(exprs) => self.run(context, exprs),
_ => self.run_expr(context, expr),
}
}

pub fn run_expr(
&self,
mut context: Context,
Expand All @@ -122,6 +133,7 @@ impl Engine {
| ExprKind::Integer(_)
| ExprKind::Float(_)
| ExprKind::String(_)
| ExprKind::List(_)
| ExprKind::Record(_) => {
context.stack_push(expr)?;
Ok(context)
Expand Down Expand Up @@ -179,6 +191,9 @@ impl Engine {
CallResult::None => unreachable!(),
}
}
}
if let ExprKind::SExpr { .. } = item.kind {
self.call_expr(context, item)
} else {
if let Some(journal) = context.journal_mut() {
journal.push_op(JournalOp::Call(expr.clone()));
Expand All @@ -202,7 +217,6 @@ impl Engine {
context.stack_push(*x)?;
Ok(context)
}
ExprKind::List(ref x) => self.run(context, x.to_vec()),
ExprKind::Function {
ref scope,
ref body,
Expand Down Expand Up @@ -451,7 +465,7 @@ mod tests {
// TODO: Move test for lets into a better place?
#[test]
fn can_use_lets() {
let source = Source::new("", "10 2 '[a b -] '[a b] let");
let source = Source::new("", "10 2 [a b -] [a b] let");
let mut lexer = Lexer::new(source);
let exprs = crate::parser::parse(&mut lexer).unwrap();

Expand All @@ -471,7 +485,7 @@ mod tests {

#[test]
fn lets_take_precedence_over_scope() {
let source = Source::new("", "0 'a def 1 '[a] '[a] let");
let source = Source::new("", "0 'a def 1 [a] [a] let");
let mut lexer = Lexer::new(source);
let exprs = crate::parser::parse(&mut lexer).unwrap();

Expand All @@ -491,7 +505,7 @@ mod tests {

#[test]
fn lets_do_not_act_as_overlays() {
let source = Source::new("", "0 'a def 1 '[a 2 'a def a] '[a] let a");
let source = Source::new("", "0 'a def 1 [a 2 'a def a] [a] let a");
let mut lexer = Lexer::new(source);
let exprs = crate::parser::parse(&mut lexer).unwrap();

Expand All @@ -515,7 +529,7 @@ mod tests {

#[test]
fn functions_work_in_lets() {
let source = Source::new("", "0 'a def 1 '[(fn a 2 'a def a)] '[a] let a");
let source = Source::new("", "0 'a def 1 [(fn a 2 'a def a)] [a] let a");
let mut lexer = Lexer::new(source);
let exprs = crate::parser::parse(&mut lexer).unwrap();

Expand All @@ -542,9 +556,9 @@ mod tests {
let source = Source::new(
"",
"0 'a def
1 '[a] '[a] let
1 '[(fn! a)] '[a] let
1 '[(fn a)] '[a] let
1 [a] [a] let
1 [(fn! a)] [a] let
1 [(fn a)] [a] let
a",
);
let mut lexer = Lexer::new(source);
Expand All @@ -571,7 +585,7 @@ mod tests {

#[test]
fn lets_can_set() {
let source = Source::new("", "1 '[a 2 'a set a] '[a] let");
let source = Source::new("", "1 [a 2 'a set a] [a] let");
let mut lexer = Lexer::new(source);
let exprs = crate::parser::parse(&mut lexer).unwrap();

Expand Down
6 changes: 3 additions & 3 deletions stack-core/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ impl Intrinsic {
let cond = context.stack_pop(&expr)?;

if cond.kind.is_truthy() {
context = engine.run_expr(context, body)?;
context = engine.call_expr(context, body)?;
}

Ok(context)
Expand All @@ -812,7 +812,7 @@ impl Intrinsic {
// MARK: Call
Self::Call => {
let item = context.stack_pop(&expr)?;
engine.run_expr(context, item)
engine.call_expr(context, item)
}

// MARK: Let
Expand Down Expand Up @@ -851,7 +851,7 @@ impl Intrinsic {
}

context.push_scope(scope);
context = engine.run_expr(context, body)?;
context = engine.call_expr(context, body)?;
context.pop_scope();

if let Some(journal) = context.journal_mut() {
Expand Down
2 changes: 1 addition & 1 deletion stack-core/tests/intrinsics/pop.stack
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(pop '[1 2 3])
(pop [1 2 3])
(pop "he")
2 changes: 1 addition & 1 deletion stack-core/tests/intrinsics/push.stack
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'[]
[]

(push _ 1)
(push _ 2)
Expand Down
2 changes: 1 addition & 1 deletion stack-core/tests/intrinsics/record.stack
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
drop

;; Test casting
(cast '[["name" "john"] ["type" "person"]] "record")
(cast [["name" "john"] ["type" "person"]] "record")

(= "john" (prop _ "name")) swap
(= "person" (prop _ "type")) swap
Expand Down

0 comments on commit 388641d

Please sign in to comment.