Skip to content

Commit

Permalink
WIP DO NOT MERGE
Browse files Browse the repository at this point in the history
  • Loading branch information
gpwclark committed Feb 1, 2024
1 parent 64fef7c commit c571dd4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
29 changes: 15 additions & 14 deletions compile_state/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,21 @@ Multiply a sequence of numbers. (*) will return 1.
Section: math
Example:
(ns-import 'math)
(test::assert-equal 1 (*))
(test::assert-equal 5 (* 5))
(test::assert-equal 5 (* 1 5))
(test::assert-equal 5.0 (* 1.0 5))
(test::assert-equal 7.5 (* 1.5 5))
(test::assert-equal 7.5 (* 1.5 5.0))
(test::assert-equal 15 (* 3 5))
(test::assert-equal 8 (* 1 2 4))
(test::assert-equal 16 (* 2 2 4))
(test::assert-equal 16.0 (* 2 2.0 4))
(test::assert-equal 16.0 (* 2.0 2.0 4.0))
(test::assert-equal 55.0000000001 (* 100 0.55))
(test::assert-error (* 1 2 4 "5"))"#),
(assert-true #f)
;;(ns-import 'math)
;;(test::assert-equal 1 (*))
;;(test::assert-equal 5 (* 5))
;;(test::assert-equal 5 (* 1 5))
;;(test::assert-equal 5.0 (* 1.0 5))
;;(test::assert-equal 7.5 (* 1.5 5))
;;(test::assert-equal 7.5 (* 1.5 5.0))
;;(test::assert-equal 15 (* 3 5))
;;(test::assert-equal 8 (* 1 2 4))
;;(test::assert-equal 16 (* 2 2 4))
;;(test::assert-equal 16.0 (* 2 2.0 4))
;;(test::assert-equal 16.0 (* 2.0 2.0 4.0))
;;(test::assert-equal 55.0000000001 (* 100 0.55))
;;(test::assert-error (* 1 2 4 "5"))"#),
div: add_special(vm, "/", r#"Usage: (/ number+)
Divide a sequence of numbers. Requires at least two numbers.
Expand Down
4 changes: 4 additions & 0 deletions lisp/core.slosh
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,7 @@ Example:
`(~condition ~(make-action action) ~@(make-cond (first (first others)) (rest (first others)) (rest others))))))
`(if ~@(make-cond (first (first branches)) (rest (first branches)) (rest branches)))))))

#%
asserts true
%#
(def assert-true (fn (x) (= x #t)))
6 changes: 6 additions & 0 deletions lisp/test.slosh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; TODO PC add test functions here! and give them the right docs.

#%
asserts true
%#
(def assert-true (fn (x) (= x #t)))
4 changes: 3 additions & 1 deletion slosh/src/load_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const fn from_utf8(bytes: &[u8]) -> &str {
}
}

//const CORE_LISP: &[u8] = include_bytes!("../lisp/core.slosh");
const TEST_LISP: &str = from_utf8(include_bytes!("../../lisp/test.slosh"));
const CORE_LISP: &str = from_utf8(include_bytes!("../../lisp/core.slosh"));
const COLORS_LISP: &str = from_utf8(include_bytes!("../../lisp/sh-color.slosh"));
pub const SLSHRC: &str = from_utf8(include_bytes!("../../init.slosh"));
Expand Down Expand Up @@ -117,6 +117,7 @@ pub(crate) fn load_internal(vm: &mut SloshVm, name: &'static str) -> VMResult<Va
Ok(file) => Reader::from_file(file, vm, name, 1, 0),
Err(e) => match name {
"core.slosh" => Reader::from_static_string(CORE_LISP, vm, name, 1, 0),
"test.slosh" => Reader::from_static_string(TEST_LISP, vm, name, 1, 0),
"sh-color.slosh" => Reader::from_static_string(COLORS_LISP, vm, name, 1, 0),
"init.slosh" => Reader::from_static_string(SLSHRC, vm, name, 1, 0),
_ => {
Expand All @@ -126,6 +127,7 @@ pub(crate) fn load_internal(vm: &mut SloshVm, name: &'static str) -> VMResult<Va
},
Err(e) => match name {
"core.slosh" => Reader::from_static_string(CORE_LISP, vm, name, 1, 0),
"test.slosh" => Reader::from_static_string(TEST_LISP, vm, name, 1, 0),
"sh-color.slosh" => Reader::from_static_string(COLORS_LISP, vm, name, 1, 0),
"init.slosh" => Reader::from_static_string(SLSHRC, vm, name, 1, 0),
_ => {
Expand Down
38 changes: 33 additions & 5 deletions slosh/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,17 +486,19 @@ fn exec_expression(res: String, env: &mut SloshVm) {
mod tests {
use super::*;
use crate::tests::utils::exec;
use builtins::print::{pr, pretty_value};
use compile_state::state::{new_slosh_vm, CompileState, SloshVm, SloshVmTrait};
use lazy_static::lazy_static;
use regex::{Regex, RegexBuilder};
use sl_compiler::pass1::pass1;
use sl_compiler::{compile, ReadError, Reader};
use slvm::{Value, RET};
use slvm::{VMResult, Value, RET};
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::HashSet;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::io::stdout;
use std::sync::Arc;

// I didn't really know this was possible but for test utilities just use
Expand Down Expand Up @@ -554,6 +556,10 @@ mod tests {
exemption_set.insert("*int-bits*");
exemption_set.insert("get-prop");
exemption_set.insert("expand-macro");

exemption_set.insert("ns-import");
exemption_set.insert("test::assert-equal");
exemption_set.insert("test::assert-error");
exemption_set
};
}
Expand Down Expand Up @@ -783,25 +789,47 @@ mod tests {

type DocResult<T> = Result<T, DocError>;

pub fn no_op(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
Ok(Value::Nil)
}

pub fn assert_equal(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
Ok(Value::True)
}

pub fn assert_error(vm: &mut SloshVm, registers: &[Value]) -> VMResult<Value> {
Ok(Value::True)
}

#[test]
fn test_global_slosh_docs() {
let mut env = new_slosh_vm();
set_builtins(&mut env);

env.set_global_builtin("ns-import", no_op);
env.set_global_builtin("test::assert-equal", assert_equal);
env.set_global_builtin("test::assert-error", assert_error);

let mut docs: Vec<SloshDoc> = vec![];
Namespace::Global.add_docs(&mut docs, &mut env).unwrap();

println!("Now go through and run tests!");

exec(&mut env, "(nil? #f)".to_string());

for doc in docs {
println!("ns: {:?}", doc.namespace);
println!(" sym: {}", doc.symbol);
println!(" type: {}", doc.symbol_type);
println!(" doc_string: {:?}", doc.doc_string);
if let Some(example) = doc.doc_string.example {
println!(" example: {:?}", example);
let val = exec(&mut env, example);
//TODO PC 2 problems.
// 1. exec_expression doesn't work, and might not w/o editing because it does
// not (by design) show errors, so might need to refactor that.
// 2. there is no assert-equal!?
// 1. trying to add the assert commands but commands in core.slosh
// do not appear to be "in" the environment yet.
// SO... load_internal is called *somehow* in main() but I need to know
// how to load it so I have access to core.slosh and test.slosh
// OR maybe i just load it directly?
}
}
}
Expand Down

0 comments on commit c571dd4

Please sign in to comment.