use std::collections::HashMap;
use crate::Tok;
use crate::pp::{Preprocessor, Severity, SourceFiles};
#[derive(Default)]
struct Files(HashMap<String, Vec<u8>>);
impl Files {
fn with(mut self, name: &str, text: &str) -> Self {
self.0.insert(name.to_owned(), text.as_bytes().to_vec());
self
}
}
impl SourceFiles for Files {
fn read(&mut self, name: &str) -> Option<Vec<u8>> {
self.0.get(&name.replace('\\', "/")).cloned()
}
fn exists(&mut self, name: &str) -> bool {
self.0.contains_key(&name.replace('\\', "/"))
}
fn decode(&mut self, charset: &str, bytes: &[u8]) -> Option<Vec<u8>> {
(charset == "shifted").then(|| bytes.iter().map(|&c| c + 1).collect())
}
}
fn preprocess_files(mut files: Files) -> (Vec<String>, Vec<String>) {
let mut pp = Preprocessor::new(&mut files);
pp.set_test_report_mode(true);
pp.add_include_path("inc");
assert!(pp.set_source("main.hxn"));
let mut lines = Vec::new();
while let Some(line) = pp.next_line() {
lines.push(String::from_utf8_lossy(&line.text).into_owned());
}
let messages = pp
.take_diagnostics()
.into_iter()
.map(|problem| {
let kind = match problem.severity {
Severity::Error => "error",
Severity::Fatal => "fatal error",
Severity::Internal => "internal error",
Severity::Warning => "warning",
Severity::Message => "message",
};
format!(
"{}({}): {kind}: {}",
problem.file, problem.line, problem.message
)
})
.collect();
(lines, messages)
}
fn preprocess(text: &str) -> Vec<String> {
let (lines, messages) = preprocess_files(Files::default().with("main.hxn", text));
assert_eq!(messages, Vec::<String>::new());
lines
}
#[test]
fn a_line_of_output_for_each_line_of_source() {
assert_eq!(
preprocess("#define N 3\nx = N; // three\ny = N /* still */ + 1;\n"),
["", "x = 3; ", "y = 3 + 1;"]
);
}
#[test]
fn macros_with_arguments() {
assert_eq!(
preprocess("#define ADD(a, b) ((a) + (b))\nADD(1, ADD(2, 3))\nADD\n"),
["", "((1) + (((2) + (3))))", "ADD "]
);
}
#[test]
fn a_macro_is_not_expanded_inside_its_own_expansion() {
assert_eq!(
preprocess("#define foo foo + bar\n#define bar foo\nfoo\n"),
["", "", "foo + foo"]
);
}
#[test]
fn stringizing_and_pasting() {
assert_eq!(
preprocess(
"#define STR(x) #x\n#define CHR(x) #@x\n#define GLUE(a, b) a##b\n\
STR(a \"b\" c) CHR(its) GLUE(left, Right) GLUE('ab', 'cd')\n"
),
["", "", "", "\"a \\\"b\\\" c\" 'its' leftRight 'abcd'"]
);
}
#[test]
fn variable_arguments() {
let lines = preprocess(
"#define ALL(first, rest...) first: rest\n\
#define EACH(rest...) rest#foreach/[rest]/, /\n\
#define COUNT(rest...) rest#argcount\n\
#define OPT(rest...) rest#ifempty/none/ rest#ifnempty/some/\n\
ALL(a, b, c)\nEACH(x, y, z)\nCOUNT() COUNT(p, q)\nOPT() OPT(1)\n",
);
assert_eq!(
&lines[4..],
["a: b, c", "[x], [y], [z]", "0 2", "none some"]
);
}
#[test]
fn conditions() {
assert_eq!(
preprocess(
"#define V 2\n#if V == 1\none\n#elif V == 2 && defined(V) && !defined W\ntwo\n\
#else\nother\n#endif\n#ifndef V\nnot\n#endif\n#if 'abc' < 'abd'\nstr\n#endif\n"
),
["", "", "", "", "two", "", "", "", "", "", "", "", "str", ""]
);
}
#[test]
fn conditions_are_read_by_the_compilers_own_parser() {
let (lines, messages) = preprocess_files(Files::default().with(
"main.hxn",
"#if 1.5 > 1\na\n#endif\n#if \"ab\" + 'c' == 'abc'\nb\n#endif\n\
#if 1 is in (2, 1)\nc\n#endif\n#if nil ?? 0\nd\n#endif\n",
));
assert_eq!(
messages,
["main.hxn(7): error: preprocessor expression is not a constant value"]
);
let shown: Vec<&str> = lines
.iter()
.map(String::as_str)
.filter(|l| !l.is_empty())
.collect();
assert_eq!(shown, ["a", "b", "c"]);
let (_, messages) = preprocess_files(Files::default().with(
"main.hxn",
"#if 1 2\n#endif\n#if 1 2 3\n#endif\n#if foo\n#endif\n#if 1 <<\n#endif\n",
));
assert_eq!(
messages,
[
"main.hxn(1): error: extra characters at end of line",
"main.hxn(3): error: extra characters at end of line",
"main.hxn(5): error: preprocessor expression is not a constant value",
"main.hxn(7): error: expected integer, string, name, '[', or '(', but found \"<End Of File>\"",
]
);
}
#[test]
fn a_line_directive_takes_a_number_and_a_name() {
let (_, messages) = preprocess_files(Files::default().with(
"main.hxn",
"#line 10\n#line 'x' 'y'\n#line 5 6\n#line 7 \"other.hxn\"\nz\n",
));
assert_eq!(
messages,
[
"main.hxn(1): error: expected integer, string, name, '[', or '(', but found \"<End Of File>\"",
"main.hxn(1): error: string value required for #line",
"main.hxn(2): error: integer value required for #line",
"main.hxn(3): error: string value required for #line",
]
);
}
#[test]
fn a_string_that_runs_over_lines_is_joined_up() {
assert_eq!(
preprocess("say(\"one\n two\n three\"); next\nafter\n"),
["say(\"one two three\"", "); next", "after"]
);
}
#[test]
fn macro_arguments_can_run_over_lines() {
assert_eq!(
preprocess("#define PAIR(a, b) <a|b>\nPAIR(one,\n two) tail\nend\n"),
["", "<one|two>", " tail", "end"]
);
}
#[test]
fn a_comment_to_end_of_line_inside_a_macro_call_loses_what_follows() {
assert_eq!(
preprocess("#define PAIR(a, b) <a|b>\nPAIR(one, // note\n two) tail\n"),
["", "<one|>", " tail"]
);
}
#[test]
fn includes_and_the_line_and_file_macros() {
let files = Files::default()
.with(
"main.hxn",
"#include \"local.h\"\n#include <system.h>\n__FILE__ __LINE__ LOCAL SYSTEM\n",
)
.with("local.h", "#pragma once\n#define LOCAL 1\n")
.with("inc/system.h", "#define SYSTEM __FILE__\n");
let (lines, messages) = preprocess_files(files);
assert_eq!(messages, Vec::<String>::new());
assert_eq!(lines, ["", "", "", "", "", "'main.hxn' 3 1 'main.hxn'"]);
}
#[test]
fn problems_are_reported_in_the_reference_wording() {
let (lines, messages) = preprocess_files(Files::default().with(
"main.hxn",
"#define ONE(a) a\nONE(1, 2)\n#include \"missing.h\"\n#bogus\n#define ONE(b) b b\n#endif\n#if 1\n",
));
assert_eq!(lines.len(), 7);
assert_eq!(
messages,
[
"main.hxn(2): error: too many arguments for macro \"ONE\"",
"main.hxn(3): error: cannot open include file \"missing.h\"",
"main.hxn(4): error: invalid preprocessor directive",
"main.hxn(5): warning: redefinition of macro \"ONE\"",
"main.hxn(6): error: #endif without #if",
"main.hxn(8): error: #if without #endif (line 7, file main.hxn)",
]
);
}
#[test]
fn a_file_says_what_character_set_it_is_in() {
let mut files = Files::default();
files.0.insert(
"main.hxn".to_owned(),
b"#charset \"iso-8859-1\"\n'caf\xe9'\n".to_vec(),
);
let (lines, messages) = preprocess_files(files);
assert_eq!(messages, Vec::<String>::new());
assert_eq!(lines, ["", "'caf\u{e9}'"]);
let mut files = Files::default();
files
.0
.insert("main.hxn".to_owned(), b"'caf\xe9'\n".to_vec());
let (lines, _) = preprocess_files(files);
assert_eq!(lines, ["'caf\u{fffd}'"]);
let (lines, messages) =
preprocess_files(Files::default().with("main.hxn", "#charset \"shifted\"\nHAL\n"));
assert_eq!(messages, Vec::<String>::new());
assert_eq!(lines, ["\u{b}IBM\u{b}"]);
let (lines, messages) =
preprocess_files(Files::default().with("main.hxn", "#charset \"klingon\"\nx\n"));
assert_eq!(lines, ["", "x"]);
assert_eq!(messages, ["main.hxn(1): error: unable to load #charset"]);
}
fn tokens(text: &str) -> (Vec<(Tok, String)>, Vec<String>) {
let mut files = Files::default().with("main.hxn", text);
let mut pp = Preprocessor::new(&mut files);
pp.set_test_report_mode(true);
pp.set_compile_mode();
assert!(pp.set_source("main.hxn"));
let mut out = Vec::new();
while pp.next_token() != Tok::Eof {
let token = pp.cur_token();
out.push((token.typ, String::from_utf8_lossy(&token.text).into_owned()));
}
let messages = pp
.take_diagnostics()
.into_iter()
.map(|problem| format!("{}: {}", problem.line, problem.message))
.collect();
(out, messages)
}
fn token_list(text: &str) -> Vec<(Tok, String)> {
let (tokens, messages) = tokens(text);
assert_eq!(messages, Vec::<String>::new());
tokens
}
fn tok(typ: Tok, text: &str) -> (Tok, String) {
(typ, text.to_owned())
}
#[test]
fn keywords_numbers_and_operators() {
assert_eq!(
token_list("local x = nil;\nif (x >>>= 0x1F) return 1.5e3;\n"),
[
tok(Tok::Local, "local"),
tok(Tok::Sym, "x"),
tok(Tok::Eq, "="),
tok(Tok::Nil, "nil"),
tok(Tok::Sem, ";"),
tok(Tok::If, "if"),
tok(Tok::LPar, "("),
tok(Tok::Sym, "x"),
tok(Tok::LShrEq, ">>>="),
tok(Tok::Int, "0x1F"),
tok(Tok::RPar, ")"),
tok(Tok::Return, "return"),
tok(Tok::Float, "1.5e3"),
tok(Tok::Sem, ";"),
]
);
}
#[test]
fn macros_are_expanded_before_tokens_are_read() {
assert_eq!(
token_list("#define TWICE(x) x x\nTWICE(if)\n"),
[tok(Tok::If, "if"), tok(Tok::If, "if")]
);
}
#[test]
fn a_string_token_has_the_translated_text() {
assert_eq!(
token_list("'it\\'s\\n\\u00e9' \"\"\"a \"quoted\" word\"\"\" R'a+'"),
[
tok(Tok::SStr, "it's\n\u{e9}"),
tok(Tok::DStr, "a \"quoted\" word"),
tok(Tok::ReStr, "a+"),
]
);
}
#[test]
fn a_string_with_embedded_expressions_is_several_tokens() {
assert_eq!(
token_list("\"a<<b + 1>>c<<%2d n>>d\""),
[
tok(Tok::DStrStart, "a"),
tok(Tok::Sym, "b"),
tok(Tok::Plus, "+"),
tok(Tok::Int, "1"),
tok(Tok::DStrMid, "c"),
tok(Tok::FmtSpec, "%2d"),
tok(Tok::Sym, "n"),
tok(Tok::DStrEnd, "d"),
]
);
assert_eq!(
token_list("'<<(a >> 1)>>'"),
[
tok(Tok::SStrStart, ""),
tok(Tok::LPar, "("),
tok(Tok::Sym, "a"),
tok(Tok::AShr, ">>"),
tok(Tok::Int, "1"),
tok(Tok::RPar, ")"),
tok(Tok::SStrEnd, ""),
]
);
}
#[test]
fn tokens_can_be_put_back() {
let mut files = Files::default().with("main.hxn", "a b c");
let mut pp = Preprocessor::new(&mut files);
pp.set_compile_mode();
assert!(pp.set_source("main.hxn"));
pp.next_token();
pp.next_token();
assert_eq!(pp.cur_token().text, b"b");
pp.unget_token();
assert_eq!(pp.cur_token().text, b"a");
assert_eq!(pp.next_token(), Tok::Sym);
assert_eq!(pp.cur_token().text, b"b");
pp.next_token();
assert_eq!(pp.cur_token().text, b"c");
assert_eq!(pp.next_token(), Tok::Eof);
assert_eq!(pp.next_token(), Tok::Eof);
}
#[test]
fn tokenizer_problems() {
let (list, messages) = tokens("a $ b\n#pragma message(\"hello \" 2)\n#error stop\n\"no end\n");
assert_eq!(
list,
[
tok(Tok::Sym, "a"),
tok(Tok::Sym, "b"),
tok(Tok::DStr, "no end ")
]
);
assert_eq!(
messages,
[
"1: invalid character in input \"$\"",
"2: hello 2",
"3: #error : stop",
"4: unterminated string literal: string started with \"no end \"",
]
);
}
use crate::{CompileOptions, Compiled, compile};
fn compile_text(text: &str) -> Compiled {
let mut files = Files::default().with("main.hxn", text);
compile(&mut files, "main.hxn", &CompileOptions::default())
}
fn compile_code_only(text: &str) -> Compiled {
let mut files = Files::default().with("main.hxn", text);
let options = CompileOptions {
line_records: false,
..CompileOptions::default()
};
compile(&mut files, "main.hxn", &options)
}
fn compile_messages(text: &str) -> Vec<String> {
compile_text(text)
.diagnostics
.into_iter()
.map(|problem| {
let kind = match problem.severity {
Severity::Error => "error",
Severity::Fatal => "fatal error",
Severity::Internal => "internal error",
Severity::Warning => "warning",
Severity::Message => "message",
};
format!("{}: {kind}: {}", problem.line, problem.message)
})
.collect()
}
fn block<'i>(image: &'i [u8], kind: &[u8; 4]) -> &'i [u8] {
let mut pos = 69;
loop {
let size = u32::from_le_bytes(image[pos + 4..pos + 8].try_into().unwrap()) as usize;
if &image[pos..pos + 4] == kind {
return &image[pos + 10..pos + 10 + size];
}
pos += 10 + size;
}
}
fn code(image: &[u8]) -> &[u8] {
&block(image, b"CPPG")[7..]
}
#[test]
fn a_function_is_a_header_its_code_and_its_locals() {
let compiled = compile_code_only(
"_main(args) { local a = 1; local b = a + 2; return b * 3; }\n\
foo(x, y) { if (x > y) return x; else return y; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[..10], [1, 0, 2, 0, 4, 0, 0, 0, 0x17, 0]);
assert_eq!(
code[10..0x17],
[0xda, 0, 0xaa, 3, 2, 0x22, 0xe0, 1, 0xab, 3, 3, 0x24, 0x50]
);
assert_eq!(
code[0x17..0x47],
[
0, 0, 0x2a, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0x0a, 0, 0x17, 0, 0, 0, 0, 0, 0, 0, 1, 0, b'a', 1, 0, 0, 0, 0, 0, 1, 0, b'b', 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
);
assert_eq!(
code[0x47 + 10..0x47 + 0x13],
[0x7c, 0x7d, 0x99, 4, 0, 0x7c, 0x50, 0x7d, 0x50]
);
assert_eq!(block(&image, b"ENTP")[..4], [0, 0, 0, 0]);
}
#[test]
fn the_parts_that_mean_nothing_are_listed() {
let compiled = compile_text("_main(args) { local a = 1; return a; }\n");
assert_eq!(compiled.unspecified[0], (41, 28));
assert_eq!(compiled.unspecified.len(), 1 + 21 + 2);
let image = compiled.image.expect("an image");
for (offset, len) in &compiled.unspecified[1..] {
assert!(image[*offset..offset + len].iter().all(|&byte| byte == 0));
}
}
#[test]
fn a_program_needs_a_main_function() {
assert_eq!(
compile_messages("f() { }\n"),
[
"0: error: function _main() is not defined: a program built without the \
system library (--no-system) has to define its own"
]
);
}
#[test]
fn constants_are_worked_out_when_compiling() {
let compiled =
compile_text("_main(args) { if (args) return 2 + 3 * 4; return 'a' + 1 + true; }\n");
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10..17], [0x7c, 0x93, 5, 0, 3, 14, 0x50]);
assert_eq!(code[17], 0x05);
let mut pages = Vec::new();
let mut pos = 69;
while pos < image.len() {
let size = u32::from_le_bytes(image[pos + 4..pos + 8].try_into().unwrap()) as usize;
if &image[pos..pos + 4] == b"CPPG" {
pages.push(&image[pos + 10..pos + 10 + size]);
}
pos += 10 + size;
}
let constants: Vec<u8> = pages.last().unwrap()[7..]
.iter()
.map(|byte| byte ^ 0xdf)
.collect();
assert_eq!(constants[..8], *b"\x06\x00a1true");
}
#[test]
fn adding_nil_to_a_constant_string_adds_nothing() {
let compiled = compile_text("_main(args) { return 'ab' + nil; }\n");
let image = compiled.image.expect("an image");
assert!(image.windows(4).any(|window| {
window.iter().map(|byte| byte ^ 0xdf).collect::<Vec<u8>>() == b"\x02\x00ab"
}));
}
#[test]
fn problems_are_reported_as_the_reference_reports_them() {
assert_eq!(
compile_messages("_main(args) { return undefinedThing; }\n"),
["1: error: \"undefinedThing\" is not defined anywhere"]
);
assert_eq!(
compile_messages("f(a) { return a; }\n_main(args) { return f(); }\n"),
["2: error: wrong number of arguments to function \"f\": 1 required, 0 actual"]
);
assert_eq!(
compile_messages("_main(args) { break; }\n"),
["1: error: invalid \"break\" - not in a for/while/do/switch"]
);
assert_eq!(
compile_messages(
"_main(args) { if (args
"
),
[
"2: error: expected ')' after the \"if\" condition, but found \"<End Of File>\"",
"2: error: unexpected end of file in code block - '}' missing",
]
);
}
#[test]
fn locals_that_are_not_used_get_warnings() {
assert_eq!(
compile_messages(
"_main(args)\n{\n local a;\n local b = 1;\n local c;\n return c;\n}\n"
),
[
"3: warning: the local variable \"a\" is never used",
"4: warning: \"b\" is given a value that is never used",
"5: warning: \"c\" is used, but never given a value",
]
);
}
#[test]
fn a_false_while_condition_is_only_noticed_on_the_first_reading() {
assert_eq!(
compile_messages("_main(args)\n{\n while (nil) args = 1;\n if (nil) args = 2;\n}\n"),
[
"3: warning: \"while\" condition is always false - loop body is unreachable",
"4: warning: \"if\" condition is always false",
]
);
}
fn constants(image: &[u8]) -> Vec<u8> {
let mut out = Vec::new();
let mut pos = 69;
while pos < image.len() {
let size = u32::from_le_bytes(image[pos + 4..pos + 8].try_into().unwrap()) as usize;
let data = &image[pos + 10..pos + 10 + size];
if &image[pos..pos + 4] == b"CPPG" && data[0] == 2 {
out.extend(data[7..].iter().map(|byte| byte ^ data[6]));
}
pos += 10 + size;
}
out
}
#[test]
fn built_in_functions_are_called_by_set_and_number() {
let compiled = compile_text(
"builtin 'first/010000' { a(); b(x, y?); }\n\
builtin 'second/020000' { c(...); }\n\
_main(args) { b(1); c(1, 2); return a; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
assert_eq!(
block(&image, b"FNSD"),
b"\x02\x00\x0cfirst/010000\x0dsecond/020000"
);
assert_eq!(
code(&image)[10..23],
[0x02, 0xb1, 1, 1, 0x03, 2, 0x02, 0xb2, 2, 0, 0xb1, 0, 0]
);
assert_eq!(
compile_messages("builtin 'x/1' { b(x, y?); }\n_main(args) { b(); b(1, 2, 3); }\n"),
[
"2: error: too few arguments to function \"b\"",
"2: error: too many arguments to function \"b\"",
]
);
}
#[test]
fn a_constant_list_goes_in_the_constants() {
let compiled = compile_text("_main(args) { return [1, 'two', [3]]; }\n");
let image = compiled.image.expect("an image");
assert_eq!(
constants(&image)[..31],
[
3, 0, 7, 1, 0, 0, 0, 8, 17, 0, 0, 0, 10, 22, 0, 0, 0, 3, 0, b't', b'w', b'o', 1, 0, 7, 3, 0, 0, 0, 4, 0, ]
);
}
#[test]
fn a_list_with_a_variable_is_made_when_the_program_runs() {
let compiled = compile_text("_main(args) { return [args, 2]; }\n");
let image = compiled.image.expect("an image");
assert_eq!(code(&image)[10..17], [0x03, 2, 0x7c, 0xc0, 2, 1, 0x54]);
}
#[test]
fn a_folded_comparison_of_lists_is_still_the_list() {
let compiled = compile_text("_main(args) { local a = [5] == [5]; return a; }\n");
let image = compiled.image.expect("an image");
assert_eq!(code(&image)[10], 0x06);
assert_eq!(constants(&image)[..7], [1, 0, 7, 5, 0, 0, 0]);
let compiled = compile_text("_main(args) { return [5] == [5]; }\n");
let image = compiled.image.expect("an image");
assert_eq!(code(&image)[10], 0x52);
}
#[test]
fn a_string_in_double_quotes_is_displayed_not_used() {
let compiled = compile_text("_main(args) { \"hello\"; }\n");
let image = compiled.image.expect("an image");
assert_eq!(code(&image)[10], 0xb0);
assert_eq!(code(&image)[15], 0x51);
assert_eq!(
compile_messages("_main(args) { local a = \"hello\"; return a; }\n"),
["1: error: a double-quoted string (\"hello\") is not valid within an expression"]
);
}
#[test]
fn source_files_are_utf8_unless_they_say_otherwise() {
let compiled = compile_text("_main(args) { return 'caf\u{e9}'; }\n");
let image = compiled.image.expect("an image");
assert_eq!(constants(&image)[..7], *"\u{5}\0caf\u{e9}".as_bytes());
}
#[test]
fn a_switch_is_a_table_of_values_and_jumps() {
let compiled = compile_text(
"_main(args) { switch (args) { case 1: return 1; case 'a': return 2; } return 3; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
assert_eq!(
code(&image)[10..38],
[
0x7c, 0x90, 2, 0, 7, 1, 0, 0, 0, 11, 0, 8, 0, 0, 0, 0, 6, 0, 7, 0, 0x02, 0x50, 0x03, 2, 0x50, 0x03, 3, 0x50, ]
);
}
#[test]
fn a_case_has_to_be_a_constant_and_nothing_is_generated_if_one_is_not() {
assert_eq!(
compile_messages("_main(args) { local q; switch (args) { case args: ; } }\n"),
["1: error: \"case\" expression has a non-constant value"]
);
assert_eq!(
compile_messages("_main(args) { case 1: ; default: ; }\n"),
[
"1: error: misplaced \"case\" keyword - not in a \"switch\" statement",
"1: error: misplaced \"default\" keyword - not in a \"switch\" statement",
]
);
}
#[test]
fn a_finally_block_is_called_from_every_way_out() {
let compiled = compile_text(
"_main(args) { top: while (args) { try { continue top; } finally { --args; } } }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[2..4], [2, 0]);
assert_eq!(
code[10..46],
[
0x7c, 0x93, 0x21, 0, 0x9c, 0x12, 0, 0x91, 0xf8, 0xff, 0x9c, 0x0c, 0, 0x91, 0xf2, 0xff, 0xe0, 1, 0x9c, 0x04, 0, 0xab, 0xb8, 0xe0, 0, 0x7c, 0x2f, 0xe2, 0, 0x9d, 0, 0, 0x91, 0xdf, 0xff, 0x51,
]
);
let table = usize::from(u16::from_le_bytes([code[6], code[7]]));
assert_eq!(
code[table..table + 12],
[1, 0, 14, 0, 25, 0, 0, 0, 0, 0, 26, 0]
);
}
#[test]
fn labels_and_the_jumps_that_name_them() {
assert_eq!(
compile_messages(
"_main(args)\n{\na: args = 1;\na: args = 2;\nb: ;\n\
goto c;\nbreak d;\nwhile (args) continue e;\n}\n"
),
["4: error: code label \"a\" already defined in this function or method"]
);
assert_eq!(
compile_messages(
"_main(args)\n{\nused: args = 1;\nunused: args = 2;\n\
if (args) goto nowhere;\nif (args) break used;\n\
while (args) continue used;\ngoto used;\n}\n"
),
[
"5: error: invalid \"goto\" - label \"nowhere\" is not defined in this function or method",
"6: error: invalid \"break\" - no label \"used\" on any enclosing statement",
"7: error: invalid \"continue\" - no label \"used\" on any enclosing loop",
"4: warning: label \"unused\" is not referenced",
]
);
assert_eq!(
compile_messages("_main(args) { try { args = 1; } finally { in: args = 2; } goto in; }\n"),
["1: error: 'goto' cannot transfer control into a 'finally' block"]
);
}
#[test]
fn an_expression_embedded_in_a_displayed_string_is_displayed() {
let compiled = compile_text("_main(args) { \"a<<args>>b<<>>\"; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10], 0xb0);
assert_eq!(code[15..17], [0x7c, 0xb9]);
assert_eq!(code[17], 0xb0);
assert_eq!(code[22], 0x51);
}
#[test]
fn an_expression_embedded_in_a_string_value_is_added_to_it() {
let compiled = compile_text("_main(args) { return 'a<<args>>b'; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10], 0x05);
assert_eq!(code[15..17], [0x7c, 0x22]);
assert_eq!(code[17], 0x05);
assert_eq!(code[22..24], [0x22, 0x50]);
assert_eq!(
compile_messages(
"_main(args) { switch (args) { case 'a<<1>>b<<if nil>>c<<else>>d<<end>>': ; } }\n"
),
[] as [&str; 0]
);
}
#[test]
fn what_is_embedded_can_depend_on_a_condition() {
let compiled = compile_text("_main(args) { \"<<if args>>yes<<else>>no<<end>>\"; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10..12], [0x7c, 0x93]);
assert_eq!(code[14], 0xb0);
assert_eq!(code[19], 0x51);
assert_eq!(code[22], 0xb0);
assert_eq!(code[27], 0x51);
assert_eq!(
compile_messages("_main(args) { \"a<<else>>b\"; }\n"),
["1: error: <<else>> found in string without a matching <<if>> or <<unless>>"]
);
assert_eq!(
compile_messages("_main(args) { \"a<<args args>>b\"; }\n"),
["1: error: expected '>>' and the string continuation, but found \"args\""]
);
assert_eq!(
compile_messages("_main(args) { \"<<one of>>a<<or>>b<<stopping>>\"; }\n"),
["1: error: <<one of>> requires class OneOfIndexGen to be defined in the program"]
);
}
#[test]
fn a_string_template_makes_an_embedded_phrase_a_call() {
let compiled = compile_text(
"string template <<* twice>> twice;\n\
twice(x) { return x + x; }\n\
_main(args) { \"<<args twice>>\"; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
let call = code
.windows(3)
.position(|bytes| bytes == [0x7c, 0x58, 1])
.expect("a call with the argument");
assert_eq!(code[call + 7..call + 9], [0x8b, 0xb9]);
assert_eq!(
compile_messages("string template <<* twice>> twice;\n_main(args) { \"<<1 twice>>\"; }\n"),
["0: error: unresolved external reference \"twice\""]
);
assert_eq!(
compile_messages("string template <<* twice>> twice;\n_main(args) { }\n"),
[] as [&str; 0]
);
assert_eq!(
compile_messages("f() { }\nstring template <<a * b * c>> f;\n_main(args) { }\n"),
[
"2: error: only one '*' is allowed in a string template",
"2: error: string template processor function f() does not match template - \
must take 1 parameter(s) and return a value",
]
);
}
#[test]
fn the_compiler_gives_up_after_a_hundred_errors() {
let mut source = String::from("_main(args) {\n");
for _ in 0..150 {
source.push_str("1 2;\n");
}
source.push_str("}\n");
let messages = compile_messages(&source);
assert_eq!(messages.len(), 102);
assert_eq!(messages[100], "102: error: expected ';', but found \"2\"");
assert_eq!(
messages[101],
"102: fatal error: too many errors (try fixing the first couple of errors and recompiling)"
);
let mut source = String::from("_main(args) {\n");
for index in 0..150 {
source.push_str(&format!("u{index}();\n"));
}
source.push_str("}\n");
let messages = compile_messages(&source);
assert_eq!(messages.len(), 102);
assert_eq!(
messages[100],
"102: error: \"u100\" is not defined anywhere"
);
assert!(messages[101].starts_with("102: fatal error: too many errors"));
}
#[test]
fn an_undefined_name_is_reported_once() {
assert_eq!(
compile_messages("f() { nosuch(); }\n_main(args) { nosuch; other = nosuch; }\n"),
[
"1: error: \"nosuch\" is not defined anywhere",
"2: error: \"other\" is not defined anywhere",
]
);
}
#[test]
fn a_template_value_is_reported_where_it_is() {
assert_eq!(
compile_messages(
"object template 'name' \"desc\";\n\
thing: object 'thing'\n\
\x20 \"A thing, <<nosuch>> and\n\
\x20 more. \"\n\
\x20 size = 2\n\
;\n\
_main(args) { }\n"
),
["3: warning: \"nosuch\" is not defined anywhere"]
);
}
#[test]
fn a_comparison_inside_type_of_is_warned_about() {
assert_eq!(
compile_messages(
"builtin 'hexicon-gen/030008' { typeOf(val); }\n\
f(x) { if (typeOf(x == 2)) return 1; return typeOf(x) == 2; }\n\
_main(args) { f(1); }\n"
),
[
"2: warning: typeOf() is given a comparison, whose type is always the same; \
the comparison belongs outside: typeOf(x) == TypeObject"
]
);
}
#[test]
fn a_long_string_in_a_message_is_cut_short() {
assert_eq!(
compile_messages("_main(args) { 1 'a string that goes on\\nand on'; }\n"),
["1: error: expected ';', but found \"'a string that goes o...'\""]
);
assert_eq!(
compile_messages("_main(args) { 1 'two\\nlines'; }\n"),
["1: error: expected ';', but found \"'two\\nlines'\""]
);
}
#[test]
fn an_object_is_its_superclasses_and_its_properties_in_order_of_number() {
let compiled = compile_text(
"thing: object\n name = 'thing'\n weight = 3\n;\n\
_main(args) { return thing.name; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[..6], [1, 0, 0, 0, 0, 0]);
assert_eq!(objs[6..12], [1, 0, 0, 0, 27, 0]);
assert_eq!(objs[12..18], [0, 0, 3, 0, 0, 0]);
assert_eq!(objs[18..25], [7, 0, 7, 1, 0, 0, 0]);
assert_eq!(objs[25..32], [26, 0, 7, 3, 0, 0, 0]);
assert_eq!(objs[32..35], [27, 0, 8]);
assert_eq!(code(&image)[10..18], [0x66, 1, 0, 0, 0, 27, 0, 0x54]);
}
#[test]
fn a_method_has_a_self_and_a_function_does_not() {
let compiled = compile_text(
"a: object\n size = 1\n grow(by) { size += by; return self.size; }\n;\n\
_main(args) { return a.grow(2); }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10], 0x63);
assert_eq!(code[13..17], [0x8b, 0x7c, 0x22, 0xe7]);
assert_eq!(code[19], 0x63);
assert_eq!(code[22], 0x54);
assert_eq!(
compile_messages(
"a: object size = 1;\n_main(args) { size = 2; return self.size + size; }\n"
),
[
"2: error: cannot assign to property \"size\" without \"object.\" prefix - no \"self\"",
"2: error: 'self' is not valid in this context",
"2: error: cannot call property \"size\" without \"object.\" prefix - no \"self\"",
]
);
}
#[test]
fn a_name_nothing_defines_is_taken_for_a_property_where_one_makes_sense() {
assert_eq!(
compile_messages(
"a: object m() { return size; }\n;\n_main(args) { return a.weight + other; }\n"
),
[
"1: warning: \"size\" is not defined anywhere",
"3: warning: \"weight\" is not defined anywhere",
"3: error: \"other\" is not defined anywhere",
]
);
}
#[test]
fn scenery_that_is_wrong_says_what_is_missing() {
let start = "+ property location;\nclass Decoration: object;\n\
Decoration template 'vocab' \"desc\";\nroom: object;\n";
let wrong = |scenery: &str| compile_messages(&format!("{start}{scenery}\n_main(args) {{ }}\n"));
assert_eq!(
wrong("+ scenery 'range' \"A range. \";"),
[
"5: error: expected ':' and a description after a piece of scenery's words, but \
found \"\"A range. \"\""
]
);
assert_eq!(
wrong("+ scenery 'range': 'A range. ';"),
[
"5: error: expected a piece of scenery's description, in double quotes, but found \
\"'A range. '\""
]
);
assert_eq!(
wrong("+ scenery 'range': \"A range. \" stove: \"A stove. \";"),
[
"5: error: expected a piece of scenery's words, in single quotes, or ';' at the end, \
but found \"stove\""
]
);
}
#[test]
fn object_definitions_that_are_wrong() {
assert_eq!(
compile_messages(
"a: object x = 1 x = 2;\na: object;\nf() { }\nf: object;\nb: b;\nc object;\n\
d: object x 1;\n_main(args) { }\n"
),
[
"1: error: property \"x\" already defined in object",
"2: error: object \"a\" is already defined - can't redefine as object",
"4: error: \"f\" is already the name of a function, so it cannot also name an object",
"5: error: circular class definition: b is a subclass of b",
"6: error: missing semicolon at end of object definition - found \"object\"",
"7: error: '=' required between property name and value - found \"1\"",
]
);
assert_eq!(
compile_messages("a: object;\nb: object x = 1\na: object y = 2;\n_main(args) { }\n"),
[
"3: error: this object has no ';' at its end",
"3: error: object \"a\" is already defined - can't redefine as object",
]
);
assert_eq!(
compile_messages("a: Nosuch;\nf() { }\nb: f;\n_main(args) { }\n"),
[
"1: error: \"Nosuch\" is not defined anywhere",
"3: error: invalid superclass \"f\" - not an object",
]
);
}
#[test]
fn a_constructor_returns_the_object_and_several_superclasses_get_one() {
let compiled = compile_text("class A: object;\nclass B: object;\nc: A, B;\n_main(args) { }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[..4], [0x80, 0, 1, 0]);
assert_eq!(
code[10..36],
[
0x0d, 0, 0xe0, 0, 0x01, 0xaa, 0x0e, 0x76, 0x74, 0, 1, 0, 1, 0, 0, 0, 0x01, 0xaa, 0x0e, 0x76, 0x74, 0, 1, 0, 2, 0, ]
);
assert_eq!(code[38..40], [0x84, 0x50]);
assert_eq!(
compile_messages("a: object construct() { return 1; }\n;\n_main(args) { }\n"),
["1: error: \"construct\" cannot return a value"]
);
}
#[test]
fn a_catch_names_a_class_of_exception() {
let compiled = compile_text(
"class Oops: object;\n_main(args) { try { throw args; } catch (Oops e) { return e; } }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
let table = usize::from(u16::from_le_bytes([code[6], code[7]]));
assert_eq!(code[table..table + 2], [1, 0]);
assert_eq!(code[table + 6..table + 10], [1, 0, 0, 0]);
assert_eq!(
compile_messages("f() { }\n_main(args) { try { } catch (f e) { } catch (nosuch e) { } }\n"),
[
"2: error: exception class \"f\" in \"catch\" clause is not an object",
"2: error: \"nosuch\" is not defined anywhere",
]
);
}
#[test]
fn a_jump_to_a_label_in_code_that_was_left_out_cannot_be_compiled() {
assert_eq!(
compile_messages("_main(args) { if (true) args = 1; else { a: args = 2; } goto a; }\n"),
[
"1: warning: \"if\" condition is always true - \"else\" part is unreachable",
"0: internal error: 1 unresolved branches",
]
);
}
#[test]
fn new_makes_an_object_of_a_class() {
let compiled = compile_text("class A: object;\n_main(args) { return new A(args, 2); }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
assert_eq!(
code(&image)[10..22],
[0x03, 2, 0x7c, 0x07, 1, 0, 0, 0, 0xc0, 3, 0, 0x54]
);
assert_eq!(
compile_messages("f() { }\n_main(args) { new f(); new args; new nosuch; }\n"),
[
"2: error: cannot apply operator 'new' to this expression",
"2: error: cannot apply operator 'new' to this expression",
"2: error: \"nosuch\" is not defined anywhere",
]
);
}
#[test]
fn inherited_calls_what_a_method_overrides() {
let compiled = compile_text(
"class A: object m(x) { return x; };\n\
class B: A m(x) { return inherited(x) + inherited A.m(x); };\n\
_main(args) { }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
let start = code
.windows(4)
.position(|bytes| bytes == [0x7c, 0x8e, 1, 0x73])
.expect("inherited through the property being run");
assert_eq!(code[start + 4..start + 6], [1, 0x8b]);
assert_eq!(code[start + 6..start + 9], [0x7c, 0x74, 1]);
assert_eq!(code[start + 11..start + 15], [1, 0, 0, 0]);
assert_eq!(
compile_messages("a: object;\n_main(args) { inherited.x; inherited a.x; targetprop; }\n"),
[
"2: error: 'self' is not valid in this context",
"2: warning: \"x\" is not defined anywhere",
"2: error: 'self' is not valid in this context",
"2: error: 'targetprop' is not available in this context",
]
);
}
#[test]
fn the_address_of_a_property_is_a_constant() {
let compiled = compile_text(
"a: object x = 1 y = &x;\n_main(args) { switch (args) { case &x: return a.(&y); } return &x; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10..15], [0x7c, 0x90, 1, 0, 6]);
assert_eq!(
compile_messages("_main(args) { return [&later, &never]; }\nlater() { }\n"),
[] as [&str; 0]
);
assert_eq!(
compile_messages("_main(args) { local q; return [&q, &1]; }\n"),
["1: error: invalid address expression - can't apply '&' operator",]
);
}
#[test]
fn a_built_in_class_is_declared_with_its_properties() {
let compiled = compile_text(
"builtin class Object 'root-object/030004' { ofKind(cls); }\n\
builtin class List 'list/030008': Object { length(); }\n\
_main(args) { return [args.length(), new List()]; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let mcld = block(&image, b"MCLD");
assert_eq!(mcld[..2], [10, 0]);
let text = String::from_utf8_lossy(mcld);
let list = text.find("list/030008").expect("the list class");
let root = text.find("root-object/030004").expect("the root class");
let last = text
.find("intrinsic-class/000000")
.expect("the class of classes");
assert!(list < root && root < last);
assert_eq!(mcld[list + 11..list + 15], [1, 0, 2, 0]);
let code = code(&image);
assert!(code.windows(3).any(|bytes| bytes == [0xc0, 0, 1]));
assert_eq!(
compile_messages(
"f() { }\nbuiltin class f 'f/030000' { }\n\
builtin class A 'a/030000': Nosuch { 1; }\n_main(args) { return A(); }\n"
),
[
"2: error: \"f\" is already the name of a function, so it cannot also name a built-in class",
"3: error: the superclass \"Nosuch\" is not defined",
"3: error: expected property name in built-in class list, but found \"1\"",
]
);
}
#[test]
fn exports_come_first_among_the_names_the_image_gives() {
let compiled = compile_text(
"property alpha;\nexport alpha 'Alpha.name';\nexport f;\nf() { }\n_main(args) { }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let symd = block(&image, b"SYMD");
assert_eq!(symd[..2], [23, 0]);
assert_eq!(symd[2..5], [6, 26, 0]);
assert_eq!(&symd[7..18], b"\x0aAlpha.name");
assert_eq!(symd[18], 12);
assert_eq!(&symd[23..25], b"\x01f");
assert_eq!(
compile_messages(
"export a 'LastProp';\nexport b 'same';\nexport c 'same';\n_main(args) { }\n"
),
[
"0: error: external name \"LastProp\" illegal in 'export' - reserved for compiler use",
"0: warning: undefined name \"a\" - assuming this is a property name",
"0: warning: undefined name \"b\" - assuming this is a property name",
"0: error: duplicate export for external name \"same\": \"b\" and \"c\"",
]
);
}
const COLLECTION: &str = "builtin class Object 'root-object/030004' { ofKind(cls); }\n\
builtin class Collection 'collection/030000': Object { createIterator(); }\n";
#[test]
fn a_loop_over_a_collection_asks_it_for_an_iterator() {
let source = format!("{COLLECTION}_main(args) {{ foreach (local x in args) args = x; }}\n");
let compiled = compile_text(&source);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[2..4], [2, 0]);
assert_eq!(code[10..12], [0x7c, 0x60]);
assert_eq!(code[14..16], [0xee, 1]);
assert_eq!(code[16..19], [0xa2, 1, 0]);
assert_eq!(code[21..23], [0xe0, 0]);
assert_eq!(
compile_messages("_main(args) { foreach (local x in args) ; for (local y in args) ; }\n"),
[
"1: error: Container.createIterator is not defined - foreach..in is not valid",
"1: error: Container.createIterator is not defined - for..in is not valid",
"1: warning: \"x\" is given a value that is never used",
"1: warning: \"y\" is given a value that is never used",
]
);
}
#[test]
fn a_loop_over_a_range_counts() {
let compiled = compile_text("_main(args) { for (local i in 1..args step -2) args = i; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[2..4], [2, 0]);
assert_eq!(code[10..12], [0xda, 0]);
assert!(code.windows(3).any(|bytes| bytes == [0x7c, 0xe0, 1]));
assert!(code[12..40].contains(&0x98));
}
#[test]
fn a_test_for_being_among_some_values_compares_with_each() {
let compiled = compile_text("_main(args) { return args is in (1, 2); }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
assert_eq!(
code(&image)[10..30],
[
0x7c, 0x88, 0x02, 0x94, 0x0d, 0, 0x88, 0x03, 2, 0x94, 0x07, 0, 0x89, 0x08, 0x50, 0xf2,
0xf2, 0x89, 0x52, 0x50,
]
);
assert_eq!(
compile_messages("_main(args) { args is in 1; args not in (1 2); }\n"),
[
"1: error: missing '(' after 'in' operator - found \"1\"",
"1: error: expected ')' at end of 'in' list, but found \";\"",
"1: error: expected ',' or ')' in 'in' list, but found \"2\"",
]
);
}
#[test]
fn a_source_that_ends_in_a_for_header_stops_at_the_limit_on_errors() {
let messages = compile_messages("_main(a) { for (a x\n");
assert_eq!(messages.len(), 102);
assert!(messages[101].contains("fatal error: too many errors"));
}
fn has_bytes(code: &[u8], bytes: &[u8]) -> bool {
code.windows(bytes.len()).any(|window| window == bytes)
}
#[test]
fn an_anonymous_function_that_shares_nothing_is_just_its_code() {
let compiled = compile_text("_main(args) { local f = {x: x + 1}; return f(2); }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[2..4], [1, 0]);
assert_eq!(code[10], 0x0b);
assert_eq!(code[15..17], [0xe0, 0]);
assert!(!has_bytes(code, &[0xc0, 2, 4]));
assert!(has_bytes(code, &[0x7c, 0x2e, 0x50]));
}
#[test]
fn a_local_an_anonymous_function_uses_lives_in_a_vector() {
let compiled =
compile_text("_main(args) { local n = 5; local f = {x: x + n}; n = 6; return f(1); }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[2..4], [3, 0]);
assert_eq!(code[10..18], [0x03, 2, 0x88, 0xc0, 2, 4, 0xee, 2]);
assert_eq!(code[18..23], [0x03, 5, 0xef, 2, 2]);
assert!(has_bytes(code, &[0xc0, 2, 5, 0xee, 1]));
assert!(has_bytes(code, &[0x8e, 4, 0xbc, 2, 0xe0, 0]));
assert!(has_bytes(code, &[0xbb, 0, 2]));
}
#[test]
fn an_anonymous_function_in_a_method_takes_the_methods_self() {
let compiled = compile_text(
"a: object v = 1 m() { local f = {: v}; return f(); } ;\n_main(args) { return a.m(); }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert!(has_bytes(code, &[0x84, 0xef, 1, 1]));
assert!(has_bytes(code, &[0x88, 0xbc, 1, 0xeb]));
}
#[test]
fn a_short_anonymous_function_has_its_own_mistakes() {
assert_eq!(
compile_messages(
"_main(args)\n{\n local a = {x: x; };\n local b = {x x };\n \
local c = function(x) multimethod { return x; };\n return {: 1 };\n}\n"
),
[
"3: error: semicolon is not allowed in a short anonymous function",
"4: error: expected a comma in parameter list, but found \"x\"",
"4: error: parameter \"x\" defined more than once",
"4: error: missing colon at end of anonymous function formal list - found \"}\"",
"4: error: expected an operand, but found \"}\"",
"5: error: 'multimethod' is not allowed here",
"6: error: expected ';' or expression after \"return\", but found \"{\"",
"6: error: expected integer, string, name, '[', or '(', but found \":\"",
"6: error: expected ';', but found \"}\"",
]
);
assert_eq!(
compile_messages("_main(args)\n{\n local f = {x: x\n"),
[
"4: error: semicolon is not allowed in a short-form anonymous function",
"4: error: expected ',' or ';' after local variable, but found \"<End Of File>\"",
"4: error: unexpected end of file in code block - '}' missing",
]
);
}
const PARAM_SUPPORT: &str = "builtin 'hexicon-vm/010006' { collectGarbage(); setSay(val); vmVersion(); \
vmId(); vmBanner(); inPreinit(); debugTrace(mode, ...); \
globalSymbols(which?); newProperty(); stackTrace(level?, flags?); \
namedArg(name, defval?); namedArgList(); }\n\
builtin 'hexicon-gen/030008' { typeOf(val); arg(idx); }\n";
#[test]
fn an_optional_parameter_is_a_local_filled_in_at_the_start() {
let source =
format!("{PARAM_SUPPORT}f(a, b = a + 2) {{ return b; }}\n_main(args) {{ return f(1); }}\n");
let compiled = compile_text(&source);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[0..4], [1, 1, 1, 0]);
assert_eq!(
code[10..31],
[
0x03, 2, 0x87, 0x96, 11, 0, 0x03, 2, 0xb2, 1, 1, 0x8b, 0x91, 6, 0, 0x7c, 0x03, 2, 0x22, 0xe0, 0, ]
);
}
#[test]
fn a_named_parameter_is_asked_for_by_name() {
let source = format!(
"{PARAM_SUPPORT}f(n:, m: = 3) {{ return n + m; }}\n\
_main(args) {{ return f(m: 1, n: 2); }}\n"
);
let compiled = compile_code_only(&source);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[0..4], [0, 0, 2, 0]);
assert_eq!(code[10], 0x05);
assert_eq!(code[15..20], [0xb1, 1, 10, 0xee, 0]);
assert_eq!(code[20..23], [0x03, 3, 0x05]);
assert_eq!(code[27..32], [0xb1, 2, 10, 0xee, 1]);
assert!(has_bytes(code, &[0x03, 2, 0x02, 0x58, 0]));
assert!(has_bytes(code, &[0x56, 2, 3, 0]));
assert!(has_bytes(
code,
&[0x57, 10, 0, 2, 0, 6, 0, 7, 0, 8, 0, b'n', b'm']
));
}
#[test]
fn optional_and_named_parameters_have_their_own_mistakes() {
let source = format!(
"{PARAM_SUPPORT}f(a?, b) {{ }}\ng(a?) {{ }}\n\
_main(args)\n{{\n g(1, 2);\n g(a: [1]...);\n}}\n"
);
assert_eq!(
compile_messages(&source),
[
"3: error: parameter 'b' must be optional because it follows an optional parameter",
"8: error: a pass-by-name argument value cannot use an ellipsis '...'",
]
);
assert_eq!(
compile_messages("f(a?) { }\n_main(args) { }\n"),
["0: error: optional parameters require support function \"arg\", which is undefined"]
);
}
#[test]
fn enum_values_are_numbered_in_the_order_of_the_table_of_names() {
let compiled = compile_text(
"enum red, green, blue;\n\
_main(args) { local a = red == red; return [blue, green, red, a]; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10..12], [0xd8, 0]);
assert_eq!(
code[13..28],
[0x0f, 2, 0, 0, 0, 0x0f, 1, 0, 0, 0, 0x0f, 3, 0, 0, 0]
);
assert_eq!(
compile_messages("f() { }\nenum f, ;\n_main(args) { }\n"),
[
"2: error: \"f\" is already the name of a function, so it cannot also name an enum",
"2: error: name expected in 'enum' list - found \";\"",
]
);
}
#[test]
fn a_static_property_is_code_that_runs_before_the_program() {
let compiled = compile_text(
"f() { return 7; }\na: object p = static f() q = static 5;\n\
_main(args) { return a.p; }\n",
);
assert!(
compiled
.diagnostics
.iter()
.all(|d| d.severity == Severity::Warning),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let sini = block(&image, b"SINI");
assert_eq!(sini[4..12], [0, 8, 0, 0, 1, 0, 0, 0]);
assert_eq!(sini[12..16], [1, 0, 0, 0]);
let prop = &sini[16..18];
assert!(has_bytes(
&image,
&[0x58, 0, 0, 0, 0, 0, 0xa5, 0xe7, prop[0], prop[1], 0x50]
));
}
fn blocks<'i>(image: &'i [u8], kind: &[u8; 4]) -> Vec<&'i [u8]> {
let mut found = Vec::new();
let mut pos = 69;
while pos + 10 <= image.len() {
let size = u32::from_le_bytes(image[pos + 4..pos + 8].try_into().unwrap()) as usize;
if &image[pos..pos + 4] == kind {
found.push(&image[pos + 10..pos + 10 + size]);
}
pos += 10 + size;
}
found
}
#[test]
fn modify_puts_a_function_in_front_of_the_one_there_was() {
let compiled = compile_text(
"f(x) { return x + 1; }\nmodify f(x) { return replaced(x) * 2; }\n\
g() { return 77; }\nreplace g() { return 88; }\n\
_main(args) { return f(1) + g(); }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert_eq!(code[10..13], [0x7c, 0x2e, 0x50]);
assert!(has_bytes(code, &[0x7c, 0x58, 1, 0, 0, 0, 0]));
assert!(has_bytes(code, &[0x03, 88, 0x50]));
assert!(!has_bytes(code, &[0x03, 77, 0x50]));
}
#[test]
fn modify_leaves_the_object_there_was_as_a_class_behind_the_new_one() {
let compiled = compile_text("a: object v = 1;\nmodify a v = 2;\n_main(args) { return a.v; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [2, 0]);
assert_eq!(objs[6..10], [1, 0, 0, 0]);
assert_eq!(objs[12..18], [0, 0, 2, 0, 1, 0]);
let second = &objs[6 + 12 + 14..];
assert_eq!(second[0..4], [2, 0, 0, 0]);
assert_eq!(second[6..16], [1, 0, 1, 0, 0, 0, 1, 0, 0, 0]);
}
#[test]
fn modify_of_a_built_in_class_makes_objects_for_what_is_added() {
let compiled = compile_text(
"builtin class Object 'root-object/030004' { ofKind(cls); }\n\
modify Object hello = 7;\n_main(args) { }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let all = blocks(&image, b"OBJS");
let added = all
.iter()
.find(|objs| objs[2..4] == [6, 0])
.expect("a block of additions");
assert_eq!(added[0..2], [2, 0]);
assert_eq!(added[6..10], [3, 0, 0, 0]);
assert_eq!(added[12..16], [1, 0, 1, 0]);
assert_eq!(added[18..22], [2, 0, 0, 0]);
let class = all.last().expect("the classes");
assert_eq!(class[6..10], [1, 0, 0, 0]);
assert_eq!(class[16..20], [3, 0, 0, 0]);
}
#[test]
fn replace_and_modify_have_their_own_mistakes() {
assert_eq!(
compile_messages("replace a: object;\n_main(args) { }\n"),
["1: error: replace/modify cannot be used with an object not previously defined"]
);
assert_eq!(
compile_messages("f() { return replaced(); }\n_main(args) { }\n"),
["1: error: 'replaced' is not available in this context"]
);
assert_eq!(
compile_messages("b: object replace x = 1;\n_main(args) { }\n"),
["1: error: \"replace\" can be used with a property only in a \"modify\" object"]
);
assert_eq!(
compile_messages("modify f() { }\n_main(args) { }\n"),
[
"0: error: modified or replaced function \"f\" found in source file \"main.hxn\" \
before original function definition was loaded"
]
);
}
#[test]
fn a_function_can_be_modified_more_than_once() {
let compiled = compile_text(
"g(x) { return x * 2; }\nmodify g(x) { return replaced(x) + 1; }\n\
modify g(x) { return replaced(x) * 10; }\n_main(args) { return g(3); }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert!(has_bytes(code, &[0x58, 1, 0, 0, 0, 0]));
let calls = code.windows(2).filter(|pair| *pair == [0x58, 1]).count();
assert_eq!(calls, 3);
}
#[test]
fn a_template_gives_properties_to_the_values_an_object_starts_with() {
let compiled = compile_text(
"object template 'name' [items]? @where;\n\
a: object 'first' @nil;\nb: object 'second' [1, 2] @a;\n\
_main(args) { return a.name; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [2, 0]);
assert_eq!(objs[14..16], [3, 0]);
let second = &objs[6 + 12 + 21..];
assert_eq!(second[8..10], [4, 0]);
assert_eq!(
compile_messages(
"object template 'name';\nclass C: object;\nC template [items];\n\
a: object [1];\nb: C 'x' 'y';\nextern class E;\nc: E 'x';\n_main(args) { }\n"
),
[
"4: error: the values written here without names fit none of the class's templates",
"5: error: the values written here without names fit none of the class's templates",
"7: error: cannot use template with an unimported extern class as superclass",
]
);
let messages = compile_messages(
"object template 'a b' @5;
_main(args) { }
",
);
assert_eq!(
messages[..2],
[
"1: error: property name required in object template string - found 'a b'",
"1: error: property name required after \"@\" in object template - found \"5\"",
]
);
assert!(messages[2].ends_with("error: empty template definition"));
}
#[test]
fn extern_declares_and_transient_objects_go_in_a_block_of_their_own() {
let compiled = compile_text(
"extern function f(x);\nextern object later;\nextern object never;\n\
_main(args) { return f(later); }\nf(x) { return x; }\n\
later: object;\ntransient t: object;\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let all = blocks(&image, b"OBJS");
assert_eq!(all.len(), 2);
assert_eq!(all[0][0..6], [1, 0, 0, 0, 0, 0]);
assert_eq!(all[1][0..6], [1, 0, 0, 0, 2, 0]);
assert_eq!(
compile_messages(
"extern function f(x);\nextern object o;\nextern object unused;\n\
_main(args) { return f(o); }\n"
),
[
"0: error: unresolved external reference \"f\"",
"0: error: unresolved external reference \"o\"",
]
);
}
#[test]
fn plus_signs_put_an_object_in_the_one_before_with_one_fewer() {
let compiled = compile_text(
"+ property loc;\na: object;\n+ b: object;\n++ c: object;\n+ d: object loc = c;\n\
_main(args) { return c.loc; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [4, 0]);
let second = &objs[6 + 6 + 13..];
assert_eq!(second[0..4], [2, 0, 0, 0]);
assert_eq!(second[8..10], [2, 0]);
assert_eq!(second[21..26], [5, 1, 0, 0, 0]);
let last = &objs[6 + 6 + 13 + 2 * 26..];
assert_eq!(last[0..4], [4, 0, 0, 0]);
assert_eq!(last[21..26], [5, 3, 0, 0, 0]);
assert_eq!(
compile_messages("a: object;\n+ b: object;\n_main(args) { }\n"),
["2: error: too many '+' signs in object definition - location not defined"]
);
assert_eq!(
compile_messages("+ property loc;\na: object;\n+++ b: object;\n+ property 5;\n"),
[
"3: error: too many '+' signs in object definition - location not defined",
"4: error: name expected after '+ property', but found \"5\" instead",
]
);
}
#[test]
fn an_object_can_be_the_value_of_a_property_of_another() {
let compiled = compile_text(
"a: object\n inner: object { v = 1 }\n;\n\
_main(args) { return a.inner.lexicalParent; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [2, 0]);
assert_eq!(objs[6..10], [1, 0, 0, 0]);
assert_eq!(objs[14..16], [2, 0]);
assert_eq!(objs[27..32], [5, 2, 0, 0, 0]);
let inner = &objs[6 + 6 + 20..];
assert_eq!(inner[0..4], [2, 0, 0, 0]);
assert_eq!(inner[8..10], [3, 0]);
assert_eq!(inner[12..19], [6, 0, 5, 1, 0, 0, 0]);
assert_eq!(
compile_messages(
"a: object\n lexicalParent = 1\n n: object { lexicalParent = 2 }\n;\n\
b: object\n n: object v = 1;\n_main(args) { }\n"
),
[
"3: error: property \"lexicalParent\" already defined in object",
"6: error: this object has no ';' at its end",
]
);
}
#[test]
fn a_propertyset_gives_its_properties_a_pattern_for_their_names_and_parameters() {
let grouped = compile_code_only(
"a: object\n propertyset 'ver*Take' (actor, *, extra) {\n\
Dobj(x) { return actor + x + extra; }\n Iobj { return actor; }\n\
val = 7\n propertyset 'x*' (*, last) { A(q) { return q + last; } }\n }\n;\n\
_main(args) { return a.verDobjTake(1, 2, 3) + a.vervalTake; }\n",
);
let written_out = compile_code_only(
"a: object\n verDobjTake(actor, x, extra) { return actor + x + extra; }\n\
verIobjTake(actor, extra) { return actor; }\n vervalTake = 7\n\
verxATake(actor, q, last, extra) { return q + last; }\n;\n\
_main(args) { return a.verDobjTake(1, 2, 3) + a.vervalTake; }\n",
);
assert!(
grouped
.diagnostics
.iter()
.all(|d| d.severity == Severity::Warning),
"{:?}",
grouped.diagnostics
);
assert!(grouped.image.is_some());
assert_eq!(grouped.image, written_out.image);
assert_eq!(
compile_messages(
"a: object\n propertyset 'no star' { x = 1 }\n propertyset 5 { }\n\
propertyset 'a*' (x, y) { }\n propertyset 'b*' w = 1 }\n;\n_main(args) { }\n"
),
[
"2: error: propertyset pattern string \"'no star'\" is invalid",
"3: error: expected property name pattern string after 'propertyset' - found \"5\"",
"4: error: invalid parameters in propertyset definition",
"5: error: missing open brace '{' in propertyset definition - found \"w\"",
]
);
}
#[test]
fn a_method_can_stand_for_an_operator() {
let compiled = compile_text(
"a: object\n operator +(x) { return x; }\n operator []=(i, v) { return self; }\n\
operator negate() { return 1; }\n;\n\
_main(args) { return [a + 1, &operator +, &operator []=]; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [1, 0]);
assert_eq!(objs[14..16], [4, 0]);
assert_eq!(objs[25..27], [11, 0]);
assert_eq!(objs[32..34], [23, 0]);
assert_eq!(objs[39..41], [25, 0]);
assert_eq!(
compile_messages(
"a: object\n operator +() { }\n operator ~(x) { }\n operator []=(i) { }\n\
operator ==(x) { }\n propertyset 'x*' { operator -(x) { } }\n;\n_main(args) { }\n"
),
[
"3: error: wrong number of parameters defined for overloaded operator (1 required)",
"4: error: wrong number of parameters defined for overloaded operator (0 required)",
"5: error: wrong number of parameters defined for overloaded operator (2 required)",
"5: error: invalid overloaded operator '=='",
"6: error: an 'operator' property cannot be defined within a 'propertyset'",
]
);
}
#[test]
fn delegated_runs_another_objects_method_as_this_ones() {
let compiled = compile_text(
"a: object m(x) { return x; };\n\
b: object\n m(x) { return delegated a.m(x) + delegated a(x) + delegated a.(&m)(x); }\n;\n\
f(...) { return argcount; }\n_main(args) { return b.m(1) + f(); }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert!(has_bytes(code, &[0x77, 1]));
assert!(has_bytes(code, &[0x78, 1]));
assert!(has_bytes(code, &[0x87, 0x50]));
assert_eq!(
compile_messages("a: object m() { return 1; };\n_main(args) { return delegated a.m(); }\n"),
["2: error: 'self' is not valid in this context"]
);
}
#[test]
fn a_number_that_is_not_an_integer_is_an_object_in_the_image() {
let compiled = compile_text("_main(args) { return [1.5, 2.5 + 1, 3.0 * 2, 1.5 < 2]; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [3, 0]);
assert_eq!(objs[6..18], [1, 0, 0, 0, 6, 0, 2, 0, 1, 0, 0, 0x15]);
assert_eq!(objs[18..30], [2, 0, 0, 0, 6, 0, 2, 0, 1, 0, 0, 0x35]);
assert_eq!(objs[30..42], [3, 0, 0, 0, 6, 0, 1, 0, 1, 0, 0, 0x60]);
let text = String::from_utf8_lossy(&image);
assert!(text.contains("bignumber/000000"));
}
#[test]
fn an_integer_too_large_to_be_one_is_a_number_that_is_not() {
assert_eq!(
compile_messages(
"_main(args)\n{\n local a = 2147483647 + 1;\n local b = 4294967296 - 4294967295;\n\
local c = -2147483648;\n local d = 0x100000000;\n return [a, b, c, d];\n}\n"
),
[
"3: warning: constant value exceeds integer range; promoted to Decimal",
"6: warning: constant value exceeds integer range; promoted to Decimal",
]
);
assert_eq!(
compile_messages("_main(args) { return [1.5 | 2, 1.5 / 0, ~1.5]; }\n"),
[
"1: error: floating point values can't be used with this operator",
"1: error: divide by zero in constant expression",
"1: error: unary '~' operator requires numeric value in constant expression",
]
);
}
#[test]
fn a_constant_integer_division_by_zero_is_done_again_as_a_big_number() {
let divide = "1: error: divide by zero in constant expression";
assert_eq!(
compile_messages("_main(args) { return [7 / 0, 7 % 0, 7 / 0 | 1]; }\n"),
[
divide,
divide,
divide,
divide,
divide,
"1: error: floating point values can't be used with this operator",
]
);
}
#[test]
fn an_object_named_by_punctuation_is_found_where_the_line_says_so() {
let redefined = "object \"(\" is already defined - can't redefine as object";
let same_place = compile_messages("_main(args) { }\n+((1));\n+((2));\n");
assert!(
same_place.iter().any(|m| m.ends_with(redefined)),
"{same_place:?}"
);
let elsewhere = compile_messages("_main(args) { }\n+((1));\n +((2));\n");
assert!(
!elsewhere.iter().any(|m| m.ends_with(redefined)),
"{elsewhere:?}"
);
}
#[test]
fn a_regular_expression_is_an_object_that_points_at_its_text() {
let compiled = compile_text("_main(args) { return R'a+b'; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [1, 0]);
assert_eq!(objs[6..13], [1, 0, 0, 0, 5, 0, 8]);
assert!(String::from_utf8_lossy(&image).contains("regex-pattern/000000"));
}
#[test]
fn a_list_of_keys_and_values_is_a_map() {
let compiled = compile_text("_main(args) { return [1 -> 2, 3 -> 4, * -> 5]; }\n");
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert!(has_bytes(code, &[0xC0, 5, 1, 0x8B, 0xC0, 1, 7]));
assert_eq!(
compile_messages(
"_main(args)\n{\n local a = [1, 2 -> 3];\n local b = [1 -> 2, 3];\n\
local c = [1 -> 2 -> 3];\n local d = [* -> 1, 2 -> 3];\n return [a, b, c, d];\n}\n"
),
[
"3: error: unexpected -> found in list (after 2nd element)",
"4: error: missing -> in the 2nd element of a [key -> value] list",
"5: error: misplaced -> in a [key -> value] list after the 1st element - expected comma",
"6: error: a [key -> value] list must end after its default *-> value, but found \",\"",
]
);
}
#[test]
fn a_choice_in_a_string_gets_an_object_to_do_the_choosing() {
let compiled = compile_text(
"class OneOfIndexGen: object getNextIndex() { return 1; };\n\
_main(args) { return 'a <<one of>>b<<or>>c<<or>>d<<cycling>> e'; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [2, 0]);
let chooser = &objs[6 + 6 + 13..];
assert_eq!(chooser[0..4], [2, 0, 0, 0]);
assert_eq!(chooser[6..12], [1, 0, 2, 0, 0, 0]);
assert_eq!(chooser[12..16], [1, 0, 0, 0]);
assert_eq!(chooser[18..23], [7, 3, 0, 0, 0]);
assert_eq!(
compile_messages(
"class OneOfIndexGen: object;\ngetNextIndex() { }\n\
_main(args) { return 'a <<one of>>b<<or>>c<<stopping>>'; }\n"
),
["3: error: <<one of>> requires getNextIndex to be defined as a property"]
);
}
#[test]
fn a_constant_division_gives_the_quotient_the_program_would_get() {
let compiled = compile_text(
"_main(args) { return [10.0 / 4, 1 / 4.00, 4294967296 / 8]; }
",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [2, 0]);
assert_eq!(objs[6..18], [1, 0, 0, 0, 6, 0, 2, 0, 1, 0, 0, 0x25]);
assert_eq!(objs[18..30], [2, 0, 0, 0, 6, 0, 2, 0, 0, 0, 0, 0x25]);
}
fn dictionaries(image: &[u8]) -> Vec<(u32, Vec<(String, usize)>)> {
let mut found = Vec::new();
for objs in blocks(image, b"OBJS") {
if objs[2..6] != [2, 0, 1, 0] {
continue;
}
let count = usize::from(u16::from_le_bytes([objs[0], objs[1]]));
let mut at = 6;
for _ in 0..count {
let id = u32::from_le_bytes(objs[at..at + 4].try_into().expect("four bytes"));
let size = u32::from_le_bytes(objs[at + 4..at + 8].try_into().expect("four bytes"));
let data = &objs[at + 8..at + 8 + size as usize];
at += 8 + size as usize;
let entries = usize::from(u16::from_le_bytes([data[4], data[5]]));
let mut words = Vec::new();
let mut pos = 6;
for _ in 0..entries {
let len = usize::from(data[pos]);
let text: Vec<u8> = data[pos + 1..pos + 1 + len]
.iter()
.map(|byte| byte ^ 0xBD)
.collect();
pos += 1 + len;
let uses = usize::from(u16::from_le_bytes([data[pos], data[pos + 1]]));
pos += 2 + 6 * uses;
words.push((String::from_utf8_lossy(&text).into_owned(), uses));
}
found.push((id, words));
}
}
found
}
#[test]
fn a_dictionary_holds_the_words_objects_have_for_its_properties() {
let compiled = compile_text(
"dictionary gDict;\ndictionary property noun, adjective;\n\
class Thing: object noun = 'thing';\n\
lamp: Thing noun = 'lamp' 'lantern' adjective = 'brass';\n\
key: Thing adjective = 'brass';\n_main(args) { return gDict; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let found = dictionaries(&image);
assert_eq!(found.len(), 1);
let (id, mut words) = found[0].clone();
assert_eq!(id, 1);
words.sort();
assert_eq!(
words,
[
("brass".to_owned(), 2),
("lamp".to_owned(), 1),
("lantern".to_owned(), 1),
("thing".to_owned(), 2),
]
);
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [3, 0]);
}
#[test]
fn words_go_in_the_dictionary_that_was_current() {
let compiled = compile_text(
"dictionary property noun;\ndictionary first;\na: object noun = 'a' 'shared';\n\
dictionary second;\nb: object noun = 'b' 'shared';\nclass C: object noun = 'c';\n\
dictionary first;\nd: C noun = 'd';\n_main(args) { return [first, second]; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let mut found = dictionaries(&image);
assert_eq!(found.len(), 2);
for (_, words) in &mut found {
words.sort();
}
let names = |words: &[(String, usize)]| -> Vec<String> {
words.iter().map(|(word, _)| word.clone()).collect()
};
assert_eq!(names(&found[0].1), ["a", "c", "d", "shared"]);
assert_eq!(names(&found[1].1), ["b", "shared"]);
}
#[test]
fn dictionary_words_have_their_own_mistakes() {
assert_eq!(
compile_messages(
"dictionary property noun;\na: object noun = 'x';\ndictionary gDict;\n\
b: object\n noun = 5\n;\nobject template 'noun';\nmodify gDict x = 1;\n\
_main(args) { }\n"
),
[
"2: error: vocabulary property cannot be defined - no dictionary is active",
"5: error: vocabulary property requires string value, but found \"5\"",
"7: error: property \"noun\" is a dictionary property - not valid in templates",
"8: error: empty template definition",
"8: error: objects of this type cannot be modified or replaced",
]
);
assert_eq!(
compile_messages("f() { }\na: object;\ndictionary f;\ndictionary a;\n_main(args) { }\n"),
[
"3: error: \"f\" is already the name of a function, so it cannot also name an object",
"4: error: redefining \"a\" as dictionary object",
]
);
}
fn productions(image: &[u8]) -> Vec<(u32, Vec<(u16, u16)>)> {
let mut found = Vec::new();
for objs in blocks(image, b"OBJS") {
if objs[2..6] != [3, 0, 1, 0] {
continue;
}
let count = usize::from(u16::from_le_bytes([objs[0], objs[1]]));
let mut at = 6;
for _ in 0..count {
let id = u32::from_le_bytes(objs[at..at + 4].try_into().expect("four bytes"));
let size = u32::from_le_bytes(objs[at + 4..at + 8].try_into().expect("four bytes"));
let data = &objs[at + 8..at + 8 + size as usize];
at += 8 + size as usize;
let alts = usize::from(u16::from_le_bytes([data[0], data[1]]));
let mut ways = Vec::new();
let mut pos = 2;
for _ in 0..alts {
let badness = u16::from_le_bytes([data[pos + 2], data[pos + 3]]);
let toks = u16::from_le_bytes([data[pos + 8], data[pos + 9]]);
pos += 10;
for _ in 0..toks {
pos += 3 + match data[pos + 2] {
1 | 4 => 4,
2 => 2,
3 => 2 + usize::from(u16::from_le_bytes([data[pos + 3], data[pos + 4]])),
6 => {
2 + 2 * usize::from(u16::from_le_bytes([data[pos + 3], data[pos + 4]]))
}
_ => 0,
};
}
ways.push((badness, toks));
}
found.push((id, ways));
}
}
found
}
#[test]
fn a_grammar_statement_says_how_a_phrase_is_made_up() {
let compiled = compile_text(
"dictionary gDict;\ndictionary property noun;\nclass Match: object;\n\
grammar np(plain): noun->n_ : Match;\n\
grammar np: 'the' np->np_ | [badness 50] 'a' np->np_ : Match;\n\
grammar cmd(put): 'put' np->d_ ('in' | 'into' | ) np->i_ ('now' | ) : Match;\n\
grammar later;\n_main(args) { return [np, cmd, later]; }\n",
);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let found = productions(&image);
assert_eq!(found.len(), 3);
assert_eq!(found[0].1, [(0, 2), (50, 2), (0, 1)]);
assert_eq!(found[1].1, [(0, 5), (0, 5), (0, 4), (0, 4), (0, 4), (0, 3)]);
assert_eq!(found[2].1, []);
let mut words: Vec<String> = dictionaries(&image)[0]
.1
.iter()
.map(|(word, _)| word.clone())
.collect();
words.sort();
assert_eq!(words, ["a", "in", "into", "now", "put", "the"]);
}
#[test]
fn grammar_statements_have_their_own_mistakes() {
assert_eq!(
compile_messages(
"class Match: object;\nf() { }\ngrammar np(a): f : Match;\n\
grammar np(b): 'x' -> 5 : Match;\ngrammar np(c): * 'x' : Match;\n\
grammar np(d): ('x' | 'y')->p_ : Match;\ngrammar np(a): 'dup' : Match;\n\
modify grammar np: 'x' : ;\n_main(args) { }\n"
),
[
"3: error: object or property name required in 'grammar' token list (found \"f\")",
"4: error: '->' in 'grammar' token list must be followed by a property name (found \"5\")",
"4: error: invalid token \"5\" in 'grammar' token list",
"5: error: '*' must be the last token in a 'grammar' alternative list (found \"'x'\")",
"6: error: '->' is not allowed after parenthesized group in 'grammar' statement",
"7: error: object \"np(a)\" is already defined - can't redefine as object",
"8: error: 'modify/replace grammar' requires name-tag",
]
);
assert_eq!(
compile_messages(
"class Match: object;\ngrammar np(a): nuon->n_ : Match;\n_main(args) { }\n"
),
["0: error: grammar rule \"nuon\" has no alternatives defined"]
);
}
const PROGRAM_OBJECT: &str = "builtin class Object 'root-object/030004' { }\n\
builtin class ProgramObject 'hexicon-object/030005': Object\n\
{ createInstanceOf(...); setMethod(prop, meth); }\n";
#[test]
fn an_object_written_in_an_expression_is_made_when_it_is_evaluated() {
let source = format!(
"{PROGRAM_OBJECT}class Thing: object;\n\
make(n) {{ return object: Thing {{ fixed = 7 size = n * 2 twice(x) {{ return x + n; }} }}; }}\n\
_main(args) {{ return make(1); }}\n"
);
let compiled = compile_text(&source);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert!(has_bytes(code, &[0x67, 1]));
assert!(has_bytes(code, &[0x03, 7, 0x8D, 0xE5]));
let bound = code
.windows(4)
.filter(|bytes| bytes[0] == 0xA6 && bytes[1] == 2 && bytes[2] == 0x61 && bytes[3] == 2)
.count();
assert_eq!(bound, 2);
assert_eq!(
compile_messages("_main(args) { return object { x = 1 }; }\n"),
["1: error: \"ProgramObject\" is not defined or is not a built-in class"]
);
assert_eq!(
compile_messages("class Thing: object;\n_main(args) { return object Thing { }; }\n")[..1],
["2: error: expected inline object property list starting with '{' (found 'Thing')"]
);
}
#[test]
fn a_question_about_a_name_is_answered_when_the_program_is_compiled() {
assert_eq!(
compile_messages(
"a: object;\nf() { }\n_main(args)\n{\n if (defined(nope))\n return 1;\n\
local x = __objref(f, warn);\n local y = __objref(nope, warn);\n\
return [defined(a), __objref(a), __objref(nope), x, y];\n}\n"
),
[
"7: warning: the name \"f\" is not an object - illegal on left of '.'",
"8: warning: \"nope\" is not defined anywhere",
]
);
assert_eq!(
compile_messages("_main(args) { return [defined(1), __objref(1, 2, 3)]; }\n"),
[
"1: error: invalid syntax for defined() operator - argument must be a name",
"1: error: invalid syntax for __objref() operator",
]
);
}
const MM_SUPPORT: &str = "_multiMethodRegister(base, func, params) { }\n\
_multiMethodBuildBindings() { }\n\
_multiMethodCall(base, args) { return nil; }\n\
_multiMethodCallInherited(from, [args]) { return nil; }\n";
#[test]
fn a_function_with_types_is_a_version_of_a_multi_method() {
let source = format!(
"{MM_SUPPORT}class Thing: object;\nclass Box: Thing;\n\
put(Thing a, Box b) {{ return 1; }}\n\
put(Box a, b) {{ return inherited(a, b); }}\n\
_main(args) {{ return put(Thing, Box); }}\n"
);
let compiled = compile_text(&source);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let code = code(&image);
assert!(has_bytes(code, &[0xC0, 2, 1, 0x8B, 0x0B]));
assert!(has_bytes(code, &[0x0D, 0, 0x0B]));
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [3, 0]);
let last = &objs[6 + 12 + 16..];
assert_eq!(last[0..4], [3, 0, 0, 0]);
assert_eq!(last[8..10], [1, 0]);
assert_eq!(last[12..15], [1, 0, 11]);
}
#[test]
fn multi_methods_have_their_own_mistakes() {
assert_eq!(
compile_messages(
"class Thing: object;\nenum red;\nf(a) { }\nf(Thing a) { }\ng(Thing a) { }\n\
g(Thing a) { }\nh(red a) { }\nk(a) { return inherited<Thing>(a); }\n\
_main(args) { }\n"
),
[
"4: error: function \"f\" is already defined - can't redefine as a multi-method",
"6: error: function \"g*Thing;\" is already defined",
"7: error: parameter type \"red\" is not an object",
]
);
assert_eq!(
compile_messages(
"class Thing: object;\nd(Thing x) { return 1; }\n_main(args) { return d; }\n"
),
[
"0: error: library support function \"_multiMethodCall\" is missing or invalid - \
check that the system library is included in the build",
"0: error: library support function \"_multiMethodBuildBindings\" is missing or \
invalid - check that the system library is included in the build",
]
);
}
fn compile_files(sources: &[(&str, &str)]) -> Compiled {
let mut files = Files::default();
for (name, text) in sources {
files = files.with(name, text);
}
let names: Vec<&str> = sources.iter().map(|(name, _)| *name).collect();
crate::compile_program(&mut files, &names, &CompileOptions::default())
}
fn messages_of(compiled: Compiled) -> Vec<String> {
compiled
.diagnostics
.into_iter()
.map(|problem| format!("{}({}): {}", problem.file, problem.line, problem.message))
.collect()
}
#[test]
fn a_program_can_be_several_source_files() {
let one = "class Thing: object weight = 1 kind = heavy;\n\
enum light, heavy;\n\
_main(args) { return rock.weight + twice(rock.size) + (rock.kind == light ? 1 : 0); }\n";
let two = "rock: Thing weight = 7 size = 3 kind = light;\n\
twice(x) { return x * 2 + Thing.weight; }\n";
let compiled = compile_files(&[("one.hxn", one), ("two.hxn", two)]);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [2, 0]);
let mut found = Vec::new();
let mut at = 6;
for _ in 0..2 {
let id = u32::from_le_bytes(objs[at..at + 4].try_into().unwrap());
let size = usize::from(u16::from_le_bytes([objs[at + 4], objs[at + 5]]));
let sc_count = u16::from_le_bytes([objs[at + 6], objs[at + 7]]);
let is_class = objs[at + 10] & 1 != 0;
let superclass =
(sc_count == 1).then(|| u32::from_le_bytes(objs[at + 12..at + 16].try_into().unwrap()));
found.push((id, is_class, superclass));
at += 6 + size;
}
found.sort_unstable();
let class = found
.iter()
.find(|(_, is_class, _)| *is_class)
.expect("the class");
let object = found
.iter()
.find(|(_, is_class, _)| !*is_class)
.expect("the object");
assert_eq!([found[0].0, found[1].0], [1, 2]);
assert_eq!(class.2, None);
assert_eq!(object.2, Some(class.0));
let whole = compile_text(&format!("{one}{two}"));
let whole = whole.image.expect("an image");
assert_eq!(block(&whole, b"CPPG").len(), block(&image, b"CPPG").len());
}
#[test]
fn what_two_files_both_define_is_found_when_they_are_put_together() {
let one = "f(x) { return 1; }\no: object;\n_main(args) { return f(1); }\n";
let two = "f(x) { return 2; }\no: object;\n";
assert_eq!(
messages_of(compile_files(&[("one.hxn", one), ("two.hxn", two)])),
[
"two.hxn(1): \"f\" is already a function, in \"one.hxn\"",
"two.hxn(2): \"o\" is already an object, in \"one.hxn\"",
]
);
}
#[test]
fn what_no_file_defines_is_unresolved() {
let one = "extern function nowhere(x);\n_main(args) { return nowhere(1) + there(2); }\n";
let two = "extern object nothing;\nthere(x) { return nothing; }\n";
assert_eq!(
messages_of(compile_files(&[("one.hxn", one), ("two.hxn", two)])),
[
"(0): unresolved external reference \"nothing\"",
"(0): unresolved external reference \"nowhere\"",
]
);
let bad = "there(x) { return x +; }\n";
let messages = messages_of(compile_files(&[("one.hxn", one), ("bad.hxn", bad)]));
assert_eq!(messages.len(), 1, "{messages:?}");
assert!(messages[0].starts_with("bad.hxn(1): "), "{messages:?}");
}
#[test]
fn what_each_file_adds_to_a_built_in_class_is_there_together() {
let decl = "builtin class Object 'root-object/030004' { }\n\
builtin class List 'list/030008': Object { }\n";
let one = format!(
"{decl}modify List first() {{ return self[1]; }};\n_main(args) {{ return [1].first() + [1, 2].second(); }}\n"
);
let two = format!("{decl}modify List second() {{ return self[2]; }};\n");
let compiled = compile_files(&[("one.hxn", &one), ("two.hxn", &two)]);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let mut pos = 69;
let mut found = None;
while pos < image.len() {
let size = u32::from_le_bytes(image[pos + 4..pos + 8].try_into().unwrap()) as usize;
let data = &image[pos + 10..pos + 10 + size];
if &image[pos..pos + 4] == b"OBJS" && data[2..4] == [6, 0] {
found = Some(data);
}
pos += 10 + size;
}
let data = found.expect("the objects that add to built-in classes");
assert_eq!(data[0..2], [4, 0]);
let mut objs = Vec::new();
let mut at = 6;
for _ in 0..4 {
let id = u32::from_le_bytes(data[at..at + 4].try_into().unwrap());
let size = usize::from(u16::from_le_bytes([data[at + 4], data[at + 5]]));
let sc_count = u16::from_le_bytes([data[at + 6], data[at + 7]]);
let superclass =
(sc_count == 1).then(|| u32::from_le_bytes(data[at + 12..at + 16].try_into().unwrap()));
objs.push((id, superclass));
at += 6 + size;
}
objs.sort_unstable();
let ids: Vec<u32> = objs.iter().map(|(id, _)| *id).collect();
let (root1, top1, root2, top2) = (ids[0], ids[1], ids[2], ids[3]);
assert_eq!(
objs,
[
(root1, None),
(top1, Some(root1)),
(root2, None),
(top2, Some(top1)),
]
);
}
#[test]
fn a_second_definition_can_be_in_another_file() {
let one = "f(x) { return 1; }\ng(x) { return 10; }\n\
class Thing: object weight = 1 name = 'thing';\n\
_main(args) { return f(1) + g(2) + Thing.weight; }\n";
let two = "replace f(x) { return 2; }\nmodify g(x) { return replaced(x) + 1; }\n\
modify Thing replace weight = 5 size = 2;\n";
let compiled = compile_files(&[("one.hxn", one), ("two.hxn", two)]);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let objs = block(&image, b"OBJS");
assert_eq!(objs[0..2], [2, 0]);
let mut found = Vec::new();
let mut at = 6;
for _ in 0..2 {
let id = u32::from_le_bytes(objs[at..at + 4].try_into().unwrap());
let size = usize::from(u16::from_le_bytes([objs[at + 4], objs[at + 5]]));
let sc_count = u16::from_le_bytes([objs[at + 6], objs[at + 7]]);
let prop_count = u16::from_le_bytes([objs[at + 8], objs[at + 9]]);
let superclass =
(sc_count == 1).then(|| u32::from_le_bytes(objs[at + 12..at + 16].try_into().unwrap()));
found.push((id, superclass, prop_count));
at += 6 + size;
}
found.sort_unstable();
assert_eq!(found, [(1, Some(2), 2), (2, None, 1)]);
let whole = compile_text(&format!("{one}{two}"));
assert!(whole.diagnostics.is_empty(), "{:?}", whole.diagnostics);
let whole = whole.image.expect("an image");
assert_eq!(block(&whole, b"CPPG").len(), block(&image, b"CPPG").len());
}
#[test]
fn a_second_definition_has_to_come_after_the_first() {
let one = "modify later x = 1;\n_main(args) { return later.x; }\n";
let two = "later: object y = 2;\n";
assert_eq!(
messages_of(compile_files(&[("one.hxn", one), ("two.hxn", two)])),
[
"two.hxn(1): \"y\" is given a value here, but nothing reads it",
"(0): modified or replaced object \"later\" found in source file \"one.hxn\" \
before original object definition was loaded",
"(0): \"later\" is already taken when \"two.hxn\" defines an object of that name",
]
);
let compiled = compile_files(&[("two.hxn", two), ("one.hxn", one)]);
assert!(
compiled
.diagnostics
.iter()
.all(|d| d.severity == Severity::Warning),
"{:?}",
compiled.diagnostics
);
}
fn sys_path(parts: &[&str]) -> String {
parts.join(std::path::MAIN_SEPARATOR_STR)
}
fn project_paths() -> crate::ProjectPaths {
crate::ProjectPaths {
user_libraries: Vec::new(),
}
}
fn read(files: &mut Files, args: &[&str]) -> Result<crate::Project, crate::ProjectError> {
let args: Vec<String> = args.iter().map(|arg| (*arg).to_owned()).collect();
crate::read_project(files, &args, &project_paths())
}
fn system_sources() -> Vec<String> {
[
"lifecycle",
"initobjects",
"exceptions",
"utilities",
"file",
"tok",
"grammarrule",
"multmeth",
]
.iter()
.map(|name| format!("{}/{name}.hxn", crate::builtin::SYSTEM))
.collect()
}
#[test]
fn options_are_a_letter_or_a_word() {
let mut files = Files::default()
.with("main.hxn", "")
.with("libs/extra.toml", "[library]\nsources = [\"more\"]\n")
.with("libs/more.hxn", "");
let project = read(
&mut files,
&[
"--no-system",
"--no-preinit",
"--charset=cp1252",
"--library-path",
"libs",
"--warnings-as-errors",
"-DLEVEL=3",
"main",
"--library",
"extra",
],
)
.unwrap_or_else(|e| panic!("{e:?}"));
assert!(!project.preinit && project.warnings_as_errors);
assert_eq!(project.options.default_charset.as_deref(), Some("cp1252"));
assert_eq!(
project.sources,
["main.hxn".to_owned(), sys_path(&["libs", "more.hxn"])]
);
for old in [
"-nopre",
"-nosystem",
"-Fs",
"-cs",
"-w0",
"-Gstg",
"-nodef",
] {
match read(&mut files, &[old, "main"]) {
Err(crate::ProjectError::Usage(problem)) => {
assert!(problem.contains("unknown option"), "{old}: {problem}");
}
other => panic!("{old}: {other:?}"),
}
}
}
#[test]
fn the_system_library_comes_first_unless_told_otherwise() {
let mut files = Files::default().with("main.hxn", "");
let with_system = |source: &str| {
let mut sources = system_sources();
sources.push(source.to_owned());
sources
};
let project = read(&mut files, &["main.hxn"]).expect("a project");
assert_eq!(project.sources, with_system("main.hxn"));
assert!(project.preinit);
assert_eq!(project.image, Some("main.hxg".to_owned()));
let project = read(&mut files, &["--no-preinit", "main"]).expect("a project");
assert_eq!(project.sources, with_system("main.hxn"));
assert!(!project.preinit);
let project =
read(&mut files, &["--no-system", "-D", "X", "-DY=2", "main"]).expect("a project");
assert_eq!(project.sources, ["main.hxn".to_owned()]);
assert_eq!(
project.options.defines,
[
("X".to_owned(), String::new()),
("Y".to_owned(), "2".to_owned())
]
);
let mut files = Files::default().with("game.t3m", "-source main\n");
match read(&mut files, &["-f", "game.t3m"]) {
Err(crate::ProjectError::Failed(lines)) => {
assert!(lines[0].contains("is not a manifest"), "{lines:?}");
}
other => panic!("{other:?}"),
}
}
#[test]
fn two_files_of_one_name_are_two_modules_but_one_file_cannot_come_twice() {
let mut files = Files::default()
.with("a/main.hxn", "")
.with("b/main.hxn", "");
let project =
read(&mut files, &["--no-system", "a/main", "b/main"]).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(project.sources, ["a/main.hxn", "b/main.hxn"]);
match read(&mut files, &["--no-system", "a/main", "a\\main"]) {
Err(crate::ProjectError::Failed(lines)) => {
assert_eq!(lines, ["error: \"a\\main.hxn\" is among the sources twice"]);
}
other => panic!("{other:?}"),
}
}
#[test]
fn the_command_line_wins_over_the_manifest() {
let manifest = "[game]\nlibraries = [\"hexad\"]\nsources = [\"main\"]\nimage = \"game.hxg\"\n\n\
[defines]\nLANGUAGE = \"english\"\nLEVEL = 1\n\n[build]\nwarnings = false\n";
let mut files = Files::default()
.with("hexicon.toml", manifest)
.with("main.hxn", "");
let project = read(&mut files, &[]).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(project.image.as_deref(), Some("game.hxg"));
assert!(!project.warnings);
let project = read(&mut files, &["-o", "other.hxg", "-DLEVEL=2", "--warnings"])
.unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(project.image.as_deref(), Some("other.hxg"));
assert!(project.warnings);
let level = project
.options
.defines
.iter()
.rev()
.find(|(name, _)| name == "LEVEL")
.map(|(_, value)| value.as_str());
assert_eq!(level, Some("2"));
let found = read(&mut files, &["-D", "LANGUAGE=klingon"]);
assert!(
matches!(&found, Err(crate::ProjectError::Failed(problems))
if problems.iter().any(|p| p.contains("klingon"))),
"{found:?}"
);
}
#[test]
fn a_game_can_have_a_source_named_as_one_of_hexads() {
let mut files = Files::default()
.with("hexicon.toml", "[game]\nlibraries = [\"hexad\"]\nsources = [\"status\", \"score\"]\n\n[defines]\nLANGUAGE = \"english\"\n")
.with("status.hxn", "")
.with("score.hxn", "");
let project = read(&mut files, &[]).unwrap_or_else(|e| panic!("{e:?}"));
let ends = |name: &str| project.sources.iter().filter(|s| s.ends_with(name)).count();
assert_eq!(ends("status.hxn"), 2);
assert_eq!(ends("score.hxn"), 2);
}
#[test]
fn the_program_s_symbols_for_preinitialization() {
use crate::RuntimeValue;
let one = "rock: object weight = 1;\n\
modify rock weight = 2;\n\
enum red;\n\
f() { return 1; }\n\
_main(args) { return f(); }\n";
let compiled = compile_files(&[("one.hxn", one)]);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let value = |name: &str| {
compiled
.symbols
.iter()
.find(|symbol| symbol.name == name.as_bytes())
.map(|symbol| symbol.value)
};
let Some(RuntimeValue::Object(rock)) = value("rock") else {
panic!("rock is an object");
};
assert!(matches!(value("weight"), Some(RuntimeValue::Property(_))));
assert!(matches!(value("red"), Some(RuntimeValue::Enum(_))));
let (Some(RuntimeValue::Function(f)), Some(RuntimeValue::Function(main))) =
(value("f"), value("_main"))
else {
panic!("f and _main are functions");
};
assert_ne!(f, main);
let set_aside: Vec<_> = compiled
.symbols
.iter()
.filter(|symbol| symbol.name.first() == Some(&b' '))
.collect();
assert_eq!(set_aside.len(), 1);
let RuntimeValue::Object(id) = set_aside[0].value else {
panic!("the definition set aside is an object");
};
assert_ne!(id, rock);
assert_eq!(set_aside[0].name, format!(" {id:x}").into_bytes());
}
#[test]
fn the_program_s_macros_for_preinitialization() {
let one = "#define TWICE(x, rest...) ((x) * 2)\n\
#define GONE 1\n\
#undef GONE\n\
#define SAME 1\n\
#define DIFFERENT 1\n\
_main(args) { }\n";
let two = "#define SAME 1\n#define DIFFERENT 2\n";
let compiled = compile_files(&[("one.hxn", one), ("two.hxn", two)]);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let find = |name: &str| {
compiled
.macros
.iter()
.find(|mac| mac.name == name.as_bytes())
};
let twice = find("TWICE").expect("TWICE is in the table");
assert!(twice.has_args && twice.has_varargs);
assert_eq!(twice.params, [b"x".to_vec(), b"rest".to_vec()]);
assert_eq!(twice.expansion, b"((x) * 2)");
assert!(find("GONE").is_none());
assert!(find("DIFFERENT").is_none());
assert!(find("SAME").is_some());
assert!(find("__HEXICON").is_some());
assert_eq!(find("__LINE__").expect("__LINE__").expansion, b"0");
assert_eq!(find("__FILE__").expect("__FILE__").expansion, b"");
let chains: Vec<usize> = compiled
.macros
.iter()
.map(|mac| crate::pp::macro_chain(&mac.name))
.collect();
assert!(chains.windows(2).all(|pair| pair[0] <= pair[1]));
}
#[test]
fn resources_come_from_libraries_and_the_game() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nlibraries = [\"art\"]\nsources = [\"start\"]\n\
resources = [\"front.png\", \"--no-recurse\", \"music\", \"logo.png=images/logo.png\"]\n",
)
.with("game/art/hexicon.toml", "[library]\nsources = [\"art\"]\nresources = [\"pics\"]\n")
.with("game/art/art.hxn", "")
.with("game/start.hxn", "");
let project = read(&mut files, &["--no-system", "-f", "game"]).expect("a project");
let spec = |file: String, alias: Option<&str>, recurse: bool| crate::ResourceSpec {
file,
alias: alias.map(str::to_owned),
recurse,
};
assert_eq!(
project.resources,
[
spec(sys_path(&["game", "art", "pics"]), Some("pics"), true),
spec(sys_path(&["game", "front.png"]), None, true),
spec(sys_path(&["game", "music"]), None, false),
spec(
sys_path(&["game", "logo.png"]),
Some("images/logo.png"),
false
),
]
);
assert!(!project.debug);
}
#[test]
fn a_games_style_sheets_are_resources() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nsources = [\"start\"]\nresources = [\"cave.jpg\"]\n\
stylesheets = [\"skull.css\"]\n",
)
.with("game/start.hxn", "");
let project = read(&mut files, &["--no-system", "-f", "game"]).expect("a project");
let files: Vec<&str> = project.resources.iter().map(|r| r.file.as_str()).collect();
assert_eq!(
files,
[
sys_path(&["game", "cave.jpg"]).as_str(),
sys_path(&["game", "skull.css"]).as_str()
]
);
}
#[test]
fn a_folder_of_resources_is_the_files_in_it() {
use crate::{DirEntry, Resource, ResourceSpec};
let entry = |name: &str, is_dir: bool, hidden: bool| DirEntry {
name: name.to_owned(),
is_dir,
hidden,
};
let sep = std::path::MAIN_SEPARATOR;
let mut list_dir = |path: &str| -> Option<Vec<DirEntry>> {
match path.replace(sep, "/").as_str() {
"g/pics" => Some(vec![
entry("a.png", false, false),
entry("sub", true, false),
entry("secret.png", false, true),
entry("z.png", false, false),
]),
"g/pics/sub" => Some(vec![entry("b.png", false, false)]),
_ => None,
}
};
let spec = |file: &str, alias: Option<&str>, recurse: bool| ResourceSpec {
file: file.replace('/', &sep.to_string()),
alias: alias.map(str::to_owned),
recurse,
};
let names = |specs: &[ResourceSpec],
list_dir: &mut dyn FnMut(&str) -> Option<Vec<DirEntry>>| {
crate::expand_resources(specs, list_dir)
.into_iter()
.map(|Resource { name, .. }| name)
.collect::<Vec<_>>()
};
assert_eq!(
names(
&[spec("g/pics", None, true), spec("g/one.txt", None, true)],
&mut list_dir
),
[
"g/pics/a.png",
"g/pics/sub/b.png",
"g/pics/z.png",
"g/one.txt"
]
);
assert_eq!(
names(&[spec("g/pics", Some("art/"), false)], &mut list_dir),
["art/a.png", "art/z.png"]
);
assert_eq!(
names(&[spec("C:x.png", None, true)], &mut list_dir),
["/C:./x.png"]
);
}
fn compile_debug(sources: &[(&str, &str)]) -> Compiled {
let mut files = Files::default();
for (name, text) in sources {
files = files.with(name, text);
}
let names: Vec<&str> = sources.iter().map(|(name, _)| *name).collect();
let options = CompileOptions {
debug: true,
..CompileOptions::default()
};
crate::compile_program(&mut files, &names, &options)
}
#[test]
fn a_program_compiled_for_debugging_says_where_its_code_came_from() {
let main = "#ifdef __DEBUG\n\
_main(args)\n\
{\n \
local x = 1;\n \
x++;\n\
}\n\
#endif\n";
let other = "helper() { return 2; }\n";
let compiled = compile_debug(&[("main.hxn", main), ("other.hxn", other)]);
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let image = compiled.image.expect("an image");
let u16_at = |data: &[u8], at: usize| u16::from_le_bytes([data[at], data[at + 1]]) as usize;
let u32_at = |data: &[u8], at: usize| u32::from_le_bytes(data[at..at + 4].try_into().unwrap());
let code = code(&image);
let table = u16_at(code, 8);
assert_ne!(table, 0, "a debug table");
let lines: Vec<(usize, u32)> = (0..u16_at(code, table))
.map(|rec| {
let at = table + 2 + 10 * rec;
(u16_at(code, at + 2), u32_at(code, at + 4))
})
.collect();
assert_eq!(lines, [(0, 4), (0, 5), (0, 6)]);
let srcf = block(&image, b"SRCF");
assert_eq!(u16_at(srcf, 0), 2);
let name_len = u16_at(srcf, 4 + 6);
assert_eq!(&srcf[4 + 8..4 + 8 + name_len], b"main.hxn");
assert_eq!(u32_at(srcf, 4 + 8 + name_len), 3, "three lines with code");
let mhls = block(&image, b"MHLS");
assert_eq!(u32_at(mhls, 0), 2);
let gsym = block(&image, b"GSYM");
assert!(gsym.windows(5).any(|w| w == b"_main"));
let macr = block(&image, b"MACR");
assert!(macr.windows(7).any(|w| w == b"__DEBUG"));
}
struct FuncIds(u32);
impl crate::dynamic::DynHost for FuncIds {
fn new_func_ids(&mut self, count: usize) -> Vec<u32> {
let first = self.0;
self.0 += count as u32;
(first..self.0).collect()
}
fn new_object(&mut self, _class: u32, _props: &[(u16, crate::dynamic::DynValue)]) -> u32 {
self.0 += 1;
self.0 - 1
}
}
#[test]
fn code_is_compiled_while_a_program_runs() {
use crate::dynamic::{DynContext, DynMode, DynObjKind, DynSymbol, compile_dynamic};
let context = DynContext {
symbols: vec![
(
b"test1".to_vec(),
DynSymbol::Object {
id: 7,
kind: DynObjKind::ProgramObject,
},
),
(b"m1".to_vec(), DynSymbol::Property(42)),
],
..DynContext::default()
};
let mut host = FuncIds(100);
let compiled = compile_dynamic(
b"function(x) { return x*2; }",
&context,
DynMode::Auto,
&mut host,
)
.expect("it compiles");
assert!(!compiled.is_method);
assert_eq!(compiled.funcs.len(), 1);
let (id, func) = &compiled.funcs[0];
assert_eq!(*id, 100);
assert_eq!(func.code[0], 1, "one parameter");
let compiled = compile_dynamic(b"test1.m1('hi');", &context, DynMode::Auto, &mut host)
.expect("it compiles");
let code = &compiled.funcs[0].1;
assert!(code.code.windows(2).any(|w| w == b"hi"));
assert_eq!(code.obj_refs.len(), 1);
let at = code.obj_refs[0];
assert_eq!(&code.code[at..at + 4], &7u32.to_le_bytes());
let failed = compile_dynamic(b"test1.mUndef(1);", &context, DynMode::Auto, &mut host);
assert_eq!(failed, Err("error: undefined name \"mUndef\"".to_owned()));
let context = DynContext {
locals: vec![crate::dynamic::DynLocal {
name: b"test1".to_vec(),
frame_ref: 9,
number: 3,
context: 0,
}],
..context
};
let compiled =
compile_dynamic(b"test1 += 1", &context, DynMode::Auto, &mut host).expect("it compiles");
let code = &compiled.funcs[0].1;
let at = code.obj_refs[0];
assert_eq!(&code.code[at..at + 4], &9u32.to_le_bytes());
assert_eq!(
code.obj_refs.len(),
2,
"the call's object, read and then set"
);
}
#[test]
fn grammar_rules_are_compiled_while_a_program_runs() {
use crate::dynamic::{
DynContext, DynGramAlt, DynGramMatch, DynGramTok, DynObjKind, DynSymbol, compile_grammar,
};
let context = DynContext {
symbols: vec![
(
b"atom".to_vec(),
DynSymbol::Object {
id: 30,
kind: DynObjKind::GrammarRule,
},
),
(b"t1".to_vec(), DynSymbol::Property(50)),
(b"tokInt".to_vec(), DynSymbol::Enum(4)),
],
..DynContext::default()
};
let alts = compile_grammar(b"atom->t1 ('x' | tokInt) | [badness 5] 'y'", &context)
.expect("it compiles");
let tok = |prop, kind| DynGramTok { prop, kind };
assert_eq!(
alts,
vec![
DynGramAlt {
badness: 0,
toks: vec![
tok(50, DynGramMatch::Prod(30)),
tok(0, DynGramMatch::Literal(b"x".to_vec())),
],
},
DynGramAlt {
badness: 0,
toks: vec![
tok(50, DynGramMatch::Prod(30)),
tok(0, DynGramMatch::TokType(4))
],
},
DynGramAlt {
badness: 5,
toks: vec![tok(0, DynGramMatch::Literal(b"y".to_vec()))],
},
]
);
assert!(compile_grammar(b"atom *", &context).is_err());
assert_eq!(
compile_grammar(b"nothing->t1", &context),
Err("error: undefined name \"nothing\"".to_owned())
);
}
fn messages_compiling(name: &str, text: &str) -> Vec<String> {
let mut files = Files::default().with(name, text);
compile(&mut files, name, &CompileOptions::default())
.diagnostics
.into_iter()
.map(|problem| {
format!(
"{}({}): {:?}: {}",
problem.file, problem.line, problem.severity, problem.message
)
})
.collect()
}
#[test]
fn extra_characters_after_a_condition_are_found_on_time() {
let text = "#if 1 2\n#endif\n_main(args) { }\n";
assert_eq!(
messages_compiling("main.hxn", text),
["main.hxn(1): Error: extra characters at end of line"]
);
let text = "#line 10 \"x.hxn\" 3\n_main(args) { }\n";
assert_eq!(
messages_compiling("main.hxn", text),
["main.hxn(1): Error: extra characters at end of line"]
);
}
#[test]
fn a_warning_is_reported_once() {
let text = "#define ONE 1\n#define ONE 2\n_main(args) { }\n";
let warning = "(2): Warning: redefinition of macro \"ONE\"";
assert_eq!(
messages_compiling("main.hxn", text),
[format!("main.hxn{warning}")]
);
}
fn project_failure(files: &mut Files, args: &[&str]) -> Vec<String> {
match read(files, args) {
Err(crate::ProjectError::Failed(messages)) => messages,
other => panic!("not a failure: {other:?}"),
}
}
#[test]
fn a_game_on_hexad_without_its_language_is_told_to_define_it() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nlibraries = [\"system\", \"hexad\"]\nsources = [\"start\"]\n",
)
.with("game/start.hxn", "");
let messages = project_failure(&mut files, &["-f", "game"]);
assert!(
messages[0].contains("library requires the macro \"LANGUAGE\" to be defined")
&& messages[0].contains("LANGUAGE = \"value\" in the manifest's [defines]"),
"{messages:?}"
);
assert!(
messages
.iter()
.any(|m| m.contains("undefined name \"LANGUAGE\"")),
"{messages:?}"
);
}
#[test]
fn a_library_that_leads_back_to_itself_is_an_error() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nlibraries = [\"a\"]\nsources = [\"start\"]\n\n[build]\nsystem = false\n",
)
.with("game/a.toml", "[library]\nlibraries = [\"b\"]\n")
.with("game/b.toml", "[library]\nlibraries = [\"a\"]\n")
.with("game/start.hxn", "");
let messages = project_failure(&mut files, &["-f", "game"]);
assert!(
messages.iter().any(|m| m.contains("leads back to itself")),
"{messages:?}"
);
}
#[test]
fn a_manifest_names_the_sources_and_the_libraries() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nname = \"The Game\"\nsources = [\"start\", \"old.hxn\"]\n\
libraries = [{ name = \"mylib\", exclude = [\"two\"] }, \"oldlib\"]\n\
image = \"out/game.hxg\"\n\n\
[defines]\nLANG = \"english\"\nLEVEL = 3\nFLAG = true\n\n\
[build]\npreinit = false\nwarnings-as-errors = true\ninclude-paths = [\"inc\"]\n\
system = false\n",
)
.with(
"game/mylib/hexicon.toml",
"[library]\nname = \"My Library\"\nsources = [\"one\", \"two\", \"three.hxn\"]\n\
libraries = [\"$(LANG)/$(LANG)\"]\n\n[needs]\nLANG = \"the language\"\nVOICE = \"who speaks\"\n",
)
.with(
"game/mylib/english/english.toml",
"[library]\nsources = [\"words\"]\n",
)
.with("game/oldlib.toml", "[library]\nsources = [\"legacy\"]\n")
.with("game/start.hxn", "")
.with("game/old.hxn", "");
let project = read(&mut files, &["-f", "game"]).unwrap_or_else(|e| panic!("{e:?}"));
let lib = |file: &str| sys_path(&["game", "mylib", file]);
assert_eq!(
project.sources,
[
lib("one.hxn"),
lib("three.hxn"),
sys_path(&["game", "mylib", "english", "words.hxn"]),
sys_path(&["game", "legacy.hxn"]),
sys_path(&["game", "start.hxn"]),
sys_path(&["game", "old.hxn"]),
]
);
let names: Vec<&str> = project
.options
.module_names
.iter()
.map(|(name, _)| name.as_str())
.collect();
assert_eq!(names, ["one", "three", "words", "legacy", "start", "old"]);
assert_eq!(project.image, Some(sys_path(&["game", "out", "game.hxg"])));
assert!(!project.preinit && project.warnings_as_errors);
assert_eq!(
project.options.defines,
[
("LANG".to_owned(), "english".to_owned()),
("LEVEL".to_owned(), "3".to_owned()),
("FLAG".to_owned(), String::new()),
]
);
assert_eq!(project.options.include_paths[0], sys_path(&["game", "inc"]));
assert_eq!(project.messages.len(), 1, "{:?}", project.messages);
assert!(project.messages[0].contains("library requires the macro \"VOICE\""));
}
#[test]
fn a_game_can_add_a_librarys_extensions() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nsources = [\"start\"]\n\
libraries = [{ name = \"mylib\", include = [\"extensions/extra\"] }]\n\n\
[build]\nsystem = false\n",
)
.with(
"game/mylib/hexicon.toml",
"[library]\nsources = [\"one\"]\n",
)
.with("game/mylib/extensions/extra.hxn", "")
.with("game/start.hxn", "");
let project = read(&mut files, &["-f", "game"]).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(
project.sources,
[
sys_path(&["game", "mylib", "one.hxn"]),
sys_path(&["game", "mylib", "extensions", "extra.hxn"]),
sys_path(&["game", "start.hxn"]),
]
);
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nsources = [\"start\"]\nlibraries = [\"outer\"]\n",
)
.with(
"game/outer.toml",
"[library]\nlibraries = [{ name = \"inner\", include = [\"x\"] }]\n",
)
.with("game/start.hxn", "");
let failed = format!("{:?}", read(&mut files, &["-f", "game"]).err());
assert!(failed.contains("cannot leave out or add to"), "{failed}");
}
#[test]
fn a_games_tests_are_built_for_debugging_alone() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nsources = [\"start\"]\ntests = [\"scenarios\"]\n\n[build]\nsystem = false\n",
)
.with("game/start.hxn", "")
.with("game/scenarios.hxn", "");
let start = sys_path(&["game", "start.hxn"]);
let scenarios = sys_path(&["game", "scenarios.hxn"]);
let project = read(&mut files, &["-f", "game"]).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(project.sources, std::slice::from_ref(&start));
let project = read(&mut files, &["-d", "-f", "game"]).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(project.sources, [start, scenarios]);
let project = read(
&mut files,
&[
"--no-system",
"--test",
"game/scenarios",
"--source",
"game/start",
],
)
.unwrap_or_else(|e| panic!("{e:?}"));
let slashed = |paths: &[String]| -> Vec<String> {
paths.iter().map(|path| path.replace('\\', "/")).collect()
};
assert_eq!(slashed(&project.sources), ["game/start.hxn"]);
assert_eq!(
project.image.map(|image| image.replace('\\', "/")),
Some("game/start.hxg".to_owned())
);
}
#[test]
fn a_manifest_found_by_itself_or_named_among_the_modules() {
let mut files = Files::default()
.with("hexicon.toml", "[game]\nsources = [\"main\"]\n")
.with("main.hxn", "")
.with("makefile.t3m", "-source other\n");
let project = read(&mut files, &[]).expect("a project");
assert_eq!(project.sources.last().map(String::as_str), Some("main.hxn"));
let project = read(&mut files, &["hexicon.toml"]).expect("a project");
assert_eq!(project.sources.last().map(String::as_str), Some("main.hxn"));
}
#[test]
fn a_manifest_does_not_ignore_what_it_does_not_know() {
let failed = |text: &str| {
let mut files = Files::default().with("hexicon.toml", text);
match read(&mut files, &[]) {
Err(crate::ProjectError::Failed(lines)) => lines.join("\n"),
other => panic!("{other:?}"),
}
};
assert_eq!(
failed("[game]\nsorces = [\"main\"]\n"),
"error: hexicon.toml: unknown key \"sorces\" in [game]"
);
assert_eq!(
failed("[game]\nsources = [\"main\"]\n[bulid]\n"),
"error: hexicon.toml: unknown section [bulid]"
);
assert_eq!(
failed("[game]\nsources = \"main\"\n"),
"error: hexicon.toml: \"sources\" in [game] must be a list of strings"
);
assert_eq!(
failed("[library]\nsources = [\"main\"]\n"),
"error: hexicon.toml: this is the manifest of a library, not of a game"
);
assert!(failed("[game\n").starts_with("error: hexicon.toml: TOML parse error"));
}
fn stub_system_headers() -> Files {
Files::default()
.with("inc/vm.hxh", "")
.with("inc/general.hxh", "")
.with("inc/io.hxh", "")
.with("inc/systype.hxh", "")
}
fn options_with_inc() -> CompileOptions {
CompileOptions {
include_paths: vec!["inc".to_owned()],
..CompileOptions::default()
}
}
#[test]
fn a_built_in_header_comes_after_the_include_path() {
let program = "#include <hexicon.hxh>\n#ifdef FROM_DISK\n#error from disk\n#endif\n\
_main(args) { print('x'); }\n";
let mut files = stub_system_headers().with("main.hxn", program);
let compiled = compile(&mut files, "main.hxn", &options_with_inc());
assert!(
compiled.diagnostics.is_empty(),
"{:?}",
compiled.diagnostics
);
let mut files = stub_system_headers()
.with("main.hxn", program)
.with("inc/hexicon.hxh", "#define FROM_DISK\n");
let messages: Vec<String> = compile(&mut files, "main.hxn", &options_with_inc())
.diagnostics
.into_iter()
.map(|d| format!("{}({}): {}", d.file, d.line, d.message))
.collect();
assert_eq!(messages[0], "main.hxn(3): #error : from disk");
let mut files = Files::default().with("main.hxn", program);
let compiled = compile(&mut files, "main.hxn", &CompileOptions::default());
assert!(compiled.image.is_some(), "{:?}", compiled.diagnostics);
}
mod system_library {
use super::{Files, read, sys_path};
#[test]
fn a_program_on_the_system_library_needs_a_main() {
let mut files = Files::default().with(
"start.hxn",
"#include <hexicon.hxh>\nhelper() { return 1; }\n",
);
let paths = crate::ProjectPaths {
user_libraries: Vec::new(),
};
let args = ["start".to_owned()];
let project =
crate::read_project(&mut files, &args, &paths).unwrap_or_else(|e| panic!("{e:?}"));
let sources: Vec<&str> = project.sources.iter().map(String::as_str).collect();
let compiled = crate::compile_program(&mut files, &sources, &project.options);
let errors: Vec<&str> = compiled
.diagnostics
.iter()
.filter(|d| d.severity != crate::pp::Severity::Warning)
.map(|d| d.message.as_str())
.collect();
assert_eq!(
errors,
[
"the program has no main(args) function, which the system library calls when it \
starts"
]
);
}
#[test]
fn a_game_builds_with_nothing_installed() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nlibraries = [\"system\"]\nsources = [\"start.hxn\"]\n",
)
.with(
"game/start.hxn",
"#include <hexicon.hxh>\nmain(args) { \"Hello, world.\"; }\n",
);
let paths = crate::ProjectPaths {
user_libraries: Vec::new(),
};
let args = ["-f".to_owned(), "game".to_owned()];
let project =
crate::read_project(&mut files, &args, &paths).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(
project.sources,
[
"<hexicon>/system/lifecycle.hxn",
"<hexicon>/system/initobjects.hxn",
"<hexicon>/system/exceptions.hxn",
"<hexicon>/system/utilities.hxn",
"<hexicon>/system/file.hxn",
"<hexicon>/system/tok.hxn",
"<hexicon>/system/grammarrule.hxn",
"<hexicon>/system/multmeth.hxn",
&super::sys_path(&["game", "start.hxn"]),
]
);
let sources: Vec<&str> = project.sources.iter().map(String::as_str).collect();
let compiled = crate::compile_program(&mut files, &sources, &project.options);
let errors: Vec<_> = compiled
.diagnostics
.iter()
.filter(|d| d.severity != crate::pp::Severity::Warning)
.collect();
assert!(errors.is_empty(), "{errors:?}");
assert!(compiled.image.is_some());
}
#[test]
fn the_built_in_files_are_those_in_lib() {
fn walk(dir: &std::path::Path, prefix: &str, out: &mut Vec<String>) {
for entry in std::fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
let name = format!("{prefix}/{}", path.file_name().unwrap().to_string_lossy());
if path.is_dir() {
walk(&path, &name, out);
} else if [".hxh", ".t", ".hxn", ".toml"]
.iter()
.any(|ext| name.ends_with(ext))
{
out.push(name);
}
}
}
let lib = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../lib");
let mut on_disk = Vec::new();
walk(&lib.join("system"), "system", &mut on_disk);
walk(&lib.join("hexad"), "hexad", &mut on_disk);
on_disk.sort();
let mut built_in: Vec<&str> = crate::builtin::FILES
.iter()
.map(|(name, _)| *name)
.collect();
built_in.sort_unstable();
assert_eq!(built_in, on_disk);
for (name, data) in crate::builtin::FILES {
assert_eq!(
*data,
std::fs::read(lib.join(name)).unwrap().as_slice(),
"{name}"
);
}
}
#[test]
fn a_built_in_folder_lists_its_own_files() {
let extensions = crate::builtin::files_in("<hexicon>/hexad/extensions");
assert!(extensions.contains(&"<hexicon>/hexad/extensions/rules.hxn".to_owned()));
let hexad = crate::builtin::files_in("<hexicon>/hexad");
assert!(hexad.contains(&"<hexicon>/hexad/thing.hxn".to_owned()));
assert!(!hexad.iter().any(|f| f.contains("/extensions/")));
assert!(crate::builtin::files_in("<hexicon>/nowhere").is_empty());
assert!(crate::builtin::files_in("elsewhere/hexad").is_empty());
}
#[test]
fn a_project_says_which_libraries_it_read() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nsources = [\"start\"]\nlibraries = [\"mylib\"]\n\n[build]\nsystem = false\n",
)
.with("game/mylib/hexicon.toml", "[library]\nsources = [\"one\"]\n")
.with("game/mylib/one.hxn", "")
.with("game/start.hxn", "");
let project = read(&mut files, &["-f", "game"]).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(
project.libraries,
[sys_path(&["game", "mylib", "hexicon.toml"])]
);
}
#[test]
fn hexad_is_found_with_no_library_paths() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nlibraries = [\"system\", \"hexad\"]\nsources = [\"start\"]\n\n\
[defines]\nLANGUAGE = \"english\"\n",
)
.with("game/start.hxn", "import hexad;\n");
let paths = crate::ProjectPaths {
user_libraries: Vec::new(),
};
let args = ["-f".to_owned(), "game".to_owned()];
let project =
crate::read_project(&mut files, &args, &paths).unwrap_or_else(|e| panic!("{e:?}"));
assert!(
project
.sources
.contains(&"<hexicon>/hexad/thing.hxn".to_owned())
);
assert!(
project
.sources
.contains(&"<hexicon>/hexad/english/english.hxn".to_owned())
);
assert_eq!(
project
.options
.imports
.iter()
.find(|(name, _)| name == "hexad"),
Some(&(
"hexad".to_owned(),
Some("<hexicon>/hexad/hexad.hxh".to_owned())
))
);
}
}
#[test]
fn a_manifest_can_give_its_modules_in_order_and_a_library_can_be_a_toml_file() {
let mut files = Files::default()
.with(
"hexicon.toml",
"[game]\nmodules = [{ library = \"sys\" }, \"early\", { library = \"lib/main\", exclude = [\"two\"] }, \"late\"]\n",
)
.with("sys.toml", "[library]\nsources = [\"base\"]\n")
.with("lib/main.toml", "[library]\nmodules = [{ library = \"sub\" }, \"one\", \"two\"]\n")
.with("lib/sub.toml", "[library]\nsources = [\"leaf\"]\n");
let project = read(&mut files, &["--no-system"]).unwrap_or_else(|e| panic!("{e:?}"));
let names: Vec<&str> = project
.options
.module_names
.iter()
.map(|(name, _)| name.as_str())
.collect();
assert_eq!(names, ["base", "early", "leaf", "one", "late"]);
assert_eq!(
project.sources,
[
"base.hxn".to_owned(),
"early.hxn".to_owned(),
sys_path(&["lib", "leaf.hxn"]),
sys_path(&["lib", "one.hxn"]),
"late.hxn".to_owned(),
]
);
}
mod checked_names {
use super::{CompileOptions, Files, messages_of};
use crate::names::is_slip;
fn messages_with(library: &str, game_name: &str, game: &str) -> Vec<String> {
let mut files = Files::default()
.with("lib.hxn", library)
.with(game_name, game);
let options = CompileOptions {
from_library: vec![true, false],
..CompileOptions::default()
};
messages_of(crate::compile_program(
&mut files,
&["lib.hxn", game_name],
&options,
))
}
const LIBRARY: &str = "property hasProp, checkDobjTake;\n\
class Thing: object isTakeable = true;\n\
_main(args) {\n\
\x20 local t = new Thing;\n\
\x20 if (t.isTakeable && t.hasProp(&verifyDobjTake)) t.checkDobjTake();\n\
}\n";
fn messages(game_name: &str, game: &str) -> Vec<String> {
messages_with(LIBRARY, game_name, game)
}
const GAME: &str = "rock: Thing\n\
\x20 isTakable = nil\n\
\x20 verifyDobjTakee() { } checkDobjTakee() { }\n\
\x20 weight = 2\n\
;\n\
f() { rock.isTakeable = nil; rock.isTakable = true; }\n";
#[test]
fn a_value_given_to_a_misspelled_property_is_reported() {
assert_eq!(
messages("game.hxn", GAME),
[
"game.hxn(2): \"isTakable\" is given a value here, but nothing reads it",
"game.hxn(3): the handler \"dobj Takee\" is never called",
"game.hxn(6): \"isTakable\" is given a value here, but nothing reads it",
]
);
}
#[test]
fn a_librarys_name_defined_again_by_the_game_is_reported_where_it_is() {
let library = "class Vapor: object;\n\
sortAll(lst) { return lst; }\n\
_main(args) { return sortAll([]); }\n";
let game = "class Vapor: object;\nsortAll(lst) { return lst; }\n";
let mut files = Files::default()
.with("lib.hxn", library)
.with("game.hxn", game);
let options = CompileOptions {
from_library: vec![true, false],
..CompileOptions::default()
};
let found: Vec<(u32, String, Option<String>)> =
crate::compile_program(&mut files, &["lib.hxn", "game.hxn"], &options)
.diagnostics
.into_iter()
.map(|d| (d.line, d.message, d.note))
.collect();
assert_eq!(
found,
[
(
1,
"\"Vapor\" is already a class of the built-in library's".to_owned(),
Some(
"rename yours, or write \"replace class Vapor\" to use it in place \
of the library's"
.to_owned()
)
),
(
2,
"\"sortAll\" is already a function of the built-in library's".to_owned(),
Some(
"rename yours, or write \"replace sortAll\" to use it in place of \
the library's"
.to_owned()
)
),
]
);
assert_eq!(
messages_with(
library,
"game.hxn",
"replace class Vapor: object;\nreplace sortAll(lst) { return lst; }\n"
),
Vec::<String>::new()
);
}
#[test]
fn a_games_own_action_takes_the_place_of_the_librarys() {
let library = "class IAction: object;\n\
action Jump: IAction n = 1;\n\
rock: object;\n\
_main(args) { return Jump.n; }\n";
assert_eq!(
messages_with(library, "game.hxn", "action Jump: IAction n = 2;\n"),
["game.hxn(1): the action \"Jump\" takes the place of the built-in library's"]
);
assert_eq!(
messages_with(library, "game.hxn", "rock: object;\n").len(),
1
);
}
#[test]
fn an_action_on_taction_without_its_objects_is_warned_of() {
let library = "class TAction: object;\n\
class TIAction: TAction;\n\
class IAction: object;\n\
action Take(dobj): TAction;\n\
_main(args) { }\n";
assert_eq!(
messages_with(library, "game.hxn", "action KState: TAction;\n"),
[
"game.hxn(1): the action \"KState\" is a TAction but does not name its \
objects (dobj), so it will fail when it is used: write \"action \
KState(dobj): TAction\""
]
);
assert_eq!(
messages_with(library, "game.hxn", "action PutOver(dobj): TIAction;\n"),
[
"game.hxn(1): the action \"PutOver\" is a TIAction but does not name its \
objects (dobj, iobj), so it will fail when it is used: write \"action \
PutOver(dobj, iobj): TIAction\""
]
);
for good in [
"action KState(dobj): TAction;\n",
"action PutOver(dobj, iobj): TIAction;\n",
"action Grab: Take;\n",
"action Hum: IAction;\n",
] {
assert_eq!(
messages_with(library, "game.hxn", good),
Vec::<String>::new(),
"{good}"
);
}
}
#[test]
fn a_games_own_verb_rule_takes_the_place_of_the_librarys_however_written() {
let library = "class IAction: object;\n\
class VerbProduction: object;\n\
action Jump: IAction;\n\
verb Jump 'jump': ;\n\
_main(args) { }\n";
let said = "game.hxn(1): the verb rule \"Jump\" takes the place of the built-in \
library's";
assert_eq!(
messages_with(library, "game.hxn", "verb Jump 'leap': ;\n"),
[said]
);
assert_eq!(
messages_with(
library,
"game.hxn",
"grammar predicate(Jump): 'leap' : VerbProduction action = Jump;\n"
),
[said]
);
}
#[test]
fn the_name_meant_is_in_the_note() {
let mut files = Files::default()
.with("lib.hxn", LIBRARY)
.with("game.hxn", GAME);
let options = CompileOptions {
from_library: vec![true, false],
..CompileOptions::default()
};
let notes: Vec<Option<String>> =
crate::compile_program(&mut files, &["lib.hxn", "game.hxn"], &options)
.diagnostics
.into_iter()
.map(|d| d.note)
.collect();
assert_eq!(
notes,
[
Some("did you mean \"isTakeable\"?".to_owned()),
Some("did you mean \"dobj Take\"?".to_owned()),
Some("did you mean \"isTakeable\"?".to_owned()),
]
);
}
#[test]
fn a_name_declared_or_read_by_address_is_not_reported() {
let game = format!("property isTakable;\n{GAME}g() {{ return &verifyDobjTakee; }}\n");
let found = messages("game.hxn", &game);
assert!(
found.iter().all(|m| !m.contains("\"isTakable\"")),
"{found:?}"
);
assert!(
found
.iter()
.any(|m| m.starts_with("game.hxn(4): the handler \"dobj Takee\"")),
"{found:?}"
);
}
#[test]
fn a_name_a_library_gives_a_value_is_not_reported() {
let library = LIBRARY.replace("isTakeable = true;", "isTakeable = true isTakable = 1;");
let found = messages_with(&library, "game.hxn", GAME);
assert!(
found.iter().all(|m| !m.contains("\"isTakable\"")),
"{found:?}"
);
assert_eq!(found.len(), 1, "{found:?}");
}
#[test]
fn a_slip_is_one_letter_or_a_swap_but_not_a_family_of_names() {
for (a, b) in [
("isTakable", "isTakeable"),
("isTakebale", "isTakeable"),
("missinqQ", "missingQ"),
("isTakeAble", "isTakeable"),
("is", "in"),
] {
assert!(is_slip(a.as_bytes(), b.as_bytes()), "{a} {b}");
}
for (a, b) in [
("num", "num_"),
("room1", "room2"),
("weight", "weight"),
("isTkble", "isTakeable"),
("stone", "store_x"),
] {
assert!(!is_slip(a.as_bytes(), b.as_bytes()), "{a} {b}");
}
}
}
mod action_declarations {
use super::{CompileOptions, Compiled, Files};
fn build(library_name: &str, game_name: &str, game: &str) -> Compiled {
let mut files = Files::default()
.with("macros.h", MACROS)
.with(library_name, LIBRARY)
.with(game_name, game);
crate::compile_program(
&mut files,
&[library_name, game_name],
&CompileOptions::default(),
)
}
const MACROS: &str = r#"
#define objFor(which, action) propertyset '*' ## #@which ## #@action
#define dobjFor(action) objFor(Dobj, action)
#define iobjFor(action) objFor(Iobj, action)
#define aobjFor(action) objFor(Aobj, action)
#define asObjFor(obj, Action) \
{ \
preCond { return preCond##obj##Action; } \
verify() { verify##obj##Action; } \
remap() { return remap##obj##Action; } \
check() { check##obj##Action; } \
action() { action##obj##Action; } \
report() { report##obj##Action; } \
}
#define asDobjFor(action) asObjFor(Dobj, action)
#define asIobjFor(action) asObjFor(Iobj, action)
#define DefineAction(name, baseClass...) \
name: ##baseClass \
baseActionClass = name
#define DefineIAction(name) DefineAction(name, IAction)
#define DefineTActionSub(name, cls) \
DefineAction(name, cls) \
verDobjProp = &verifyDobj##name \
remapDobjProp = &remapDobj##name \
preCondDobjProp = &preCondDobj##name \
checkDobjProp = &checkDobj##name \
actionDobjProp = &actionDobj##name \
reportDobjProp = &reportDobj##name
#define DefineTAction(name) DefineTActionSub(name, TAction)
#define DefineTIActionSub(name, cls) \
DefineAction(name, cls) \
verDobjProp = &verifyDobj##name \
verIobjProp = &verifyIobj##name \
remapDobjProp = &remapDobj##name \
remapIobjProp = &remapIobj##name \
preCondDobjProp = &preCondDobj##name \
preCondIobjProp = &preCondIobj##name \
checkDobjProp = &checkDobj##name \
checkIobjProp = &checkIobj##name \
actionDobjProp = &actionDobj##name \
actionIobjProp = &actionIobj##name \
reportDobjProp = &reportDobj##name \
reportIobjProp = &reportIobj##name
#define DefineTIAction(name) DefineTIActionSub(name, TIAction)
"#;
const LIBRARY: &str = r#"#include "macros.h"
class Action: object;
class IAction: Action;
class TAction: Action;
class TIAction: TAction;
class Thing: object;
property verifyDobjTake, verifyDobjPutIn, verifyIobjPutIn, isFull;
_main(args) {
local verify = PutIn.verDobjProp;
return box.(verify)() + Take.baseActionClass.execAction(nil) + Jump.execAction(nil);
}
"#;
const WITH_MACROS: &str = r#"#include "macros.h"
DefineTAction(Take)
execAction(cmd) { return 1; }
;
DefineTIAction(PutIn);
DefineIAction(Jump)
execAction(cmd) { return 3; }
;
box: Thing
dobjFor(Take) { verify() { return 2; } action() { } }
dobjFor(PutIn) asDobjFor(Take)
iobjFor(PutIn) {
preCond = []
verify() { if (isFull) return nil; }
}
isFull = nil
;
"#;
const IN_HEXICON: &str = r#"#include "macros.h"
action Take(dobj): TAction
execAction(cmd) { return 1; }
;
action PutIn(dobj, iobj): TIAction;
action Jump: IAction
execAction(cmd) { return 3; }
;
box: Thing
dobj Take { verify() { return 2; } action() { } }
dobj PutIn as Take
iobj PutIn {
preCond = []
verify() { if (isFull) return nil; }
}
isFull = nil
;
"#;
fn image(game: &str) -> Vec<u8> {
let compiled = build("lib.hxn", "game.hxn", game);
let errors: Vec<_> = compiled
.diagnostics
.iter()
.filter(|d| d.severity != crate::pp::Severity::Warning)
.map(|d| format!("{}({}): {}", d.file, d.line, d.message))
.collect();
assert_eq!(errors, Vec::<String>::new());
compiled.image.expect("an image")
}
#[test]
fn actions_and_handlers_are_what_the_macros_make() {
assert_eq!(image(IN_HEXICON), image(WITH_MACROS));
}
#[test]
fn names_called_dobj_and_action_are_still_names() {
let game = format!(
"{IN_HEXICON}action: Thing dobj = 1 iobj(x) {{ return x; }} ;\n\
crate: Thing dobj = action aobj {{ return dobj; }} ;\n"
);
let compiled = build("lib.hxn", "game.hxn", &game);
assert!(compiled.image.is_some(), "{:?}", compiled.diagnostics);
}
#[test]
fn a_handler_needs_a_body_or_as() {
let game = "box: Thing dobj Take = 1 ;\n";
let compiled = build("lib.hxn", "game.hxn", game);
let messages: Vec<_> = compiled
.diagnostics
.iter()
.map(|d| d.message.clone())
.collect();
assert!(
messages
.iter()
.any(|m| m
== "expected '{' or 'as' after the action a handler is for, but found \"=\""),
"{messages:?}"
);
}
}
mod imports {
use super::{CompileOptions, Compiled, Files, read, sys_path};
const HEADER: &str = "#define GREETING 'hello'\nclass Thing: object;\nThing template 'name';\n";
const START: &str = "_main(args) { return [GREETING, rock.name]; }\nrock: Thing 'rock';\n";
fn compile(name: &str, start: &str) -> Compiled {
let mut files = Files::default()
.with("lib/lib.h", HEADER)
.with(name, &format!("{start}{START}"));
let options = CompileOptions {
imports: vec![
("lib".to_owned(), Some("lib/lib.h".to_owned())),
("old".to_owned(), None),
],
include_paths: vec!["lib".to_owned()],
line_records: false,
..CompileOptions::default()
};
crate::compile_program(&mut files, &[name], &options)
}
fn messages(compiled: &Compiled) -> Vec<String> {
compiled
.diagnostics
.iter()
.map(|d| format!("{}({}): {}", d.file, d.line, d.message))
.collect()
}
#[test]
fn importing_a_library_is_including_its_header() {
let imported = compile("start.hxn", "import lib;\n");
let included = compile("start.hxn", "#include <lib.h>\n");
assert_eq!(messages(&imported), Vec::<String>::new());
assert!(imported.image.is_some());
assert_eq!(imported.image, included.image);
let twice = compile("start.hxn", "import lib;\n import lib ;\n");
assert_eq!(
twice.image.as_ref().map(Vec::len),
imported.image.as_ref().map(Vec::len)
);
}
#[test]
fn a_library_not_in_the_build_or_without_a_header_cannot_be_imported() {
assert_eq!(
messages(&compile("start.hxn", "import nothing;\nimport old;\n"))[..2],
[
"start.hxn(1): import nothing: no library \"nothing\" in this build",
"start.hxn(2): import old: library \"old\" names no header to import",
]
);
}
#[test]
fn an_import_is_hexicons_and_a_line_of_its_own() {
let skipped = compile("start.hxn", "#if 0\nimport nothing;\n#endif\nimport lib;\n");
assert_eq!(messages(&skipped), Vec::<String>::new());
}
#[test]
fn a_librarys_manifest_names_its_header() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nsources = [\"start\"]\nlibraries = [\"mylib\", \"old\"]\n",
)
.with(
"game/mylib/hexicon.toml",
"[library]\nheader = \"my.h\"\nsources = [\"one\"]\n",
)
.with("game/old.toml", "[library]\nsources = [\"legacy\"]\n")
.with("game/start.hxn", "");
let project =
read(&mut files, &["--no-system", "-f", "game"]).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(
project.options.imports,
[
(
"mylib".to_owned(),
Some(sys_path(&["game", "mylib", "my.h"]))
),
("old".to_owned(), None),
]
);
}
}
#[test]
fn a_hexicon_source_is_told_it_is_one() {
let text = "#ifdef __HEXICON\n#error hexicon\n#endif\n\
#ifdef __TADS3\n#error tads\n#endif\n_main(args) { }\n";
let errors = |name: &str| -> Vec<String> {
messages_compiling(name, text)
.into_iter()
.filter(|m| m.contains("#error"))
.collect()
};
assert_eq!(
errors("main.hxn"),
["main.hxn(2): Error: #error : hexicon"]
);
}
#[test]
fn a_library_on_the_search_path_comes_before_the_one_built_in() {
let mut files = Files::default()
.with("syslib/system.toml", "[library]\nsources = [\"_main\"]\n")
.with("syslib/_main.hxn", "")
.with(
"game/hexicon.toml",
"[game]\nlibraries = [\"system\"]\nsources = [\"main\"]\n",
)
.with("game/main.hxn", "");
let project = read(&mut files, &["--library-path", "syslib", "-f", "game"])
.unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(
project.sources,
[
sys_path(&["syslib", "_main.hxn"]),
sys_path(&["game", "main.hxn"])
]
);
}
mod hexicon_messages {
use super::{CompileOptions, Files};
use crate::pp::Diagnostic;
fn diagnostics(name: &str, text: &str) -> Vec<Diagnostic> {
let mut files = Files::default().with(name, text);
crate::compile_program(&mut files, &[name], &CompileOptions::default()).diagnostics
}
const MISSING: &str = "+ property location;\nbox: object\n name = 'box'\n\n+ key: object\n;\n_main(args) { }\n";
#[test]
fn a_missing_semicolon_is_said_where_the_object_ends() {
let found = diagnostics("game.hxn", MISSING);
assert_eq!(found.len(), 1, "{found:?}");
let d = &found[0];
assert_eq!(d.message, "the object above ends without a ';'");
assert_eq!((d.line, d.lines), (3, Some((3, 5))));
assert!(d.hexicon && d.note.is_some());
}
#[test]
fn an_increment_on_its_own_line_is_still_one() {
let text = "_main(args) { local n = 1; n++; local m = n\n++; return m; }\n";
let found = diagnostics("game.hxn", text);
assert!(!found.is_empty());
let fine = diagnostics("game.hxn", "_main(args) { local n = 1; n++; return n; }\n");
assert_eq!(fine, Vec::<Diagnostic>::new());
}
#[test]
fn messages_are_in_hexicons_words_with_names_suggested() {
let text = "rock: object weight = 3;\n\
_main(args) { local x = rock.weigth; return x + nothere(); }\n\
notThere() { }\n";
let found = diagnostics("game.hxn", text);
let shown: Vec<(String, Option<String>)> = found
.iter()
.map(|d| (d.message.clone(), d.note.clone()))
.collect();
assert!(
shown.contains(&(
"\"weigth\" is not defined anywhere".to_owned(),
Some("did you mean \"weight\"?".to_owned())
)),
"{shown:?}"
);
assert!(
shown.contains(&(
"\"nothere\" is not defined anywhere".to_owned(),
Some("did you mean \"notThere\"?".to_owned())
)),
"{shown:?}"
);
}
#[test]
fn a_description_after_the_properties_is_one_error() {
let text = "class Lever: object;\n\
lever: Lever\n\
\x20 n = 1\n\
\x20 \"Lever <<n>>, <<if n>>pulled<<else>>back<<end>>. \"\n\
\x20 m = 2\n\
;\n\
_main(args) { return lever.m; }\n";
let found: Vec<(u32, String, Option<String>)> = diagnostics("game.hxn", text)
.into_iter()
.map(|d| (d.line, d.message, d.note))
.collect();
assert_eq!(
found,
[(
4,
"a description in double quotes after the properties".to_owned(),
Some(
"the values a template gives, such as the description, come before the \
properties: move it up, or write it as desc = \"...\""
.to_owned()
)
)]
);
}
#[test]
fn a_handler_as_an_action_nothing_defines_is_one_warning() {
let text = "bell: object\n\
\x20 dobj Push as Ring\n\
;\n\
_main(args) { }\n";
let found: Vec<(u32, String)> = diagnostics("game.hxn", text)
.into_iter()
.map(|d| (d.line, d.message))
.collect();
assert_eq!(found, [(2, "\"Ring\" is not defined anywhere".to_owned())]);
}
#[test]
fn a_verb_rule_for_an_action_nothing_defines_is_an_error() {
let text = "class VerbProduction: object;\n\
verb Pastward 'pastward': ;\n\
_main(args) { }\n";
let found: Vec<(u32, String)> = diagnostics("game.hxn", text)
.into_iter()
.filter(|d| d.severity == crate::pp::Severity::Error)
.map(|d| (d.line, d.message))
.collect();
assert_eq!(
found,
[(
2,
"the verb rule \"Pastward\" is for an action, \"Pastward\", that is not defined \
anywhere"
.to_owned()
)]
);
}
#[test]
fn an_object_named_with_a_macro_is_told_so() {
let text = "#define stop return 1;\n\
+ property location;\n\
room: object;\n\
+ stop: object;\n\
_main(args) { stop }\n";
let found = diagnostics("game.hxn", text);
assert_eq!(found[0].line, 4, "{found:?}");
assert_eq!(
found[0].note.as_deref(),
Some(
"\"stop\" is a macro, which stands for \"return 1;\", so it cannot be an \
object's name: give the object a name of its own"
)
);
assert!(
found
.iter()
.filter(|d| d.note.as_deref().is_some_and(|n| n.contains("is a macro")))
.count()
== 1,
"{found:?}"
);
}
#[test]
fn a_slip_for_a_parameter_is_said_to_be_one() {
let text = "State: object;\n\
lamp: object makeOn(stat) { if (stats) return 1; return 0; }\n;\n\
_main(args) { }\n";
let found = diagnostics("game.hxn", text);
let shown: Vec<(String, Option<String>)> = found
.iter()
.map(|d| (d.message.clone(), d.note.clone()))
.collect();
assert_eq!(
shown,
[(
"\"stats\" is not defined anywhere".to_owned(),
Some("did you mean \"stat\"?".to_owned())
)]
);
}
}
mod image_names {
use super::{CompileOptions, Files};
fn image(text: &str) -> Vec<u8> {
let mut files = Files::default().with("game.hxn", text);
let compiled =
crate::compile_program(&mut files, &["game.hxn"], &CompileOptions::default());
compiled.image.expect("the program compiles")
}
fn has(image: &[u8], name: &[u8]) -> bool {
image.windows(name.len()).any(|at| at == name)
}
#[test]
fn a_program_on_hexicons_names_is_a_hexicon_image() {
let image = image(
"builtin 'hexicon-gen/030008' { typeOf(val); }\n\
builtin class ProgramObject 'hexicon-object/030005' { }\n\
rock: object;\n\
_main(args) { return typeOf(rock); }\n",
);
assert!(image.starts_with(b"HX-image\r\n\x1a"));
assert!(has(&image, b"hexicon-object/030005"));
assert!(!has(&image, b"tads-object"));
}
}
mod verb_declarations {
use super::{CompileOptions, Compiled, Files};
fn build(library_name: &str, game_name: &str, game: &str) -> Compiled {
let mut files = Files::default()
.with("macros.h", MACROS)
.with(library_name, LIBRARY)
.with(game_name, game);
let options = CompileOptions {
line_records: false,
..CompileOptions::default()
};
crate::compile_program(&mut files, &[library_name, game_name], &options)
}
const MACROS: &str = "#define VerbRule(tag) grammar predicate(tag):\n\
#define singleDobj singleNoun->dobjMatch\n";
const LIBRARY: &str = r#"#include "macros.h"
class VerbProduction: object;
class Action: object;
Ring: Action;
Sing: Action;
Examine: Action;
grammar singleNoun: 'it' : object;
property dobjMatch, verbPhrase, missingQ, priority;
_main(args) { return predicate; }
"#;
const WITH_MACROS: &str = r#"#include "macros.h"
VerbRule(Ring)
'ring' singleDobj
: VerbProduction
action = Ring
verbPhrase = 'ring/ringing (what)'
missingQ = 'what do you want to ring'
;
VerbRule(Sing) 'sing' : VerbProduction action = Sing verbPhrase = 'sing/singing';
VerbRule(LookAtWhat)
('look' | 'l') 'at' singleDobj
| 'examine' singleDobj
: VerbProduction
action = Examine
verbPhrase = 'examine/examining (what)'
;
modify VerbRule(Ring) ('ring' | 'toll') singleDobj : ;
modify VerbRule(Sing) : priority = 3;
replace VerbRule(LookAtWhat) 'look' singleDobj : VerbProduction action = Examine;
"#;
const IN_HEXICON: &str = r#"#include "macros.h"
verb Ring 'ring' singleDobj:
verbPhrase = 'ring/ringing (what)'
missingQ = 'what do you want to ring'
;
verb Sing 'sing': verbPhrase = 'sing/singing';
verb LookAtWhat for Examine
('look' | 'l') 'at' singleDobj
| 'examine' singleDobj:
verbPhrase = 'examine/examining (what)'
;
modify verb Ring ('ring' | 'toll') singleDobj: ;
modify verb Sing: priority = 3;
replace verb LookAtWhat for Examine 'look' singleDobj: ;
"#;
fn image(game: &str) -> Vec<u8> {
let compiled = build("lib.hxn", "game.hxn", game);
let errors: Vec<_> = compiled
.diagnostics
.iter()
.filter(|d| d.severity != crate::pp::Severity::Warning)
.map(|d| format!("{}({}): {}", d.file, d.line, d.message))
.collect();
assert_eq!(errors, Vec::<String>::new());
compiled.image.expect("an image")
}
#[test]
fn verb_rules_are_what_the_macro_makes() {
assert_eq!(image(IN_HEXICON), image(WITH_MACROS));
}
#[test]
fn a_name_called_verb_is_still_a_name() {
for extra in [
"verb: object size = 1;\nbell: object size = verb.size;\n",
"bell: object verb = 1 ring() { local verb = self.verb; return verb; } ;\n",
] {
let game = format!("{IN_HEXICON}{extra}");
let compiled = build("lib.hxn", "game.hxn", &game);
assert!(compiled.image.is_some(), "{:?}", compiled.diagnostics);
}
}
#[test]
fn a_verbs_words_end_with_a_colon() {
let compiled = build("lib.hxn", "game.hxn", "verb Ring 'ring' singleDobj;\n");
assert!(
compiled
.diagnostics
.iter()
.any(|d| d.message == "expected ':' at the end of a verb's words, but found \";\""),
"{:?}",
compiled.diagnostics
);
}
}
mod shared_names {
use super::{CompileOptions, Files, messages_of};
fn messages(library: &str, game: &str) -> Vec<String> {
let mut files = Files::default()
.with("lib.hxn", library)
.with("game.hxn", game);
let options = CompileOptions {
from_library: vec![true, false],
..CompileOptions::default()
};
messages_of(crate::compile_program(
&mut files,
&["lib.hxn", "game.hxn"],
&options,
))
}
const LIBRARY: &str = "class Thing: object match = nil weight = 1\n\
\x20 lit() { return match; }\n\
;\n\
_main(args) { return rock.lit(); }\n";
#[test]
fn an_object_can_be_called_what_a_property_is() {
let game = "match: Thing weight = 2;\n\
rock: Thing match = true\n\
\x20 heavier() { return match.weight > self.weight; }\n\
\x20 which() { return [&match, self.match]; }\n\
;\n";
assert_eq!(messages(LIBRARY, game), Vec::<String>::new());
}
#[test]
fn an_object_defined_before_the_property_is_the_same() {
let library = "class Thing: object;\n_main(args) { return nil; }\n";
let game = "match: Thing;\nrock: Thing match = 3 count() { return self.match; };\n";
assert_eq!(messages(library, game), Vec::<String>::new());
}
#[test]
fn giving_the_object_a_value_says_how_to_give_the_property_one() {
let game = "match: Thing;\nrock: Thing strike() { match = true; };\n";
assert_eq!(
messages(LIBRARY, game),
[
"game.hxn(2): \"match\" here is the object match, which can't be given a value; \
for this object's property, write self.match"
]
);
}
}
mod documented {
use super::{CompileOptions, Files};
use crate::document;
const LIBRARY: &str = "/*\n\
\x20* A thing: anything the player can see.\n\
\x20*\n\
\x20* It has a weight.\n\
\x20*/\n\
class Thing: object\n\
\x20 // How heavy it is.\n\
\x20 weight = 1\n\
\n\
\x20 /* What it says when it is looked at. */\n\
\x20 lookMsg = 'It looks\n\
\x20 like a thing. '\n\
\n\
\x20 describe(how) { return lookMsg; }\n\
;\n\
_main(args) { return rock.describe(nil); }\n";
const GAME: &str = "property verifyDobjTake, actionDobjTake;\n\
// A rock, the game's own.\n\
rock: Thing\n\
\x20 weight = 9\n\
;\n\
modify Thing\n\
\x20 // Hot to the touch.\n\
\x20 isHot = nil\n\
;\n";
fn documented() -> crate::Documented {
let mut files = Files::default()
.with("lib.hxn", LIBRARY)
.with("game.hxn", GAME);
let options = CompileOptions {
from_library: vec![true, false],
..CompileOptions::default()
};
document(&mut files, &["lib.hxn", "game.hxn"], &options)
}
#[test]
fn an_anonymous_object_is_documented_with_its_values() {
let source = "+ property location;\n\
property leadsTo;\n\
class Pick: object\n\
\x20 label = ''\n\
;\n\
Pick template 'label' ->leadsTo?;\n\
menu: object;\n\
+ Pick 'Go on' ->menu;\n\
+ Pick 'Stop' ->nil\n\
\x20 isOnce = nil\n\
\x20 isAvailable = (menu != nil)\n\
;\n\
_main(args) { }\n";
let mut files = Files::default().with("menu.hxn", source);
let found = document(&mut files, &["menu.hxn"], &CompileOptions::default());
assert!(found.diagnostics.is_empty(), "{:?}", found.diagnostics);
assert!(found.objects.iter().any(|o| o.name == "menu"));
assert!(
!found
.objects
.iter()
.any(|o| o.name.is_empty() || o.name == ".anon")
);
assert_eq!(found.anonymous.len(), 2);
let (on, stop) = (&found.anonymous[0], &found.anonymous[1]);
assert_eq!(on.superclasses, ["Pick"]);
assert_eq!(on.place.line, 8);
assert_eq!(on.value("label"), Some("'Go on'"));
assert_eq!(on.value("leadsTo"), Some("menu"));
assert_eq!(on.value("location"), Some("menu"));
assert_eq!(stop.value("leadsTo"), Some("nil"));
assert_eq!(stop.value("isOnce"), Some("nil"));
assert_eq!(stop.value("isAvailable"), None);
assert!(stop.members.iter().any(|m| m.name == "isAvailable"));
}
#[test]
fn a_class_and_its_members_are_documented_from_their_comments() {
let found = documented();
assert!(found.diagnostics.is_empty(), "{:?}", found.diagnostics);
let thing = found.objects.iter().find(|o| o.name == "Thing").unwrap();
assert!(thing.is_class && thing.from_library);
assert_eq!(thing.superclasses, Vec::<String>::new());
assert_eq!(thing.place.line, 6);
assert_eq!(
thing.comment,
"A thing: anything the player can see.\n\nIt has a weight."
);
let member = |name: &str| thing.members.iter().find(|m| m.name == name).unwrap();
assert_eq!(member("weight").comment, "How heavy it is.");
assert_eq!(member("weight").declaration, "weight = 1");
assert_eq!(
member("lookMsg").declaration,
"lookMsg = 'It looks like a thing. '"
);
assert_eq!(member("lookMsg").place.line, 11);
assert_eq!(
member("lookMsg").comment,
"What it says when it is looked at."
);
assert!(member("describe").is_method);
assert_eq!(member("describe").declaration, "describe(how)");
}
#[test]
fn a_function_is_documented_and_the_files_are_kept() {
let mut files = Files::default().with(
"lib.hxn",
"// Twice what it is given.
twice(n)
{
return n * 2;
}
_main(args) { return twice(1); }
",
);
let found = document(&mut files, &["lib.hxn"], &CompileOptions::default());
let twice = found.functions.iter().find(|f| f.name == "twice").unwrap();
assert_eq!(twice.declaration, "twice(n)");
assert_eq!(twice.comment, "Twice what it is given.");
assert_eq!(twice.place.line, 2);
assert!(found.functions.iter().all(|f| !f.name.starts_with('_')));
let file = found.files.iter().find(|f| f.name == "lib.hxn").unwrap();
assert_eq!(file.lines[1], "twice(n)");
}
#[test]
fn the_grammar_and_the_messages_are_read_from_the_text() {
let lines: Vec<String> = [
"verb Toll 'toll' singleDobj:",
" verbPhrase = 'toll/tolling (what)'",
";",
"verb Peal for Toll [badness 50] 'peal'",
" singleDobj:",
" cannotTollMsg = msgText(cannot toll, '{The subj dobj} {is} not",
" something you can toll. ')",
" lateMsg = msgText(too late,",
" 'Too late. ')",
" showMsg(id, txt);",
]
.iter()
.map(|s| (*s).to_owned())
.collect();
let mut verbs = Vec::new();
crate::docs::scan_verbs("game.hxn", &lines, &mut verbs);
let found: Vec<(&str, &str, u32)> = verbs
.iter()
.map(|v| (v.action.as_str(), v.words.as_str(), v.place.line))
.collect();
assert_eq!(
found,
[
("Toll", "'toll' singleDobj", 1),
("Toll", "'peal' singleDobj", 4)
]
);
let mut messages = Vec::new();
crate::docs::scan_messages("game.hxn", &lines, &mut messages);
let found: Vec<(&str, &str)> = messages
.iter()
.map(|m| (m.id.as_str(), m.text.as_str()))
.collect();
assert_eq!(
found,
[
(
"cannot toll",
"{The subj dobj} {is} not something you can toll."
),
("too late", "Too late."),
]
);
}
#[test]
fn macros_templates_and_enums_are_read_from_the_text() {
let lines: Vec<String> = [
"// Twice a value.",
"#define TWICE(x) ((x) * 2)",
"#define LONG a + \\",
" b",
"Thing template 'vocab' \"desc\"?;",
"Room template 'roomTitle'",
" \"desc\"?;",
"/* The colors. */",
"enum red, green,",
" blue;",
"enum token tokWord;",
]
.iter()
.map(|s| (*s).to_owned())
.collect();
let mut macros = Vec::new();
crate::docs::scan_macros("lib.h", &lines, &mut macros);
let found: Vec<(&str, &str, &str, &str)> = macros
.iter()
.map(|m| {
(
m.name.as_str(),
m.params.as_str(),
m.expansion.as_str(),
m.comment.as_str(),
)
})
.collect();
assert_eq!(
found,
[
("TWICE", "(x)", "((x) * 2)", "Twice a value."),
("LONG", "", "a + b", ""),
]
);
let mut templates = Vec::new();
crate::docs::scan_templates("lib.h", &lines, &mut templates);
let found: Vec<(&str, &str)> = templates
.iter()
.map(|t| (t.class.as_str(), t.text.as_str()))
.collect();
assert_eq!(
found,
[
("Thing", "Thing template 'vocab' \"desc\"?;"),
("Room", "Room template 'roomTitle' \"desc\"?;"),
]
);
let mut enums = Vec::new();
crate::docs::scan_enums("lib.h", &lines, &mut enums);
let found: Vec<(Vec<String>, bool, &str)> = enums
.iter()
.map(|e| (e.names.clone(), e.is_token, e.comment.as_str()))
.collect();
assert_eq!(
found,
[
(
vec!["red".to_owned(), "green".to_owned(), "blue".to_owned()],
false,
"The colors."
),
(vec!["tokWord".to_owned()], true, ""),
]
);
}
#[test]
fn a_modify_adds_to_what_it_modifies() {
let found = documented();
let thing = found.objects.iter().find(|o| o.name == "Thing").unwrap();
assert_eq!(thing.modified_in.len(), 1);
assert_eq!(thing.modified_in[0].file, "game.hxn");
let hot = thing.members.iter().find(|m| m.name == "isHot").unwrap();
assert_eq!(hot.comment, "Hot to the touch.");
let rock = found.objects.iter().find(|o| o.name == "rock").unwrap();
assert!(!rock.is_class && !rock.from_library);
assert_eq!(rock.superclasses, ["Thing"]);
assert_eq!(rock.comment, "A rock, the game's own.");
}
}
fn preprocess_in_game(files: &mut Files, args: &[&str], source: &str) -> (Vec<String>, String) {
let project = read(files, args).unwrap_or_else(|e| panic!("{e:?}"));
let sources: Vec<&str> = project.sources.iter().map(String::as_str).collect();
let source = sources
.iter()
.find(|s| s.replace('\\', "/") == source)
.copied()
.unwrap_or(source);
crate::with_preprocessor(files, &sources, source, &project.options, |pp| {
let mut read_from = Vec::new();
let mut text = String::new();
while let Some(line) = pp.next_line() {
let name = pp.file_name(line.file).replace('\\', "/");
if !read_from.contains(&name) {
read_from.push(name);
}
text += &String::from_utf8_lossy(&line.text);
text.push('\n');
}
let problems = pp.take_diagnostics();
assert!(
problems.iter().all(|p| p.severity == Severity::Warning),
"{problems:?}"
);
(read_from, text)
})
.unwrap_or_else(|e| panic!("{e:?}"))
}
#[test]
fn a_source_is_preprocessed_as_its_game_s_build_reads_it() {
let mut files = Files::default()
.with(
"game/hexicon.toml",
"[game]\nname = \"A game\"\nlibraries = [\"system\", \"hexad\"]\n\
sources = [\"start\", \"more.hxe\"]\n\n[defines]\nLANGUAGE = \"english\"\n",
)
.with(
"game/start.hxn",
"import hexad;\n#ifdef __DEBUG\nbuiltForDebugging\n#endif\nLANGUAGE\n",
)
.with("game/more.hxe", "The Hall is a room.\n");
let (read_from, text) =
preprocess_in_game(&mut files, &["-f", "game/hexicon.toml"], "game/start.hxn");
assert!(
read_from.iter().any(|f| f == "<hexicon>/hexad/hexad.hxh"),
"{read_from:?}"
);
assert!(text.contains("english"), "{text}");
assert!(!text.contains("builtForDebugging"));
let (_, text) = preprocess_in_game(
&mut files,
&["-d", "-f", "game/hexicon.toml"],
"game/start.hxn",
);
assert!(text.contains("builtForDebugging"), "{text}");
let (_, text) = preprocess_in_game(&mut files, &["-f", "game/hexicon.toml"], "game/more.hxe");
assert!(text.contains("hall: Room"), "{text}");
}
struct Disk;
impl SourceFiles for Disk {
fn read(&mut self, name: &str) -> Option<Vec<u8>> {
std::fs::read(name).ok()
}
fn exists(&mut self, name: &str) -> bool {
std::path::Path::new(name).is_file()
}
fn decode(&mut self, _charset: &str, _bytes: &[u8]) -> Option<Vec<u8>> {
None
}
}
#[test]
fn a_build_that_documents_says_what_document_says() {
let examples =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/guide/examples");
for game in [
"wellhouse",
"belfry",
"harbor",
"house-english",
"gorge-english",
] {
let manifest = examples.join(game).join("hexicon.toml");
let args = [
"-d".to_owned(),
"-f".to_owned(),
manifest.to_string_lossy().into_owned(),
];
let project = crate::read_project(&mut Disk, &args, &project_paths())
.unwrap_or_else(|e| panic!("{game}: {e:?}"));
let sources: Vec<&str> = project.sources.iter().map(String::as_str).collect();
let alone = crate::document(&mut Disk, &sources, &project.options);
let (compiled, together) =
crate::compile_and_document(&mut Disk, &sources, &project.options);
assert!(
compiled.image.is_some(),
"{game}: {:?}",
compiled.diagnostics
);
assert!(!alone.objects.is_empty(), "{game}");
let (alone, together) = (format!("{alone:?}"), format!("{together:?}"));
if let Some(at) = alone
.bytes()
.zip(together.bytes())
.position(|(a, b)| a != b)
{
let from = at.saturating_sub(400);
panic!(
"{game}: they differ at {at}:
document: {}
together: {}",
&alone[from..(at + 200).min(alone.len())],
&together[from..(at + 200).min(together.len())]
);
}
assert_eq!(alone.len(), together.len(), "{game}");
let built = crate::compile_program(&mut Disk, &sources, &project.options);
assert_eq!(built.diagnostics, compiled.diagnostics, "{game}");
}
}
#[test]
fn a_problem_at_a_token_says_where_on_its_line_it_is() {
let source = "function main(args)
{
local x = 1 + ;
/* a */ local y = 2 * ;
#define PLUS +
local z = 3 PLUS ;
#define CLOSE )
local w = CLOSE;
}
";
let mut files = Files::default().with("main.hxn", source);
let compiled = crate::compile(&mut files, "main.hxn", &crate::CompileOptions::default());
let at = |line: u32| {
compiled
.diagnostics
.iter()
.find(|d| d.line == line)
.unwrap_or_else(|| panic!("a problem on line {line}: {:?}", compiled.diagnostics))
.columns
};
assert_eq!(at(3), Some((18, 19)));
assert_eq!(at(4), Some((26, 27)));
assert_eq!(at(6), Some((21, 22)));
assert_eq!(at(8), None);
}
#[test]
fn the_names_a_source_uses_are_kept_with_where_they_are() {
let source = "class Lamp: object\n lit = nil\n light(level) { local glow = level; lit = glow; }\n;\nlamp: Lamp\n lit = true\n;\nfunction f(x) { return lamp.lit && x; }\n";
let mut files = Files::default().with("main.hxn", source);
let documented = crate::document(&mut files, &["main.hxn"], &crate::CompileOptions::default());
assert!(
documented.diagnostics.is_empty(),
"{:?}",
documented.diagnostics
);
let at = |name: &str| -> Vec<(u32, u32)> {
documented
.uses
.iter()
.filter(|u| u.name == name)
.map(|u| (u.place.line, u.columns.0))
.collect()
};
assert_eq!(at("Lamp"), [(1, 6), (5, 6)]);
assert_eq!(at("lamp"), [(5, 0), (8, 23)]);
assert_eq!(at("lit"), [(2, 4), (3, 39), (6, 4), (8, 28)]);
assert_eq!(at("light"), [(3, 4)]);
assert_eq!(at("f"), [(8, 9)]);
for name in ["level", "glow", "x"] {
assert!(at(name).is_empty(), "{name}: {:?}", at(name));
}
}
#[test]
fn a_value_written_out_is_kept_as_the_source_writes_it() {
let source = "hall: object\n north = \"The way is blocked. \"\n regions = [yard, cellar]\n south = yard\n up = 'No. '\n;\nyard: object;\ncellar: object;\n";
let mut files = Files::default().with("main.hxn", source);
let documented = crate::document(&mut files, &["main.hxn"], &crate::CompileOptions::default());
let hall = documented
.objects
.iter()
.find(|o| o.name == "hall")
.expect("the hall");
assert_eq!(hall.value("north"), Some("\"The way is blocked. \""));
assert_eq!(hall.value("regions"), Some("[yard, cellar]"));
assert_eq!(hall.value("south"), Some("yard"));
assert_eq!(hall.value("up"), Some("'No. '"));
}
#[test]
fn an_english_source_s_members_say_no_line_of_another_text() {
let examples =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/guide/examples");
let manifest = examples.join("harbor").join("hexicon.toml");
let args = [
"-d".to_owned(),
"-f".to_owned(),
manifest.to_string_lossy().into_owned(),
];
let project = crate::read_project(&mut Disk, &args, &project_paths()).expect("the Harbor");
let sources: Vec<&str> = project.sources.iter().map(String::as_str).collect();
let documented = crate::document(&mut Disk, &sources, &project.options);
let english: Vec<_> = documented
.objects
.iter()
.filter(|o| crate::english::is_english(&o.place.file))
.collect();
assert!(!english.is_empty());
for object in english {
assert!(
!object.comment.contains("#line"),
"{}: {}",
object.name,
object.comment
);
for member in &object.members {
assert!(
!member.declaration.contains("#line"),
"{}.{}",
object.name,
member.name
);
assert!(
!member.declaration.is_empty(),
"{}.{}",
object.name,
member.name
);
}
}
}