Skip to content

Commit

Permalink
fib test
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieCrisman committed Sep 10, 2022
1 parent 651f22b commit 6b3993e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
5 changes: 2 additions & 3 deletions EXPLORE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Because I find it interesting. Integrating Lua or some other language would prob

> Implemented
There's a few things I really admire from the [APL](https://en.wikipedia.org/wiki/APL_(programming_language)) family of langauges. In a way, Iverson's original notation that he developed was a form of refactoring of mathematics. One aspect was that there were a lot of ambiguous rules for order of evaluation. I wanted to play with that idea as well. So, classic mathematical precedence (like PEMDAS) has been tossed out the window. Things just simply evaluate left to right.
There's a few things I really admire from the [APL](<https://en.wikipedia.org/wiki/APL_(programming_language)>) family of langauges. In a way, Iverson's original notation that he developed was a form of refactoring of mathematics. One aspect was that there were a lot of ambiguous rules for order of evaluation. I wanted to play with that idea as well. So, classic mathematical precedence (like PEMDAS) has been tossed out the window. Things just simply evaluate left to right.

```
i <- 10 - 2 / 2 + 5
Expand Down Expand Up @@ -52,8 +52,7 @@ APL's reduce is about where my mind exploded on how wonderful a notation can be
// [0.25, 0.4, 0.5]
```

Functions aren't implemented yet, but would be nice to be able to reduce with functions as well. Need to explore how that would work.

Functions Reductions aren't implemented (yet?), but would be nice to be able to reduce with functions as well. Need to explore how that would work.

# Problems

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
use self::envir::Envir;
use self::symbol_table::SymbolTable;

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Instructions {
pub data: Vec<u8>,
}
Expand Down Expand Up @@ -716,7 +716,7 @@ impl DerefMut for Objects {
}

#[allow(clippy::upper_case_acronyms)]
#[derive(PartialEq, Clone, Debug, Hash)]
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum SymbolType {
UNKNOWN,
PLUS,
Expand Down Expand Up @@ -803,7 +803,7 @@ impl Object {
}
}

#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum ObjectType {
Number,
// String,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::HashMap;
use super::builtin::new_builtins;
//use std::ops::{DerefMut, Deref};

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SymbolScope {
Global,
Local,
Expand All @@ -16,7 +16,7 @@ pub enum SymbolScope {
Function,
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Symbol {
pub name: String,
pub scope: SymbolScope,
Expand Down
10 changes: 5 additions & 5 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::parser::token::TokenType as TT;

use std::fmt;

#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Identifier(pub String);

impl From<TT> for Identifier {
Expand Down Expand Up @@ -124,7 +124,7 @@ impl fmt::Display for Expression {
}
}

#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum Prefix {
// Tally,
Minus,
Expand All @@ -141,7 +141,7 @@ impl fmt::Display for Prefix {
}
}

#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum PostfixModifier {
Reduce,
}
Expand Down Expand Up @@ -170,7 +170,7 @@ impl fmt::Display for Postfix {
}
}

#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum Infix {
// Modifier(InfixModifier, Box<Infix>),
// Unknown,
Expand Down Expand Up @@ -277,7 +277,7 @@ pub fn precedence_of(t: TT) -> Precedence {
}
}

#[derive(PartialEq, PartialOrd, Debug, Clone)]
#[derive(PartialEq, Eq, PartialOrd, Debug, Clone)]
pub enum Precedence {
Lowest,
Equals, // ==
Expand Down
89 changes: 81 additions & 8 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl VM {
// sp,
// );
self.halted = true;
// println!("last: {:?}", self.last_popped());
return Ok(1);
}
let start_time = Instant::now();
Expand Down Expand Up @@ -827,10 +828,10 @@ impl VM {
// } else if left.object_type() == ObjectType::Hash {
// return self.execute_hash_index(left, index);
} else {
return Err(VMError::Reason(format!(
Err(VMError::Reason(format!(
"index operator not supported for {:?}",
left.object_type()
)));
)))
}
}

Expand Down Expand Up @@ -861,12 +862,10 @@ impl VM {
.clone(),
);
}
_ => {
return Err(VMError::Reason(format!(
"expected array type, but got {:?}",
left.object_type()
)))
}
_ => Err(VMError::Reason(format!(
"expected array type, but got {:?}",
left.object_type()
))),
}
}

Expand Down Expand Up @@ -1800,6 +1799,21 @@ mod tests {
"#
.to_string(),
},
VMTestCase {
expected_cycles: 140,
expected_top: Some(Object::Number(0.0)),
input: r#"
countDown <- fn(x) {
if (x = 0) {
return 0;
} else {
countDown(x - 1);
}
};
countDown(5);
"#
.to_string(),
},
VMTestCase {
expected_cycles: 90,
expected_top: Some(Object::Number(0.0)),
Expand Down Expand Up @@ -2086,6 +2100,65 @@ mod tests {
run_vm_test(tests);
}

#[test]
fn test_fib() {
let fib = r#"
me <- fn(n) {
a <- n
if (n > 1) {
a <- me(n-1) + me(n-2)
a
}
a
}
"#;
let tests: Vec<VMTestCase> = vec![
VMTestCase {
expected_cycles: 90,
expected_top: Some(Object::Number(1.0)),
input: format!("{}\nme(1)", fib),
},
VMTestCase {
expected_cycles: 90,
expected_top: Some(Object::Number(1.0)),
input: format!("{}\nme(2)", fib),
},
VMTestCase {
// no idea what the cycles are
expected_cycles: 550,
expected_top: Some(Object::Number(5.0)),
input: format!("{}\nme(5)", fib),
},
VMTestCase {
// no idea what the cycles are
expected_cycles: 2550,
expected_top: Some(Object::Number(21.0)),
input: format!("{}\nme(8)", fib),
},
// VMTestCase {
// expected_top: Some(Object::Int(150)),
// input: r#"
// let foo = fn() { let foo = 50; foo };
// let alsoFoo = fn() { let foo = 100; foo };
// foo() + alsoFoo();
// "#
// .to_string(),
// },
// VMTestCase {
// expected_top: Some(Object::Int(97)),
// input: r#"
// let global = 50;
// let minusOne = fn() { let foo = 1; global - foo };
// let minusTwo = fn() { let foo = 2; global - foo };
// minusOne() + minusTwo();
// "#
// .to_string(),
// },
];

run_vm_test(tests);
}

#[test]
fn test_first_class_function_calls() {
let tests: Vec<VMTestCase> = vec![
Expand Down
4 changes: 4 additions & 0 deletions test/test07.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
me <- fn(n) {
me(n-1) + me(n-2)
}
me(3)

0 comments on commit 6b3993e

Please sign in to comment.