5 Commits

Author SHA1 Message Date
John Alanbrook
4de0659474 allow tokens as properties 2026-02-08 00:34:15 -06:00
John Alanbrook
27a9b72b07 functino tests; default args for mach and mcode 2026-02-08 00:31:18 -06:00
John Alanbrook
a3622bd5bd better parser error reporting 2026-02-08 00:23:47 -06:00
John Alanbrook
2f6700415e add functinos 2026-02-07 23:38:39 -06:00
John Alanbrook
243d92f7f3 rm ?? and .? 2026-02-07 22:09:40 -06:00
4 changed files with 1049 additions and 177 deletions

View File

@@ -114,12 +114,19 @@ static int print_tree_errors(cJSON *root) {
cJSON *fname = cJSON_GetObjectItemCaseSensitive(root, "filename");
if (cJSON_IsString(fname))
filename = fname->valuestring;
int prev_line = -1;
const char *prev_msg = NULL;
cJSON *e;
cJSON_ArrayForEach(e, errors) {
const char *msg = cJSON_GetStringValue(
cJSON_GetObjectItemCaseSensitive(e, "message"));
cJSON *line = cJSON_GetObjectItemCaseSensitive(e, "line");
cJSON *col = cJSON_GetObjectItemCaseSensitive(e, "column");
int cur_line = cJSON_IsNumber(line) ? (int)line->valuedouble : -1;
if (prev_msg && msg && cur_line == prev_line && strcmp(msg, prev_msg) == 0)
continue;
prev_line = cur_line;
prev_msg = msg;
if (msg && cJSON_IsNumber(line) && cJSON_IsNumber(col))
fprintf(stderr, "%s:%d:%d: error: %s\n",
filename, (int)line->valuedouble, (int)col->valuedouble, msg);

File diff suppressed because it is too large Load Diff

View File

@@ -604,6 +604,48 @@ run("self-referencing object", function() {
assert_eq(o.self.self.name, "root", "cycle access")
})
// === IDENTIFIER ? AND ! ===
run("question mark in identifier", function() {
var nil? = (x) => x == null
assert_eq(nil?(null), true, "nil? null")
assert_eq(nil?(42), false, "nil? 42")
})
run("bang in identifier", function() {
var set! = (x) => x + 1
assert_eq(set!(5), 6, "set! call")
})
run("question mark mid identifier", function() {
var is?valid = (x) => x > 0
assert_eq(is?valid(3), true, "is?valid true")
assert_eq(is?valid(-1), false, "is?valid false")
})
run("bang mid identifier", function() {
var do!stuff = () => 42
assert_eq(do!stuff(), 42, "do!stuff call")
})
run("ternary after question ident", function() {
var nil? = (x) => x == null
var a = nil?(null) ? "yes" : "no"
assert_eq(a, "yes", "ternary true branch")
var b = nil?(42) ? "yes" : "no"
assert_eq(b, "no", "ternary false branch")
})
run("bang not confused with logical not", function() {
assert_eq(!true, false, "logical not true")
assert_eq(!false, true, "logical not false")
})
run("inequality not confused with bang ident", function() {
assert_eq(1 != 2, true, "inequality true")
assert_eq(1 != 1, false, "inequality false")
})
// === SUMMARY ===
print(text(passed) + " passed, " + text(failed) + " failed out of " + text(passed + failed))

View File

@@ -3369,6 +3369,182 @@ run("gc object from keys function under pressure", function() {
}
})
// ============================================================================
// DEFAULT PARAMETER TESTS
// ============================================================================
run("default param constant", function() {
var f = function(a, b=10) { return a + b }
assert_eq(f(1), 11, "default param constant")
})
run("default param overridden", function() {
var f = function(a, b=10) { return a + b }
assert_eq(f(1, 2), 3, "default param overridden")
})
run("default param uses earlier param", function() {
var f = function(a, b=a+1) { return b }
assert_eq(f(5), 6, "default param uses earlier param")
})
run("default param uses earlier param overridden", function() {
var f = function(a, b=a+1) { return b }
assert_eq(f(5, 20), 20, "default param uses earlier param overridden")
})
run("multiple default params", function() {
var f = function(a, b=10, c=a+1) { return a + b + c }
assert_eq(f(1), 13, "multiple defaults f(1)")
assert_eq(f(1, 2), 5, "multiple defaults f(1,2)")
assert_eq(f(1, 2, 3), 6, "multiple defaults f(1,2,3)")
})
run("arrow function default param", function() {
var g = (x, y=100) => x + y
assert_eq(g(5), 105, "arrow default param")
assert_eq(g(5, 20), 25, "arrow default param overridden")
})
run("default param null passed explicitly", function() {
var f = function(a, b=10) { return b }
assert_eq(f(1, null), 10, "explicit null triggers default")
})
run("default param with string", function() {
var f = function(a, b="hello") { return b }
assert_eq(f(1), "hello", "default string param")
assert_eq(f(1, "world"), "world", "default string overridden")
})
run("default param first param has no default", function() {
var f = function(a, b=42) { return a }
assert_eq(f(7), 7, "first param no default")
})
// ============================================================================
// FUNCTINO TESTS
// ============================================================================
run("functino +! addition", function() {
assert_eq(+!(3, 4), 7, "+! addition")
})
run("functino -! subtraction", function() {
assert_eq(-!(10, 3), 7, "-! subtraction")
})
run("functino *! multiplication", function() {
assert_eq(*!(5, 6), 30, "*! multiplication")
})
run("functino /! division", function() {
assert_eq(/!(10, 2), 5, "/! division")
})
run("functino %! modulo", function() {
assert_eq(%!(10, 3), 1, "%! modulo")
})
run("functino **! power", function() {
assert_eq(**!(2, 10), 1024, "**! power")
})
run("functino <! less than", function() {
assert_eq(<!(3, 5), true, "<! true")
assert_eq(<!(5, 3), false, "<! false")
assert_eq(<!(3, 3), false, "<! equal")
})
run("functino >! greater than", function() {
assert_eq(>!(5, 3), true, ">! true")
assert_eq(>!(3, 5), false, ">! false")
assert_eq(>!(3, 3), false, ">! equal")
})
run("functino <=! less or equal", function() {
assert_eq(<=!(3, 5), true, "<=! less")
assert_eq(<=!(3, 3), true, "<=! equal")
assert_eq(<=!(5, 3), false, "<=! greater")
})
run("functino >=! greater or equal", function() {
assert_eq(>=!(5, 3), true, ">=! greater")
assert_eq(>=!(3, 3), true, ">=! equal")
assert_eq(>=!(3, 5), false, ">=! less")
})
run("functino =! equality", function() {
assert_eq(=!(5, 5), true, "=! true")
assert_eq(=!(5, 3), false, "=! false")
})
run("functino !=! inequality", function() {
assert_eq(!=!(5, 3), true, "!=! true")
assert_eq(!=!(5, 5), false, "!=! false")
})
run("functino =! with tolerance", function() {
assert_eq(=!(1.0, 1.0001, 0.001), true, "=! within tolerance")
assert_eq(=!(1.0, 1.01, 0.001), false, "=! outside tolerance")
})
run("functino !=! with tolerance", function() {
assert_eq(!=!(1.0, 1.01, 0.001), true, "!=! outside tolerance")
assert_eq(!=!(1.0, 1.0001, 0.001), false, "!=! within tolerance")
})
run("functino &! bitwise and", function() {
assert_eq(&!(0xff, 0x0f), 0x0f, "&! bitwise and")
})
run("functino |! bitwise or", function() {
assert_eq(|!(0xf0, 0x0f), 0xff, "|! bitwise or")
})
run("functino ^! bitwise xor", function() {
assert_eq(^!(0xff, 0x0f), 0xf0, "^! bitwise xor")
})
run("functino <<! shift left", function() {
assert_eq(<<!(1, 4), 16, "<<! shift left")
})
run("functino >>! shift right", function() {
assert_eq(>>!(16, 4), 1, ">>! shift right")
})
run("functino ~! bitwise not", function() {
assert_eq(~!(0), -1, "~! bitwise not 0")
})
run("functino []! array index", function() {
var arr = [10, 20, 30]
assert_eq([]!(arr, 0), 10, "[]! index 0")
assert_eq([]!(arr, 1), 20, "[]! index 1")
assert_eq([]!(arr, 2), 30, "[]! index 2")
})
run("functino &&! logical and", function() {
assert_eq(&&!(true, true), true, "&&! true true")
assert_eq(&&!(true, false), false, "&&! true false")
assert_eq(&&!(false, true), false, "&&! false true")
assert_eq(&&!(1, 2), 2, "&&! truthy returns right")
assert_eq(&&!(0, 2), 0, "&&! falsy returns left")
})
run("functino ||! logical or", function() {
assert_eq(||!(false, true), true, "||! false true")
assert_eq(||!(true, false), true, "||! true false")
assert_eq(||!(false, false), false, "||! false false")
assert_eq(||!(0, 5), 5, "||! falsy returns right")
assert_eq(||!(3, 5), 3, "||! truthy returns left")
})
run("functino >>>! unsigned shift right", function() {
assert_eq(>>>!(-1, 28), 15, ">>>! unsigned shift right")
})
// ============================================================================
// SUMMARY
// ============================================================================