From f437bb2cc353ba0463b31c48851673b1f4a7dd28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 21 Feb 2021 17:12:01 +0100 Subject: don't store multiple versions of same constant --- src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 91938f1..5908fa4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ use std::collections::hash_map::Entry; use std::fmt::Debug; use std::path::{Path, PathBuf}; use std::rc::Rc; +use std::hash::{Hash, Hasher}; use owo_colors::OwoColorize; @@ -198,6 +199,43 @@ impl Debug for Value { } } +impl PartialEq for Value { + fn eq(&self, other: &Value) -> bool { + match (self, other) { + (Value::Float(a), Value::Float(b)) => a == b, + (Value::Int(a), Value::Int(b)) => a == b, + (Value::Bool(a), Value::Bool(b)) => a == b, + (Value::String(a), Value::String(b)) => a == b, + (Value::Tuple(a), Value::Tuple(b)) => { + a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| a == b) + } + (Value::Nil, Value::Nil) => true, + _ => false, + } + } +} + +impl Eq for Value {} + +impl Hash for Value { + fn hash(&self, state: &mut H) { + match self { + Value::Float(a) => { + // We have to limit the values, because + // floats are wierd. + assert!(a.is_finite()); + a.to_bits().hash(state); + }, + Value::Int(a) => a.hash(state), + Value::Bool(a) => a.hash(state), + Value::String(a) => a.hash(state), + Value::Tuple(a) => a.hash(state), + Value::Nil => state.write_i8(0), + _ => {}, + }; + } +} + impl Value { fn identity(self) -> Self { match self { -- cgit v1.2.1 From 8e001b54fb2f74e4e68ea2c75ab0be8db5ea9de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 22 Feb 2021 21:19:29 +0100 Subject: fix all the tests that had unused variables --- src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 5908fa4..9b938ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1244,6 +1244,11 @@ for i := 0, i < 10, i += 1 { if i == 2 { break } + + q + qq + qqq + qqqq } a <=> 3 ", @@ -1260,6 +1265,11 @@ for i := 0, i < 4, i += 1 { continue } a = a + 1 + + q + qq + qqq + qqqq } a <=> 3 ", @@ -1301,7 +1311,9 @@ a := 0 b := 99999 { a := 99999 + a } + b a -= 1 } a <=> -1 -- cgit v1.2.1 From f6393ccf3f5c60bfe746ef33b7dc321c01b34be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 23 Feb 2021 19:35:49 +0100 Subject: fix some bugs, and parse all the sections --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 91938f1..347dfb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ pub fn compile_file(path: &Path, functions: Vec<(String, RustFunction)> ) -> Result> { let tokens = tokenizer::file_to_tokens(path); - match compiler::Compiler::new(path, tokens).compile("main", path, &functions) { + match compiler::Compiler::new(path, &tokens).compile("main", path, &functions) { Ok(prog) => { let mut vm = vm::VM::new(); vm.print_blocks = print; @@ -53,7 +53,7 @@ pub fn run_string(s: &str, print: bool, functions: Vec<(String, RustFunction)>) } fn run(tokens: TokenStream, path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec> { - match compiler::Compiler::new(path, tokens).compile("main", path, &functions) { + match compiler::Compiler::new(path, &tokens).compile("main", path, &functions) { Ok(prog) => { let mut vm = vm::VM::new(); vm.print_blocks = print; -- cgit v1.2.1 From 701c38433454d5afd833bb94723ab2e1f4bbe9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 23 Feb 2021 20:02:41 +0100 Subject: try to make the tests more jank --- src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 347dfb0..06df162 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -842,7 +842,8 @@ mod tests { #[test] fn $fn() { crate::tests::panic_after(std::time::Duration::from_millis(500), || { - match $crate::run_string($prog, true, Vec::new()) { + let prog = std::concat!("q :: fn {", $prog, "\n{}\n}\nq()"); + match $crate::run_string(&prog, true, Vec::new()) { Ok(()) => {}, Err(errs) => { for e in errs.iter() { @@ -859,7 +860,8 @@ mod tests { #[test] fn $fn() { crate::tests::panic_after(std::time::Duration::from_millis(500), || { - $crate::assert_errs!($crate::run_string($prog, true, Vec::new()), $errs); + let prog = std::concat!("q :: fn {", $prog, "\n{}\n}\nq()"); + $crate::assert_errs!($crate::run_string(&prog, true, Vec::new()), $errs); }) } } -- cgit v1.2.1 From 064bd91e7ceadde5272bf27fde59b43c27372cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 26 Feb 2021 17:56:42 +0100 Subject: FIXME comment out failing tests --- src/lib.rs | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 4fc02a4..69bb66e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1023,6 +1023,7 @@ a } add(1, 1) <=> 2 add(10, 20) <=> 30", + /* calls_inside_calls: "one := fn -> int { ret 1 } @@ -1046,6 +1047,7 @@ a ret inner(a) } f(g, 2) <=> 4", + */ multiple_returns: "f := fn a: int -> int { if a == 1 { ret 2 @@ -1072,6 +1074,7 @@ a factorial(6) <=> 720 factorial(12) <=> 479001600", +/* returning_closures: " f : fn -> fn -> int = fn -> fn -> int { x : int = 0 @@ -1094,15 +1097,17 @@ b() <=> 3 a() <=> 4 ", +*/ ); test_multiple!( blob, simple: "blob A {}", + field: "blob A { a: int }", + /* instantiate: "blob A {} a := A() a", - field: "blob A { a: int }", field_assign: "blob A { a: int } a := A() a.a = 2", @@ -1120,6 +1125,7 @@ a() <=> 4 a.b = 3 a.a + a.b <=> 5 5 <=> a.a + a.b", + */ blob_infer: " blob A { } a : A = A() @@ -1136,7 +1142,7 @@ a ); test_file!(scoping, "progs/tests/scoping.sy"); - test_file!(for_, "progs/tests/for.sy"); + // test_file!(for_, "progs/tests/for.sy"); test_multiple!( op_assign, @@ -1144,6 +1150,7 @@ a sub: "a := 2\na -= 1\na <=> 1", mul: "a := 2\na *= 2\na <=> 4", div: "a := 2\na /= 2\na <=> 1", +/* cluster: " blob A { a: int } a := A() @@ -1156,6 +1163,7 @@ a.a /= 2 a.a <=> 1 a.a -= 1 a.a <=> 0" +*/ ); test_multiple!( @@ -1309,6 +1317,7 @@ a ", +/* constant_function: " a() a :: fn {} @@ -1333,21 +1342,6 @@ q :: fn -> int { ret k() } ", - - constant_function_closure: " -q := 1 - -f :: fn -> int { - q += 1 - ret q -} - -f() <=> 2 -f() <=> 3 -f() <=> 4 -f() <=> 5 -", - constants_in_inner_functions: " q : int = 0 @@ -1366,9 +1360,24 @@ q <=> 2 g() q <=> 3 ", +*/ + + constant_function_closure: " +q := 1 +f :: fn -> int { + q += 1 + ret q +} + +f() <=> 2 +f() <=> 3 +f() <=> 4 +f() <=> 5 +", ); +/* test_string!(conflict_markers, " <<<<<<< HEAD print extern_test(4.0) @@ -1378,5 +1387,6 @@ print extern_test(5.0) ", [ErrorKind::SyntaxError(_, _), ErrorKind::GitConflictError(2, 6)] ); +*/ } -- cgit v1.2.1 From f2e87b1cf38de715e840440898b7a6cee91eb258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 26 Feb 2021 18:26:20 +0100 Subject: keep a hashmap of compiler contextes A context is a path and its frames. --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 69bb66e..97f555c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,10 +20,11 @@ mod tokenizer; /// Compiles a file and links the supplied functions as callable external /// functions. Use this if you want your programs to be able to yield. -pub fn compile_file(path: &Path, - print: bool, - functions: Vec<(String, RustFunction)> - ) -> Result> { +pub fn compile_file( + path: &Path, + print: bool, + functions: Vec<(String, RustFunction)> +) -> Result> { let tokens = tokenizer::file_to_tokens(path); match compiler::Compiler::new(path, &tokens).compile("main", path, &functions) { Ok(prog) => { -- cgit v1.2.1 From 538360aef838743be6e89409fe5653c7020dec97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Fri, 26 Feb 2021 20:36:11 +0100 Subject: some tests fail but we're almost there --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 9463c73..019d2d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -928,6 +928,7 @@ mod tests { } #[test] + #[ignore = "Gives faulty line number"] fn assign_to_constant_upvalue() { assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\nq()\na", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); } -- cgit v1.2.1 From 48d73237d2ecab3827c50597f651f49ac4ca26b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Fri, 26 Feb 2021 20:44:08 +0100 Subject: add in start function --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 019d2d3..19ee8d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -881,7 +881,7 @@ mod tests { #[test] fn $fn() { crate::tests::panic_after(std::time::Duration::from_millis(500), || { - let prog = std::concat!("q :: fn {", $prog, "\n{}\n}\nq()"); + let prog = std::concat!("start :: fn {", $prog, "\n{}\n}\n"); match $crate::run_string(&prog, true, Vec::new()) { Ok(()) => {}, Err(errs) => { @@ -899,7 +899,7 @@ mod tests { #[test] fn $fn() { crate::tests::panic_after(std::time::Duration::from_millis(500), || { - let prog = std::concat!("q :: fn {", $prog, "\n{}\n}\nq()"); + let prog = std::concat!("start :: fn {", $prog, "\n{}\n}\n"); $crate::assert_errs!($crate::run_string(&prog, true, Vec::new()), $errs); }) } -- cgit v1.2.1 From ba39d232db23c2ceb031fce613b377581d8b37b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 26 Feb 2021 20:50:11 +0100 Subject: Revert "FIXME comment out failing tests" This reverts commit 064bd91e7ceadde5272bf27fde59b43c27372cd3. --- src/lib.rs | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 19ee8d7..4d669d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1063,7 +1063,6 @@ a } add(1, 1) <=> 2 add(10, 20) <=> 30", - /* calls_inside_calls: "one := fn -> int { ret 1 } @@ -1087,7 +1086,6 @@ a ret inner(a) } f(g, 2) <=> 4", - */ multiple_returns: "f := fn a: int -> int { if a == 1 { ret 2 @@ -1114,7 +1112,6 @@ a factorial(6) <=> 720 factorial(12) <=> 479001600", -/* returning_closures: " f : fn -> fn -> int = fn -> fn -> int { x : int = 0 @@ -1137,17 +1134,15 @@ b() <=> 3 a() <=> 4 ", -*/ ); test_multiple!( blob, simple: "blob A {}", - field: "blob A { a: int }", - /* instantiate: "blob A {} a := A() a", + field: "blob A { a: int }", field_assign: "blob A { a: int } a := A() a.a = 2", @@ -1165,7 +1160,6 @@ a() <=> 4 a.b = 3 a.a + a.b <=> 5 5 <=> a.a + a.b", - */ blob_infer: " blob A { } a : A = A() @@ -1182,7 +1176,7 @@ a ); test_file!(scoping, "progs/tests/scoping.sy"); - // test_file!(for_, "progs/tests/for.sy"); + test_file!(for_, "progs/tests/for.sy"); test_multiple!( op_assign, @@ -1190,7 +1184,6 @@ a sub: "a := 2\na -= 1\na <=> 1", mul: "a := 2\na *= 2\na <=> 4", div: "a := 2\na /= 2\na <=> 1", -/* cluster: " blob A { a: int } a := A() @@ -1203,7 +1196,6 @@ a.a /= 2 a.a <=> 1 a.a -= 1 a.a <=> 0" -*/ ); test_multiple!( @@ -1369,7 +1361,6 @@ a ", -/* constant_function: " a() a :: fn {} @@ -1394,6 +1385,21 @@ q :: fn -> int { ret k() } ", + + constant_function_closure: " +q := 1 + +f :: fn -> int { + q += 1 + ret q +} + +f() <=> 2 +f() <=> 3 +f() <=> 4 +f() <=> 5 +", + constants_in_inner_functions: " q : int = 0 @@ -1412,24 +1418,9 @@ q <=> 2 g() q <=> 3 ", -*/ - - constant_function_closure: " -q := 1 -f :: fn -> int { - q += 1 - ret q -} - -f() <=> 2 -f() <=> 3 -f() <=> 4 -f() <=> 5 -", ); -/* test_string!(conflict_markers, " <<<<<<< HEAD print extern_test(4.0) @@ -1439,6 +1430,5 @@ print extern_test(5.0) ", [ErrorKind::SyntaxError(_, _), ErrorKind::GitConflictError(2, 6)] ); -*/ } -- cgit v1.2.1 From bd703c481822d0c3bf310a226e6a12b9579238e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 26 Feb 2021 20:50:37 +0100 Subject: unignore test --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 4d669d8..8a023b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -928,7 +928,6 @@ mod tests { } #[test] - #[ignore = "Gives faulty line number"] fn assign_to_constant_upvalue() { assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\nq()\na", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); } -- cgit v1.2.1 From c3e1e3bcbb177a5cbdb972389626e8c7347cbfc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 28 Feb 2021 17:23:09 +0100 Subject: move sectionizer, sections own tokens --- src/lib.rs | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 8a023b6..5542d17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ use std::hash::{Hash, Hasher}; use owo_colors::OwoColorize; use error::Error; -use tokenizer::TokenStream; use crate::error::ErrorKind; @@ -17,45 +16,39 @@ pub mod error; pub mod vm; mod compiler; +mod sectionizer; mod tokenizer; /// Compiles a file and links the supplied functions as callable external /// functions. Use this if you want your programs to be able to yield. -pub fn compile_file( - path: &Path, - print: bool, - functions: Vec<(String, RustFunction)> -) -> Result> { - let tokens = tokenizer::file_to_tokens(path); - match compiler::Compiler::new(path, &tokens).compile("main", path, &functions) { - Ok(prog) => { - let mut vm = vm::VM::new(); - vm.print_blocks = print; - vm.print_ops = print; - vm.typecheck(&prog)?; - vm.init(&prog); - Ok(vm) - } - Err(errors) => Err(errors), - } -} +//pub fn compile_file( +// path: &Path, +// print: bool, +// functions: Vec<(String, RustFunction)> +//) -> Result> { +// match compiler::Compiler::new(path).compile("main", path, &functions) { +// Ok(prog) => { +// let mut vm = vm::VM::new(); +// vm.print_blocks = print; +// vm.print_ops = print; +// vm.typecheck(&prog)?; +// vm.init(&prog); +// Ok(vm) +// } +// Err(errors) => Err(errors), +// } +//} /// Compiles, links and runs the given file. Supplied functions are callable /// external functions. If you want your program to be able to yield, use /// [compile_file]. pub fn run_file(path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec> { - run(tokenizer::file_to_tokens(path), path, print, functions) -} - -/// Compile and run a string containing source code. The supplied functions are -/// linked as callable external functions. This is useful for short test -/// programs. -pub fn run_string(s: &str, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec> { - run(tokenizer::string_to_tokens(s), Path::new("builtin"), print, functions) + run(path, print, functions) } -fn run(tokens: TokenStream, path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec> { - match compiler::Compiler::new(path, &tokens).compile("main", path, &functions) { +fn run(path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec> { + let sections = sectionizer::sectionize(path); + match compiler::Compiler::new(sections).compile("main", path, &functions) { Ok(prog) => { let mut vm = vm::VM::new(); vm.print_blocks = print; -- cgit v1.2.1 From 89bdd138aa5e0843d57fc847cb4026220c06209e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 28 Feb 2021 17:24:01 +0100 Subject: uncomment missed function --- src/lib.rs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 5542d17..629d39f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,23 +21,24 @@ mod tokenizer; /// Compiles a file and links the supplied functions as callable external /// functions. Use this if you want your programs to be able to yield. -//pub fn compile_file( -// path: &Path, -// print: bool, -// functions: Vec<(String, RustFunction)> -//) -> Result> { -// match compiler::Compiler::new(path).compile("main", path, &functions) { -// Ok(prog) => { -// let mut vm = vm::VM::new(); -// vm.print_blocks = print; -// vm.print_ops = print; -// vm.typecheck(&prog)?; -// vm.init(&prog); -// Ok(vm) -// } -// Err(errors) => Err(errors), -// } -//} +pub fn compile_file( + path: &Path, + print: bool, + functions: Vec<(String, RustFunction)> +) -> Result> { + let sections = sectionizer::sectionize(path); + match compiler::Compiler::new(sections).compile("main", path, &functions) { + Ok(prog) => { + let mut vm = vm::VM::new(); + vm.print_blocks = print; + vm.print_ops = print; + vm.typecheck(&prog)?; + vm.init(&prog); + Ok(vm) + } + Err(errors) => Err(errors), + } +} /// Compiles, links and runs the given file. Supplied functions are callable /// external functions. If you want your program to be able to yield, use -- cgit v1.2.1 From 6b4b3d9d01057c27909bab7675c2ad3ec5e910a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 28 Feb 2021 17:37:24 +0100 Subject: run strings from temporary files --- src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 629d39f..76486dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,6 +47,13 @@ pub fn run_file(path: &Path, print: bool, functions: Vec<(String, RustFunction)> run(path, print, functions) } +pub fn run_string(source: &str, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec> { + let mut path = std::env::temp_dir(); + path.push(format!("test_{}.sy", rand::random::())); + std::fs::write(path.clone(), source).expect("Failed to write source to temporary file"); + run(&path, print, functions) +} + fn run(path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec> { let sections = sectionizer::sectionize(path); match compiler::Compiler::new(sections).compile("main", path, &functions) { -- cgit v1.2.1 From ba2a7b3290102e3573cc7b2727026f4efe8a1d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 17:43:16 +0100 Subject: macro that generates tests from files --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 76486dd..ee5f9c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -918,6 +918,8 @@ mod tests { }; } + sylt_macro::find_tests!(); + #[test] fn unreachable_token() { assert_errs!(run_string("\n", true, Vec::new()), [ErrorKind::Unreachable]); -- cgit v1.2.1 From e1e7337239066677dd6bc82b826e861cf6710836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 18:14:05 +0100 Subject: expand test-files in modules --- src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index ee5f9c4..e26b3ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -802,11 +802,9 @@ pub struct Prog { #[cfg(test)] mod tests { - use std::path::Path; - use crate::error::ErrorKind; - use super::{run_file, run_string}; + use super::run_string; #[macro_export] macro_rules! assert_errs { @@ -912,8 +910,8 @@ mod tests { ($fn:ident, $path:literal) => { #[test] fn $fn() { - let file = Path::new($path); - run_file(&file, true, Vec::new()).unwrap(); + let file = std::path::Path::new($path); + crate::run_file(&file, true, Vec::new()).unwrap(); } }; } -- cgit v1.2.1 From 9d0a930d811b825b39ee16614e645b6934130cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 18:37:23 +0100 Subject: parse wanted errors from test files --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index e26b3ab..03abd48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -914,6 +914,16 @@ mod tests { crate::run_file(&file, true, Vec::new()).unwrap(); } }; + ($fn:ident, $path:literal, $errs:tt) => { + #[test] + fn $fn() { + use crate::error::ErrorKind; + + let file = std::path::Path::new($path); + let res = crate::run_file(&file, true, Vec::new()); + $crate::assert_errs!(res, $errs); + } + }; } sylt_macro::find_tests!(); -- cgit v1.2.1 From 2570830850c6dadadc2c86bf9d6f3203c9aba488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 19:07:13 +0100 Subject: convert tests to files --- src/lib.rs | 546 +------------------------------------------------------------ 1 file changed, 1 insertion(+), 545 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 03abd48..6b1bc08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -874,37 +874,6 @@ mod tests { panic!(msg); } - #[macro_export] - macro_rules! test_string { - ($fn:ident, $prog:literal) => { - #[test] - fn $fn() { - crate::tests::panic_after(std::time::Duration::from_millis(500), || { - let prog = std::concat!("start :: fn {", $prog, "\n{}\n}\n"); - match $crate::run_string(&prog, true, Vec::new()) { - Ok(()) => {}, - Err(errs) => { - for e in errs.iter() { - eprintln!("{}", e); - } - eprintln!(" {} - failed\n", stringify!($fn)); - unreachable!(); - } - } - }); - } - }; - ($fn:ident, $prog:literal, $errs:tt) => { - #[test] - fn $fn() { - crate::tests::panic_after(std::time::Duration::from_millis(500), || { - let prog = std::concat!("start :: fn {", $prog, "\n{}\n}\n"); - $crate::assert_errs!($crate::run_string(&prog, true, Vec::new()), $errs); - }) - } - } - } - #[macro_export] macro_rules! test_file { ($fn:ident, $path:literal) => { @@ -918,6 +887,7 @@ mod tests { #[test] fn $fn() { use crate::error::ErrorKind; + use crate::Type; let file = std::path::Path::new($path); let res = crate::run_file(&file, true, Vec::new()); @@ -927,518 +897,4 @@ mod tests { } sylt_macro::find_tests!(); - - #[test] - fn unreachable_token() { - assert_errs!(run_string("\n", true, Vec::new()), [ErrorKind::Unreachable]); - } - - #[test] - fn assign_to_constant() { - assert_errs!(run_string("a :: 2\na = 2", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); - } - - #[test] - fn assign_to_constant_upvalue() { - assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\nq()\na", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); - } - - #[test] - fn undefined_blob() { - assert_errs!(run_string("a :: B()\n", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); - } - - #[test] - fn call_before_link() { - let prog = " -a := 1 -f() -c := 5 - -f :: fn { - c <=> 5 -} -a - "; - assert_errs!(run_string(prog, true, Vec::new()), [ErrorKind::InvalidProgram, ErrorKind::TypeError(_, _)]); - } - - #[test] - fn unused_variable() { - assert_errs!(run_string("a := 1", true, Vec::new()), [ErrorKind::SyntaxError(1, _)]); - } - - #[test] - fn unused_upvalue() { - assert_errs!(run_string("a := 1\nf :: fn { a = 2 }\nf()", true, Vec::new()), [ErrorKind::SyntaxError(1, _)]); - } - - #[test] - fn unused_function() { - assert_errs!(run_string("a := 1\nf := fn { a }\n", true, Vec::new()), [ErrorKind::SyntaxError(2, _)]); - } - - macro_rules! test_multiple { - ($mod:ident, $( $fn:ident : $prog:literal ),+ $( , )? ) => { - mod $mod { - $( test_string!($fn, $prog); )+ - } - } - } - - test_multiple!( - order_of_operations, - terms_and_factors: "1 + 1 * 2 <=> 3 - 1 * 2 + 3 <=> 5", - in_rhs: "5 <=> 1 * 2 + 3", - parenthesis: "(1 + 2) * 3 <=> 9", - negation: "-1 <=> 0 - 1 - -1 + 2 <=> 1 - -(1 + 2) <=> -3 - 1 + -1 <=> 0 - 2 * -1 <=> -2", - ); - - test_multiple!( - variables, - single_variable: "a := 1 - a <=> 1", - two_variables: "a := 1 - b := 2 - a <=> 1 - b <=> 2", - stack_ordering: "a := 1 - b := 2 - b <=> 2 - a <=> 1", - assignment: "a := 1 - b := 2 - a = b - a <=> 2 - b <=> 2", - ); - - test_multiple!( - if_, - compare_constants_equality: "if 1 == 2 { - - }", - compare_constants_unequality: "if 1 != 1 { - - }", - compare_variable: "a := 1 - if a == 0 { - - } - if a != 1 { - - }", - else_: "a := 1 - res := 0 - if a == 0 { - - } else { - res = 1 - } - res <=> 1", - else_if: "a := 1 - res := 0 - if a == 0 { - - } else if a == 1 { - res = 1 - } else { - - } - res <=> 1", - ); - - test_multiple!( - fun, - simplest: "f := fn {} - f()", - param_1: "f := fn a: int {} - f(1)", - return_1: "f := fn -> int { - ret 1 - } - f() <=> 1", - param_and_return: "f := fn a: int -> int { - ret a * 2 - } - f(1) <=> 2 - f(5) <=> 10", - param_2: "add := fn a: int, b: int -> int { - ret a + b - } - add(1, 1) <=> 2 - add(10, 20) <=> 30", - calls_inside_calls: "one := fn -> int { - ret 1 - } - add := fn a: int, b: int -> int { - ret a + b - } - add(one(), one()) <=> 2 - add(add(one(), one()), one()) <=> 3 - add(one(), add(one(), one())) <=> 3", - passing_functions: "g := fn -> int { - ret 1 - } - f := fn inner: fn -> int -> int { - ret inner() - } - f(g) <=> 1", - passing_functions_mixed: "g := fn a: int -> int { - ret a * 2 - } - f := fn inner: fn int -> int, a: int -> int { - ret inner(a) - } - f(g, 2) <=> 4", - multiple_returns: "f := fn a: int -> int { - if a == 1 { - ret 2 - } else { - ret 3 - } - } - f(0) <=> 3 - f(1) <=> 2 - f(2) <=> 3", - precedence: "f := fn a: int, b: int -> int { - ret a + b - } - 1 + f(2, 3) <=> 6 - 2 * f(2, 3) <=> 10 - f(2, 3) - (2 + 3) <=> 0", - factorial: "factorial : fn int -> int = fn n: int -> int { - if n <= 1 { - ret 1 - } - ret n * factorial(n - 1) - } - factorial(5) <=> 120 - factorial(6) <=> 720 - factorial(12) <=> 479001600", - - returning_closures: " -f : fn -> fn -> int = fn -> fn -> int { - x : int = 0 - f := fn -> int { - x = x + 1 - ret x - } - f() <=> 1 - ret f -} - -a := f() -b := f() - -a() <=> 2 -a() <=> 3 - -b() <=> 2 -b() <=> 3 - -a() <=> 4 -", - ); - - test_multiple!( - blob, - simple: "blob A {}", - instantiate: "blob A {} - a := A() - a", - field: "blob A { a: int }", - field_assign: "blob A { a: int } - a := A() - a.a = 2", - field_get: "blob A { a: int } - a := A() - a.a = 2 - a.a <=> 2 - 2 <=> a.a", - multiple_fields: "blob A { - a: int - b: int - } - a := A() - a.a = 2 - a.b = 3 - a.a + a.b <=> 5 - 5 <=> a.a + a.b", - blob_infer: " -blob A { } -a : A = A() -a -", - ); - - test_multiple!(tuples, - add: "(1, 2, 3, 4) + (4, 3, 2, 1) <=> (5, 5, 5, 5)", - sub: "(1, -2, 3, -4) - (4, 3, -2, -1) <=> (-3, 1, 1, -5)", - mul: "(0, 1, 2) * (2, 3, 4) <=> (0, 3, 8)", - types: "a: (int, float, int) = (1, 1., 1)\na", - more_types: "a: (str, bool, int) = (\"abc\", true, 1)\na", - ); - - test_file!(scoping, "progs/tests/scoping.sy"); - test_file!(for_, "progs/tests/for.sy"); - - test_multiple!( - op_assign, - add: "a := 1\na += 1\na <=> 2", - sub: "a := 2\na -= 1\na <=> 1", - mul: "a := 2\na *= 2\na <=> 4", - div: "a := 2\na /= 2\na <=> 1", - cluster: " -blob A { a: int } -a := A() -a.a = 0 -a.a += 1 -a.a <=> 1 -a.a *= 2 -a.a <=> 2 -a.a /= 2 -a.a <=> 1 -a.a -= 1 -a.a <=> 0" - ); - - test_multiple!( - fancy_call, - not: "f := fn {}\n f!\n", - one_arg: "f := fn a:int { a <=> 1 }\n f! 1\n", - two_arg: "f := fn a:int, b:int { b <=> 3 }\n f! 1, 1 + 2\n", - three_arg: "f := fn a:int, b:int, c:int { c <=> 13 }\n f! 1, 1 + 2, 1 + 4 * 3\n", - ); - - test_multiple!( - newline_regression, - simple: "a := 1 // blargh \na += 1 // blargh \n a <=> 2 // HARGH", - expressions: "1 + 1 // blargh \n 2 // blargh \n // HARGH \n", - ); - - test_multiple!( - break_and_continue, - simple_break: " -a := 0 -for i := 0, i < 10, i += 1 { - a = a + 1 - if i == 2 { - break - } -} -a <=> 3 -", - - simple_continue: " -a := 0 -for i := 0, i < 4, i += 1 { - if i == 2 { - continue - } - a = a + 1 -} -a <=> 3 -", - - advanced_break: " -a := 0 -for i := 0, i < 10, i += 1 { - q := 0 - qq := 0 - qqq := 0 - qqqq := 0 - - a = a + 1 - if i == 2 { - break - } - - q - qq - qqq - qqqq -} -a <=> 3 -", - - advanced_continue: " -a := 0 -for i := 0, i < 4, i += 1 { - q := 0 - qq := 0 - qqq := 0 - qqqq := 0 - - if i == 2 { - continue - } - a = a + 1 - - q - qq - qqq - qqqq -} -a <=> 3 -", - ); - - test_multiple!( - read_constants, - simple: " -a :: 1 -a <=> 1 -b := 2 -{ - a <=> 1 - b <=> 2 -}", - ); - - test_multiple!( - assignment_op_regression, - simple_add: " -a := 0 -b := 99999 -a += 1 -a <=> 1 -b <=> 99999 -", - - simple_sub: " -a := 0 -b := 99999 -a -= 1 -a <=> -1 -b <=> 99999 -", - - strange: " -a := 0 -{ - b := 99999 - { - a := 99999 - a - } - b - a -= 1 -} -a <=> -1 -", - ); - - test_multiple!( - declaration_order, - blob_simple: " -a := A() -a - -blob A { - a: int -} -", - - blob_complex: " -a := A() -b := B() -c := C() -b2 := B() - -a -b -c -b2 - -blob A { - c: C -} -blob C { } -blob B { } -", - - blob_infer: " -blob A { } - -a : A = A() -a -", - - - constant_function: " -a() -a :: fn {} -", - - constant_function_complex: " -h :: fn -> int { - ret 3 -} - -a() <=> 3 - -k :: fn -> int { - ret h() -} - -a :: fn -> int { - ret q() -} - -q :: fn -> int { - ret k() -} -", - - constant_function_closure: " -q := 1 - -f :: fn -> int { - q += 1 - ret q -} - -f() <=> 2 -f() <=> 3 -f() <=> 4 -f() <=> 5 -", - - constants_in_inner_functions: " -q : int = 0 - -f :: fn -> fn -> { - g :: fn { - q += 1 - } - ret g -} - -g := f() -g() -q <=> 1 -g() -q <=> 2 -g() -q <=> 3 -", - - ); - - test_string!(conflict_markers, " -<<<<<<< HEAD -print extern_test(4.0) -======= -print extern_test(5.0) ->>>>>>> 2 -", - [ErrorKind::SyntaxError(_, _), ErrorKind::GitConflictError(2, 6)] - ); - } -- cgit v1.2.1 From d31c62339c819649d98ff789797ae22d2a0b97d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 19:28:16 +0100 Subject: fix some warnings --- src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 6b1bc08..adb7eed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -802,10 +802,6 @@ pub struct Prog { #[cfg(test)] mod tests { - use crate::error::ErrorKind; - - use super::run_string; - #[macro_export] macro_rules! assert_errs { ($result:expr, [ $( $kind:pat ),* ]) => { @@ -887,6 +883,7 @@ mod tests { #[test] fn $fn() { use crate::error::ErrorKind; + #[allow(unused_imports)] use crate::Type; let file = std::path::Path::new($path); -- cgit v1.2.1 From 84d350d3793355da8a7caa4df57d08e38e1f532f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 19:29:03 +0100 Subject: fix all warnings --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index adb7eed..afde461 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -842,6 +842,7 @@ mod tests { use owo_colors::OwoColorize; // Shamelessly stolen from https://github.com/rust-lang/rfcs/issues/2798 + #[allow(dead_code)] pub fn panic_after(d: Duration, f: F) -> T where T: Send + 'static, -- cgit v1.2.1 From b1fab16befb78232d7a913f58beef639c5891e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 5 Mar 2021 19:47:00 +0100 Subject: set no_print in tests --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index afde461..e94eaad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -873,14 +873,14 @@ mod tests { #[macro_export] macro_rules! test_file { - ($fn:ident, $path:literal) => { + ($fn:ident, $path:literal, $print:expr) => { #[test] fn $fn() { let file = std::path::Path::new($path); - crate::run_file(&file, true, Vec::new()).unwrap(); + crate::run_file(&file, $print, Vec::new()).unwrap(); } }; - ($fn:ident, $path:literal, $errs:tt) => { + ($fn:ident, $path:literal, $print:expr, $errs:tt) => { #[test] fn $fn() { use crate::error::ErrorKind; @@ -888,7 +888,7 @@ mod tests { use crate::Type; let file = std::path::Path::new($path); - let res = crate::run_file(&file, true, Vec::new()); + let res = crate::run_file(&file, $print, Vec::new()); $crate::assert_errs!(res, $errs); } }; -- cgit v1.2.1